-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e27275
commit 6580e4d
Showing
32 changed files
with
4,963 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Anchor, Group, Text, rem } from "@mantine/core"; | ||
import { anyPass, equals } from "ramda"; | ||
import { FC } from "react"; | ||
import { TbExternalLink } from "react-icons/tb"; | ||
import { useConfig } from "wagmi"; | ||
|
||
interface BlockExplorerLinkProps { | ||
value: string; | ||
type: "tx" | "block" | "address"; | ||
} | ||
|
||
const isTxOrAddress = anyPass([equals("tx"), equals("address")]); | ||
|
||
/** | ||
* | ||
* Works in conjuction with Wagmi. It requires a Wagmi-Provider to work as expected. | ||
* When running devnet it will not render a block-explorer link. | ||
* | ||
*/ | ||
export const BlockExplorerLink: FC<BlockExplorerLinkProps> = ({ | ||
value, | ||
type, | ||
}) => { | ||
const config = useConfig(); | ||
const explorerUrl = config.chains[0].blockExplorers?.default.url; | ||
|
||
if (!explorerUrl) return; | ||
|
||
const shouldShorten = isTxOrAddress(type); | ||
|
||
const text = shouldShorten | ||
? `${value.slice(0, 8)}...${value.slice(-6)}` | ||
: value; | ||
|
||
const href = `${explorerUrl}/${type}/${value}`; | ||
|
||
return ( | ||
<Anchor href={href} target="_blank"> | ||
<Group gap="xs"> | ||
<Text>{text}</Text> | ||
<TbExternalLink style={{ width: rem(21), height: rem(21) }} /> | ||
</Group> | ||
</Anchor> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Address } from "viem"; | ||
import { useMultiTokensQuery } from "../graphql/explorer/hooks/queries"; | ||
|
||
type SearchInput = { address?: string; limit?: number }; | ||
type SearchOutput = { fetching: boolean; multiTokens: Address[] }; | ||
type UseSearchMultiTokens = (args: SearchInput) => SearchOutput; | ||
|
||
export const useSearchMultiTokens: UseSearchMultiTokens = ({ | ||
address, | ||
limit, | ||
}) => { | ||
const [{ data, fetching }] = useMultiTokensQuery({ | ||
variables: { | ||
limit: limit ?? 10, | ||
where: { | ||
id_containsInsensitive: address ?? "", | ||
}, | ||
}, | ||
}); | ||
|
||
const multiTokens = (data?.multiTokens ?? []).map((t) => t.id as Address); | ||
return { multiTokens, fetching }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { cleanup, render, screen } from "@testing-library/react"; | ||
import { foundry, sepolia } from "viem/chains"; | ||
import { afterEach, beforeEach, describe, it } from "vitest"; | ||
import { useConfig } from "wagmi"; | ||
import { BlockExplorerLink } from "../../src/components/BlockExplorerLink"; | ||
import withMantineTheme from "../utils/WithMantineTheme"; | ||
|
||
vi.mock("wagmi"); | ||
const useConfigMock = vi.mocked(useConfig, { partial: true }); | ||
const Component = withMantineTheme(BlockExplorerLink); | ||
const txHash = | ||
"0x4d6ce102c5aedd46aff879dbb42eef3465a2b7aa7fb39e64296194fb313efebf"; | ||
const address = "0xedB53860A6B52bbb7561Ad596416ee9965B055Aa"; | ||
const blockNumber = 5298115; | ||
|
||
describe("BlockExplorerLink component", () => { | ||
beforeEach(() => { | ||
useConfigMock.mockReturnValue({ | ||
chains: [sepolia], | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
cleanup(); | ||
}); | ||
|
||
it("should render nothing when the block-explorer URL is not available", () => { | ||
useConfigMock.mockReturnValue({ | ||
chains: [foundry], | ||
}); | ||
|
||
render(<Component type="tx" value={txHash} />); | ||
const link = screen.queryByText("0x4d6ce1...3efebf")?.closest("a"); | ||
|
||
expect(screen.queryByText("0x4d6ce1...3efebf")).not.toBeInTheDocument(); | ||
expect(link).not.toBeDefined(); | ||
}); | ||
|
||
it("should render the correct link to the block-explorer given a transaction hash", () => { | ||
render(<Component type="tx" value={txHash} />); | ||
const link = screen.getByText("0x4d6ce1...3efebf").closest("a"); | ||
|
||
expect(screen.getByText("0x4d6ce1...3efebf")).toBeInTheDocument(); | ||
expect(link?.getAttribute("href")).toEqual( | ||
`https://sepolia.etherscan.io/tx/${txHash}`, | ||
); | ||
}); | ||
|
||
it("should render the correct link to the block-explorer given an address", () => { | ||
render(<Component type="address" value={address} />); | ||
const textEl = screen.getByText("0xedB538...B055Aa"); | ||
const link = textEl.closest("a"); | ||
|
||
expect(textEl).toBeInTheDocument(); | ||
expect(link?.getAttribute("href")).toEqual( | ||
`https://sepolia.etherscan.io/address/0xedB53860A6B52bbb7561Ad596416ee9965B055Aa`, | ||
); | ||
}); | ||
|
||
it("should render the correct link to the block-explorer given an block-number", () => { | ||
render(<Component type="block" value={blockNumber.toString()} />); | ||
const textEl = screen.getByText("5298115"); | ||
const link = textEl.closest("a"); | ||
|
||
expect(textEl).toBeInTheDocument(); | ||
expect(link?.getAttribute("href")).toEqual( | ||
`https://sepolia.etherscan.io/block/5298115`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.