Skip to content

Commit

Permalink
Add backwards compatibility for ETH <-> WETH contract address validat…
Browse files Browse the repository at this point in the history
…ion (#10962)
  • Loading branch information
dan437 authored Apr 30, 2021
1 parent 04ab1e7 commit d6d924b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
9 changes: 5 additions & 4 deletions ui/pages/swaps/swaps.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()
);
};
40 changes: 37 additions & 3 deletions ui/pages/swaps/swaps.util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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 =
Expand Down

0 comments on commit d6d924b

Please sign in to comment.