A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/controllers/balance.js

62 lines
1.6 KiB

7 years ago
const ObservableStore = require('obs-store')
const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
const BN = require('ethereumjs-util').BN
7 years ago
class BalanceController {
constructor (opts = {}) {
const { address, accountTracker, txController, blockTracker } = opts
this.address = address
this.accountTracker = accountTracker
7 years ago
this.txController = txController
this.blockTracker = blockTracker
7 years ago
const initState = {
7 years ago
ethBalance: undefined,
}
7 years ago
this.store = new ObservableStore(initState)
this.balanceCalc = new PendingBalanceCalculator({
getBalance: () => this._getBalance(),
getPendingTransactions: this._getPendingTransactions.bind(this),
7 years ago
})
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.subscribe(update)
this.blockTracker.on('block', update)
}
async _getBalance () {
const { accounts } = this.accountTracker.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
7 years ago
}
}
module.exports = BalanceController