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 = {}) { async _findTopQuoteAndCalculateSavings(quotes = {}) {
const tokenConversionRates = this.tokenRatesStore.getState() const tokenConversionRates = this.tokenRatesStore.contractExchangeRates;
.contractExchangeRates;
const { const {
swapsState: { customGasPrice, customMaxPriorityFeePerGas }, swapsState: { customGasPrice, customMaxPriorityFeePerGas },
} = this.store.getState(); } = this.store.getState();

View File

@ -4,7 +4,6 @@ import sinon from 'sinon';
import { ethers } from 'ethers'; import { ethers } from 'ethers';
import { mapValues } from 'lodash'; import { mapValues } from 'lodash';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import { ObservableStore } from '@metamask/obs-store';
import { import {
ROPSTEN_NETWORK_ID, ROPSTEN_NETWORK_ID,
MAINNET_NETWORK_ID, MAINNET_NETWORK_ID,
@ -83,12 +82,12 @@ const MOCK_FETCH_METADATA = {
chainId: MAINNET_CHAIN_ID, chainId: MAINNET_CHAIN_ID,
}; };
const MOCK_TOKEN_RATES_STORE = new ObservableStore({ 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' });
@ -816,9 +815,9 @@ describe('SwapsController', function () {
.stub(swapsController, '_getERC20Allowance') .stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1)); .resolves(ethers.BigNumber.from(1));
swapsController.tokenRatesStore.updateState({ swapsController.tokenRatesStore = {
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,

View File

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

View File

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