mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
detect tokens through infura
This commit is contained in:
parent
910713c6b3
commit
2fffe09873
@ -1,10 +1,12 @@
|
|||||||
|
const Web3 = require('web3')
|
||||||
const contracts = require('eth-contract-metadata')
|
const contracts = require('eth-contract-metadata')
|
||||||
|
const { warn } = require('loglevel')
|
||||||
const {
|
const {
|
||||||
MAINNET,
|
MAINNET,
|
||||||
} = require('./network/enums')
|
} = require('./network/enums')
|
||||||
|
|
||||||
// By default, poll every 3 minutes
|
// By default, poll every 3 minutes
|
||||||
const DEFAULT_INTERVAL = 180 * 1000
|
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
|
* A controller that polls for token exchange
|
||||||
@ -28,17 +30,12 @@ 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.store.getState().provider.type !== MAINNET) { return }
|
||||||
let detectedTokenBalance, token
|
this.web3.setProvider(this._network._provider)
|
||||||
for (const contractAddress in contracts) {
|
for (const contractAddress in contracts) {
|
||||||
const contract = contracts[contractAddress]
|
if (contracts[contractAddress].erc20 && !(this.tokenAddresses.includes(contractAddress.toLowerCase()))) {
|
||||||
if (contract.erc20 && !(contractAddress in this.tokens)) {
|
this.detectTokenBalance(contractAddress)
|
||||||
detectedTokenBalance = await this.detectTokenBalance(contractAddress)
|
}
|
||||||
if (detectedTokenBalance) {
|
|
||||||
token = contracts[contractAddress]
|
|
||||||
this._preferences.addToken(contractAddress, token['symbol'], token['decimals'])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,17 +43,20 @@ 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 {boolean} If balance is detected in token contract for address.
|
* @returns {boolean} If balance is detected, token is added.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async detectTokenBalance (contractAddress) {
|
async detectTokenBalance (contractAddress) {
|
||||||
const address = this._preferences.store.getState().selectedAddress
|
const ethContract = this.web3.eth.contract(ERC20_ABI).at(contractAddress)
|
||||||
const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`)
|
ethContract.balanceOf(this.selectedAddress, (error, result) => {
|
||||||
const parsedResponse = await response.json()
|
if (!error) {
|
||||||
if (parsedResponse.result !== '0') {
|
if (!result.isZero()) {
|
||||||
return true
|
this._preferences.addToken(contractAddress, contracts[contractAddress].symbol, contracts[contractAddress].decimals)
|
||||||
}
|
}
|
||||||
return false
|
} else {
|
||||||
|
warn(`MetaMask - DetectTokensController balance fetch failed for ${contractAddress}.`, error)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,8 +74,10 @@ class DetectTokensController {
|
|||||||
set preferences (preferences) {
|
set preferences (preferences) {
|
||||||
if (!preferences) { return }
|
if (!preferences) { return }
|
||||||
this._preferences = preferences
|
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) {
|
set network (network) {
|
||||||
if (!network) { return }
|
if (!network) { return }
|
||||||
this._network = network
|
this._network = network
|
||||||
|
this.web3 = new Web3(network._provider)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
// detect tokens controller
|
// detect tokens controller
|
||||||
this.detectTokensController = new DetectTokensController({
|
this.detectTokensController = new DetectTokensController({
|
||||||
preferences: this.preferencesController,
|
preferences: this.preferencesController,
|
||||||
network: this.networkController.store,
|
network: this.networkController,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.recentBlocksController = new RecentBlocksController({
|
this.recentBlocksController = new RecentBlocksController({
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens')
|
const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens')
|
||||||
|
const NetworkController = require('../../../../app/scripts/controllers/network/network')
|
||||||
const PreferencesController = require('../../../../app/scripts/controllers/preferences')
|
const PreferencesController = require('../../../../app/scripts/controllers/preferences')
|
||||||
const ObservableStore = require('obs-store')
|
|
||||||
|
|
||||||
describe('DetectTokensController', () => {
|
describe('DetectTokensController', () => {
|
||||||
const sandbox = sinon.createSandbox()
|
const sandbox = sinon.createSandbox()
|
||||||
@ -22,7 +22,8 @@ describe('DetectTokensController', () => {
|
|||||||
|
|
||||||
it('should be called on every polling period', async () => {
|
it('should be called on every polling period', async () => {
|
||||||
clock = sandbox.useFakeTimers()
|
clock = sandbox.useFakeTimers()
|
||||||
const network = new ObservableStore({provider: {type: 'mainnet'}})
|
const network = new NetworkController()
|
||||||
|
network.setProviderType('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
|
||||||
@ -40,7 +41,8 @@ describe('DetectTokensController', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
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'}})
|
const network = new NetworkController()
|
||||||
|
network.setProviderType('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
|
||||||
@ -54,14 +56,17 @@ describe('DetectTokensController', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
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 NetworkController()
|
||||||
|
network.setProviderType('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
|
||||||
|
|
||||||
sandbox.stub(controller, 'detectTokenBalance')
|
sandbox.stub(controller, 'detectTokenBalance')
|
||||||
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
|
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
|
||||||
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
|
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
|
||||||
|
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
|
||||||
|
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
|
||||||
|
|
||||||
await controller.exploreNewTokens()
|
await controller.exploreNewTokens()
|
||||||
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
|
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 () => {
|
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()
|
const preferences = new PreferencesController()
|
||||||
preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)
|
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
|
||||||
|
|
||||||
sandbox.stub(controller, 'detectTokenBalance')
|
sandbox.stub(controller, 'detectTokenBalance')
|
||||||
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
|
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
|
||||||
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
|
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
|
||||||
|
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
|
||||||
|
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
|
||||||
|
|
||||||
await controller.exploreNewTokens()
|
await controller.exploreNewTokens()
|
||||||
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
|
assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'},
|
||||||
|
Loading…
Reference in New Issue
Block a user