-
Notifications
You must be signed in to change notification settings - Fork 619
Feature: Adds chains function #6956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
| --- | ||
| "thirdweb": minor | ||
| --- | ||
|
|
||
| feat(bridge): Add chains endpoint to retrieve Universal Bridge supported chains | ||
|
|
||
| ```typescript | ||
| import { Bridge } from "thirdweb"; | ||
|
|
||
| const chains = await Bridge.chains({ | ||
| client: thirdwebClient, | ||
| }); | ||
| ``` | ||
|
|
||
| Returned chains include chain information such as chainId, name, icon, and nativeCurrency details. |
This file contains hidden or 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,28 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { TEST_CLIENT } from "~test/test-clients.js"; | ||
| import { chains } from "./Chains.js"; | ||
|
|
||
| describe("chains", () => { | ||
| it("should fetch chains", async () => { | ||
| // Setup | ||
| const client = TEST_CLIENT; | ||
|
|
||
| // Test | ||
| const result = await chains({ client }); | ||
|
|
||
| // Verify | ||
| expect(result).toBeInstanceOf(Array); | ||
|
|
||
| // Basic structure validation | ||
| if (result.length > 0) { | ||
| const chain = result[0]; | ||
| expect(chain).toHaveProperty("chainId"); | ||
| expect(chain).toHaveProperty("name"); | ||
| expect(chain).toHaveProperty("icon"); | ||
| expect(chain).toHaveProperty("nativeCurrency"); | ||
| expect(chain?.nativeCurrency).toHaveProperty("name"); | ||
| expect(chain?.nativeCurrency).toHaveProperty("symbol"); | ||
| expect(chain?.nativeCurrency).toHaveProperty("decimals"); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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,75 @@ | ||
| import type { ThirdwebClient } from "../client/client.js"; | ||
| import { getClientFetch } from "../utils/fetch.js"; | ||
| import { UNIVERSAL_BRIDGE_URL } from "./constants.js"; | ||
| import type { Chain } from "./types/Chain.js"; | ||
|
|
||
| /** | ||
| * Retrieves supported Universal Bridge chains. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { Bridge } from "thirdweb"; | ||
| * | ||
| * const chains = await Bridge.chains({ | ||
| * client: thirdwebClient, | ||
| * }); | ||
| * ``` | ||
| * | ||
| * Returned chains might look something like: | ||
| * ```typescript | ||
| * [ | ||
| * { | ||
| * chainId: 1, | ||
| * name: "Ethereum", | ||
| * icon: "https://assets.thirdweb.com/chains/1.png", | ||
| * nativeCurrency: { | ||
| * name: "Ether", | ||
| * symbol: "ETH", | ||
| * decimals: 18 | ||
| * } | ||
| * }, | ||
| * { | ||
| * chainId: 137, | ||
| * name: "Polygon", | ||
| * icon: "https://assets.thirdweb.com/chains/137.png", | ||
| * nativeCurrency: { | ||
| * name: "MATIC", | ||
| * symbol: "MATIC", | ||
| * decimals: 18 | ||
| * } | ||
| * } | ||
| * ] | ||
| * ``` | ||
| * | ||
| * @param options - The options for fetching chains. | ||
| * @param options.client - Your thirdweb client. | ||
| * | ||
| * @returns A promise that resolves to an array of chains. | ||
| * | ||
| * @throws Will throw an error if there is an issue fetching the chains. | ||
| * @bridge | ||
| * @beta | ||
| */ | ||
| export async function chains(options: chains.Options): Promise<chains.Result> { | ||
| const { client } = options; | ||
|
|
||
| const clientFetch = getClientFetch(client); | ||
| const url = new URL(`${UNIVERSAL_BRIDGE_URL}/chains`); | ||
|
|
||
| const response = await clientFetch(url.toString()); | ||
| if (!response.ok) { | ||
| const errorJson = await response.json(); | ||
| throw new Error(`${errorJson.code} | ${errorJson.message}`); | ||
| } | ||
|
|
||
| const { data }: { data: Chain[] } = await response.json(); | ||
| return data; | ||
| } | ||
|
|
||
| export declare namespace chains { | ||
| type Options = { | ||
| client: ThirdwebClient; | ||
| }; | ||
|
|
||
| type Result = Chain[]; | ||
| } | ||
This file contains hidden or 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 hidden or 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,40 @@ | ||
| /** | ||
| * Represents a blockchain chain in the Universal Bridge. | ||
| * @public | ||
| */ | ||
| export interface Chain { | ||
| /** | ||
| * The chain ID of the chain. | ||
| */ | ||
| chainId: number; | ||
|
|
||
| /** | ||
| * The name of the chain. | ||
| */ | ||
| name: string; | ||
|
|
||
| /** | ||
| * The URL of the chain's icon. | ||
| */ | ||
| icon: string; | ||
|
|
||
| /** | ||
| * Information about the native currency of the chain. | ||
| */ | ||
| nativeCurrency: { | ||
| /** | ||
| * The name of the native currency. | ||
| */ | ||
| name: string; | ||
|
|
||
| /** | ||
| * The symbol of the native currency. | ||
| */ | ||
| symbol: string; | ||
|
|
||
| /** | ||
| * The number of decimals used by the native currency. | ||
| */ | ||
| decimals: number; | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.