From e93b3b97d6aace43571b08c049d3947101238cc0 Mon Sep 17 00:00:00 2001 From: Alex Donesky Date: Mon, 22 Nov 2021 06:04:31 -0600 Subject: [PATCH] fix issue where contractExchangeRates are not available in swaps controller, make token address matching case insensitive (#12770) --- app/scripts/controllers/swaps.js | 28 ++++++++++++++++++++++----- app/scripts/controllers/swaps.test.js | 17 ++++++++-------- app/scripts/metamask-controller.js | 2 +- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 0d69dd77c..df29bc365 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -28,6 +28,7 @@ import { } from '../../../ui/pages/swaps/swaps.util'; import fetchWithCache from '../../../ui/helpers/utils/fetch-with-cache'; import { MINUTE, SECOND } from '../../../shared/constants/time'; +import { isEqualCaseInsensitive } from '../../../ui/helpers/utils/util'; import { NETWORK_EVENTS } from './network'; // The MAX_GAS_LIMIT is a number that is higher than the maximum gas costs we have observed on any aggregator @@ -91,7 +92,7 @@ export default class SwapsController { networkController, provider, getProviderConfig, - tokenRatesStore, + getTokenRatesState, fetchTradesInfo = defaultFetchTradesInfo, getCurrentChainId, getEIP1559GasFeeEstimates, @@ -105,7 +106,7 @@ export default class SwapsController { this._getEIP1559GasFeeEstimates = getEIP1559GasFeeEstimates; this.getBufferedGasLimit = getBufferedGasLimit; - this.tokenRatesStore = tokenRatesStore; + this.getTokenRatesState = getTokenRatesState; this.pollCount = 0; this.getProviderConfig = getProviderConfig; @@ -610,7 +611,9 @@ export default class SwapsController { } async _findTopQuoteAndCalculateSavings(quotes = {}) { - const tokenConversionRates = this.tokenRatesStore.contractExchangeRates; + const { + contractExchangeRates: tokenConversionRates, + } = this.getTokenRatesState(); const { swapsState: { customGasPrice, customMaxPriorityFeePerGas }, } = this.store.getState(); @@ -734,7 +737,12 @@ export default class SwapsController { decimalAdjustedDestinationAmount, ); - const tokenConversionRate = tokenConversionRates[destinationToken]; + const tokenConversionRate = + tokenConversionRates[ + Object.keys(tokenConversionRates).find((tokenAddress) => + isEqualCaseInsensitive(tokenAddress, destinationToken), + ) + ]; const conversionRateForSorting = tokenConversionRate || 1; const ethValueOfTokens = decimalAdjustedDestinationAmount.times( @@ -777,7 +785,17 @@ export default class SwapsController { isSwapsDefaultTokenAddress( newQuotes[topAggId].destinationToken, chainId, - ) || Boolean(tokenConversionRates[newQuotes[topAggId]?.destinationToken]); + ) || + Boolean( + tokenConversionRates[ + Object.keys(tokenConversionRates).find((tokenAddress) => + isEqualCaseInsensitive( + tokenAddress, + newQuotes[topAggId]?.destinationToken, + ), + ) + ], + ); let savings = null; diff --git a/app/scripts/controllers/swaps.test.js b/app/scripts/controllers/swaps.test.js index 7bc26a5aa..d0f4556ab 100644 --- a/app/scripts/controllers/swaps.test.js +++ b/app/scripts/controllers/swaps.test.js @@ -82,12 +82,12 @@ const MOCK_FETCH_METADATA = { chainId: MAINNET_CHAIN_ID, }; -const MOCK_TOKEN_RATES_STORE = { +const MOCK_TOKEN_RATES_STORE = () => ({ contractExchangeRates: { '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': 2, '0x1111111111111111111111111111111111111111': 0.1, }, -}; +}); const MOCK_GET_PROVIDER_CONFIG = () => ({ type: 'FAKE_NETWORK' }); @@ -161,7 +161,7 @@ describe('SwapsController', function () { networkController: getMockNetworkController(), provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, - tokenRatesStore: MOCK_TOKEN_RATES_STORE, + getTokenRatesState: MOCK_TOKEN_RATES_STORE, fetchTradesInfo: fetchTradesInfoStub, getCurrentChainId: getCurrentChainIdStub, getEIP1559GasFeeEstimates: getEIP1559GasFeeEstimatesStub, @@ -211,7 +211,7 @@ describe('SwapsController', function () { networkController, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, - tokenRatesStore: MOCK_TOKEN_RATES_STORE, + getTokenRatesState: MOCK_TOKEN_RATES_STORE, fetchTradesInfo: fetchTradesInfoStub, getCurrentChainId: getCurrentChainIdStub, }); @@ -235,7 +235,7 @@ describe('SwapsController', function () { networkController, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, - tokenRatesStore: MOCK_TOKEN_RATES_STORE, + getTokenRatesState: MOCK_TOKEN_RATES_STORE, fetchTradesInfo: fetchTradesInfoStub, getCurrentChainId: getCurrentChainIdStub, }); @@ -259,7 +259,7 @@ describe('SwapsController', function () { networkController, provider, getProviderConfig: MOCK_GET_PROVIDER_CONFIG, - tokenRatesStore: MOCK_TOKEN_RATES_STORE, + getTokenRatesState: MOCK_TOKEN_RATES_STORE, fetchTradesInfo: fetchTradesInfoStub, getCurrentChainId: getCurrentChainIdStub, }); @@ -816,9 +816,10 @@ describe('SwapsController', function () { .stub(swapsController, '_getERC20Allowance') .resolves(ethers.BigNumber.from(1)); - swapsController.tokenRatesStore = { + swapsController.getTokenRatesState = () => ({ contractExchangeRates: {}, - }; + }); + const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes( MOCK_FETCH_PARAMS, MOCK_FETCH_METADATA, diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 16b41ec54..563ef40ed 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -545,7 +545,7 @@ export default class MetamaskController extends EventEmitter { getProviderConfig: this.networkController.getProviderConfig.bind( this.networkController, ), - tokenRatesStore: this.tokenRatesController.state, + getTokenRatesState: () => this.tokenRatesController.state, getCurrentChainId: this.networkController.getCurrentChainId.bind( this.networkController, ),