Add backwards compatibility for ETH <-> WETH contract address validation (#10962)

feature/default_network_editable
Daniel 4 years ago committed by GitHub
parent 04ab1e72cc
commit d6d924b2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      ui/pages/swaps/swaps.util.js
  2. 40
      ui/pages/swaps/swaps.util.test.js

@ -695,7 +695,8 @@ export const isContractAddressValid = (
swapMetaData, swapMetaData,
chainId = MAINNET_CHAIN_ID, chainId = MAINNET_CHAIN_ID,
) => { ) => {
if (!contractAddress) { const contractAddressForChainId = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[chainId];
if (!contractAddress || !contractAddressForChainId) {
return false; return false;
} }
if ( if (
@ -707,12 +708,12 @@ export const isContractAddressValid = (
// Sometimes we get a contract address with a few upper-case chars and since addresses are // Sometimes we get a contract address with a few upper-case chars and since addresses are
// case-insensitive, we compare uppercase versions for validity. // case-insensitive, we compare uppercase versions for validity.
return ( 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 ( return (
contractAddressForChainId &&
contractAddressForChainId.toUpperCase() === contractAddress.toUpperCase() contractAddressForChainId.toUpperCase() === contractAddress.toUpperCase()
); );
}; };

@ -245,7 +245,7 @@ describe('Swaps Util', () => {
).toBe(true); ).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 = usedTradeTxParams.to =
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID]; SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID];
expect( expect(
@ -254,14 +254,38 @@ describe('Swaps Util', () => {
swapMetaData, swapMetaData,
MAINNET_CHAIN_ID, 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_from = WETH_SYMBOL;
swapMetaData.token_to = ETH_SYMBOL; swapMetaData.token_to = ETH_SYMBOL;
usedTradeTxParams.to = usedTradeTxParams.to =
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID]; 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( expect(
isContractAddressValid( isContractAddressValid(
usedTradeTxParams.to, usedTradeTxParams.to,
@ -277,6 +301,16 @@ describe('Swaps Util', () => {
).toBe(false); ).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', () => { it('returns true if "token_from" is BAT and "to" is mainnet contract address', () => {
swapMetaData.token_from = 'BAT'; swapMetaData.token_from = 'BAT';
usedTradeTxParams.to = usedTradeTxParams.to =

Loading…
Cancel
Save