1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-03 14:44:27 +01:00
metamask-extension/test/unit/app/controllers/detect-tokens.test.js

310 lines
9.4 KiB
JavaScript
Raw Normal View History

import assert from 'assert';
import sinon from 'sinon';
import { ObservableStore } from '@metamask/obs-store';
import contracts from '@metamask/contract-metadata';
import BigNumber from 'bignumber.js';
import DetectTokensController from '../../../../app/scripts/controllers/detect-tokens';
import NetworkController from '../../../../app/scripts/controllers/network/network';
import PreferencesController from '../../../../app/scripts/controllers/preferences';
import { MAINNET, ROPSTEN } from '../../../../shared/constants/network';
2018-06-28 04:18:06 +02:00
describe('DetectTokensController', function () {
const sandbox = sinon.createSandbox();
let keyringMemStore, network, preferences;
2018-07-30 15:26:34 +02:00
const noop = () => undefined;
2018-07-30 15:26:34 +02:00
const networkControllerProviderConfig = {
getAccounts: noop,
};
2018-07-30 15:26:34 +02:00
beforeEach(async function () {
keyringMemStore = new ObservableStore({ isUnlocked: false });
network = new NetworkController();
network.setInfuraProjectId('foo');
preferences = new PreferencesController({ network });
preferences.setAddresses([
'0x7e57e2',
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
]);
network.initializeProvider(networkControllerProviderConfig);
});
2018-07-30 15:26:34 +02:00
after(function () {
sandbox.restore();
});
2018-07-11 21:59:05 +02:00
it('should poll on correct interval', async function () {
const stub = sinon.stub(global, 'setInterval');
new DetectTokensController({ interval: 1337 }); // eslint-disable-line no-new
assert.strictEqual(stub.getCall(0).args[1], 1337);
stub.restore();
});
2018-06-28 04:18:06 +02:00
it('should be called on every polling period', async function () {
const clock = sandbox.useFakeTimers();
network.setProviderType(MAINNET);
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
const stub = sandbox.stub(controller, 'detectNewTokens');
clock.tick(1);
sandbox.assert.notCalled(stub);
clock.tick(180000);
sandbox.assert.called(stub);
clock.tick(180000);
sandbox.assert.calledTwice(stub);
clock.tick(180000);
sandbox.assert.calledThrice(stub);
});
2018-07-11 21:59:05 +02:00
it('should not check tokens while on test network', async function () {
sandbox.useFakeTimers();
network.setProviderType(ROPSTEN);
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
2018-07-11 21:59:05 +02:00
const stub = sandbox.stub(controller, '_getTokenBalances');
2018-07-11 21:59:05 +02:00
await controller.detectNewTokens();
sandbox.assert.notCalled(stub);
});
2018-06-28 04:18:06 +02:00
it('should skip adding tokens listed in hiddenTokens array', async function () {
sandbox.useFakeTimers();
network.setProviderType(MAINNET);
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
const contractAddresses = Object.keys(contracts);
const erc20ContractAddresses = contractAddresses.filter(
(contractAddress) => contracts[contractAddress].erc20 === true,
);
const existingTokenAddress = erc20ContractAddresses[0];
const existingToken = contracts[existingTokenAddress];
await preferences.addToken(
existingTokenAddress,
existingToken.symbol,
existingToken.decimals,
);
const tokenAddressToSkip = erc20ContractAddresses[1];
sandbox
.stub(controller, '_getTokenBalances')
.callsFake((tokensToDetect) =>
tokensToDetect.map((token) =>
token === tokenAddressToSkip ? new BigNumber(10) : 0,
),
);
await preferences.removeToken(tokenAddressToSkip);
await controller.detectNewTokens();
assert.deepEqual(preferences.store.getState().tokens, [
{
address: existingTokenAddress.toLowerCase(),
decimals: existingToken.decimals,
symbol: existingToken.symbol,
},
]);
});
it('should check and add tokens while on main network', async function () {
sandbox.useFakeTimers();
network.setProviderType(MAINNET);
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
2018-07-11 21:59:05 +02:00
const contractAddresses = Object.keys(contracts);
2020-11-03 00:41:28 +01:00
const erc20ContractAddresses = contractAddresses.filter(
(contractAddress) => contracts[contractAddress].erc20 === true,
);
2018-07-11 21:59:05 +02:00
const existingTokenAddress = erc20ContractAddresses[0];
const existingToken = contracts[existingTokenAddress];
2020-11-03 00:41:28 +01:00
await preferences.addToken(
existingTokenAddress,
existingToken.symbol,
existingToken.decimals,
);
const tokenAddressToAdd = erc20ContractAddresses[1];
const tokenToAdd = contracts[tokenAddressToAdd];
const contractAddresssesToDetect = contractAddresses.filter(
(address) => address !== existingTokenAddress,
);
const indexOfTokenToAdd = contractAddresssesToDetect.indexOf(
tokenAddressToAdd,
);
const balances = new Array(contractAddresssesToDetect.length);
balances[indexOfTokenToAdd] = new BigNumber(10);
sandbox
.stub(controller, '_getTokenBalances')
.returns(Promise.resolve(balances));
await controller.detectNewTokens();
assert.deepEqual(preferences.store.getState().tokens, [
{
address: existingTokenAddress.toLowerCase(),
decimals: existingToken.decimals,
symbol: existingToken.symbol,
},
{
address: tokenAddressToAdd.toLowerCase(),
decimals: tokenToAdd.decimals,
symbol: tokenToAdd.symbol,
},
]);
});
it('should check and add tokens while on non-default Mainnet', async function () {
sandbox.useFakeTimers();
network.setRpcTarget('https://some-fake-RPC-endpoint.metamask.io', '0x1');
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
const contractAddresses = Object.keys(contracts);
const erc20ContractAddresses = contractAddresses.filter(
(contractAddress) => contracts[contractAddress].erc20 === true,
);
const existingTokenAddress = erc20ContractAddresses[0];
const existingToken = contracts[existingTokenAddress];
await preferences.addToken(
existingTokenAddress,
2020-11-03 00:41:28 +01:00
existingToken.symbol,
existingToken.decimals,
);
2018-06-28 04:18:06 +02:00
const tokenAddressToAdd = erc20ContractAddresses[1];
const tokenToAdd = contracts[tokenAddressToAdd];
2018-07-11 21:59:05 +02:00
2020-11-03 00:41:28 +01:00
const contractAddresssesToDetect = contractAddresses.filter(
(address) => address !== existingTokenAddress,
);
2020-11-03 00:41:28 +01:00
const indexOfTokenToAdd = contractAddresssesToDetect.indexOf(
tokenAddressToAdd,
);
const balances = new Array(contractAddresssesToDetect.length);
balances[indexOfTokenToAdd] = new BigNumber(10);
2020-11-03 00:41:28 +01:00
sandbox
.stub(controller, '_getTokenBalances')
.returns(Promise.resolve(balances));
2018-07-11 21:59:05 +02:00
await controller.detectNewTokens();
2020-11-03 00:41:28 +01:00
assert.deepEqual(preferences.store.getState().tokens, [
{
address: existingTokenAddress.toLowerCase(),
decimals: existingToken.decimals,
symbol: existingToken.symbol,
},
{
address: tokenAddressToAdd.toLowerCase(),
decimals: tokenToAdd.decimals,
symbol: tokenToAdd.symbol,
},
]);
});
2018-07-20 01:46:46 +02:00
it('should trigger detect new tokens when change address', async function () {
sandbox.useFakeTimers();
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = true;
const stub = sandbox.stub(controller, 'detectNewTokens');
2020-11-03 00:41:28 +01:00
await preferences.setSelectedAddress(
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
);
sandbox.assert.called(stub);
});
2018-07-20 01:46:46 +02:00
it('should trigger detect new tokens when submit password', async function () {
sandbox.useFakeTimers();
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.selectedAddress = '0x0';
const stub = sandbox.stub(controller, 'detectNewTokens');
await controller._keyringMemStore.updateState({ isUnlocked: true });
sandbox.assert.called(stub);
});
2018-07-21 01:58:03 +02:00
it('should not trigger detect new tokens when not unlocked', async function () {
const clock = sandbox.useFakeTimers();
network.setProviderType(MAINNET);
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
controller.isOpen = true;
controller.isUnlocked = false;
const stub = sandbox.stub(controller, '_getTokenBalances');
clock.tick(180000);
sandbox.assert.notCalled(stub);
});
it('should not trigger detect new tokens when not open', async function () {
const clock = sandbox.useFakeTimers();
network.setProviderType(MAINNET);
2020-11-03 00:41:28 +01:00
const controller = new DetectTokensController({
preferences,
network,
keyringMemStore,
});
// trigger state update from preferences controller
2020-11-03 00:41:28 +01:00
await preferences.setSelectedAddress(
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
);
controller.isOpen = false;
controller.isUnlocked = true;
const stub = sandbox.stub(controller, '_getTokenBalances');
clock.tick(180000);
sandbox.assert.notCalled(stub);
});
});