Skip to content

Commit 08cff4b

Browse files
authored
Feature: Adds chains function (#6956)
1 parent f2b4fe6 commit 08cff4b

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

.changeset/free-lemons-find.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
feat(bridge): Add chains endpoint to retrieve Universal Bridge supported chains
6+
7+
```typescript
8+
import { Bridge } from "thirdweb";
9+
10+
const chains = await Bridge.chains({
11+
client: thirdwebClient,
12+
});
13+
```
14+
15+
Returned chains include chain information such as chainId, name, icon, and nativeCurrency details.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vitest";
2+
import { TEST_CLIENT } from "~test/test-clients.js";
3+
import { chains } from "./Chains.js";
4+
5+
describe("chains", () => {
6+
it("should fetch chains", async () => {
7+
// Setup
8+
const client = TEST_CLIENT;
9+
10+
// Test
11+
const result = await chains({ client });
12+
13+
// Verify
14+
expect(result).toBeInstanceOf(Array);
15+
16+
// Basic structure validation
17+
if (result.length > 0) {
18+
const chain = result[0];
19+
expect(chain).toHaveProperty("chainId");
20+
expect(chain).toHaveProperty("name");
21+
expect(chain).toHaveProperty("icon");
22+
expect(chain).toHaveProperty("nativeCurrency");
23+
expect(chain?.nativeCurrency).toHaveProperty("name");
24+
expect(chain?.nativeCurrency).toHaveProperty("symbol");
25+
expect(chain?.nativeCurrency).toHaveProperty("decimals");
26+
}
27+
});
28+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { ThirdwebClient } from "../client/client.js";
2+
import { getClientFetch } from "../utils/fetch.js";
3+
import { UNIVERSAL_BRIDGE_URL } from "./constants.js";
4+
import type { Chain } from "./types/Chain.js";
5+
6+
/**
7+
* Retrieves supported Universal Bridge chains.
8+
*
9+
* @example
10+
* ```typescript
11+
* import { Bridge } from "thirdweb";
12+
*
13+
* const chains = await Bridge.chains({
14+
* client: thirdwebClient,
15+
* });
16+
* ```
17+
*
18+
* Returned chains might look something like:
19+
* ```typescript
20+
* [
21+
* {
22+
* chainId: 1,
23+
* name: "Ethereum",
24+
* icon: "https://assets.thirdweb.com/chains/1.png",
25+
* nativeCurrency: {
26+
* name: "Ether",
27+
* symbol: "ETH",
28+
* decimals: 18
29+
* }
30+
* },
31+
* {
32+
* chainId: 137,
33+
* name: "Polygon",
34+
* icon: "https://assets.thirdweb.com/chains/137.png",
35+
* nativeCurrency: {
36+
* name: "MATIC",
37+
* symbol: "MATIC",
38+
* decimals: 18
39+
* }
40+
* }
41+
* ]
42+
* ```
43+
*
44+
* @param options - The options for fetching chains.
45+
* @param options.client - Your thirdweb client.
46+
*
47+
* @returns A promise that resolves to an array of chains.
48+
*
49+
* @throws Will throw an error if there is an issue fetching the chains.
50+
* @bridge
51+
* @beta
52+
*/
53+
export async function chains(options: chains.Options): Promise<chains.Result> {
54+
const { client } = options;
55+
56+
const clientFetch = getClientFetch(client);
57+
const url = new URL(`${UNIVERSAL_BRIDGE_URL}/chains`);
58+
59+
const response = await clientFetch(url.toString());
60+
if (!response.ok) {
61+
const errorJson = await response.json();
62+
throw new Error(`${errorJson.code} | ${errorJson.message}`);
63+
}
64+
65+
const { data }: { data: Chain[] } = await response.json();
66+
return data;
67+
}
68+
69+
export declare namespace chains {
70+
type Options = {
71+
client: ThirdwebClient;
72+
};
73+
74+
type Result = Chain[];
75+
}

packages/thirdweb/src/bridge/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ export * as Buy from "./Buy.js";
22
export * as Sell from "./Sell.js";
33
export { status } from "./Status.js";
44
export { routes } from "./Routes.js";
5+
export { chains } from "./Chains.js";
56

67
export type { Status } from "./types/Status.js";
78
export type { Route } from "./types/Route.js";
89
export type { Quote, PreparedQuote } from "./types/Quote.js";
10+
export type { Chain } from "./types/Chain.js";
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Represents a blockchain chain in the Universal Bridge.
3+
* @public
4+
*/
5+
export interface Chain {
6+
/**
7+
* The chain ID of the chain.
8+
*/
9+
chainId: number;
10+
11+
/**
12+
* The name of the chain.
13+
*/
14+
name: string;
15+
16+
/**
17+
* The URL of the chain's icon.
18+
*/
19+
icon: string;
20+
21+
/**
22+
* Information about the native currency of the chain.
23+
*/
24+
nativeCurrency: {
25+
/**
26+
* The name of the native currency.
27+
*/
28+
name: string;
29+
30+
/**
31+
* The symbol of the native currency.
32+
*/
33+
symbol: string;
34+
35+
/**
36+
* The number of decimals used by the native currency.
37+
*/
38+
decimals: number;
39+
};
40+
}

0 commit comments

Comments
 (0)