Skip to content

Commit

Permalink
Merge pull request #175 from poanetwork/poa-autodetect-tokens
Browse files Browse the repository at this point in the history
(Feature) auto-detect tokens for POA
  • Loading branch information
vbaranov authored Nov 6, 2018
2 parents e8ece66 + 97ea358 commit 7fb61d8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
22 changes: 19 additions & 3 deletions app/scripts/controllers/detect-tokens.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Web3 = require('web3')
const contracts = require('eth-contract-metadata')
const contractsETH = require('eth-contract-metadata')
const contractsPOA = require('poa-contract-metadata')
const { warn } = require('loglevel')
const { MAINNET } = require('./network/enums')
const { MAINNET, POA } = 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'}]
Expand All @@ -28,9 +29,11 @@ class DetectTokensController {
*
*/
async detectNewTokens () {
const isTestnet = this._network.store.getState().provider.type !== MAINNET && this._network.store.getState().provider.type !== POA
if (!this.isActive) { return }
if (this._network.store.getState().provider.type !== MAINNET) { return }
if (isTestnet) { return }
this.web3.setProvider(this._network._provider)
const contracts = this.getContracts()
for (const contractAddress in contracts) {
if (contracts[contractAddress].erc20 && !(this.tokenAddresses.includes(contractAddress.toLowerCase()))) {
this.detectTokenBalance(contractAddress)
Expand All @@ -47,6 +50,7 @@ class DetectTokensController {
*/
async detectTokenBalance (contractAddress) {
const ethContract = this.web3.eth.contract(ERC20_ABI).at(contractAddress)
const contracts = this.getContracts()
ethContract.balanceOf(this.selectedAddress, (error, result) => {
if (!error) {
if (!result.isZero()) {
Expand All @@ -58,6 +62,18 @@ class DetectTokensController {
})
}

/**
* Returns corresponding contracts JSON object depending on chain
* @returns {Object}
*/
getContracts () {
const isMainnet = this._network.store.getState().provider.type === MAINNET
const isPOA = this._network.store.getState().provider.type === POA
// todo: isDAI
const contracts = isMainnet ? contractsETH : isPOA ? contractsPOA : {}
return contracts
}

/**
* Restart token detection polling period and call detectNewTokens
* in case of address change or user session initialization.
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module.exports = class MetamaskController extends EventEmitter {
// ensure accountTracker updates balances after network change
this.networkController.on('networkDidChange', () => {
this.accountTracker._updateAccounts()
this.detectTokensController.restartTokenDetection()
})

// key mgmt
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 31 additions & 5 deletions test/e2e/metamask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Metamask popup page', async function () {
})

after(async function () {
// await driver.quit()
// await driver.quit()
})

describe('Setup', async function () {
Expand Down Expand Up @@ -294,6 +294,7 @@ describe('Metamask popup page', async function () {
describe('Import Account', () => {

it('opens import account menu', async function () {
await setProvider(NETWORKS.POA)
const menu = await waitUntilShowUp(menus.account.menu)
await menu.click()
const item = await waitUntilShowUp(menus.account.import)
Expand All @@ -305,19 +306,42 @@ describe('Metamask popup page', async function () {
it('imports account', async function () {
await delay(2000)
const privateKeyBox = await waitUntilShowUp(screens.importAccounts.fieldPrivateKey)
await privateKeyBox.sendKeys('c6b81c1252415d1acfda94474ab8f662a44c045f96749c805ff12a6074081586')// demo private key
await privateKeyBox.sendKeys('76bd0ced0a47055bb5d060e1ae4a8cb3ece658d668823e250dae6e79d3ab4435')// 0xf4702CbA917260b2D6731Aea6385215073e8551b
const button = await waitUntilShowUp(screens.importAccounts.buttonImport)
await click(button)
assert.equal(await button.getText(), 'Import', 'button has incorrect name')
const menu = await waitUntilShowUp(menus.account.menu)
await menu.click()
const importedLabel = await waitUntilShowUp(menus.account.labelImported)
assert.equal(await importedLabel.getText(), 'IMPORTED')

await menu.click()
})

it('Auto-detect tokens for POA core network ', async function () {
// await setProvider(NETWORKS.POA)
const tab = await waitUntilShowUp(screens.main.tokens.menu)
await tab.click()
const balance = await waitUntilShowUp(screens.main.tokens.balance)
console.log(await balance.getText())
assert.equal(await balance.getText(), '1 DOPR', 'token isnt\' auto-detected')
})

it('Auto-detect tokens for MAIN core network ', async function () {
await setProvider(NETWORKS.MAINNET)
await waitUntilShowUp(elements.loader, 25)
await waitUntilDisappear(elements.loader, 25)
const balance = await waitUntilShowUp(screens.main.tokens.balance)
console.log(await balance.getText())
assert.equal(await balance.getText(), '0.001 WETH', 'token isnt\' auto-detected')
})

it('opens delete imported account screen', async function () {
const menu = await waitUntilShowUp(menus.account.delete)
await setProvider(NETWORKS.LOCALHOST)
const menu = await waitUntilShowUp(menus.account.menu)
await menu.click()
const item = await waitUntilShowUp(menus.account.delete)
await item.click()
const deleteImportedAccountTitle = await waitUntilShowUp(screens.deleteImportedAccount.title)
assert.equal(await deleteImportedAccountTitle.getText(), screens.deleteImportedAccount.titleText)
})
Expand Down Expand Up @@ -1435,7 +1459,6 @@ describe('Metamask popup page', async function () {
})

it('token should not be displayed in LOCALHOST network', async function () {
console.log('https://github.com/poanetwork/metamask-extension/issues/131')
await setProvider(NETWORKS.LOCALHOST)
assert.equal(await assertTokensNotDisplayed(), true, 'tokens are displayed')
})
Expand All @@ -1458,12 +1481,15 @@ describe('Metamask popup page', async function () {
describe('remove Mainnet\'s tokens', function () {

it('remove tokens', async function () {
await setProvider(NETWORKS.MAINNET)

let menu
let button
let counter
let buttonYes

await setProvider(NETWORKS.MAINNET)
await waitUntilShowUp(elements.loader, 25)
await waitUntilDisappear(elements.loader, 50)
menu = await waitUntilShowUp(menus.token.menu)
await menu.click()
button = await waitUntilShowUp(menus.token.remove)
Expand Down

0 comments on commit 7fb61d8

Please sign in to comment.