Skip to content

Commit c63fefd

Browse files
committed
[SDK] Add tests for chain utils (#5280)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR enhances the `CUSTOM_CHAIN_MAP` and `convertLegacyChain` function to be exported for testing purposes. It also adds new tests for chain handling, including legacy chain conversion and caching functionality. ### Detailed summary - Exported `CUSTOM_CHAIN_MAP` for testing. - Exported `convertLegacyChain` function. - Added a `legacyChain` object for testing. - Created tests for: - `getChainSymbol` - `getChainDecimals` - `getChainNativeCurrencyName` - `convertLegacyChain` - `defineChain` with a legacy chain. - Caching functionality with `cacheChains`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent f98059c commit c63fefd

File tree

2 files changed

+131
-2
lines changed

2 files changed

+131
-2
lines changed

packages/thirdweb/src/chains/utils.test.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,69 @@ import { describe, expect, it } from "vitest";
33
import { ANVIL_CHAIN } from "../../test/src/chains.js";
44
import { TEST_CLIENT } from "../../test/src/test-clients.js";
55
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
6+
import { scroll } from "../chains/chain-definitions/scroll.js";
67
import { getRpcClient, prepareTransaction } from "../exports/thirdweb.js";
78
import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js";
89
import { privateKeyToAccount } from "../wallets/private-key.js";
910
import { avalanche } from "./chain-definitions/avalanche.js";
1011
import { ethereum } from "./chain-definitions/ethereum.js";
12+
import type { LegacyChain } from "./types.js";
13+
1114
import {
15+
CUSTOM_CHAIN_MAP,
16+
cacheChains,
17+
convertLegacyChain,
1218
defineChain,
1319
getCachedChain,
1420
getChainDecimals,
1521
getChainNativeCurrencyName,
1622
getChainSymbol,
1723
} from "./utils.js";
1824

25+
const legacyChain: LegacyChain = {
26+
chain: "ETH",
27+
chainId: 1,
28+
ens: {
29+
registry: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
30+
},
31+
explorers: [
32+
{
33+
name: "etherscan",
34+
url: "https://etherscan.io",
35+
standard: "EIP3091",
36+
},
37+
],
38+
faucets: [],
39+
features: [
40+
{
41+
name: "EIP155",
42+
},
43+
{
44+
name: "EIP1559",
45+
},
46+
],
47+
icon: {
48+
url: "ipfs://QmcxZHpyJa8T4i63xqjPYrZ6tKrt55tZJpbXcjSDKuKaf9/ethereum/512.png",
49+
width: 512,
50+
height: 512,
51+
format: "png",
52+
},
53+
infoURL: "https://ethereum.org",
54+
name: "Ethereum Mainnet",
55+
nativeCurrency: {
56+
name: "Ether",
57+
symbol: "ETH",
58+
decimals: 18,
59+
},
60+
networkId: 1,
61+
redFlags: [],
62+
rpc: ["https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}"],
63+
shortName: "eth",
64+
slip44: 60,
65+
slug: "ethereum",
66+
testnet: false,
67+
};
68+
1969
describe("defineChain", () => {
2070
it("should convert viem chain to thirdweb chain", () => {
2171
const zoraViem = viemChain({
@@ -114,4 +164,77 @@ describe("defineChain", () => {
114164
const ethResult = await getChainNativeCurrencyName(ethereum);
115165
expect(ethResult).toBe("Ether");
116166
});
167+
168+
it("getChainSymbol should work for chain object without symbol", async () => {
169+
const chain = defineChain(1);
170+
expect(await getChainSymbol(chain)).toBe("ETH");
171+
});
172+
173+
it("getChainDecimals should work for chain object without decimals", async () => {
174+
const chain = defineChain(1);
175+
expect(await getChainDecimals(chain)).toBe(18);
176+
});
177+
178+
it("getChainDecimals should return `18` if fails to resolve chain decimals", async () => {
179+
const nonExistentChain = defineChain(-1);
180+
expect(await getChainDecimals(nonExistentChain)).toBe(18);
181+
});
182+
183+
it("getChainNativeCurrencyName should work for chain object without nativeCurrency", async () => {
184+
const chain = defineChain(1);
185+
expect(await getChainNativeCurrencyName(chain)).toBe("Ether");
186+
});
187+
188+
it("should convert LegacyChain", () => {
189+
expect(convertLegacyChain(legacyChain)).toStrictEqual({
190+
id: 1,
191+
name: "Ethereum Mainnet",
192+
rpc: "https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}",
193+
blockExplorers: [
194+
{
195+
name: "etherscan",
196+
url: "https://etherscan.io",
197+
apiUrl: "https://etherscan.io",
198+
},
199+
],
200+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
201+
faucets: [],
202+
icon: {
203+
url: "ipfs://QmcxZHpyJa8T4i63xqjPYrZ6tKrt55tZJpbXcjSDKuKaf9/ethereum/512.png",
204+
width: 512,
205+
height: 512,
206+
format: "png",
207+
},
208+
testnet: undefined,
209+
});
210+
});
211+
212+
it("`defineChain` should work with Legacy chain", () => {
213+
expect(defineChain(legacyChain)).toStrictEqual({
214+
id: 1,
215+
name: "Ethereum Mainnet",
216+
rpc: "https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}",
217+
blockExplorers: [
218+
{
219+
name: "etherscan",
220+
url: "https://etherscan.io",
221+
apiUrl: "https://etherscan.io",
222+
},
223+
],
224+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
225+
faucets: [],
226+
icon: {
227+
url: "ipfs://QmcxZHpyJa8T4i63xqjPYrZ6tKrt55tZJpbXcjSDKuKaf9/ethereum/512.png",
228+
width: 512,
229+
height: 512,
230+
format: "png",
231+
},
232+
testnet: undefined,
233+
});
234+
});
235+
236+
it("should cache chains properly", () => {
237+
cacheChains([scroll]);
238+
expect(CUSTOM_CHAIN_MAP.get(scroll.id)).toStrictEqual(scroll);
239+
});
117240
});

packages/thirdweb/src/chains/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type {
1010
LegacyChain,
1111
} from "./types.js";
1212

13-
const CUSTOM_CHAIN_MAP = new Map<number, Chain>();
13+
/**
14+
* @internal Exported for tests
15+
*/
16+
export const CUSTOM_CHAIN_MAP = new Map<number, Chain>();
1417

1518
/**
1619
* Defines a chain with the given options.
@@ -98,7 +101,10 @@ function isLegacyChain(
98101
return "rpc" in chain && Array.isArray(chain.rpc) && "slug" in chain;
99102
}
100103

101-
function convertLegacyChain(legacyChain: LegacyChain): Chain {
104+
/**
105+
* @internal
106+
*/
107+
export function convertLegacyChain(legacyChain: LegacyChain): Chain {
102108
const RPC_URL = getThirdwebDomains().rpc;
103109
return {
104110
id: legacyChain.chainId,

0 commit comments

Comments
 (0)