dev
dart 237 lines 6.54 KB
Raw
1 import 'package:cw_core/crypto_currency.dart';
2 import 'package:cw_core/wallet_type.dart';
3 import 'package:cw_evm/utils/network_chain_utils.dart';
4
5 /// Centralized registry for all EVM chain configurations
6 class EvmChainRegistry {
7 static final EvmChainRegistry _instance = EvmChainRegistry._internal();
8 factory EvmChainRegistry() => _instance;
9 EvmChainRegistry._internal() {
10 initialize();
11 }
12
13 final Map<int, ChainConfig> _chains = {};
14 final Map<WalletType, int> _walletTypeToChainId = {};
15 final Map<int, WalletType> _chainIdToWalletType = {};
16 final Map<String, int> _tagToChainId = {};
17 final Map<String, int> _caip2ToChainId = {};
18
19 bool _initialized = false;
20
21 /// Initialize registry with all supported EVM chains
22 void initialize() {
23 if (_initialized) return;
24 _initialized = true;
25
26 // Ethereum Mainnet
27 _registerChain(
28 const ChainConfig(
29 chainId: 1,
30 name: 'Ethereum',
31 shortCode: 'eth',
32 caip2: 'eip155:1',
33 nativeCurrency: CryptoCurrency.eth,
34 capabilities: ChainCapabilities(
35 supportsERC20: true,
36 supportsEIP1559: true,
37 supportsInternalTx: true,
38 supportsSubscriptions: false,
39 supportsENS: true,
40 ),
41 defaultRpcEndpoints: [
42 'ethereum-rpc.publicnode.com',
43 'eth.llamarpc.com',
44 'rpc.flashbots.net',
45 'eth-mainnet.public.blastapi.io',
46 'eth.nownodes.io',
47 'ethereum.publicnode.com',
48 ],
49 explorerUrls: [
50 'https://etherscan.io',
51 ],
52 feeModel: FeeModel(
53 type: FeeType.eip1559,
54 defaultGasLimit: 21000,
55 ),
56 ),
57 WalletType.ethereum,
58 'ETH',
59 );
60
61 // Polygon
62 _registerChain(
63 const ChainConfig(
64 chainId: 137,
65 name: 'Polygon',
66 shortCode: 'polygon',
67 caip2: 'eip155:137',
68 nativeCurrency: CryptoCurrency.maticpoly,
69 capabilities: ChainCapabilities(
70 supportsERC20: true,
71 supportsEIP1559: true,
72 supportsInternalTx: true,
73 supportsSubscriptions: false,
74 supportsENS: false,
75 ),
76 defaultRpcEndpoints: [
77 'polygon-rpc.com',
78 'polygon-bor-rpc.publicnode.com',
79 'polygon.llamarpc.com',
80 'matic.nownodes.io',
81 ],
82 explorerUrls: [
83 'https://polygonscan.com',
84 ],
85 feeModel: FeeModel(
86 type: FeeType.eip1559,
87 defaultGasLimit: 21000,
88 ),
89 ),
90 WalletType.polygon,
91 'POL',
92 );
93
94 // Base
95 _registerChain(
96 const ChainConfig(
97 chainId: 8453,
98 name: 'Base',
99 shortCode: 'base',
100 caip2: 'eip155:8453',
101 nativeCurrency: CryptoCurrency.baseEth,
102 capabilities: ChainCapabilities(
103 supportsERC20: true,
104 supportsEIP1559: true,
105 supportsInternalTx: true,
106 supportsSubscriptions: false,
107 supportsENS: false,
108 ),
109 defaultRpcEndpoints: [
110 'base.nownodes.io',
111 'base.llamarpc.com',
112 'base-rpc.publicnode.com',
113 '1rpc.io/base',
114 ],
115 explorerUrls: [
116 'https://basescan.org',
117 ],
118 feeModel: FeeModel(
119 type: FeeType.eip1559,
120 defaultGasLimit: 21000,
121 ),
122 ),
123 WalletType.base,
124 'BASE',
125 );
126
127 // Arbitrum
128 _registerChain(
129 const ChainConfig(
130 chainId: 42161,
131 name: 'Arbitrum',
132 shortCode: 'arbitrum',
133 caip2: 'eip155:42161',
134 nativeCurrency: CryptoCurrency.arbEth,
135 capabilities: ChainCapabilities(
136 supportsERC20: true,
137 supportsEIP1559: true,
138 supportsInternalTx: true,
139 supportsSubscriptions: false,
140 supportsENS: false,
141 ),
142 defaultRpcEndpoints: [
143 'arbitrum.nownodes.io',
144 'arbitrum.drpc.org',
145 'arbitrum-one-rpc.publicnode.com',
146 ],
147 explorerUrls: [
148 'https://arbiscan.io',
149 ],
150 feeModel: FeeModel(
151 type: FeeType.eip1559,
152 defaultGasLimit: 21000,
153 ),
154 ),
155 WalletType.arbitrum,
156 'ARB',
157 );
158
159 // BNB Smart Chain
160 _registerChain(
161 const ChainConfig(
162 chainId: 56,
163 name: 'BNB Smart Chain',
164 shortCode: 'bsc',
165 caip2: 'eip155:56',
166 nativeCurrency: CryptoCurrency.bnb,
167 capabilities: ChainCapabilities(
168 supportsERC20: true,
169 supportsEIP1559: true,
170 supportsInternalTx: true,
171 supportsSubscriptions: false,
172 supportsENS: false,
173 ),
174 defaultRpcEndpoints: [
175 'bsc-dataseed1.binance.org',
176 'bsc-dataseed2.binance.org',
177 'bsc-dataseed1.defibit.io',
178 'bsc-dataseed1.nodereal.io',
179 'bsc.llamarpc.com',
180 'bsc-rpc.publicnode.com',
181 ],
182 explorerUrls: [
183 'https://bscscan.com',
184 ],
185 feeModel: FeeModel(
186 type: FeeType.eip1559,
187 defaultGasLimit: 21000,
188 ),
189 ),
190 WalletType.bsc,
191 'BSC',
192 );
193 }
194
195 void _registerChain(
196 ChainConfig config,
197 WalletType walletType,
198 String tag,
199 ) {
200 _chains[config.chainId] = config;
201 _walletTypeToChainId[walletType] = config.chainId;
202 _chainIdToWalletType[config.chainId] = walletType;
203 _tagToChainId[tag.toUpperCase()] = config.chainId;
204 _caip2ToChainId[config.caip2] = config.chainId;
205 }
206
207 ChainConfig? getChainConfig(int chainId) => _chains[chainId];
208
209 ChainConfig? getChainConfigByWalletType(WalletType walletType) {
210 final chainId = _walletTypeToChainId[walletType];
211 return chainId != null ? _chains[chainId] : null;
212 }
213
214 /// Get chain configuration by tag (e.g., 'ETH', 'POL', 'BASE', 'ARB')
215 ChainConfig? getChainConfigByTag(String tag) {
216 final chainId = _tagToChainId[tag.toUpperCase()];
217 return chainId != null ? _chains[chainId] : null;
218 }
219
220 /// Get chain configuration by CAIP-2 identifier (e.g. 'eip155:1')
221 ChainConfig? getChainConfigByCaip2(String caip2) {
222 final chainId = _caip2ToChainId[caip2];
223 return chainId != null ? _chains[chainId] : null;
224 }
225
226 WalletType? getWalletTypeByChainId(int chainId) => _chainIdToWalletType[chainId];
227
228 int? getChainIdByWalletType(WalletType walletType) => _walletTypeToChainId[walletType];
229
230 bool isChainRegistered(int chainId) => _chains.containsKey(chainId);
231
232 List<int> getRegisteredChainIds() => _chains.keys.toList();
233
234 List<ChainConfig> getAllChains() => _chains.values.toList();
235
236 List<WalletType> getRegisteredWalletTypes() => _walletTypeToChainId.keys.toList();
237 }