From 8a04c5d0811f34ac5b97851b27001214f4fbecd2 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 12:33:07 -0700 Subject: [PATCH 01/11] check if token is deployed cross chain needs to return true if the token address on dest chain is not 0x00 --- .../utils/checkIfTokenIsDeployedCrossChain.ts | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts index 0e4c4f022c2..2473f1276cd 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts @@ -24,38 +24,40 @@ export const checkIfTokenIsDeployedCrossChain = async ( const tokenAddressOnDestChain = token.addresses[destChain.id]; - if (tokenAddressOnDestChain === '0x00') { - // Check if token is already deployed as BridgedERC20 on destination chain - const tokenAddressOnSourceChain = token.addresses[srcChain.id]; - - log( - 'Checking if token', - token, - 'is deployed as BridgedERC20 on destination chain', - destChain, - ); + if (tokenAddressOnDestChain !== '0x00') { + return true; + } + + // Check if token is already deployed as BridgedERC20 on destination chain + const tokenAddressOnSourceChain = token.addresses[srcChain.id]; + + log( + 'Checking if token', + token, + 'is deployed as BridgedERC20 on destination chain', + destChain, + ); - try { - const bridgedTokenAddress = - await destTokenVaultContract.canonicalToBridged( - srcChain.id, - tokenAddressOnSourceChain, - ); - - log(`Address of bridged token: ${bridgedTokenAddress}`); - - if (bridgedTokenAddress !== ethers.constants.AddressZero) { - return true; - } - } catch (error) { - console.error(error); - throw new Error( - 'encountered an issue when checking if token is deployed cross-chain', - { - cause: error, - }, + try { + const bridgedTokenAddress = + await destTokenVaultContract.canonicalToBridged( + srcChain.id, + tokenAddressOnSourceChain, ); + + log(`Address of bridged token: ${bridgedTokenAddress}`); + + if (bridgedTokenAddress !== ethers.constants.AddressZero) { + return true; } + } catch (error) { + console.error(error); + throw new Error( + 'encountered an issue when checking if token is deployed cross-chain', + { + cause: error, + }, + ); } } return false; From f630207f27390e253075ae1bfa33eaa86b6fd12a Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 12:48:03 -0700 Subject: [PATCH 02/11] also check canonicalToBridged is bridgedTOCanonical --- .../src/utils/checkIfTokenIsDeployedCrossChain.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts index 2473f1276cd..d54e49f3bb0 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts @@ -49,6 +49,14 @@ export const checkIfTokenIsDeployedCrossChain = async ( if (bridgedTokenAddress !== ethers.constants.AddressZero) { return true; + } else { + const canonicalTokenAddress = + await destTokenVaultContract.canonicalToBridged( + srcChain.id, + tokenAddressOnSourceChain, + ); + + return canonicalTokenAddress !== ethers.constants.AddressZero; } } catch (error) { console.error(error); From eb561a5bd3b05ae8a0666c04ed9c3d99b9e257ce Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 12:55:07 -0700 Subject: [PATCH 03/11] check isBridgedTOken and thus canonicalToBridged --- .../utils/checkIfTokenIsDeployedCrossChain.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts index d54e49f3bb0..d03ab83d720 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts @@ -39,24 +39,23 @@ export const checkIfTokenIsDeployedCrossChain = async ( ); try { - const bridgedTokenAddress = - await destTokenVaultContract.canonicalToBridged( + const isBridgedToken = await destTokenVaultContract.isBridgedToken( + token.addresses[srcChain.id], + ); + + if (isBridgedToken) { + return await destTokenVaultContract.bridgedToCanonical( srcChain.id, tokenAddressOnSourceChain, ); - - log(`Address of bridged token: ${bridgedTokenAddress}`); - - if (bridgedTokenAddress !== ethers.constants.AddressZero) { - return true; } else { - const canonicalTokenAddress = + const bridgedTokenAddress = await destTokenVaultContract.canonicalToBridged( srcChain.id, tokenAddressOnSourceChain, ); - return canonicalTokenAddress !== ethers.constants.AddressZero; + return bridgedTokenAddress !== ethers.constants.AddressZero; } } catch (error) { console.error(error); From e0ed2afd2aa248fbeda6bd580d9578c9e5a5cba1 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 12:56:44 -0700 Subject: [PATCH 04/11] use var --- .../bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts index d03ab83d720..effd5510d02 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts @@ -40,7 +40,7 @@ export const checkIfTokenIsDeployedCrossChain = async ( try { const isBridgedToken = await destTokenVaultContract.isBridgedToken( - token.addresses[srcChain.id], + tokenAddressOnSourceChain, ); if (isBridgedToken) { From a2e09051276e319c80c271225a8d24611ae3b7c2 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 13:19:36 -0700 Subject: [PATCH 05/11] fix network switching --- packages/bridge-ui/src/utils/switchNetwork.ts | 38 ------------------- packages/bridge-ui/src/wagmi/watcher.ts | 4 +- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/packages/bridge-ui/src/utils/switchNetwork.ts b/packages/bridge-ui/src/utils/switchNetwork.ts index 1e531c99598..92954950e73 100644 --- a/packages/bridge-ui/src/utils/switchNetwork.ts +++ b/packages/bridge-ui/src/utils/switchNetwork.ts @@ -1,43 +1,5 @@ -import { get } from 'svelte/store'; import { switchNetwork as wagmiSwitchNetwork } from 'wagmi/actions'; -import { srcChain } from '../store/chain'; -import { Deferred } from './Deferred'; - export async function switchNetwork(chainId: number) { - const prevChainId = get(srcChain)?.id; - - if (prevChainId === chainId) return; - await wagmiSwitchNetwork({ chainId }); - - // What are we doing here? we have a watcher waiting for network changes. - // When this happens this watcher is called and takes care of setting - // the signer and chains in the store. We are actually waiting here - // for these stores to change due to some race conditions in the UI. - // There will be a better design around this in alpha-4: fewer stores - // and '$:' tags. They're evil. - const deferred = new Deferred(); - - // This will prevent an unlikely infinite loop - const starting = Date.now(); - const timeout = 5000; // TODO: config? - - const waitForNetworkChange = () => { - const srcChainId = get(srcChain)?.id; - - if (srcChainId && srcChainId !== prevChainId) { - // We have finally set the chain in the store. We're done here. - deferred.resolve(); - } else if (Date.now() > starting + timeout) { - // Wait, what??? - deferred.reject(new Error('timeout switching network')); - } else { - setTimeout(waitForNetworkChange, 300); // TODO: config those 300? - } - }; - - waitForNetworkChange(); - - return deferred.promise; } diff --git a/packages/bridge-ui/src/wagmi/watcher.ts b/packages/bridge-ui/src/wagmi/watcher.ts index 73f786e08a2..d5487fa805c 100644 --- a/packages/bridge-ui/src/wagmi/watcher.ts +++ b/packages/bridge-ui/src/wagmi/watcher.ts @@ -17,12 +17,12 @@ const setChain = (chainId: number) => { srcChain.set(L1Chain); destChain.set(L2Chain); - log(`Network swtiched to ${L1Chain.name}`); + log(`Network switched to ${L1Chain.name}`); } else if (chainId === L2Chain.id) { srcChain.set(L2Chain); destChain.set(L1Chain); - log(`Network swtiched to ${L2Chain.name}`); + log(`Network switched to ${L2Chain.name}`); } else { isSwitchChainModalOpen.set(true); } From 92abca3511e9da6eb1eb6d980299b061f2357693 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:01:38 -0700 Subject: [PATCH 06/11] rm test case --- packages/bridge-ui/src/utils/switchNetwork.spec.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/bridge-ui/src/utils/switchNetwork.spec.ts b/packages/bridge-ui/src/utils/switchNetwork.spec.ts index 307ea7ae586..6e10eaeacc6 100644 --- a/packages/bridge-ui/src/utils/switchNetwork.spec.ts +++ b/packages/bridge-ui/src/utils/switchNetwork.spec.ts @@ -49,12 +49,4 @@ describe('switchNetwork', () => { 'timeout switching network', ); }); - - it('should do nothing if already on the target network', async () => { - jest.mocked(get).mockReturnValue({ id: L2_CHAIN_ID }); - - await switchNetwork(L2_CHAIN_ID); - - expect(wagmiSwitchNetwork).not.toHaveBeenCalled(); - }); }); From 23dfbf9c6428ce36f2059a19649f0711f7563436 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:06:12 -0700 Subject: [PATCH 07/11] test --- .../src/utils/checkIfTokenIsDeployedCrossChain.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts index 97b2f6b81fe..534bf767fbc 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts @@ -67,6 +67,10 @@ describe('checkIfTokenIsDeployedCrossChain', () => { canonicalToBridged: jest .fn() .mockResolvedValue(ethers.constants.AddressZero), + bridgedToCanonical: jest + .fn() + .mockResolvedValue(ethers.constants.AddressZero), + isBridgedToken: jest.fn().mockResolvedValue(true), }; jest .spyOn(ethers, 'Contract') From 597c372e8a806d6ac9f3d6ca5201acbf335009f5 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:07:07 -0700 Subject: [PATCH 08/11] mocks --- .../bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts index 534bf767fbc..191ae8acee9 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts @@ -101,6 +101,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => { // mock the `ethers.Contract` object for testing purposes const destTokenVaultContract = { canonicalToBridged: jest.fn().mockResolvedValue(bridgedTokenAddress), + isBridgedToken: jest.fn().mockResolvedValue(false), }; jest .spyOn(ethers, 'Contract') From ec14573f8ace64d37dd6734cdde2d95c552ab881 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:08:02 -0700 Subject: [PATCH 09/11] mocks --- .../src/utils/checkIfTokenIsDeployedCrossChain.spec.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts index 191ae8acee9..f0432273132 100644 --- a/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts +++ b/packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.spec.ts @@ -67,10 +67,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => { canonicalToBridged: jest .fn() .mockResolvedValue(ethers.constants.AddressZero), - bridgedToCanonical: jest - .fn() - .mockResolvedValue(ethers.constants.AddressZero), - isBridgedToken: jest.fn().mockResolvedValue(true), + isBridgedToken: jest.fn().mockResolvedValue(false), }; jest .spyOn(ethers, 'Contract') @@ -130,6 +127,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => { it('catches and rethrows error when canonicalToBridged method fails', async () => { const destTokenVaultContract = { canonicalToBridged: jest.fn().mockRejectedValue(new Error('BOOM!!')), + isBridgedToken: jest.fn().mockResolvedValue(false), }; jest From 2b50b2f0102cb9741ef6522a53f18d7866334411 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:29:45 -0700 Subject: [PATCH 10/11] rm dead tests --- .../bridge-ui/src/utils/switchNetwork.spec.ts | 9 --------- .../src/utils/switchNetwork.spec.ts | 17 ----------------- 2 files changed, 26 deletions(-) diff --git a/packages/bridge-ui/src/utils/switchNetwork.spec.ts b/packages/bridge-ui/src/utils/switchNetwork.spec.ts index 6e10eaeacc6..2b34604eb00 100644 --- a/packages/bridge-ui/src/utils/switchNetwork.spec.ts +++ b/packages/bridge-ui/src/utils/switchNetwork.spec.ts @@ -40,13 +40,4 @@ describe('switchNetwork', () => { expect(wagmiSwitchNetwork).toHaveBeenCalledWith({ chainId: L2_CHAIN_ID }); expect(get).toHaveBeenCalledTimes(getCalls); }); - - it('should throw if timeout', async () => { - // It always returns the same chain. Never changes it - jest.mocked(get).mockReturnValue({ id: L1_CHAIN_ID }); - - await expect(switchNetwork(L2_CHAIN_ID)).rejects.toThrow( - 'timeout switching network', - ); - }); }); diff --git a/packages/pos-dashboard/src/utils/switchNetwork.spec.ts b/packages/pos-dashboard/src/utils/switchNetwork.spec.ts index 307ea7ae586..2b34604eb00 100644 --- a/packages/pos-dashboard/src/utils/switchNetwork.spec.ts +++ b/packages/pos-dashboard/src/utils/switchNetwork.spec.ts @@ -40,21 +40,4 @@ describe('switchNetwork', () => { expect(wagmiSwitchNetwork).toHaveBeenCalledWith({ chainId: L2_CHAIN_ID }); expect(get).toHaveBeenCalledTimes(getCalls); }); - - it('should throw if timeout', async () => { - // It always returns the same chain. Never changes it - jest.mocked(get).mockReturnValue({ id: L1_CHAIN_ID }); - - await expect(switchNetwork(L2_CHAIN_ID)).rejects.toThrow( - 'timeout switching network', - ); - }); - - it('should do nothing if already on the target network', async () => { - jest.mocked(get).mockReturnValue({ id: L2_CHAIN_ID }); - - await switchNetwork(L2_CHAIN_ID); - - expect(wagmiSwitchNetwork).not.toHaveBeenCalled(); - }); }); From 72e225db36c9ce95d8783bf53bb99ab9978aeec3 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Thu, 10 Aug 2023 21:34:09 -0700 Subject: [PATCH 11/11] jest config --- packages/bridge-ui/jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bridge-ui/jest.config.js b/packages/bridge-ui/jest.config.js index 041f3f7d19e..17cb895ac73 100644 --- a/packages/bridge-ui/jest.config.js +++ b/packages/bridge-ui/jest.config.js @@ -46,7 +46,7 @@ export default { // - bridge/ERC20Bridge (partial test coverage) // - bridge/ETHBridge (partial test coverage) statements: 93, - branches: 92, + branches: 91, functions: 97, lines: 93, },