aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
authorEsteban MIno <efmino@uc.cl>2018-07-20 03:56:38 +0800
committerEsteban MIno <efmino@uc.cl>2018-07-20 03:56:38 +0800
commit3b97d816ffcaebc7606d4564ea95918f647ba413 (patch)
tree428d2f9a033b4de12620b21a5d4a7de0505b2399 /app/scripts/controllers
parent2fffe098736e2461b9238c7dcd91f9ef3d61dcc1 (diff)
downloadtangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar.gz
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar.bz2
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar.lz
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar.xz
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.tar.zst
tangerine-wallet-browser-3b97d816ffcaebc7606d4564ea95918f647ba413.zip
detect tokens when submit password and new account selected
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/detect-tokens.js52
1 files changed, 39 insertions, 13 deletions
diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js
index e245a7f9b..db21f7489 100644
--- a/app/scripts/controllers/detect-tokens.js
+++ b/app/scripts/controllers/detect-tokens.js
@@ -1,9 +1,7 @@
const Web3 = require('web3')
const contracts = require('eth-contract-metadata')
const { warn } = require('loglevel')
-const {
- MAINNET,
- } = require('./network/enums')
+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'}]
@@ -22,14 +20,15 @@ class DetectTokensController {
this.preferences = preferences
this.interval = interval
this.network = network
+ this._isActive = false
}
- /**
+ /**
* For each token in eth-contract-metada, find check selectedAddress balance.
*
*/
- async exploreNewTokens () {
- if (!this.isActive) { return }
+ async detectNewTokens () {
+ if (!this._isActive) { return }
if (this._network.store.getState().provider.type !== MAINNET) { return }
this.web3.setProvider(this._network._provider)
for (const contractAddress in contracts) {
@@ -60,27 +59,43 @@ class DetectTokensController {
}
/**
+ * Restart token detection polling period and call detectNewTokens
+ * in case of address change or user session initialization.
+ *
+ */
+ restartTokenDetection () {
+ if (this._isActive && this.selectedAddress) {
+ this.detectNewTokens()
+ this.interval = DEFAULT_INTERVAL
+ }
+ }
+
+ /**
* @type {Number}
*/
set interval (interval) {
this._handle && clearInterval(this._handle)
if (!interval) { return }
- this._handle = setInterval(() => { this.exploreNewTokens() }, interval)
+ this._handle = setInterval(() => { this.detectNewTokens() }, interval)
}
- /**
+ /**
+ * In setter when selectedAddress is changed, detectNewTokens and restart polling
* @type {Object}
*/
set preferences (preferences) {
if (!preferences) { return }
this._preferences = preferences
- 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 })
+ preferences.store.subscribe(({ tokens }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) })
+ preferences.store.subscribe(({ selectedAddress }) => {
+ if (this.selectedAddress !== selectedAddress) {
+ this.selectedAddress = selectedAddress
+ this.restartTokenDetection()
+ }
+ })
}
- /**
+ /**
* @type {Object}
*/
set network (network) {
@@ -88,6 +103,17 @@ class DetectTokensController {
this._network = network
this.web3 = new Web3(network._provider)
}
+
+ /**
+ * In setter, when _isActive is changed, detectNewTokens and restart polling
+ * @type {Object}
+ */
+ set isActive (active) {
+ if (this._isActive !== active) {
+ this._isActive = active
+ this.restartTokenDetection()
+ }
+ }
}
module.exports = DetectTokensController