@ -1,8 +1,6 @@
import nock from 'nock' ;
import { MOCKS } from '../../../test/jest' ;
import {
ETH _SYMBOL ,
WETH _SYMBOL ,
MAINNET _CHAIN _ID ,
BSC _CHAIN _ID ,
POLYGON _CHAIN _ID ,
@ -12,7 +10,10 @@ import {
} from '../../../shared/constants/network' ;
import {
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP ,
ETH _WETH _CONTRACT _ADDRESS ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP ,
WETH _CONTRACT _ADDRESS ,
WBNB _CONTRACT _ADDRESS ,
WMATIC _CONTRACT _ADDRESS ,
ETHEREUM ,
POLYGON ,
BSC ,
@ -34,6 +35,7 @@ import {
getNetworkNameByChainId ,
getSwapsLivenessForNetwork ,
countDecimals ,
shouldEnableDirectWrapping ,
} from './swaps.util' ;
jest . mock ( '../../helpers/utils/storage-helpers.js' , ( ) => ( {
@ -188,197 +190,113 @@ describe('Swaps Util', () => {
} ) ;
describe ( 'isContractAddressValid' , ( ) => {
let swapMetaData ;
let usedTradeTxParams ;
beforeEach ( ( ) => {
swapMetaData = {
available _quotes : undefined ,
average _savings : undefined ,
best _quote _source : 'paraswap' ,
custom _slippage : true ,
estimated _gas : '134629' ,
fee _savings : undefined ,
gas _fees : '47.411896' ,
median _metamask _fee : undefined ,
other _quote _selected : false ,
other _quote _selected _source : '' ,
performance _savings : undefined ,
slippage : 5 ,
suggested _gas _price : '164' ,
token _from : ETH _SYMBOL ,
token _from _amount : '1' ,
token _to : WETH _SYMBOL ,
token _to _amount : '1.0000000' ,
used _gas _price : '164' ,
} ;
usedTradeTxParams = {
data : 'testData' ,
from : '0xe53a5bc256898bfa5673b20aceeb2b2152075d17' ,
gas : '2427c' ,
gasPrice : '27592f5a00' ,
to : ETH _ WETH_CONTRACT _ADDRESS ,
to : WETH _CONTRACT _ADDRESS ,
value : '0xde0b6b3a7640000' ,
} ;
} ) ;
it ( 'returns true if "token_from" is ETH, "token_to" is WETH and "to" is ETH_WETH contract address' , ( ) => {
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_from" is WETH, "token_to" is ETH and "to" is ETH_WETH contract address' , ( ) => {
swapMetaData . token _from = WETH _SYMBOL ;
swapMetaData . token _to = ETH _SYMBOL ;
it ( 'returns true if "to" is WETH contract address' , ( ) => {
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , MAINNET _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_from " is ETH, "token_to" is WETH and "to" is ETH_ WETH contract address with some uppercase chars' , ( ) => {
it ( 'returns true if "to" is WETH contract address with some uppercase chars' , ( ) => {
usedTradeTxParams . to = '0xc02AAA39B223fe8d0a0e5c4f27ead9083c756cc2' ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , MAINNET _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_from " is ETH, "token_to" is W ETH and "to" is mainnet contract address' , ( ) => {
it ( 'returns true if "to" is ETH mainnet contract address on ETH mainnet' , ( ) => {
usedTradeTxParams . to =
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ MAINNET _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , MAINNET _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
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 true if "to" is WBNB contract address on BSC mainnet' , ( ) => {
usedTradeTxParams . to = WBNB _CONTRACT _ADDRESS ;
expect ( isContractAddressValid ( usedTradeTxParams . to , BSC _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 ] ;
it ( 'returns true if "to" is WMATIC contract address on Polygon mainnet' , ( ) => {
usedTradeTxParams . to = WMATIC _CONTRACT _ADDRESS ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
) . toBe ( false ) ;
isContractAddressValid ( usedTradeTxParams . to , POLYGON _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
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 ;
it ( 'returns false if "to" is BSC contract address on ETH mainnet' , ( ) => {
usedTradeTxParams . to = SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ BSC _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , MAINNET _CHAIN _ID ) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns false if contractAddress is null' , ( ) => {
expect (
isContractAddressValid ( null , swapMetaData , LOCALHOST _CHAIN _ID ) ,
) . toBe ( false ) ;
expect ( isContractAddressValid ( null , LOCALHOST _CHAIN _ID ) ) . toBe ( false ) ;
} ) ;
it ( 'returns false if chainId is incorrect' , ( ) => {
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
'incorrectChainId' ,
) ,
isContractAddressValid ( usedTradeTxParams . to , 'incorrectChainId' ) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns true if "token_from" is BAT and "to" is mainnet contract address' , ( ) => {
swapMetaData . token _from = 'BAT' ;
it ( 'returns true if "to" is BSC contract address on BSC network' , ( ) => {
usedTradeTxParams . to = SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ BSC _CHAIN _ID ] ;
expect ( isContractAddressValid ( usedTradeTxParams . to , BSC _CHAIN _ID ) ) . toBe (
true ,
) ;
} ) ;
it ( 'returns true if "to" is Polygon contract address on Polygon network' , ( ) => {
usedTradeTxParams . to =
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ MAINNET _CHAIN _ID ] ;
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ POLYGON _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
MAINNET _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , POLYGON _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_to" is BAT and "to" is BSC contract address ' , ( ) => {
swapMetaData . token _to = 'BAT' ;
usedTradeTxParams . to = SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ BSC _CHAIN _ID ] ;
it ( 'returns true if "to" is Rinkeby contract address on Rinkeby network' , ( ) => {
usedTradeTxParams . to =
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ RINKEBY _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
BSC _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , RINKEBY _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_to" is BAT and "to" is testnet contract address' , ( ) => {
swapMetaData . token _to = 'BAT' ;
it ( 'returns true if "to" is testnet contract address' , ( ) => {
usedTradeTxParams . to =
SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ LOCALHOST _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
LOCALHOST _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , LOCALHOST _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if "token_to" is BAT and "to" is testnet contract address with some uppercase chars' , ( ) => {
swapMetaData . token _to = 'BAT' ;
it ( 'returns true if "to" is testnet contract address with some uppercase chars' , ( ) => {
usedTradeTxParams . to = '0x881D40237659C251811CEC9c364ef91dC08D300C' ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
LOCALHOST _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , LOCALHOST _CHAIN _ID ) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns false if "token_to" is BAT and "to " has mismatch with current chainId' , ( ) => {
swapMetaData . token _to = 'BAT' ;
it ( 'returns false if "to" has mismatch with current chainId' , ( ) => {
usedTradeTxParams . to = SWAPS _CHAINID _CONTRACT _ADDRESS _MAP [ BSC _CHAIN _ID ] ;
expect (
isContractAddressValid (
usedTradeTxParams . to ,
swapMetaData ,
LOCALHOST _CHAIN _ID ,
) ,
isContractAddressValid ( usedTradeTxParams . to , LOCALHOST _CHAIN _ID ) ,
) . toBe ( false ) ;
} ) ;
} ) ;
@ -492,4 +410,114 @@ describe('Swaps Util', () => {
expect ( countDecimals ( 1.123456789 ) ) . toBe ( 9 ) ;
} ) ;
} ) ;
describe ( 'shouldEnableDirectWrapping' , ( ) => {
const randomTokenAddress = '0x881d40237659c251811cec9c364ef91234567890' ;
it ( 'returns true if swapping from ETH to WETH' , ( ) => {
expect (
shouldEnableDirectWrapping (
MAINNET _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ MAINNET _CHAIN _ID ] ? . address ,
WETH _CONTRACT _ADDRESS ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if swapping from WETH to ETH' , ( ) => {
expect (
shouldEnableDirectWrapping (
MAINNET _CHAIN _ID ,
WETH _CONTRACT _ADDRESS ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ MAINNET _CHAIN _ID ] ? . address ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns false if swapping from ETH to a non-WETH token' , ( ) => {
expect (
shouldEnableDirectWrapping (
MAINNET _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ MAINNET _CHAIN _ID ] ? . address ,
randomTokenAddress ,
) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns true if swapping from BNB to WBNB' , ( ) => {
expect (
shouldEnableDirectWrapping (
BSC _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ BSC _CHAIN _ID ] ? . address ,
WBNB _CONTRACT _ADDRESS ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if swapping from WBNB to BNB' , ( ) => {
expect (
shouldEnableDirectWrapping (
BSC _CHAIN _ID ,
WBNB _CONTRACT _ADDRESS ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ BSC _CHAIN _ID ] ? . address ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns false if swapping from BNB to a non-WBNB token' , ( ) => {
expect (
shouldEnableDirectWrapping (
BSC _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ BSC _CHAIN _ID ] ? . address ,
randomTokenAddress ,
) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns true if swapping from MATIC to WMATIC' , ( ) => {
expect (
shouldEnableDirectWrapping (
POLYGON _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ POLYGON _CHAIN _ID ] ? . address ,
WMATIC _CONTRACT _ADDRESS ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns true if swapping from WMATIC to MATIC' , ( ) => {
expect (
shouldEnableDirectWrapping (
POLYGON _CHAIN _ID ,
WMATIC _CONTRACT _ADDRESS ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ POLYGON _CHAIN _ID ] ? . address ,
) ,
) . toBe ( true ) ;
} ) ;
it ( 'returns false if swapping from MATIC to a non-WMATIC token' , ( ) => {
expect (
shouldEnableDirectWrapping (
POLYGON _CHAIN _ID ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ POLYGON _CHAIN _ID ] ? . address ,
randomTokenAddress ,
) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns false if a source token is undefined' , ( ) => {
expect (
shouldEnableDirectWrapping (
MAINNET _CHAIN _ID ,
undefined ,
WETH _CONTRACT _ADDRESS ,
) ,
) . toBe ( false ) ;
} ) ;
it ( 'returns false if a destination token is undefined' , ( ) => {
expect (
shouldEnableDirectWrapping ( MAINNET _CHAIN _ID , WETH _CONTRACT _ADDRESS ) ,
) . toBe ( false ) ;
} ) ;
} ) ;
} ) ;