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,
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()
);
};

@ -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 =

Loading…
Cancel
Save