1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00

detect tokens through infura

This commit is contained in:
Esteban MIno 2018-07-12 20:43:43 -04:00
parent 910713c6b3
commit 2fffe09873
3 changed files with 42 additions and 31 deletions

View File

@ -1,10 +1,12 @@
const Web3 = require('web3')
const contracts = require('eth-contract-metadata')
const { warn } = require('loglevel')
const {
MAINNET,
} = require('./network/enums')
// By default, poll every 3 minutes
const DEFAULT_INTERVAL = 180 * 1000
const ERC20_ABI = [{'constant': true, 'inputs': [{'name': '_owner', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'name': 'balance', 'type': 'uint256'}], 'payable': false, 'type': 'function'}]
/**
* A controller that polls for token exchange
@ -28,17 +30,12 @@ class DetectTokensController {
*/
async exploreNewTokens () {
if (!this.isActive) { return }
if (this._network.getState().provider.type !== MAINNET) { return }
let detectedTokenBalance, token
if (this._network.store.getState().provider.type !== MAINNET) { return }
this.web3.setProvider(this._network._provider)
for (const contractAddress in contracts) {
const contract = contracts[contractAddress]
if (contract.erc20 && !(contractAddress in this.tokens)) {
detectedTokenBalance = await this.detectTokenBalance(contractAddress)
if (detectedTokenBalance) {
token = contracts[contractAddress]
this._preferences.addToken(contractAddress, token['symbol'], token['decimals'])
}
}
if (contracts[contractAddress].erc20 && !(this.tokenAddresses.includes(contractAddress.toLowerCase()))) {
this.detectTokenBalance(contractAddress)
}
}
}
@ -46,17 +43,20 @@ class DetectTokensController {
* Find if selectedAddress has tokens with contract in contractAddress.
*
* @param {string} contractAddress Hex address of the token contract to explore.
* @returns {boolean} If balance is detected in token contract for address.
* @returns {boolean} If balance is detected, token is added.
*
*/
async detectTokenBalance (contractAddress) {
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 parsedResponse = await response.json()
if (parsedResponse.result !== '0') {
return true
}
return false
const ethContract = this.web3.eth.contract(ERC20_ABI).at(contractAddress)
ethContract.balanceOf(this.selectedAddress, (error, result) => {
if (!error) {
if (!result.isZero()) {
this._preferences.addToken(contractAddress, contracts[contractAddress].symbol, contracts[contractAddress].decimals)
}
} else {
warn(`MetaMask - DetectTokensController balance fetch failed for ${contractAddress}.`, error)
}
})
}
/**
@ -74,8 +74,10 @@ class DetectTokensController {
set preferences (preferences) {
if (!preferences) { return }
this._preferences = preferences
this.tokens = preferences.store.getState().tokens
this.tokenAddresses = preferences.store.getState().tokens.map((obj) => { return obj.address })
this.selectedAddress = preferences.store.getState().selectedAddress
preferences.store.subscribe(({ tokens = [] }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) })
preferences.store.subscribe(({ selectedAddress = [] }) => { this.selectedAddress = selectedAddress })
}
/**
@ -84,6 +86,7 @@ class DetectTokensController {
set network (network) {
if (!network) { return }
this._network = network
this.web3 = new Web3(network._provider)
}
}

View File

@ -116,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter {
// detect tokens controller
this.detectTokensController = new DetectTokensController({
preferences: this.preferencesController,
network: this.networkController.store,
network: this.networkController,
})
this.recentBlocksController = new RecentBlocksController({

View File

@ -1,8 +1,8 @@
const assert = require('assert')
const sinon = require('sinon')
const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens')
const NetworkController = require('../../../../app/scripts/controllers/network/network')
const PreferencesController = require('../../../../app/scripts/controllers/preferences')
const ObservableStore = require('obs-store')
describe('DetectTokensController', () => {
const sandbox = sinon.createSandbox()
@ -22,7 +22,8 @@ describe('DetectTokensController', () => {
it('should be called on every polling period', async () => {
clock = sandbox.useFakeTimers()
const network = new ObservableStore({provider: {type: 'mainnet'}})
const network = new NetworkController()
network.setProviderType('mainnet')
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
@ -40,7 +41,8 @@ describe('DetectTokensController', () => {
})
it('should not check tokens while in test network', async () => {
var network = new ObservableStore({provider: {type: 'rinkeby'}})
const network = new NetworkController()
network.setProviderType('rinkeby')
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
@ -54,14 +56,17 @@ describe('DetectTokensController', () => {
})
it('should only check and add tokens while in main network', async () => {
const network = new ObservableStore({provider: {type: 'mainnet'}})
const network = new NetworkController()
network.setProviderType('mainnet')
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
await controller.exploreNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
@ -69,15 +74,18 @@ describe('DetectTokensController', () => {
})
it('should not detect same token while in main network', async () => {
const network = new ObservableStore({provider: {type: 'mainnet'}})
const network = new NetworkController()
network.setProviderType('mainnet')
const preferences = new PreferencesController()
preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
await controller.exploreNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},