1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/app/scripts/controllers/balance.js

137 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-09-13 00:06:19 +02:00
const ObservableStore = require('obs-store')
const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
2017-09-13 23:20:19 +02:00
const BN = require('ethereumjs-util').BN
2017-09-13 00:06:19 +02:00
class BalanceController {
/**
* Controller responsible for storing and updating an account's balance.
*
* @typedef {Object} BalanceController
* @param {Object} opts Initialize various properties of the class.
* @property {string} address A base 16 hex string. The account address which has the balance managed by this
* BalanceController.
2018-04-20 20:18:58 +02:00
* @property {AccountTracker} accountTracker Stores and updates the users accounts
* for which this BalanceController manages balance.
* @property {TransactionController} txController Stores, tracks and manages transactions. Here used to create a listener for
* transaction updates.
* @property {BlockTracker} blockTracker Tracks updates to blocks. On new blocks, this BalanceController updates its balance
* @property {Object} store The store for the ethBalance
* @property {string} store.ethBalance A base 16 hex string. The balance for the current account.
* @property {PendingBalanceCalculator} balanceCalc Used to calculate the accounts balance with possible pending
* transaction costs taken into account.
*
*/
2017-09-13 00:06:19 +02:00
constructor (opts = {}) {
2017-10-18 21:21:22 +02:00
this._validateParams(opts)
2017-09-25 23:39:22 +02:00
const { address, accountTracker, txController, blockTracker } = opts
2017-10-18 21:21:22 +02:00
2017-09-13 23:20:19 +02:00
this.address = address
2017-09-22 23:13:56 +02:00
this.accountTracker = accountTracker
2017-09-13 00:06:19 +02:00
this.txController = txController
2017-09-25 23:39:22 +02:00
this.blockTracker = blockTracker
2017-09-13 00:06:19 +02:00
const initState = {
2017-09-13 00:06:19 +02:00
ethBalance: undefined,
}
2017-09-13 00:06:19 +02:00
this.store = new ObservableStore(initState)
this.balanceCalc = new PendingBalanceCalculator({
2017-09-25 20:42:08 +02:00
getBalance: () => this._getBalance(),
2017-09-13 23:20:19 +02:00
getPendingTransactions: this._getPendingTransactions.bind(this),
2017-09-13 00:06:19 +02:00
})
2017-09-13 23:20:19 +02:00
2017-09-25 23:36:49 +02:00
this._registerUpdates()
2017-09-13 23:20:19 +02:00
}
/**
* Updates the ethBalance property to the current pending balance
*
* @returns {Promise<void>} Promises undefined
*/
2017-09-13 23:20:19 +02:00
async updateBalance () {
const balance = await this.balanceCalc.getBalance()
this.store.updateState({
ethBalance: balance,
})
}
/**
* Sets up listeners and subscriptions which should trigger an update of ethBalance. These updates include:
2018-04-20 20:18:58 +02:00
* - when a transaction changes state to 'submitted', 'confirmed' or 'failed'
* - when the current account changes (i.e. a new account is selected)
2018-07-03 00:49:33 +02:00
* - when there is a block update
*
* @private
*
*/
2017-09-25 23:36:49 +02:00
_registerUpdates () {
2017-09-13 23:20:19 +02:00
const update = this.updateBalance.bind(this)
this.txController.on('tx:status-update', (txId, status) => {
switch (status) {
case 'submitted':
case 'confirmed':
case 'failed':
update()
return
default:
return
}
})
this.accountTracker.store.subscribe(update)
2017-09-25 23:39:22 +02:00
this.blockTracker.on('block', update)
2017-09-13 23:20:19 +02:00
}
/**
* Gets the balance, as a base 16 hex string, of the account at this BalanceController's current address.
* If the current account has no balance, returns undefined.
*
* @returns {Promise<BN|void>} Promises a BN with a value equal to the balance of the current account, or undefined
* if the current account has no balance
*
*/
2017-09-25 20:42:08 +02:00
async _getBalance () {
const { accounts } = this.accountTracker.store.getState()
2017-09-25 20:42:08 +02:00
const entry = accounts[this.address]
2017-09-13 23:20:19 +02:00
const balance = entry.balance
return balance ? new BN(balance.substring(2), 16) : undefined
2017-09-13 23:20:19 +02:00
}
/**
* Gets the pending transactions (i.e. those with a 'submitted' status). These are accessed from the
2018-07-03 00:49:33 +02:00
* TransactionController passed to this BalanceController during construction.
*
* @private
* @returns {Promise<array>} Promises an array of transaction objects.
*
*/
2017-09-25 20:42:08 +02:00
async _getPendingTransactions () {
2017-09-13 23:20:19 +02:00
const pending = this.txController.getFilteredTxList({
from: this.address,
status: 'submitted',
err: undefined,
})
2017-09-25 20:42:08 +02:00
return pending
2017-09-13 00:06:19 +02:00
}
/**
* Validates that the passed options have all required properties.
*
* @param {Object} opts The options object to validate
* @throws {string} Throw a custom error indicating that address, accountTracker, txController and blockTracker are
* missing and at least one is required
*
*/
2017-10-18 21:21:22 +02:00
_validateParams (opts) {
const { address, accountTracker, txController, blockTracker } = opts
if (!address || !accountTracker || !txController || !blockTracker) {
const error = 'Cannot construct a balance checker without address, accountTracker, txController, and blockTracker.'
throw new Error(error)
}
}
2017-09-13 00:06:19 +02:00
}
module.exports = BalanceController