aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/balance.js
diff options
context:
space:
mode:
authorDan Finlay <542863+danfinlay@users.noreply.github.com>2017-09-28 01:57:02 +0800
committerGitHub <noreply@github.com>2017-09-28 01:57:02 +0800
commite72083f6e81f888f714125f7c050e777ea5c9bdd (patch)
tree8177fb150ae254b838cabec028b3c5e08fbc951e /app/scripts/controllers/balance.js
parentff5e8746bee0d7461df02e7f2186ff7c90bfcb50 (diff)
parentca0dff06f5d9283e57b452dcc7e85c31b248b8aa (diff)
downloadtangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.gz
tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.zst
tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.zip
Merge branch 'master' into filter-fixes-moar
Diffstat (limited to 'app/scripts/controllers/balance.js')
-rw-r--r--app/scripts/controllers/balance.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js
new file mode 100644
index 000000000..964dff0df
--- /dev/null
+++ b/app/scripts/controllers/balance.js
@@ -0,0 +1,61 @@
+const ObservableStore = require('obs-store')
+const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
+const BN = require('ethereumjs-util').BN
+
+class BalanceController {
+
+ constructor (opts = {}) {
+ const { address, accountTracker, txController, blockTracker } = opts
+ this.address = address
+ this.accountTracker = accountTracker
+ this.txController = txController
+ this.blockTracker = blockTracker
+
+ const initState = {
+ ethBalance: undefined,
+ }
+ this.store = new ObservableStore(initState)
+
+ this.balanceCalc = new PendingBalanceCalculator({
+ getBalance: () => this._getBalance(),
+ getPendingTransactions: this._getPendingTransactions.bind(this),
+ })
+
+ this._registerUpdates()
+ }
+
+ async updateBalance () {
+ const balance = await this.balanceCalc.getBalance()
+ this.store.updateState({
+ ethBalance: balance,
+ })
+ }
+
+ _registerUpdates () {
+ const update = this.updateBalance.bind(this)
+ this.txController.on('submitted', update)
+ this.txController.on('confirmed', update)
+ this.txController.on('failed', update)
+ this.accountTracker.store.subscribe(update)
+ this.blockTracker.on('block', update)
+ }
+
+ async _getBalance () {
+ const { accounts } = this.accountTracker.store.getState()
+ const entry = accounts[this.address]
+ const balance = entry.balance
+ return balance ? new BN(balance.substring(2), 16) : undefined
+ }
+
+ async _getPendingTransactions () {
+ const pending = this.txController.getFilteredTxList({
+ from: this.address,
+ status: 'submitted',
+ err: undefined,
+ })
+ return pending
+ }
+
+}
+
+module.exports = BalanceController