fix issue where contractExchangeRates are not available in swaps controller, make token address matching case insensitive (#12770)

feature/default_network_editable
Alex Donesky 3 years ago committed by GitHub
parent c9baf39c4d
commit 6838a3d074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/scripts/controllers/swaps.js
  2. 17
      app/scripts/controllers/swaps.test.js
  3. 2
      app/scripts/metamask-controller.js

@ -28,6 +28,7 @@ import {
} from '../../../ui/pages/swaps/swaps.util'; } from '../../../ui/pages/swaps/swaps.util';
import fetchWithCache from '../../../ui/helpers/utils/fetch-with-cache'; import fetchWithCache from '../../../ui/helpers/utils/fetch-with-cache';
import { MINUTE, SECOND } from '../../../shared/constants/time'; import { MINUTE, SECOND } from '../../../shared/constants/time';
import { isEqualCaseInsensitive } from '../../../ui/helpers/utils/util';
import { NETWORK_EVENTS } from './network'; 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 // 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, networkController,
provider, provider,
getProviderConfig, getProviderConfig,
tokenRatesStore, getTokenRatesState,
fetchTradesInfo = defaultFetchTradesInfo, fetchTradesInfo = defaultFetchTradesInfo,
getCurrentChainId, getCurrentChainId,
getEIP1559GasFeeEstimates, getEIP1559GasFeeEstimates,
@ -105,7 +106,7 @@ export default class SwapsController {
this._getEIP1559GasFeeEstimates = getEIP1559GasFeeEstimates; this._getEIP1559GasFeeEstimates = getEIP1559GasFeeEstimates;
this.getBufferedGasLimit = getBufferedGasLimit; this.getBufferedGasLimit = getBufferedGasLimit;
this.tokenRatesStore = tokenRatesStore; this.getTokenRatesState = getTokenRatesState;
this.pollCount = 0; this.pollCount = 0;
this.getProviderConfig = getProviderConfig; this.getProviderConfig = getProviderConfig;
@ -610,7 +611,9 @@ export default class SwapsController {
} }
async _findTopQuoteAndCalculateSavings(quotes = {}) { async _findTopQuoteAndCalculateSavings(quotes = {}) {
const tokenConversionRates = this.tokenRatesStore.contractExchangeRates; const {
contractExchangeRates: tokenConversionRates,
} = this.getTokenRatesState();
const { const {
swapsState: { customGasPrice, customMaxPriorityFeePerGas }, swapsState: { customGasPrice, customMaxPriorityFeePerGas },
} = this.store.getState(); } = this.store.getState();
@ -734,7 +737,12 @@ export default class SwapsController {
decimalAdjustedDestinationAmount, decimalAdjustedDestinationAmount,
); );
const tokenConversionRate = tokenConversionRates[destinationToken]; const tokenConversionRate =
tokenConversionRates[
Object.keys(tokenConversionRates).find((tokenAddress) =>
isEqualCaseInsensitive(tokenAddress, destinationToken),
)
];
const conversionRateForSorting = tokenConversionRate || 1; const conversionRateForSorting = tokenConversionRate || 1;
const ethValueOfTokens = decimalAdjustedDestinationAmount.times( const ethValueOfTokens = decimalAdjustedDestinationAmount.times(
@ -777,7 +785,17 @@ export default class SwapsController {
isSwapsDefaultTokenAddress( isSwapsDefaultTokenAddress(
newQuotes[topAggId].destinationToken, newQuotes[topAggId].destinationToken,
chainId, chainId,
) || Boolean(tokenConversionRates[newQuotes[topAggId]?.destinationToken]); ) ||
Boolean(
tokenConversionRates[
Object.keys(tokenConversionRates).find((tokenAddress) =>
isEqualCaseInsensitive(
tokenAddress,
newQuotes[topAggId]?.destinationToken,
),
)
],
);
let savings = null; let savings = null;

@ -82,12 +82,12 @@ const MOCK_FETCH_METADATA = {
chainId: MAINNET_CHAIN_ID, chainId: MAINNET_CHAIN_ID,
}; };
const MOCK_TOKEN_RATES_STORE = { const MOCK_TOKEN_RATES_STORE = () => ({
contractExchangeRates: { contractExchangeRates: {
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': 2, '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': 2,
'0x1111111111111111111111111111111111111111': 0.1, '0x1111111111111111111111111111111111111111': 0.1,
}, },
}; });
const MOCK_GET_PROVIDER_CONFIG = () => ({ type: 'FAKE_NETWORK' }); const MOCK_GET_PROVIDER_CONFIG = () => ({ type: 'FAKE_NETWORK' });
@ -161,7 +161,7 @@ describe('SwapsController', function () {
networkController: getMockNetworkController(), networkController: getMockNetworkController(),
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
tokenRatesStore: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub, fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
getEIP1559GasFeeEstimates: getEIP1559GasFeeEstimatesStub, getEIP1559GasFeeEstimates: getEIP1559GasFeeEstimatesStub,
@ -211,7 +211,7 @@ describe('SwapsController', function () {
networkController, networkController,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
tokenRatesStore: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub, fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
@ -235,7 +235,7 @@ describe('SwapsController', function () {
networkController, networkController,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
tokenRatesStore: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub, fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
@ -259,7 +259,7 @@ describe('SwapsController', function () {
networkController, networkController,
provider, provider,
getProviderConfig: MOCK_GET_PROVIDER_CONFIG, getProviderConfig: MOCK_GET_PROVIDER_CONFIG,
tokenRatesStore: MOCK_TOKEN_RATES_STORE, getTokenRatesState: MOCK_TOKEN_RATES_STORE,
fetchTradesInfo: fetchTradesInfoStub, fetchTradesInfo: fetchTradesInfoStub,
getCurrentChainId: getCurrentChainIdStub, getCurrentChainId: getCurrentChainIdStub,
}); });
@ -816,9 +816,10 @@ describe('SwapsController', function () {
.stub(swapsController, '_getERC20Allowance') .stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1)); .resolves(ethers.BigNumber.from(1));
swapsController.tokenRatesStore = { swapsController.getTokenRatesState = () => ({
contractExchangeRates: {}, contractExchangeRates: {},
}; });
const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes( const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS, MOCK_FETCH_PARAMS,
MOCK_FETCH_METADATA, MOCK_FETCH_METADATA,

@ -619,7 +619,7 @@ export default class MetamaskController extends EventEmitter {
getProviderConfig: this.networkController.getProviderConfig.bind( getProviderConfig: this.networkController.getProviderConfig.bind(
this.networkController, this.networkController,
), ),
tokenRatesStore: this.tokenRatesController.state, getTokenRatesState: () => this.tokenRatesController.state,
getCurrentChainId: this.networkController.getCurrentChainId.bind( getCurrentChainId: this.networkController.getCurrentChainId.bind(
this.networkController, this.networkController,
), ),

Loading…
Cancel
Save