From 6fbe62d9ecc875bc3d2b36e15e34840eb5f349f5 Mon Sep 17 00:00:00 2001 From: Charlie Andrews-Jubelt Date: Fri, 23 Feb 2024 11:35:34 -0800 Subject: [PATCH] chore(networkConfig): remove 1 place where manual testing and code searching would be needed to add chains (#4857) ### Description we should try to maintain a status quo where all we have to do in every repo to add a chain is update a "NetworkId" enum and fix all compilation errors that result from it ### Test plan added spot checks for the two affected configs ### Related issues cc https://linear.app/valora/issue/ACT-1060/add-optimism-arbitrum-to-wallet ### Backwards compatibility na --------- Co-authored-by: Tom McGuire --- src/web3/networkConfig.test.ts | 18 ++++++++++++++++++ src/web3/networkConfig.ts | 26 +++++++------------------- 2 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 src/web3/networkConfig.test.ts diff --git a/src/web3/networkConfig.test.ts b/src/web3/networkConfig.test.ts new file mode 100644 index 00000000000..1404ec3fda8 --- /dev/null +++ b/src/web3/networkConfig.test.ts @@ -0,0 +1,18 @@ +import { + walletConnectChainIdToNetwork, + walletConnectChainIdToNetworkId, +} from 'src/web3/networkConfig' +import { NetworkId, Network } from 'src/transactions/types' + +describe('networkConfig', () => { + it('walletConnectChainIdToNetworkId spot checks', () => { + // not attempting to check every value (too circular). just making sure inversion of networkIdToWalletConnectChainId is working as expected + expect(walletConnectChainIdToNetworkId['eip155:42220']).toEqual(NetworkId['celo-mainnet']) + expect(walletConnectChainIdToNetworkId['eip155:1']).toEqual(NetworkId['ethereum-mainnet']) + }) + it('walletConnectChainIdToNetwork spot checks', () => { + // not checking every value (too circular), just testing the logic used to populate this object + expect(walletConnectChainIdToNetwork['eip155:42220']).toEqual(Network.Celo) + expect(walletConnectChainIdToNetwork['eip155:1']).toEqual(Network.Ethereum) + }) +}) diff --git a/src/web3/networkConfig.ts b/src/web3/networkConfig.ts index 8c6e081c4c2..544efed4186 100644 --- a/src/web3/networkConfig.ts +++ b/src/web3/networkConfig.ts @@ -15,6 +15,7 @@ import { optimism, optimismSepolia, } from 'viem/chains' +import _ from 'lodash' export enum Testnets { alfajores = 'alfajores', @@ -482,26 +483,13 @@ export const networkIdToWalletConnectChainId: Record = { [NetworkId['op-sepolia']]: 'eip155:11155420', } -export const walletConnectChainIdToNetworkId: Record = { - 'eip155:44787': NetworkId['celo-alfajores'], - 'eip155:42220': NetworkId['celo-mainnet'], - 'eip155:1': NetworkId['ethereum-mainnet'], - 'eip155:11155111': NetworkId['ethereum-sepolia'], - 'eip155:42161': NetworkId['arbitrum-one'], - 'eip155:421614': NetworkId['arbitrum-sepolia'], - 'eip155:10': NetworkId['op-mainnet'], - 'eip155:11155420': NetworkId['op-sepolia'], -} +export const walletConnectChainIdToNetworkId: Record = _.invert( + networkIdToWalletConnectChainId +) as Record -export const walletConnectChainIdToNetwork: Record = { - 'eip155:44787': Network.Celo, - 'eip155:42220': Network.Celo, - 'eip155:1': Network.Ethereum, - 'eip155:11155111': Network.Ethereum, - 'eip155:42161': Network.Arbitrum, - 'eip155:421614': Network.Arbitrum, - 'eip155:10': Network.Optimism, - 'eip155:11155420': Network.Optimism, +export const walletConnectChainIdToNetwork: Record = {} +for (const [walletConnectChainId, networkId] of Object.entries(walletConnectChainIdToNetworkId)) { + walletConnectChainIdToNetwork[walletConnectChainId] = networkIdToNetwork[networkId] } Logger.info('Connecting to testnet: ', DEFAULT_TESTNET)