1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Hook up shared TokenRatesController (#12066)

This commit is contained in:
Alex Donesky 2021-09-15 14:02:28 -05:00 committed by GitHub
parent cf569161fb
commit 9d968bae1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 18 deletions

View File

@ -580,8 +580,7 @@ export default class SwapsController {
}
async _findTopQuoteAndCalculateSavings(quotes = {}) {
const tokenConversionRates = this.tokenRatesStore.getState()
.contractExchangeRates;
const tokenConversionRates = this.tokenRatesStore.contractExchangeRates;
const {
swapsState: { customGasPrice, customMaxPriorityFeePerGas },
} = this.store.getState();

View File

@ -4,7 +4,6 @@ import sinon from 'sinon';
import { ethers } from 'ethers';
import { mapValues } from 'lodash';
import BigNumber from 'bignumber.js';
import { ObservableStore } from '@metamask/obs-store';
import {
ROPSTEN_NETWORK_ID,
MAINNET_NETWORK_ID,
@ -83,12 +82,12 @@ const MOCK_FETCH_METADATA = {
chainId: MAINNET_CHAIN_ID,
};
const MOCK_TOKEN_RATES_STORE = new ObservableStore({
const MOCK_TOKEN_RATES_STORE = {
contractExchangeRates: {
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': 2,
'0x1111111111111111111111111111111111111111': 0.1,
},
});
};
const MOCK_GET_PROVIDER_CONFIG = () => ({ type: 'FAKE_NETWORK' });
@ -816,9 +815,9 @@ describe('SwapsController', function () {
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
swapsController.tokenRatesStore.updateState({
swapsController.tokenRatesStore = {
contractExchangeRates: {},
});
};
const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS,
MOCK_FETCH_METADATA,

View File

@ -26,6 +26,7 @@ import {
GasFeeController,
TokenListController,
TokensController,
TokenRatesController,
} from '@metamask/controllers';
import { TRANSACTION_STATUSES } from '../../shared/constants/transaction';
import {
@ -66,7 +67,6 @@ import TransactionController from './controllers/transactions';
import DetectTokensController from './controllers/detect-tokens';
import SwapsController from './controllers/swaps';
import { PermissionsController } from './controllers/permissions';
import TokenRatesController from './controllers/token-rates';
import { NOTIFICATION_NAMES } from './controllers/permissions/enums';
import getRestrictedMethods from './controllers/permissions/restrictedMethods';
import nodeify from './lib/nodeify';
@ -282,12 +282,26 @@ export default class MetamaskController extends EventEmitter {
initState.NotificationController,
);
// token exchange rate tracker
this.tokenRatesController = new TokenRatesController({
tokensController: this.tokensController,
getNativeCurrency: () => {
const { ticker } = this.networkController.getProviderConfig();
return ticker ?? 'ETH';
},
onTokensStateChange: (listener) =>
this.tokensController.subscribe(listener),
onCurrencyRateStateChange: (listener) =>
this.controllerMessenger.subscribe(
`${this.currencyRateController.name}:stateChange`,
listener,
),
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
});
this.ensController = new EnsController({
@ -328,13 +342,11 @@ export default class MetamaskController extends EventEmitter {
if (activeControllerConnections > 0) {
this.accountTracker.start();
this.incomingTransactionsController.start();
this.tokenRatesController.start();
this.currencyRateController.start();
this.tokenListController.start();
} else {
this.accountTracker.stop();
this.incomingTransactionsController.stop();
this.tokenRatesController.stop();
this.currencyRateController.stop();
this.tokenListController.stop();
}
@ -525,7 +537,7 @@ export default class MetamaskController extends EventEmitter {
getProviderConfig: this.networkController.getProviderConfig.bind(
this.networkController,
),
tokenRatesStore: this.tokenRatesController.store,
tokenRatesStore: this.tokenRatesController.state,
getCurrentChainId: this.networkController.getCurrentChainId.bind(
this.networkController,
),
@ -581,7 +593,7 @@ export default class MetamaskController extends EventEmitter {
AccountTracker: this.accountTracker.store,
TxController: this.txController.memStore,
CachedBalancesController: this.cachedBalancesController.store,
TokenRatesController: this.tokenRatesController.store,
TokenRatesController: this.tokenRatesController,
MessageManager: this.messageManager.memStore,
PersonalMessageManager: this.personalMessageManager.memStore,
DecryptMessageManager: this.decryptMessageManager.memStore,

View File

@ -22,6 +22,7 @@ import {
getMaximumGasTotalInHexWei,
getMinimumGasTotalInHexWei,
} from '../../shared/modules/gas.utils';
import { isEqualCaseInsensitive } from '../helpers/utils/util';
import { getAveragePriceEstimateInHexWEI } from './custom-gas';
import { getCurrentChainId, deprecatedGetCurrentNetworkId } from './selectors';
import { checkNetworkAndAccountSupports1559 } from '.';
@ -222,7 +223,12 @@ export const sendTokenTokenAmountAndToAddressSelector = createSelector(
export const contractExchangeRateSelector = createSelector(
contractExchangeRatesSelector,
tokenAddressSelector,
(contractExchangeRates, tokenAddress) => contractExchangeRates[tokenAddress],
(contractExchangeRates, tokenAddress) =>
contractExchangeRates[
Object.keys(contractExchangeRates).find((address) =>
isEqualCaseInsensitive(address, tokenAddress),
)
],
);
export const transactionFeeSelector = function (state, txData) {