2017-09-13 00:06:19 +02:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const extend = require('xtend')
|
|
|
|
const BalanceController = require('./balance')
|
|
|
|
|
2018-04-19 21:12:04 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} ComputedBalancesOptions
|
|
|
|
* @property {Object} accountTracker Account tracker store reference
|
|
|
|
* @property {Object} txController Token controller reference
|
|
|
|
* @property {Object} blockTracker Block tracker reference
|
|
|
|
* @property {Object} initState Initial state to populate this internal store with
|
|
|
|
*/
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Background controller responsible for syncing
|
|
|
|
* and computing ETH balances for all accounts
|
|
|
|
*/
|
2017-09-22 22:59:25 +02:00
|
|
|
class ComputedbalancesController {
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
2018-07-03 00:49:33 +02:00
|
|
|
* @param {ComputedBalancesOptions} [opts] Controller configuration parameters
|
2018-04-19 20:37:08 +02:00
|
|
|
*/
|
2017-09-13 00:06:19 +02:00
|
|
|
constructor (opts = {}) {
|
2017-09-25 23:39:22 +02:00
|
|
|
const { accountTracker, txController, blockTracker } = opts
|
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 = extend({
|
2017-09-13 23:20:19 +02:00
|
|
|
computedBalances: {},
|
2017-09-13 00:06:19 +02:00
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
2017-09-14 00:02:05 +02:00
|
|
|
this.balances = {}
|
2017-09-13 00:06:19 +02:00
|
|
|
|
|
|
|
this._initBalanceUpdating()
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Updates balances associated with each internal address
|
|
|
|
*/
|
2017-09-14 00:02:05 +02:00
|
|
|
updateAllBalances () {
|
2017-10-19 18:59:57 +02:00
|
|
|
Object.keys(this.balances).forEach((balance) => {
|
|
|
|
const address = balance.address
|
2017-09-14 00:02:05 +02:00
|
|
|
this.balances[address].updateBalance()
|
2017-10-19 18:59:57 +02:00
|
|
|
})
|
2017-09-14 00:02:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Initializes internal address tracking
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2017-09-13 23:20:19 +02:00
|
|
|
_initBalanceUpdating () {
|
2017-09-26 23:15:16 +02:00
|
|
|
const store = this.accountTracker.store.getState()
|
2017-10-19 00:08:34 +02:00
|
|
|
this.syncAllAccountsFromStore(store)
|
|
|
|
this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
|
2017-09-13 00:06:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Uses current account state to sync and track all
|
|
|
|
* addresses associated with the current account
|
|
|
|
*
|
2018-04-19 21:12:04 +02:00
|
|
|
* @param {{ accounts: Object }} store Account tracking state
|
2018-04-19 20:37:08 +02:00
|
|
|
*/
|
2017-10-21 21:06:39 +02:00
|
|
|
syncAllAccountsFromStore (store) {
|
2017-10-19 00:08:34 +02:00
|
|
|
const upstream = Object.keys(store.accounts)
|
|
|
|
const balances = Object.keys(this.balances)
|
|
|
|
.map(address => this.balances[address])
|
2017-09-13 00:06:19 +02:00
|
|
|
|
2017-10-19 00:08:34 +02:00
|
|
|
// Follow new addresses
|
2017-10-21 21:06:39 +02:00
|
|
|
for (const address in balances) {
|
2017-09-13 23:20:19 +02:00
|
|
|
this.trackAddressIfNotAlready(address)
|
2017-09-13 00:06:19 +02:00
|
|
|
}
|
2017-10-19 00:08:34 +02:00
|
|
|
|
|
|
|
// Unfollow old ones
|
|
|
|
balances.forEach(({ address }) => {
|
|
|
|
if (!upstream.includes(address)) {
|
|
|
|
delete this.balances[address]
|
|
|
|
}
|
|
|
|
})
|
2017-09-13 00:06:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Conditionally establishes a new subscription
|
|
|
|
* to track an address associated with the current
|
|
|
|
* account
|
|
|
|
*
|
|
|
|
* @param {string} address Address to conditionally subscribe to
|
|
|
|
*/
|
2017-09-13 23:20:19 +02:00
|
|
|
trackAddressIfNotAlready (address) {
|
|
|
|
const state = this.store.getState()
|
|
|
|
if (!(address in state.computedBalances)) {
|
|
|
|
this.trackAddress(address)
|
2017-09-13 00:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:37:08 +02:00
|
|
|
/**
|
|
|
|
* Establishes a new subscription to track an
|
|
|
|
* address associated with the current account
|
|
|
|
*
|
|
|
|
* @param {string} address Address to conditionally subscribe to
|
|
|
|
*/
|
2017-09-13 23:20:19 +02:00
|
|
|
trackAddress (address) {
|
2017-10-21 21:06:39 +02:00
|
|
|
const updater = new BalanceController({
|
2017-09-13 23:20:19 +02:00
|
|
|
address,
|
2017-09-22 23:13:56 +02:00
|
|
|
accountTracker: this.accountTracker,
|
2017-09-13 23:20:19 +02:00
|
|
|
txController: this.txController,
|
2017-09-25 23:39:22 +02:00
|
|
|
blockTracker: this.blockTracker,
|
2017-09-13 23:20:19 +02:00
|
|
|
})
|
|
|
|
updater.store.subscribe((accountBalance) => {
|
2017-10-21 21:06:39 +02:00
|
|
|
const newState = this.store.getState()
|
2017-09-13 23:20:19 +02:00
|
|
|
newState.computedBalances[address] = accountBalance
|
|
|
|
this.store.updateState(newState)
|
|
|
|
})
|
2017-09-14 00:02:05 +02:00
|
|
|
this.balances[address] = updater
|
2017-09-13 23:20:19 +02:00
|
|
|
updater.updateBalance()
|
2017-09-13 00:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 22:59:25 +02:00
|
|
|
module.exports = ComputedbalancesController
|