improve tests

feature/default_network_editable
Esteban MIno 6 years ago
parent 63b9c8796b
commit 910713c6b3
  1. 27
      app/scripts/controllers/detect-tokens.js
  2. 105
      test/unit/app/controllers/detect-tokens-test.js

@ -20,7 +20,6 @@ class DetectTokensController {
this.preferences = preferences this.preferences = preferences
this.interval = interval this.interval = interval
this.network = network this.network = network
this.contracts = contracts
} }
/** /**
@ -30,18 +29,16 @@ class DetectTokensController {
async exploreNewTokens () { async exploreNewTokens () {
if (!this.isActive) { return } if (!this.isActive) { return }
if (this._network.getState().provider.type !== MAINNET) { return } if (this._network.getState().provider.type !== MAINNET) { return }
let detectedTokenAddress, token let detectedTokenBalance, token
for (const address in this.contracts) { for (const contractAddress in contracts) {
const contract = this.contracts[address] const contract = contracts[contractAddress]
if (contract.erc20 && !(address in this.tokens)) { if (contract.erc20 && !(contractAddress in this.tokens)) {
detectedTokenAddress = await this.fetchContractAccountBalance(address) detectedTokenBalance = await this.detectTokenBalance(contractAddress)
if (detectedTokenAddress) { if (detectedTokenBalance) {
token = this.contracts[detectedTokenAddress] token = contracts[contractAddress]
this._preferences.addToken(detectedTokenAddress, token['symbol'], token['decimals']) this._preferences.addToken(contractAddress, token['symbol'], token['decimals'])
} }
} }
// etherscan restriction, 5 request/second, lazy scan
setTimeout(() => {}, 200)
} }
} }
@ -49,17 +46,17 @@ class DetectTokensController {
* Find if selectedAddress has tokens with contract in contractAddress. * Find if selectedAddress has tokens with contract in contractAddress.
* *
* @param {string} contractAddress Hex address of the token contract to explore. * @param {string} contractAddress Hex address of the token contract to explore.
* @returns {string} Contract address to be added to tokens. * @returns {boolean} If balance is detected in token contract for address.
* *
*/ */
async fetchContractAccountBalance (contractAddress) { async detectTokenBalance (contractAddress) {
const address = this._preferences.store.getState().selectedAddress const address = this._preferences.store.getState().selectedAddress
const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`) const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`)
const parsedResponse = await response.json() const parsedResponse = await response.json()
if (parsedResponse.result !== '0') { if (parsedResponse.result !== '0') {
return contractAddress return true
} }
return null return false
} }
/** /**

@ -5,6 +5,14 @@ const PreferencesController = require('../../../../app/scripts/controllers/prefe
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
describe('DetectTokensController', () => { describe('DetectTokensController', () => {
const sandbox = sinon.createSandbox()
let clock
before(async () => {
})
after(() => {
sandbox.restore()
})
it('should poll on correct interval', async () => { it('should poll on correct interval', async () => {
const stub = sinon.stub(global, 'setInterval') const stub = sinon.stub(global, 'setInterval')
new DetectTokensController({ interval: 1337 }) // eslint-disable-line no-new new DetectTokensController({ interval: 1337 }) // eslint-disable-line no-new
@ -12,82 +20,67 @@ describe('DetectTokensController', () => {
stub.restore() stub.restore()
}) })
it('should be called on every polling period', async () => {
clock = sandbox.useFakeTimers()
const network = new ObservableStore({provider: {type: 'mainnet'}})
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
var stub = sandbox.stub(controller, 'exploreNewTokens')
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)
})
it('should not check tokens while in test network', async () => { it('should not check tokens while in test network', async () => {
var network = new ObservableStore({provider: {type: 'rinkeby'}}) var network = new ObservableStore({provider: {type: 'rinkeby'}})
const preferences = new PreferencesController() const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network}) const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true controller.isActive = true
controller.contracts = {
'0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { var stub = sandbox.stub(controller, 'detectTokenBalance')
'name': 'JET8 Token', .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
'logo': 'J8T.svg', .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
'erc20': true,
'symbol': 'J8T',
'decimals': 8,
},
'0xBC86727E770de68B1060C91f6BB6945c73e10388': {
'name': 'Ink Protocol',
'logo': 'ink_protocol.svg',
'erc20': true,
'symbol': 'XNK',
'decimals': 18,
},
}
controller.fetchContractAccountBalance = address => address
await controller.exploreNewTokens() await controller.exploreNewTokens()
assert.deepEqual(preferences.store.getState().tokens, []) sandbox.assert.notCalled(stub)
}) })
it('should only check and add tokens while in main network', async () => { it('should only check and add tokens while in main network', async () => {
const network = new ObservableStore({provider: {type: 'mainnet'}}) const network = new ObservableStore({provider: {type: 'mainnet'}})
const preferences = new PreferencesController() const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network}) const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true controller.isActive = true
controller.contracts = {
'0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { sandbox.stub(controller, 'detectTokenBalance')
'name': 'JET8 Token', .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
'logo': 'J8T.svg', .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
'erc20': true,
'symbol': 'J8T',
'decimals': 8,
},
'0xBC86727E770de68B1060C91f6BB6945c73e10388': {
'name': 'Ink Protocol',
'logo': 'ink_protocol.svg',
'erc20': true,
'symbol': 'XNK',
'decimals': 18,
},
}
controller.fetchContractAccountBalance = address => address
await controller.exploreNewTokens() await controller.exploreNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
{address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}])
}) })
it('should not detect same token while in main network', async () => { it('should not detect same token while in main network', async () => {
const network = new ObservableStore({provider: {type: 'mainnet'}}) const network = new ObservableStore({provider: {type: 'mainnet'}})
const preferences = new PreferencesController() const preferences = new PreferencesController()
preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 8, 'J8T') preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)
const controller = new DetectTokensController({preferences: preferences, network: network}) const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true controller.isActive = true
controller.contracts = {
'0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { sandbox.stub(controller, 'detectTokenBalance')
'name': 'JET8 Token', .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
'logo': 'J8T.svg', .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
'erc20': true,
'symbol': 'J8T',
'decimals': 8,
},
'0xBC86727E770de68B1060C91f6BB6945c73e10388': {
'name': 'Ink Protocol',
'logo': 'ink_protocol.svg',
'erc20': true,
'symbol': 'XNK',
'decimals': 18,
},
}
controller.fetchContractAccountBalance = address => address
await controller.exploreNewTokens() await controller.exploreNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
{address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}])
}) })
}) })

Loading…
Cancel
Save