2020-01-09 04:34:58 +01:00
|
|
|
import assert from 'assert'
|
|
|
|
import nock from 'nock'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
import ObservableStore from 'obs-store'
|
2020-04-13 22:14:42 +02:00
|
|
|
import contracts from 'eth-contract-metadata'
|
|
|
|
import BigNumber from 'bignumber.js'
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
import DetectTokensController from '../../../../app/scripts/controllers/detect-tokens'
|
|
|
|
import NetworkController from '../../../../app/scripts/controllers/network/network'
|
|
|
|
import PreferencesController from '../../../../app/scripts/controllers/preferences'
|
2020-04-13 22:14:42 +02:00
|
|
|
import { MAINNET, ROPSTEN } from '../../../../app/scripts/controllers/network/enums'
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('DetectTokensController', function () {
|
2018-07-30 15:26:34 +02:00
|
|
|
const sandbox = sinon.createSandbox()
|
2020-04-13 22:14:42 +02:00
|
|
|
let keyringMemStore, network, preferences
|
2018-07-30 15:26:34 +02:00
|
|
|
|
|
|
|
const noop = () => {}
|
|
|
|
|
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: noop,
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(async function () {
|
2018-07-30 15:26:34 +02:00
|
|
|
|
2018-08-14 19:44:42 +02:00
|
|
|
|
2018-07-30 15:26:34 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get(/.*/)
|
|
|
|
.reply(200)
|
|
|
|
|
2019-12-03 21:50:55 +01:00
|
|
|
keyringMemStore = new ObservableStore({ isUnlocked: false })
|
2018-07-30 15:26:34 +02:00
|
|
|
network = new NetworkController()
|
2018-08-14 19:44:42 +02:00
|
|
|
preferences = new PreferencesController({ network })
|
2020-04-13 22:14:42 +02:00
|
|
|
preferences.setAddresses([
|
|
|
|
'0x7e57e2',
|
|
|
|
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
|
|
|
|
])
|
2018-07-30 15:26:34 +02:00
|
|
|
network.initializeProvider(networkControllerProviderConfig)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
after(function () {
|
2018-08-14 19:44:42 +02:00
|
|
|
sandbox.restore()
|
|
|
|
nock.cleanAll()
|
2018-07-11 21:59:05 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should poll on correct interval', async function () {
|
2018-06-28 04:18:06 +02:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should be called on every polling period', async function () {
|
2020-04-13 22:14:42 +02:00
|
|
|
const clock = sandbox.useFakeTimers()
|
|
|
|
network.setProviderType(MAINNET)
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = true
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2019-12-03 15:52:01 +01:00
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens')
|
2018-07-11 21:59:05 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
it('should not check tokens while on test network', async function () {
|
|
|
|
sandbox.useFakeTimers()
|
|
|
|
network.setProviderType(ROPSTEN)
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = true
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
const stub = sandbox.stub(controller, '_getTokenBalances')
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
await controller.detectNewTokens()
|
2018-07-11 21:59:05 +02:00
|
|
|
sandbox.assert.notCalled(stub)
|
|
|
|
})
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
it('should check and add tokens while on main network', async function () {
|
|
|
|
sandbox.useFakeTimers()
|
|
|
|
network.setProviderType(MAINNET)
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = true
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
const contractAddressses = Object.keys(contracts)
|
|
|
|
const erc20ContractAddresses = contractAddressses
|
|
|
|
.filter((contractAddress) => contracts[contractAddress].erc20 === true)
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
const existingTokenAddress = erc20ContractAddresses[0]
|
|
|
|
const existingToken = contracts[existingTokenAddress]
|
|
|
|
await preferences.addToken(existingTokenAddress, existingToken.symbol, existingToken.decimals)
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
const tokenAddressToAdd = erc20ContractAddresses[1]
|
|
|
|
const tokenToAdd = contracts[tokenAddressToAdd]
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
const contractAddresssesToDetect = contractAddressses
|
|
|
|
.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))
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
await controller.detectNewTokens()
|
2020-04-13 22:14:42 +02: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-06-28 04:18:06 +02:00
|
|
|
})
|
2018-07-20 01:46:46 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should trigger detect new tokens when change address', async function () {
|
2020-04-13 22:14:42 +02:00
|
|
|
sandbox.useFakeTimers()
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = true
|
2019-12-03 15:52:01 +01:00
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens')
|
2018-07-20 01:46:46 +02:00
|
|
|
await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388')
|
|
|
|
sandbox.assert.called(stub)
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should trigger detect new tokens when submit password', async function () {
|
2020-04-13 22:14:42 +02:00
|
|
|
sandbox.useFakeTimers()
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
2018-07-20 01:46:46 +02:00
|
|
|
controller.selectedAddress = '0x0'
|
2019-12-03 15:52:01 +01:00
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens')
|
2018-07-20 01:46:46 +02:00
|
|
|
await controller._keyringMemStore.updateState({ isUnlocked: true })
|
|
|
|
sandbox.assert.called(stub)
|
|
|
|
})
|
2018-07-21 01:58:03 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
it('should not trigger detect new tokens when not unlocked', async function () {
|
|
|
|
const clock = sandbox.useFakeTimers()
|
|
|
|
network.setProviderType(MAINNET)
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = false
|
2020-04-13 22:14:42 +02:00
|
|
|
const stub = sandbox.stub(controller, '_getTokenBalances')
|
2018-07-21 01:58:03 +02:00
|
|
|
clock.tick(180000)
|
|
|
|
sandbox.assert.notCalled(stub)
|
2020-04-13 22:14:42 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not trigger detect new tokens when not open', async function () {
|
|
|
|
const clock = sandbox.useFakeTimers()
|
|
|
|
network.setProviderType(MAINNET)
|
|
|
|
const controller = new DetectTokensController({ preferences, network, keyringMemStore })
|
|
|
|
// trigger state update from preferences controller
|
|
|
|
await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388')
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = false
|
|
|
|
controller.isUnlocked = true
|
2020-04-13 22:14:42 +02:00
|
|
|
const stub = sandbox.stub(controller, '_getTokenBalances')
|
2018-07-21 01:58:03 +02:00
|
|
|
clock.tick(180000)
|
|
|
|
sandbox.assert.notCalled(stub)
|
|
|
|
})
|
2018-09-10 23:11:57 +02:00
|
|
|
})
|