@ -3,9 +3,15 @@ import BigNumber from 'bignumber.js';
import abi from 'human-standard-token-abi' ;
import { isValidAddress } from 'ethereumjs-util' ;
import {
ETH _SWAPS _TOKEN _OBJECT ,
METASWAP _API _HOST ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP ,
METASWAP _CHAINID _ API _HOST _MAP ,
} from '../../../../shared/constants/swaps' ;
import {
isSwapsDefaultTokenAddress ,
isSwapsDefaultTokenSymbol ,
} from '../../../../shared/modules/swaps.utils' ;
import { MAINNET _CHAIN _ID } from '../../../../shared/constants/network' ;
import {
calcTokenValue ,
calcTokenAmount ,
@ -30,22 +36,22 @@ const TOKEN_TRANSFER_LOG_TOPIC_HASH =
const CACHE _REFRESH _ONE _HOUR = 3600000 ;
const getBaseApi = function ( type ) {
const getBaseApi = function ( type , chainId = MAINNET _CHAIN _ID ) {
switch ( type ) {
case 'trade' :
return ` ${ METASWAP _API _HOST } /trades? ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /trades? ` ;
case 'tokens' :
return ` ${ METASWAP _API _HOST } /tokens ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /tokens ` ;
case 'topAssets' :
return ` ${ METASWAP _API _HOST } /topAssets ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /topAssets ` ;
case 'featureFlag' :
return ` ${ METASWAP _API _HOST } /featureFlag ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /featureFlag ` ;
case 'aggregatorMetadata' :
return ` ${ METASWAP _API _HOST } /aggregatorMetadata ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /aggregatorMetadata ` ;
case 'gasPrices' :
return ` ${ METASWAP _API _HOST } /gasPrices ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /gasPrices ` ;
case 'refreshTime' :
return ` ${ METASWAP _API _HOST } /quoteRefreshRate ` ;
return ` ${ METASWAP _CHAINID _ API _HOST _MAP [ chainId ] } /quoteRefreshRate ` ;
default :
throw new Error ( 'getBaseApi requires an api call type' ) ;
}
@ -205,7 +211,8 @@ function validateData(validators, object, urlUsed) {
} ) ;
}
export async function fetchTradesInfo ( {
export async function fetchTradesInfo (
{
slippage ,
sourceToken ,
sourceDecimals ,
@ -213,7 +220,9 @@ export async function fetchTradesInfo({
value ,
fromAddress ,
exchangeList ,
} ) {
} ,
{ chainId } ,
) {
const urlParams = {
destinationToken ,
sourceToken ,
@ -228,7 +237,7 @@ export async function fetchTradesInfo({
}
const queryString = new URLSearchParams ( urlParams ) . toString ( ) ;
const tradeURL = ` ${ getBaseApi ( 'trade' ) } ${ queryString } ` ;
const tradeURL = ` ${ getBaseApi ( 'trade' , chainId ) } ${ queryString } ` ;
const tradesResponse = await fetchWithCache (
tradeURL ,
{ method : 'GET' } ,
@ -272,21 +281,21 @@ export async function fetchTradesInfo({
return newQuotes ;
}
export async function fetchTokens ( ) {
const tokenUrl = getBaseApi ( 'tokens' ) ;
export async function fetchTokens ( chainId ) {
const tokenUrl = getBaseApi ( 'tokens' , chainId ) ;
const tokens = await fetchWithCache (
tokenUrl ,
{ method : 'GET' } ,
{ cacheRefreshTime : CACHE _REFRESH _ONE _HOUR } ,
) ;
const filteredTokens = [
ETH _SWAPS _TOKEN _OBJECT ,
SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ chainId ] ,
... tokens . filter ( ( token ) => {
return (
validateData ( TOKEN _VALIDATORS , token , tokenUrl ) &&
! (
token . symbol === ETH _SWAPS _TOKEN _OBJECT . symbol ||
token . address === ETH _SWAPS _TOKEN _OBJECT . address
isSwapsDefaultTokenSymbol ( token . symbol , chainId ) ||
isSwapsDefaultTokenAddress ( token . address , chainId )
)
) ;
} ) ,
@ -294,8 +303,8 @@ export async function fetchTokens() {
return filteredTokens ;
}
export async function fetchAggregatorMetadata ( ) {
const aggregatorMetadataUrl = getBaseApi ( 'aggregatorMetadata' ) ;
export async function fetchAggregatorMetadata ( chainId ) {
const aggregatorMetadataUrl = getBaseApi ( 'aggregatorMetadata' , chainId ) ;
const aggregators = await fetchWithCache (
aggregatorMetadataUrl ,
{ method : 'GET' } ,
@ -316,8 +325,8 @@ export async function fetchAggregatorMetadata() {
return filteredAggregators ;
}
export async function fetchTopAssets ( ) {
const topAssetsUrl = getBaseApi ( 'topAssets' ) ;
export async function fetchTopAssets ( chainId ) {
const topAssetsUrl = getBaseApi ( 'topAssets' , chainId ) ;
const response = await fetchWithCache (
topAssetsUrl ,
{ method : 'GET' } ,
@ -332,18 +341,18 @@ export async function fetchTopAssets() {
return topAssetsMap ;
}
export async function fetchSwapsFeatureLiveness ( ) {
export async function fetchSwapsFeatureLiveness ( chainId ) {
const status = await fetchWithCache (
getBaseApi ( 'featureFlag' ) ,
getBaseApi ( 'featureFlag' , chainId ) ,
{ method : 'GET' } ,
{ cacheRefreshTime : 600000 } ,
) ;
return status ? . active ;
}
export async function fetchSwapsQuoteRefreshTime ( ) {
export async function fetchSwapsQuoteRefreshTime ( chainId ) {
const response = await fetchWithCache (
getBaseApi ( 'refreshTime' ) ,
getBaseApi ( 'refreshTime' , chainId ) ,
{ method : 'GET' } ,
{ cacheRefreshTime : 600000 } ,
) ;
@ -378,8 +387,8 @@ export async function fetchTokenBalance(address, userAddress) {
return usersToken ;
}
export async function fetchSwapsGasPrices ( ) {
const gasPricesUrl = getBaseApi ( 'gasPrices' ) ;
export async function fetchSwapsGasPrices ( chainId ) {
const gasPricesUrl = getBaseApi ( 'gasPrices' , chainId ) ;
const response = await fetchWithCache (
gasPricesUrl ,
{ method : 'GET' } ,
@ -408,7 +417,7 @@ export async function fetchSwapsGasPrices() {
} ;
}
export function getRenderableNetworkFeesForQuote (
export function getRenderableNetworkFeesForQuote ( {
tradeGas ,
approveGas ,
gasPrice ,
@ -417,14 +426,18 @@ export function getRenderableNetworkFeesForQuote(
tradeValue ,
sourceSymbol ,
sourceAmount ,
) {
chainId ,
} ) {
const totalGasLimitForCalculation = new BigNumber ( tradeGas || '0x0' , 16 )
. plus ( approveGas || '0x0' , 16 )
. toString ( 16 ) ;
const gasTotalInWeiHex = calcGasTotal ( totalGasLimitForCalculation , gasPrice ) ;
const nonGasFee = new BigNumber ( tradeValue , 16 )
. minus ( sourceSymbol === 'ETH' ? sourceAmount : 0 , 10 )
. minus (
isSwapsDefaultTokenSymbol ( sourceSymbol , chainId ) ? sourceAmount : 0 ,
10 ,
)
. toString ( 16 ) ;
const totalWeiCost = new BigNumber ( gasTotalInWeiHex , 16 )
@ -447,7 +460,7 @@ export function getRenderableNetworkFeesForQuote(
rawNetworkFees ,
rawEthFee : ethFee ,
feeInFiat : formattedNetworkFee ,
feeInEth : ` ${ ethFee } ETH ` ,
feeInEth : ` ${ ethFee } ${ SWAPS _CHAINID _DEFAULT _TOKEN _MAP [ chainId ] . symbol } ` ,
nonGasFee ,
} ;
}
@ -459,6 +472,7 @@ export function quotesToRenderableData(
currentCurrency ,
approveGas ,
tokenConversionRates ,
chainId ,
) {
return Object . values ( quotes ) . map ( ( quote ) => {
const {
@ -488,16 +502,17 @@ export function quotesToRenderableData(
rawNetworkFees ,
rawEthFee ,
feeInEth ,
} = getRenderableNetworkFeesForQuote (
gasEstimateWithRefund || decimalToHex ( averageGas || 800000 ) ,
} = getRenderableNetworkFeesForQuote ( {
tradeGas : gasEstimateWithRefund || decimalToHex ( averageGas || 800000 ) ,
approveGas ,
gasPrice ,
currentCurrency ,
conversionRate ,
trade . value ,
sourceTokenInfo . symbol ,
tradeValue : trade . value ,
sourceSymbol : source TokenInfo . symbol ,
sourceAmount ,
) ;
chainId ,
} ) ;
const slippageMultiplier = new BigNumber ( 100 - slippage ) . div ( 100 ) ;
const minimumAmountReceived = new BigNumber ( destinationValue )
@ -506,12 +521,14 @@ export function quotesToRenderableData(
const tokenConversionRate =
tokenConversionRates [ destinationTokenInfo . address ] ;
const ethValueOfTrade =
destinationTokenInfo . symbol === 'ETH'
? calcTokenAmount (
destinationAmount ,
destinationTokenInfo . decimals ,
) . minus ( rawEthFee , 10 )
const ethValueOfTrade = isSwapsDefaultTokenSymbol (
destinationTokenInfo . symbol ,
chainId ,
)
? calcTokenAmount ( destinationAmount , destinationTokenInfo . decimals ) . minus (
rawEthFee ,
10 ,
)
: new BigNumber ( tokenConversionRate || 0 , 10 )
. times (
calcTokenAmount ( destinationAmount , destinationTokenInfo . decimals ) ,
@ -566,9 +583,10 @@ export function getSwapsTokensReceivedFromTxMeta(
accountAddress ,
tokenDecimals ,
approvalTxMeta ,
chainId ,
) {
const txReceipt = txMeta ? . txReceipt ;
if ( tokenSymbol === 'ETH' ) {
if ( isSwapsDefaultTokenSymbol ( tokenSymbol , chainId ) ) {
if (
! txReceipt ||
! txMeta ||