1
1
import type { ThirdwebClient } from "../client/client.js" ;
2
2
import { isThirdwebUrl } from "../utils/fetch.js" ;
3
3
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" ;
5
5
import type { Chain as ViemChain } from "viem" ;
6
6
7
7
/**
@@ -20,12 +20,16 @@ import type { Chain as ViemChain } from "viem";
20
20
* });
21
21
* ```
22
22
*/
23
- export function defineChain ( options : number | ChainOptions | ViemChain ) : Chain {
23
+ export function defineChain (
24
+ options : number | ChainOptions | ViemChain | LegacyChain ,
25
+ ) : Chain {
24
26
if ( typeof options === "number" ) {
25
27
return { id : options , rpc : `https://${ options } .rpc.thirdweb.com` } as const ;
26
28
}
27
29
if ( isViemChain ( options ) ) {
28
30
return convertViemChain ( options ) ;
31
+ } else if ( isLegacyChain ( options ) ) {
32
+ return convertLegacyChain ( options ) ;
29
33
}
30
34
// otherwise if it's not a viem chain, continue
31
35
let rpc = options . rpc ;
@@ -35,7 +39,38 @@ export function defineChain(options: number | ChainOptions | ViemChain): Chain {
35
39
return { ...options , rpc } as const ;
36
40
}
37
41
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 {
39
74
return "rpcUrls" in chain && ! ( "rpc" in chain ) ;
40
75
}
41
76
0 commit comments