From 69e883c72856eb8a9aceb3dc34518ba23ae722fa Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Fri, 8 Jan 2021 10:55:46 -0330 Subject: [PATCH] Detect tokens on custom Mainnet RPC endpoints (#10157) Our automatic token detection was hard-coded to only work on our built- in Infura Mainnet endpoint. It now works with custom Mainnet RPC endpoints as well. Relates to #6992 --- app/scripts/controllers/detect-tokens.js | 4 +- .../app/controllers/detect-tokens-test.js | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index df821335c..1ad46193d 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -2,7 +2,7 @@ import Web3 from 'web3' import contracts from '@metamask/contract-metadata' import { warn } from 'loglevel' import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi' -import { MAINNET } from './network/enums' +import { MAINNET_CHAIN_ID } from './network/enums' // By default, poll every 3 minutes const DEFAULT_INTERVAL = 180 * 1000 @@ -38,7 +38,7 @@ export default class DetectTokensController { if (!this.isActive) { return } - if (this._network.store.getState().provider.type !== MAINNET) { + if (this._network.store.getState().provider.chainId !== MAINNET_CHAIN_ID) { return } diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index b7ab22ed6..6039a943c 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -189,6 +189,63 @@ describe('DetectTokensController', function () { ]) }) + 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, + 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 trigger detect new tokens when change address', async function () { sandbox.useFakeTimers() const controller = new DetectTokensController({