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';
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;

@ -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,

@ -619,7 +619,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,
),

Loading…
Cancel
Save