Skip to content

Commit 1f0e771

Browse files
committed
refactor(utils): Add support for converting legacy chains
1 parent 93f2a80 commit 1f0e771

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

packages/thirdweb/src/chains/types.ts

+34
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,37 @@ export type ApiChain = {
6868
bridges?: Readonly<Array<{ url: string }>>;
6969
};
7070
};
71+
72+
// legacy chain type
73+
export type LegacyChain = {
74+
name: string;
75+
title?: string;
76+
chain: string;
77+
icon?: Icon;
78+
rpc: readonly string[];
79+
features?: Readonly<Array<{ name: string }>>;
80+
faucets?: readonly string[];
81+
nativeCurrency: {
82+
name: string;
83+
symbol: string;
84+
decimals: number;
85+
};
86+
infoURL?: string;
87+
shortName: string;
88+
chainId: number;
89+
networkId?: number;
90+
ens?: {
91+
registry: string;
92+
};
93+
explorers?: Readonly<Array<ChainExplorer>>;
94+
testnet: boolean;
95+
slug: string;
96+
slip44?: number;
97+
status?: string;
98+
redFlags?: readonly string[];
99+
parent?: {
100+
chain: string;
101+
type: string;
102+
bridges?: Readonly<Array<{ url: string }>>;
103+
};
104+
};

packages/thirdweb/src/chains/utils.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ThirdwebClient } from "../client/client.js";
22
import { isThirdwebUrl } from "../utils/fetch.js";
33
import { withCache } from "../utils/promise/withCache.js";
4-
import type { ApiChain, Chain, ChainOptions } from "./types.js";
4+
import type { ApiChain, Chain, ChainOptions, LegacyChain } from "./types.js";
55
import type { Chain as ViemChain } from "viem";
66

77
/**
@@ -20,12 +20,16 @@ import type { Chain as ViemChain } from "viem";
2020
* });
2121
* ```
2222
*/
23-
export function defineChain(options: number | ChainOptions | ViemChain): Chain {
23+
export function defineChain(
24+
options: number | ChainOptions | ViemChain | LegacyChain,
25+
): Chain {
2426
if (typeof options === "number") {
2527
return { id: options, rpc: `https://${options}.rpc.thirdweb.com` } as const;
2628
}
2729
if (isViemChain(options)) {
2830
return convertViemChain(options);
31+
} else if (isLegacyChain(options)) {
32+
return convertLegacyChain(options);
2933
}
3034
// otherwise if it's not a viem chain, continue
3135
let rpc = options.rpc;
@@ -35,7 +39,38 @@ export function defineChain(options: number | ChainOptions | ViemChain): Chain {
3539
return { ...options, rpc } as const;
3640
}
3741

38-
function isViemChain(chain: ChainOptions | ViemChain): chain is ViemChain {
42+
function isLegacyChain(
43+
chain: ChainOptions | ViemChain | LegacyChain,
44+
): chain is LegacyChain {
45+
return "rpc" in chain && Array.isArray(chain.rpc) && "slug" in chain;
46+
}
47+
48+
function convertLegacyChain(legacyChain: LegacyChain): Chain {
49+
const c: Chain = {
50+
id: legacyChain.chainId,
51+
name: legacyChain.name,
52+
rpc:
53+
legacyChain.rpc[0] ?? `https://${legacyChain.chainId}.rpc.thirdweb.com`,
54+
blockExplorers: legacyChain?.explorers?.map((explorer) => ({
55+
name: explorer.name,
56+
url: explorer.url,
57+
apiUrl: explorer.url,
58+
})),
59+
nativeCurrency: {
60+
name: legacyChain.nativeCurrency.name,
61+
symbol: legacyChain.nativeCurrency.symbol,
62+
decimals: legacyChain.nativeCurrency.decimals,
63+
},
64+
};
65+
if (legacyChain.testnet) {
66+
return { ...c, testnet: true };
67+
}
68+
return c;
69+
}
70+
71+
function isViemChain(
72+
chain: ChainOptions | ViemChain | LegacyChain,
73+
): chain is ViemChain {
3974
return "rpcUrls" in chain && !("rpc" in chain);
4075
}
4176

0 commit comments

Comments
 (0)