| 1 | import 'package:cw_evm/clients/arbitrum_client.dart'; |
| 2 | import 'package:cw_evm/clients/base_client.dart'; |
| 3 | import 'package:cw_evm/clients/bsc_client.dart'; |
| 4 | import 'package:cw_evm/clients/ethereum_client.dart'; |
| 5 | import 'package:cw_evm/clients/polygon_client.dart'; |
| 6 | import 'package:cw_evm/clients/evm_chain_client.dart'; |
| 7 | import 'package:cw_evm/evm_chain_registry.dart'; |
| 8 | |
| 9 | /// Factory to create appropriate EVMChainClient based on chainId |
| 10 | class EVMChainClientFactory { |
| 11 | static final EvmChainRegistry _registry = EvmChainRegistry(); |
| 12 | |
| 13 | /// Create an EVMChainClient for the given chainId |
| 14 | /// |
| 15 | /// Throws an exception if chainId is not registered |
| 16 | static EVMChainClient createClient(int chainId) { |
| 17 | final config = _registry.getChainConfig(chainId); |
| 18 | |
| 19 | if (config == null) { |
| 20 | throw Exception('Chain config not found for chainId: $chainId'); |
| 21 | } |
| 22 | |
| 23 | // Check if chain needs custom client |
| 24 | switch (chainId) { |
| 25 | case 1: |
| 26 | return EthereumClient(); |
| 27 | case 137: |
| 28 | return PolygonClient(); |
| 29 | case 8453: |
| 30 | return BaseClient(); |
| 31 | case 42161: |
| 32 | return ArbitrumClient(); |
| 33 | case 56: |
| 34 | return BSCClient(); |
| 35 | default: |
| 36 | return EVMChainClient(chainId: chainId); |
| 37 | } |
| 38 | } |
| 39 | } |