aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/balance.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-09-23 05:30:31 +0800
committerDan Finlay <dan@danfinlay.com>2017-09-23 05:30:31 +0800
commitaa2abc00eb86c1cb746a59a6e307a6bc255e6466 (patch)
treec6056fb31ac0c79bfebc5e36d7dd56e8cabfd543 /app/scripts/controllers/balance.js
parent08b36b9b582853b411a19e48b407c70596381f61 (diff)
parentf128240e7f877280fa59bf22f2ea8285bb467022 (diff)
downloadtangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar.gz
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar.bz2
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar.lz
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar.xz
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.tar.zst
tangerine-wallet-browser-aa2abc00eb86c1cb746a59a6e307a6bc255e6466.zip
Merge branch 'AddBalanceController' into BreakOutKeyringController
Diffstat (limited to 'app/scripts/controllers/balance.js')
-rw-r--r--app/scripts/controllers/balance.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js
new file mode 100644
index 000000000..ddeb06cf9
--- /dev/null
+++ b/app/scripts/controllers/balance.js
@@ -0,0 +1,60 @@
+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 } = opts
+ this.address = address
+ this.accountTracker = accountTracker
+ this.txController = txController
+
+ const initState = {
+ ethBalance: undefined,
+ }
+ this.store = new ObservableStore(initState)
+
+ this.balanceCalc = new PendingBalanceCalculator({
+ getBalance: () => Promise.resolve(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.txController.blockTracker.on('block', update)
+ }
+
+ _getBalance () {
+ const store = this.accountTracker.getState()
+ const balances = store.accounts
+ const entry = balances[this.address]
+ const balance = entry.balance
+ return balance ? new BN(balance.substring(2), 16) : undefined
+ }
+
+ _getPendingTransactions () {
+ const pending = this.txController.getFilteredTxList({
+ from: this.address,
+ status: 'submitted',
+ err: undefined,
+ })
+ return Promise.resolve(pending)
+ }
+
+}
+
+module.exports = BalanceController