mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
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
This commit is contained in:
parent
bc6663d849
commit
69e883c728
@ -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
|
||||
}
|
||||
|
||||
|
@ -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({
|
||||
|
Loading…
Reference in New Issue
Block a user