From d6d924b2b5f28698d30144e1d76c3e3a326a67d5 Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Fri, 30 Apr 2021 11:48:56 -0700 Subject: [PATCH] Add backwards compatibility for ETH <-> WETH contract address validation (#10962) --- ui/pages/swaps/swaps.util.js | 9 +++---- ui/pages/swaps/swaps.util.test.js | 40 ++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/ui/pages/swaps/swaps.util.js b/ui/pages/swaps/swaps.util.js index dbc447222..45f200a2f 100644 --- a/ui/pages/swaps/swaps.util.js +++ b/ui/pages/swaps/swaps.util.js @@ -695,7 +695,8 @@ export const isContractAddressValid = ( swapMetaData, chainId = MAINNET_CHAIN_ID, ) => { - if (!contractAddress) { + const contractAddressForChainId = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[chainId]; + if (!contractAddress || !contractAddressForChainId) { return false; } if ( @@ -707,12 +708,12 @@ export const isContractAddressValid = ( // Sometimes we get a contract address with a few upper-case chars and since addresses are // case-insensitive, we compare uppercase versions for validity. return ( - contractAddress.toUpperCase() === ETH_WETH_CONTRACT_ADDRESS.toUpperCase() + contractAddress.toUpperCase() === + ETH_WETH_CONTRACT_ADDRESS.toUpperCase() || + contractAddressForChainId.toUpperCase() === contractAddress.toUpperCase() ); } - const contractAddressForChainId = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[chainId]; return ( - contractAddressForChainId && contractAddressForChainId.toUpperCase() === contractAddress.toUpperCase() ); }; diff --git a/ui/pages/swaps/swaps.util.test.js b/ui/pages/swaps/swaps.util.test.js index d86273f0c..936864f0f 100644 --- a/ui/pages/swaps/swaps.util.test.js +++ b/ui/pages/swaps/swaps.util.test.js @@ -245,7 +245,7 @@ describe('Swaps Util', () => { ).toBe(true); }); - it('returns false if "token_from" is ETH, "token_to" is WETH and "to" is mainnet contract address', () => { + it('returns true if "token_from" is ETH, "token_to" is WETH and "to" is mainnet contract address', () => { usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID]; expect( @@ -254,14 +254,38 @@ describe('Swaps Util', () => { swapMetaData, MAINNET_CHAIN_ID, ), - ).toBe(false); + ).toBe(true); }); - it('returns false if "token_from" is WETH, "token_to" is ETH and "to" is mainnet contract address', () => { + it('returns true if "token_from" is WETH, "token_to" is ETH and "to" is mainnet contract address', () => { swapMetaData.token_from = WETH_SYMBOL; swapMetaData.token_to = ETH_SYMBOL; usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID]; + expect( + isContractAddressValid( + usedTradeTxParams.to, + swapMetaData, + MAINNET_CHAIN_ID, + ), + ).toBe(true); + }); + + it('returns false if "token_from" is ETH, "token_to" is WETH and "to" is BSC contract address', () => { + usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[BSC_CHAIN_ID]; + expect( + isContractAddressValid( + usedTradeTxParams.to, + swapMetaData, + MAINNET_CHAIN_ID, + ), + ).toBe(false); + }); + + it('returns false if "token_from" is WETH, "token_to" is ETH and "to" is BSC contract address', () => { + swapMetaData.token_from = WETH_SYMBOL; + swapMetaData.token_to = ETH_SYMBOL; + usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[BSC_CHAIN_ID]; expect( isContractAddressValid( usedTradeTxParams.to, @@ -277,6 +301,16 @@ describe('Swaps Util', () => { ).toBe(false); }); + it('returns false if chainId is incorrect', () => { + expect( + isContractAddressValid( + usedTradeTxParams.to, + swapMetaData, + 'incorrectChainId', + ), + ).toBe(false); + }); + it('returns true if "token_from" is BAT and "to" is mainnet contract address', () => { swapMetaData.token_from = 'BAT'; usedTradeTxParams.to =