1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/controllers/cached-balances.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

import { ObservableStore } from '@metamask/obs-store';
/**
* @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
* @property {object} initState The initial controller state
*/
/**
* Background controller responsible for maintaining
* a cache of account balances in local storage
*/
export default class CachedBalancesController {
/**
* Creates a new controller instance
*
* @param {CachedBalancesOptions} [opts] - Controller configuration parameters
*/
2020-11-03 00:41:28 +01:00
constructor(opts = {}) {
2021-03-02 23:53:07 +01:00
const { accountTracker, getCurrentChainId } = opts;
this.accountTracker = accountTracker;
2021-03-02 23:53:07 +01:00
this.getCurrentChainId = getCurrentChainId;
const initState = { cachedBalances: {}, ...opts.initState };
this.store = new ObservableStore(initState);
this._registerUpdates();
}
/**
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
* if balances in the passed accounts are truthy.
*
* @param {object} obj - The the recently updated accounts object for the current chain
* @param obj.accounts
* @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,
);
this.store.updateState({
cachedBalances: balancesToCache,
});
}
2021-03-02 23:53:07 +01:00
_generateBalancesToCache(newAccounts, chainId) {
const { cachedBalances } = this.store.getState();
2021-03-02 23:53:07 +01:00
const currentChainBalancesToCache = { ...cachedBalances[chainId] };
2020-02-15 21:34:12 +01:00
Object.keys(newAccounts).forEach((accountID) => {
const account = newAccounts[accountID];
if (account.balance) {
2021-03-02 23:53:07 +01:00
currentChainBalancesToCache[accountID] = account.balance;
}
});
const balancesToCache = {
...cachedBalances,
2021-03-02 23:53:07 +01:00
[chainId]: currentChainBalancesToCache,
};
return balancesToCache;
}
/**
* Removes cachedBalances
*/
2020-11-03 00:41:28 +01:00
clearCachedBalances() {
this.store.updateState({ cachedBalances: {} });
}
/**
* 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() {
const update = this.updateCachedBalances.bind(this);
this.accountTracker.store.subscribe(update);
}
}