mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Removing unused TokenRatesController (#12376)
This commit is contained in:
parent
54f57826c4
commit
2712048c0e
@ -1,68 +0,0 @@
|
|||||||
import { strict as assert } from 'assert';
|
|
||||||
import sinon from 'sinon';
|
|
||||||
import { TokensController } from '@metamask/controllers';
|
|
||||||
import TokenRatesController from './token-rates';
|
|
||||||
import NetworkController from './network';
|
|
||||||
import PreferencesController from './preferences';
|
|
||||||
|
|
||||||
const networkControllerProviderConfig = {
|
|
||||||
getAccounts: () => undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('TokenRatesController', function () {
|
|
||||||
let nativeCurrency,
|
|
||||||
getNativeCurrency,
|
|
||||||
network,
|
|
||||||
provider,
|
|
||||||
preferences,
|
|
||||||
tokensController;
|
|
||||||
beforeEach(function () {
|
|
||||||
nativeCurrency = 'ETH';
|
|
||||||
getNativeCurrency = () => nativeCurrency;
|
|
||||||
network = new NetworkController();
|
|
||||||
network.setInfuraProjectId('foo');
|
|
||||||
network.initializeProvider(networkControllerProviderConfig);
|
|
||||||
provider = network.getProviderAndBlockTracker().provider;
|
|
||||||
preferences = new PreferencesController({ network, provider });
|
|
||||||
tokensController = new TokensController({
|
|
||||||
onPreferencesStateChange: preferences.store.subscribe.bind(
|
|
||||||
preferences.store,
|
|
||||||
),
|
|
||||||
onNetworkStateChange: network.store.subscribe.bind(network.store),
|
|
||||||
});
|
|
||||||
sinon.stub(network, 'getLatestBlock').callsFake(() => Promise.resolve({}));
|
|
||||||
sinon.stub(tokensController, '_instantiateNewEthersProvider').returns(null);
|
|
||||||
sinon
|
|
||||||
.stub(tokensController, '_detectIsERC721')
|
|
||||||
.returns(Promise.resolve(false));
|
|
||||||
});
|
|
||||||
it('should listen for tokenControllers state updates', async function () {
|
|
||||||
const controller = new TokenRatesController({
|
|
||||||
tokensController,
|
|
||||||
getNativeCurrency,
|
|
||||||
});
|
|
||||||
await tokensController.addToken('0x1', 'TEST', 1);
|
|
||||||
assert.deepEqual(controller._tokens, [
|
|
||||||
{
|
|
||||||
address: '0x1',
|
|
||||||
decimals: 1,
|
|
||||||
symbol: 'TEST',
|
|
||||||
image: undefined,
|
|
||||||
isERC721: false,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should poll on correct interval', async function () {
|
|
||||||
const stub = sinon.stub(global, 'setInterval');
|
|
||||||
const controller = new TokenRatesController({
|
|
||||||
tokensController,
|
|
||||||
getNativeCurrency,
|
|
||||||
});
|
|
||||||
controller.start(1337);
|
|
||||||
|
|
||||||
assert.strictEqual(stub.getCall(0).args[1], 1337);
|
|
||||||
stub.restore();
|
|
||||||
controller.stop();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,88 +0,0 @@
|
|||||||
import { ObservableStore } from '@metamask/obs-store';
|
|
||||||
import log from 'loglevel';
|
|
||||||
import { normalize as normalizeAddress } from 'eth-sig-util';
|
|
||||||
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
|
||||||
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
|
|
||||||
import { MINUTE, SECOND } from '../../../shared/constants/time';
|
|
||||||
|
|
||||||
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
|
||||||
|
|
||||||
// By default, poll every 3 minutes
|
|
||||||
const DEFAULT_INTERVAL = MINUTE * 3;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A controller that polls for token exchange
|
|
||||||
* rates based on a user's current token list
|
|
||||||
*/
|
|
||||||
export default class TokenRatesController {
|
|
||||||
/**
|
|
||||||
* Creates a TokenRatesController
|
|
||||||
*
|
|
||||||
* @param {Object} [config] - Options to configure controller
|
|
||||||
*/
|
|
||||||
constructor({ tokensController, getNativeCurrency } = {}) {
|
|
||||||
this.store = new ObservableStore();
|
|
||||||
this.getNativeCurrency = getNativeCurrency;
|
|
||||||
this.tokens = tokensController.state.tokens;
|
|
||||||
tokensController.subscribe(({ tokens = [] }) => {
|
|
||||||
this.tokens = tokens;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates exchange rates for all tokens
|
|
||||||
*/
|
|
||||||
async updateExchangeRates() {
|
|
||||||
const contractExchangeRates = {};
|
|
||||||
const nativeCurrency = this.getNativeCurrency().toLowerCase();
|
|
||||||
const pairs = this._tokens.map((token) => token.address).join(',');
|
|
||||||
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`;
|
|
||||||
if (this._tokens.length > 0) {
|
|
||||||
try {
|
|
||||||
const response = await fetchWithTimeout(
|
|
||||||
`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`,
|
|
||||||
);
|
|
||||||
const prices = await response.json();
|
|
||||||
this._tokens.forEach((token) => {
|
|
||||||
const price =
|
|
||||||
prices[token.address.toLowerCase()] ||
|
|
||||||
prices[toChecksumHexAddress(token.address)];
|
|
||||||
contractExchangeRates[normalizeAddress(token.address)] = price
|
|
||||||
? price[nativeCurrency]
|
|
||||||
: 0;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
log.warn(
|
|
||||||
`MetaMask - TokenRatesController exchange rate fetch failed.`,
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.store.putState({ contractExchangeRates });
|
|
||||||
}
|
|
||||||
|
|
||||||
/* eslint-disable accessor-pairs */
|
|
||||||
/**
|
|
||||||
* @type {Array}
|
|
||||||
*/
|
|
||||||
set tokens(tokens) {
|
|
||||||
this._tokens = tokens;
|
|
||||||
this.updateExchangeRates();
|
|
||||||
}
|
|
||||||
/* eslint-enable accessor-pairs */
|
|
||||||
|
|
||||||
start(interval = DEFAULT_INTERVAL) {
|
|
||||||
this._handle && clearInterval(this._handle);
|
|
||||||
if (!interval) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this._handle = setInterval(() => {
|
|
||||||
this.updateExchangeRates();
|
|
||||||
}, interval);
|
|
||||||
this.updateExchangeRates();
|
|
||||||
}
|
|
||||||
|
|
||||||
stop() {
|
|
||||||
this._handle && clearInterval(this._handle);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user