aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/computed-balances.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-09-23 04:59:25 +0800
committerDan Finlay <dan@danfinlay.com>2017-09-23 04:59:25 +0800
commit11c8c07bfc6677e347873f02ae8c401f8d6c4dcf (patch)
treef94da3caa86d7fd7de6275d25cb9c161175208e3 /app/scripts/controllers/computed-balances.js
parent977405fc7d89256a911e73b83a6678235fa1cfb8 (diff)
downloadtangerine-wallet-browser-11c8c07bfc6677e347873f02ae8c401f8d6c4dcf.tar.gz
tangerine-wallet-browser-11c8c07bfc6677e347873f02ae8c401f8d6c4dcf.tar.zst
tangerine-wallet-browser-11c8c07bfc6677e347873f02ae8c401f8d6c4dcf.zip
Refactor eth-store into account-tracker
EthStore was only being used for tracking account balances and nonces now, so I removed its block-tracking duties, renamed it account-tracker, and removed it as a dependency from `KeyringController`, so that KRC can go live on without a hard dep on it.
Diffstat (limited to 'app/scripts/controllers/computed-balances.js')
-rw-r--r--app/scripts/controllers/computed-balances.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js
new file mode 100644
index 000000000..a85eb5590
--- /dev/null
+++ b/app/scripts/controllers/computed-balances.js
@@ -0,0 +1,64 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+const BalanceController = require('./balance')
+
+class ComputedbalancesController {
+
+ constructor (opts = {}) {
+ const { ethStore, txController } = opts
+ this.ethStore = ethStore
+ this.txController = txController
+
+ const initState = extend({
+ computedBalances: {},
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ this.balances = {}
+
+ this._initBalanceUpdating()
+ }
+
+ updateAllBalances () {
+ for (let address in this.balances) {
+ this.balances[address].updateBalance()
+ }
+ }
+
+ _initBalanceUpdating () {
+ const store = this.ethStore.getState()
+ this.addAnyAccountsFromStore(store)
+ this.ethStore.subscribe(this.addAnyAccountsFromStore.bind(this))
+ }
+
+ addAnyAccountsFromStore(store) {
+ const balances = store.accounts
+
+ for (let address in balances) {
+ this.trackAddressIfNotAlready(address)
+ }
+ }
+
+ trackAddressIfNotAlready (address) {
+ const state = this.store.getState()
+ if (!(address in state.computedBalances)) {
+ this.trackAddress(address)
+ }
+ }
+
+ trackAddress (address) {
+ let updater = new BalanceController({
+ address,
+ ethStore: this.ethStore,
+ txController: this.txController,
+ })
+ updater.store.subscribe((accountBalance) => {
+ let newState = this.store.getState()
+ newState.computedBalances[address] = accountBalance
+ this.store.updateState(newState)
+ })
+ this.balances[address] = updater
+ updater.updateBalance()
+ }
+}
+
+module.exports = ComputedbalancesController