diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 07:24:43 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 07:24:43 +0800 |
commit | 8ab23c713db1f5d45abb0ba433450591b8759809 (patch) | |
tree | c192fcf5eec3c7b422f3d0a91a4a96037c7f2fa6 /app/scripts/controllers/balance.js | |
parent | 77a48fb0b16d7415377b02a3b7d383c9ef86fcb6 (diff) | |
parent | 6ca519e97c4c282023ab6b7788715ff8d7ec8189 (diff) | |
download | tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar.gz tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar.bz2 tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar.lz tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar.xz tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.tar.zst tangerine-wallet-browser-8ab23c713db1f5d45abb0ba433450591b8759809.zip |
Merge branch 'master' into transactionControllerRefractorPt3
Diffstat (limited to 'app/scripts/controllers/balance.js')
-rw-r--r-- | app/scripts/controllers/balance.js | 61 |
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 |