@@ -3,19 +3,69 @@ import { describe, expect, it } from "vitest";
33import { ANVIL_CHAIN } from "../../test/src/chains.js" ;
44import { TEST_CLIENT } from "../../test/src/test-clients.js" ;
55import { ANVIL_PKEY_A , TEST_ACCOUNT_B } from "../../test/src/test-wallets.js" ;
6+ import { scroll } from "../chains/chain-definitions/scroll.js" ;
67import { getRpcClient , prepareTransaction } from "../exports/thirdweb.js" ;
78import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js" ;
89import { privateKeyToAccount } from "../wallets/private-key.js" ;
910import { avalanche } from "./chain-definitions/avalanche.js" ;
1011import { ethereum } from "./chain-definitions/ethereum.js" ;
12+ import type { LegacyChain } from "./types.js" ;
13+
1114import {
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+
1969describe ( "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} ) ;
0 commit comments