2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} CachedBalancesOptions
|
|
|
|
* @property {Object} accountTracker An {@code AccountTracker} reference
|
2021-03-02 23:53:07 +01:00
|
|
|
* @property {Function} getCurrentChainId A function to get the current chain id
|
2018-11-30 23:51:24 +01:00
|
|
|
* @property {Object} initState The initial controller state
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background controller responsible for maintaining
|
|
|
|
* a cache of account balances in local storage
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class CachedBalancesController {
|
2018-11-30 23:51:24 +01:00
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {CachedBalancesOptions} [opts] - Controller configuration parameters
|
2018-11-30 23:51:24 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2021-03-02 23:53:07 +01:00
|
|
|
const { accountTracker, getCurrentChainId } = opts;
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.accountTracker = accountTracker;
|
2021-03-02 23:53:07 +01:00
|
|
|
this.getCurrentChainId = getCurrentChainId;
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const initState = { cachedBalances: {}, ...opts.initState };
|
|
|
|
this.store = new ObservableStore(initState);
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this._registerUpdates();
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-02 23:53:07 +01:00
|
|
|
* Updates the cachedBalances property for the current chain. Cached balances will be updated to those in the passed accounts
|
2018-11-30 23:51:24 +01:00
|
|
|
* if balances in the passed accounts are truthy.
|
|
|
|
*
|
2021-03-02 23:53:07 +01:00
|
|
|
* @param {Object} obj - The the recently updated accounts object for the current chain
|
2018-11-30 23:51:24 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async updateCachedBalances({ accounts }) {
|
2021-03-02 23:53:07 +01:00
|
|
|
const chainId = this.getCurrentChainId();
|
2020-11-03 00:41:28 +01:00
|
|
|
const balancesToCache = await this._generateBalancesToCache(
|
|
|
|
accounts,
|
2021-03-02 23:53:07 +01:00
|
|
|
chainId,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-11-30 23:51:24 +01:00
|
|
|
this.store.updateState({
|
|
|
|
cachedBalances: balancesToCache,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 23:53:07 +01:00
|
|
|
_generateBalancesToCache(newAccounts, chainId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { cachedBalances } = this.store.getState();
|
2021-03-02 23:53:07 +01:00
|
|
|
const currentChainBalancesToCache = { ...cachedBalances[chainId] };
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
Object.keys(newAccounts).forEach((accountID) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const account = newAccounts[accountID];
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
if (account.balance) {
|
2021-03-02 23:53:07 +01:00
|
|
|
currentChainBalancesToCache[accountID] = account.balance;
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
const balancesToCache = {
|
|
|
|
...cachedBalances,
|
2021-03-02 23:53:07 +01:00
|
|
|
[chainId]: currentChainBalancesToCache,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return balancesToCache;
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 04:09:38 +02:00
|
|
|
/**
|
|
|
|
* Removes cachedBalances
|
|
|
|
*/
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
clearCachedBalances() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ cachedBalances: {} });
|
2020-07-17 04:09:38 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 23:51:24 +01:00
|
|
|
/**
|
|
|
|
* Sets up listeners and subscriptions which should trigger an update of cached balances. These updates will
|
|
|
|
* happen when the current account changes. Which happens on block updates, as well as on network and account
|
|
|
|
* selections.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_registerUpdates() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const update = this.updateCachedBalances.bind(this);
|
|
|
|
this.accountTracker.store.subscribe(update);
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
|
|
|
}
|