@metamask/assets-controllers 45.1.1 → 46.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -1
- package/dist/AccountTrackerController.d.cts +2 -2
- package/dist/AccountTrackerController.d.mts +2 -2
- package/dist/CurrencyRateController.d.cts +2 -2
- package/dist/CurrencyRateController.d.mts +2 -2
- package/dist/MultichainBalancesController/BalancesTracker.cjs +122 -0
- package/dist/MultichainBalancesController/BalancesTracker.cjs.map +1 -0
- package/dist/MultichainBalancesController/BalancesTracker.d.cts +54 -0
- package/dist/MultichainBalancesController/BalancesTracker.d.cts.map +1 -0
- package/dist/MultichainBalancesController/BalancesTracker.d.mts +54 -0
- package/dist/MultichainBalancesController/BalancesTracker.d.mts.map +1 -0
- package/dist/MultichainBalancesController/BalancesTracker.mjs +118 -0
- package/dist/MultichainBalancesController/BalancesTracker.mjs.map +1 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.cjs +194 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.cjs.map +1 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.d.cts +94 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.d.cts.map +1 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.d.mts +94 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.d.mts.map +1 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.mjs +189 -0
- package/dist/MultichainBalancesController/MultichainBalancesController.mjs.map +1 -0
- package/dist/MultichainBalancesController/Poller.cjs +45 -0
- package/dist/MultichainBalancesController/Poller.cjs.map +1 -0
- package/dist/MultichainBalancesController/Poller.d.cts +7 -0
- package/dist/MultichainBalancesController/Poller.d.cts.map +1 -0
- package/dist/MultichainBalancesController/Poller.d.mts +7 -0
- package/dist/MultichainBalancesController/Poller.d.mts.map +1 -0
- package/dist/MultichainBalancesController/Poller.mjs +41 -0
- package/dist/MultichainBalancesController/Poller.mjs.map +1 -0
- package/dist/MultichainBalancesController/constants.cjs +45 -0
- package/dist/MultichainBalancesController/constants.cjs.map +1 -0
- package/dist/MultichainBalancesController/constants.d.cts +29 -0
- package/dist/MultichainBalancesController/constants.d.cts.map +1 -0
- package/dist/MultichainBalancesController/constants.d.mts +29 -0
- package/dist/MultichainBalancesController/constants.d.mts.map +1 -0
- package/dist/MultichainBalancesController/constants.mjs +42 -0
- package/dist/MultichainBalancesController/constants.mjs.map +1 -0
- package/dist/MultichainBalancesController/error.cjs +18 -0
- package/dist/MultichainBalancesController/error.cjs.map +1 -0
- package/dist/MultichainBalancesController/error.d.cts +7 -0
- package/dist/MultichainBalancesController/error.d.cts.map +1 -0
- package/dist/MultichainBalancesController/error.d.mts +7 -0
- package/dist/MultichainBalancesController/error.d.mts.map +1 -0
- package/dist/MultichainBalancesController/error.mjs +13 -0
- package/dist/MultichainBalancesController/error.mjs.map +1 -0
- package/dist/MultichainBalancesController/index.cjs +13 -0
- package/dist/MultichainBalancesController/index.cjs.map +1 -0
- package/dist/MultichainBalancesController/index.d.cts +5 -0
- package/dist/MultichainBalancesController/index.d.cts.map +1 -0
- package/dist/MultichainBalancesController/index.d.mts +5 -0
- package/dist/MultichainBalancesController/index.d.mts.map +1 -0
- package/dist/MultichainBalancesController/index.mjs +4 -0
- package/dist/MultichainBalancesController/index.mjs.map +1 -0
- package/dist/MultichainBalancesController/utils.cjs +73 -0
- package/dist/MultichainBalancesController/utils.cjs.map +1 -0
- package/dist/MultichainBalancesController/utils.d.cts +36 -0
- package/dist/MultichainBalancesController/utils.d.cts.map +1 -0
- package/dist/MultichainBalancesController/utils.d.mts +36 -0
- package/dist/MultichainBalancesController/utils.d.mts.map +1 -0
- package/dist/MultichainBalancesController/utils.mjs +66 -0
- package/dist/MultichainBalancesController/utils.mjs.map +1 -0
- package/dist/NftController.cjs.map +1 -1
- package/dist/NftController.mjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +2 -2
- package/dist/TokenBalancesController.d.mts +2 -2
- package/dist/TokenDetectionController.d.cts +2 -2
- package/dist/TokenDetectionController.d.mts +2 -2
- package/dist/TokenListController.d.cts +2 -2
- package/dist/TokenListController.d.mts +2 -2
- package/dist/TokenRatesController.cjs.map +1 -1
- package/dist/TokenRatesController.d.cts +2 -2
- package/dist/TokenRatesController.d.mts +2 -2
- package/dist/TokenRatesController.mjs.map +1 -1
- package/dist/TokensController.cjs +6 -4
- package/dist/TokensController.cjs.map +1 -1
- package/dist/TokensController.d.cts.map +1 -1
- package/dist/TokensController.d.mts.map +1 -1
- package/dist/TokensController.mjs +6 -4
- package/dist/TokensController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +16 -1
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +7 -0
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +7 -0
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +14 -0
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.cjs +5 -2
- package/dist/crypto-compare-service/crypto-compare.cjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.cts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.mts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.mjs +5 -2
- package/dist/crypto-compare-service/crypto-compare.mjs.map +1 -1
- package/dist/index.cjs +10 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -1
- package/dist/index.mjs.map +1 -1
- package/dist/multicall.cjs +16 -3
- package/dist/multicall.cjs.map +1 -1
- package/dist/multicall.d.cts.map +1 -1
- package/dist/multicall.d.mts.map +1 -1
- package/dist/multicall.mjs +16 -3
- package/dist/multicall.mjs.map +1 -1
- package/package.json +17 -11
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [46.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add new `MultichainBalancesController` ([#4965](https://github.com/MetaMask/core/pull/4965))
|
|
15
|
+
- This controller has been migrated from the MetaMask extension codebase.
|
|
16
|
+
- Added utility function `getKeyByValue` ([#5099](https://github.com/MetaMask/core/pull/5099))
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- **BREAKING:** Bump `@metamask/accounts-controller` peer dependency from `^20.0.0` to `^21.0.0` ([#5140](https://github.com/MetaMask/core/pull/5140))
|
|
21
|
+
- Bump `@metamask/base-controller` from `^7.0.0` to `^7.1.1` ([#5079](https://github.com/MetaMask/core/pull/5079)), ([#5135](https://github.com/MetaMask/core/pull/5135))
|
|
22
|
+
- Bump `@metamask/keyring-api` from `^12.0.0` to `^13.0.0` ([#5066](https://github.com/MetaMask/core/pull/5066))
|
|
23
|
+
- Bump `@metamask/utils` to `^11.0.1` ([#5080](https://github.com/MetaMask/core/pull/5080))
|
|
24
|
+
- Bump `@metamask/rpc-errors` to `^7.0.2` ([#5080](https://github.com/MetaMask/core/pull/5080))
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- Fix Mantle price when calling `fetchMultiExchangeRate` ([#5099](https://github.com/MetaMask/core/pull/5099))
|
|
29
|
+
- Fix multicall revert in `TokenBalancesController` ([#5083](https://github.com/MetaMask/core/pull/5083))
|
|
30
|
+
- `TokenBalancesController` was fixed to fetch erc20 token balances even if there's an invalid token in state whose address does not point to a smart contract.
|
|
31
|
+
- Fix state changes for `ignoreTokens` for non-selected networks ([#5014](https://github.com/MetaMask/core/pull/5014))
|
|
32
|
+
|
|
33
|
+
## [45.1.2]
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Remove use of `@metamask/keyring-api` ([#4695](https://github.com/MetaMask/core/pull/4695))
|
|
38
|
+
- `@metamask/providers` and `webextension-polyfill` peer dependencies are no longer required.
|
|
39
|
+
- Use new `@metamask/keyring-internal-api@^1.0.0` ([#4695](https://github.com/MetaMask/core/pull/4695))
|
|
40
|
+
- This package has been split out from the Keyring API. Its types are compatible with the `@metamask/keyring-api` package used previously.
|
|
41
|
+
|
|
10
42
|
## [45.1.1]
|
|
11
43
|
|
|
12
44
|
### Changed
|
|
@@ -1291,7 +1323,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1291
1323
|
|
|
1292
1324
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
1293
1325
|
|
|
1294
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
1326
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@46.0.0...HEAD
|
|
1327
|
+
[46.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@45.1.2...@metamask/assets-controllers@46.0.0
|
|
1328
|
+
[45.1.2]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@45.1.1...@metamask/assets-controllers@45.1.2
|
|
1295
1329
|
[45.1.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@45.1.0...@metamask/assets-controllers@45.1.1
|
|
1296
1330
|
[45.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@45.0.0...@metamask/assets-controllers@45.1.0
|
|
1297
1331
|
[45.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@44.1.0...@metamask/assets-controllers@45.0.0
|
|
@@ -65,8 +65,8 @@ type AccountTrackerPollingInput = {
|
|
|
65
65
|
networkClientId: NetworkClientId;
|
|
66
66
|
};
|
|
67
67
|
declare const AccountTrackerController_base: (abstract new (...args: any[]) => {
|
|
68
|
-
readonly "__#
|
|
69
|
-
"__#
|
|
68
|
+
readonly "__#13@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
69
|
+
"__#13@#intervalLength": number | undefined;
|
|
70
70
|
setIntervalLength(intervalLength: number): void;
|
|
71
71
|
getIntervalLength(): number | undefined;
|
|
72
72
|
_startPolling(input: AccountTrackerPollingInput): void;
|
|
@@ -65,8 +65,8 @@ type AccountTrackerPollingInput = {
|
|
|
65
65
|
networkClientId: NetworkClientId;
|
|
66
66
|
};
|
|
67
67
|
declare const AccountTrackerController_base: (abstract new (...args: any[]) => {
|
|
68
|
-
readonly "__#
|
|
69
|
-
"__#
|
|
68
|
+
readonly "__#13@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
69
|
+
"__#13@#intervalLength": number | undefined;
|
|
70
70
|
setIntervalLength(intervalLength: number): void;
|
|
71
71
|
getIntervalLength(): number | undefined;
|
|
72
72
|
_startPolling(input: AccountTrackerPollingInput): void;
|
|
@@ -29,8 +29,8 @@ type CurrencyRatePollingInput = {
|
|
|
29
29
|
nativeCurrencies: string[];
|
|
30
30
|
};
|
|
31
31
|
declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
32
|
-
readonly "__#
|
|
33
|
-
"__#
|
|
32
|
+
readonly "__#13@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
33
|
+
"__#13@#intervalLength": number | undefined;
|
|
34
34
|
setIntervalLength(intervalLength: number): void;
|
|
35
35
|
getIntervalLength(): number | undefined;
|
|
36
36
|
_startPolling(input: CurrencyRatePollingInput): void;
|
|
@@ -29,8 +29,8 @@ type CurrencyRatePollingInput = {
|
|
|
29
29
|
nativeCurrencies: string[];
|
|
30
30
|
};
|
|
31
31
|
declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
32
|
-
readonly "__#
|
|
33
|
-
"__#
|
|
32
|
+
readonly "__#13@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
33
|
+
"__#13@#intervalLength": number | undefined;
|
|
34
34
|
setIntervalLength(intervalLength: number): void;
|
|
35
35
|
getIntervalLength(): number | undefined;
|
|
36
36
|
_startPolling(input: CurrencyRatePollingInput): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _BalancesTracker_instances, _BalancesTracker_poller, _BalancesTracker_updateBalance, _BalancesTracker_balances, _BalancesTracker_isBalanceOutdated;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BalancesTracker = void 0;
|
|
16
|
+
const Poller_1 = require("./Poller.cjs");
|
|
17
|
+
const BALANCES_TRACKING_INTERVAL = 5000; // Every 5s in milliseconds.
|
|
18
|
+
class BalancesTracker {
|
|
19
|
+
constructor(updateBalanceCallback) {
|
|
20
|
+
_BalancesTracker_instances.add(this);
|
|
21
|
+
_BalancesTracker_poller.set(this, void 0);
|
|
22
|
+
_BalancesTracker_updateBalance.set(this, void 0);
|
|
23
|
+
_BalancesTracker_balances.set(this, {});
|
|
24
|
+
__classPrivateFieldSet(this, _BalancesTracker_updateBalance, updateBalanceCallback, "f");
|
|
25
|
+
__classPrivateFieldSet(this, _BalancesTracker_poller, new Poller_1.Poller(() => this.updateBalances(), BALANCES_TRACKING_INTERVAL), "f");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Starts the tracking process.
|
|
29
|
+
*/
|
|
30
|
+
start() {
|
|
31
|
+
__classPrivateFieldGet(this, _BalancesTracker_poller, "f").start();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Stops the tracking process.
|
|
35
|
+
*/
|
|
36
|
+
stop() {
|
|
37
|
+
__classPrivateFieldGet(this, _BalancesTracker_poller, "f").stop();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Checks if an account ID is being tracked.
|
|
41
|
+
*
|
|
42
|
+
* @param accountId - The account ID.
|
|
43
|
+
* @returns True if the account is being tracked, false otherwise.
|
|
44
|
+
*/
|
|
45
|
+
isTracked(accountId) {
|
|
46
|
+
return Object.prototype.hasOwnProperty.call(__classPrivateFieldGet(this, _BalancesTracker_balances, "f"), accountId);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Asserts that an account ID is being tracked.
|
|
50
|
+
*
|
|
51
|
+
* @param accountId - The account ID.
|
|
52
|
+
* @throws If the account ID is not being tracked.
|
|
53
|
+
*/
|
|
54
|
+
assertBeingTracked(accountId) {
|
|
55
|
+
if (!this.isTracked(accountId)) {
|
|
56
|
+
throw new Error(`Account is not being tracked: ${accountId}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Starts tracking a new account ID. This method has no effect on already tracked
|
|
61
|
+
* accounts.
|
|
62
|
+
*
|
|
63
|
+
* @param accountId - The account ID.
|
|
64
|
+
* @param blockTime - The block time (used when refreshing the account balances).
|
|
65
|
+
*/
|
|
66
|
+
track(accountId, blockTime) {
|
|
67
|
+
// Do not overwrite current info if already being tracked!
|
|
68
|
+
if (!this.isTracked(accountId)) {
|
|
69
|
+
__classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId] = {
|
|
70
|
+
lastUpdated: 0,
|
|
71
|
+
blockTime,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Stops tracking a tracked account ID.
|
|
77
|
+
*
|
|
78
|
+
* @param accountId - The account ID.
|
|
79
|
+
* @throws If the account ID is not being tracked.
|
|
80
|
+
*/
|
|
81
|
+
untrack(accountId) {
|
|
82
|
+
this.assertBeingTracked(accountId);
|
|
83
|
+
delete __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Update the balances for a tracked account ID.
|
|
87
|
+
*
|
|
88
|
+
* @param accountId - The account ID.
|
|
89
|
+
* @throws If the account ID is not being tracked.
|
|
90
|
+
*/
|
|
91
|
+
async updateBalance(accountId) {
|
|
92
|
+
this.assertBeingTracked(accountId);
|
|
93
|
+
// We check if the balance is outdated (by comparing to the block time associated
|
|
94
|
+
// with this kind of account).
|
|
95
|
+
//
|
|
96
|
+
// This might not be super accurate, but we could probably compute this differently
|
|
97
|
+
// and try to sync with the "real block time"!
|
|
98
|
+
const info = __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
|
|
99
|
+
if (__classPrivateFieldGet(this, _BalancesTracker_instances, "m", _BalancesTracker_isBalanceOutdated).call(this, info)) {
|
|
100
|
+
await __classPrivateFieldGet(this, _BalancesTracker_updateBalance, "f").call(this, accountId);
|
|
101
|
+
__classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId].lastUpdated = Date.now();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Update the balances of all tracked accounts (only if the balances
|
|
106
|
+
* is considered outdated).
|
|
107
|
+
*/
|
|
108
|
+
async updateBalances() {
|
|
109
|
+
await Promise.allSettled(Object.keys(__classPrivateFieldGet(this, _BalancesTracker_balances, "f")).map(async (accountId) => {
|
|
110
|
+
await this.updateBalance(accountId);
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.BalancesTracker = BalancesTracker;
|
|
115
|
+
_BalancesTracker_poller = new WeakMap(), _BalancesTracker_updateBalance = new WeakMap(), _BalancesTracker_balances = new WeakMap(), _BalancesTracker_instances = new WeakSet(), _BalancesTracker_isBalanceOutdated = function _BalancesTracker_isBalanceOutdated({ lastUpdated, blockTime }) {
|
|
116
|
+
return (
|
|
117
|
+
// Never been updated:
|
|
118
|
+
lastUpdated === 0 ||
|
|
119
|
+
// Outdated:
|
|
120
|
+
Date.now() - lastUpdated >= blockTime);
|
|
121
|
+
};
|
|
122
|
+
//# sourceMappingURL=BalancesTracker.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BalancesTracker.cjs","sourceRoot":"","sources":["../../src/MultichainBalancesController/BalancesTracker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,yCAAkC;AAOlC,MAAM,0BAA0B,GAAG,IAAI,CAAC,CAAC,4BAA4B;AAErE,MAAa,eAAe;IAO1B,YAAY,qBAA2D;;QANvE,0CAAgB;QAEhB,iDAAqD;QAErD,oCAAyC,EAAE,EAAC;QAG1C,uBAAA,IAAI,kCAAkB,qBAAqB,MAAA,CAAC;QAE5C,uBAAA,IAAI,2BAAW,IAAI,eAAM,CACvB,GAAG,EAAE,CAAC,IAAI,CAAC,cAAc,EAAE,EAC3B,0BAA0B,CAC3B,MAAA,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,+BAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,uBAAA,IAAI,+BAAQ,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,SAAiB;QACzB,OAAO,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,uBAAA,IAAI,iCAAU,EAAE,SAAS,CAAC,CAAC;IACzE,CAAC;IAED;;;;;OAKG;IACH,kBAAkB,CAAC,SAAiB;QAClC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,iCAAiC,SAAS,EAAE,CAAC,CAAC;SAC/D;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,SAAiB,EAAE,SAAiB;QACxC,0DAA0D;QAC1D,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;YAC9B,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,GAAG;gBAC1B,WAAW,EAAE,CAAC;gBACd,SAAS;aACV,CAAC;SACH;IACH,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,SAAiB;QACvB,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;QACnC,OAAO,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,aAAa,CAAC,SAAiB;QACnC,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;QAEnC,iFAAiF;QACjF,8BAA8B;QAC9B,EAAE;QACF,mFAAmF;QACnF,8CAA8C;QAC9C,MAAM,IAAI,GAAG,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC;QACvC,IAAI,uBAAA,IAAI,sEAAmB,MAAvB,IAAI,EAAoB,IAAI,CAAC,EAAE;YACjC,MAAM,uBAAA,IAAI,sCAAe,MAAnB,IAAI,EAAgB,SAAS,CAAC,CAAC;YACrC,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;SACpD;IACH,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,cAAc;QAClB,MAAM,OAAO,CAAC,UAAU,CACtB,MAAM,CAAC,IAAI,CAAC,uBAAA,IAAI,iCAAU,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;YAClD,MAAM,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QACtC,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CAkBF;AAjID,0CAiIC;iQARoB,EAAE,WAAW,EAAE,SAAS,EAAe;IACxD,OAAO;IACL,sBAAsB;IACtB,WAAW,KAAK,CAAC;QACjB,YAAY;QACZ,IAAI,CAAC,GAAG,EAAE,GAAG,WAAW,IAAI,SAAS,CACtC,CAAC;AACJ,CAAC","sourcesContent":["import { Poller } from './Poller';\n\ntype BalanceInfo = {\n lastUpdated: number;\n blockTime: number;\n};\n\nconst BALANCES_TRACKING_INTERVAL = 5000; // Every 5s in milliseconds.\n\nexport class BalancesTracker {\n #poller: Poller;\n\n #updateBalance: (accountId: string) => Promise<void>;\n\n #balances: Record<string, BalanceInfo> = {};\n\n constructor(updateBalanceCallback: (accountId: string) => Promise<void>) {\n this.#updateBalance = updateBalanceCallback;\n\n this.#poller = new Poller(\n () => this.updateBalances(),\n BALANCES_TRACKING_INTERVAL,\n );\n }\n\n /**\n * Starts the tracking process.\n */\n start(): void {\n this.#poller.start();\n }\n\n /**\n * Stops the tracking process.\n */\n stop(): void {\n this.#poller.stop();\n }\n\n /**\n * Checks if an account ID is being tracked.\n *\n * @param accountId - The account ID.\n * @returns True if the account is being tracked, false otherwise.\n */\n isTracked(accountId: string) {\n return Object.prototype.hasOwnProperty.call(this.#balances, accountId);\n }\n\n /**\n * Asserts that an account ID is being tracked.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n assertBeingTracked(accountId: string) {\n if (!this.isTracked(accountId)) {\n throw new Error(`Account is not being tracked: ${accountId}`);\n }\n }\n\n /**\n * Starts tracking a new account ID. This method has no effect on already tracked\n * accounts.\n *\n * @param accountId - The account ID.\n * @param blockTime - The block time (used when refreshing the account balances).\n */\n track(accountId: string, blockTime: number) {\n // Do not overwrite current info if already being tracked!\n if (!this.isTracked(accountId)) {\n this.#balances[accountId] = {\n lastUpdated: 0,\n blockTime,\n };\n }\n }\n\n /**\n * Stops tracking a tracked account ID.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n untrack(accountId: string) {\n this.assertBeingTracked(accountId);\n delete this.#balances[accountId];\n }\n\n /**\n * Update the balances for a tracked account ID.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n async updateBalance(accountId: string) {\n this.assertBeingTracked(accountId);\n\n // We check if the balance is outdated (by comparing to the block time associated\n // with this kind of account).\n //\n // This might not be super accurate, but we could probably compute this differently\n // and try to sync with the \"real block time\"!\n const info = this.#balances[accountId];\n if (this.#isBalanceOutdated(info)) {\n await this.#updateBalance(accountId);\n this.#balances[accountId].lastUpdated = Date.now();\n }\n }\n\n /**\n * Update the balances of all tracked accounts (only if the balances\n * is considered outdated).\n */\n async updateBalances() {\n await Promise.allSettled(\n Object.keys(this.#balances).map(async (accountId) => {\n await this.updateBalance(accountId);\n }),\n );\n }\n\n /**\n * Checks if the balance is outdated according to the provided data.\n *\n * @param param - The balance info.\n * @param param.lastUpdated - The last updated timestamp.\n * @param param.blockTime - The block time.\n * @returns True if the balance is outdated, false otherwise.\n */\n #isBalanceOutdated({ lastUpdated, blockTime }: BalanceInfo): boolean {\n return (\n // Never been updated:\n lastUpdated === 0 ||\n // Outdated:\n Date.now() - lastUpdated >= blockTime\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export declare class BalancesTracker {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(updateBalanceCallback: (accountId: string) => Promise<void>);
|
|
4
|
+
/**
|
|
5
|
+
* Starts the tracking process.
|
|
6
|
+
*/
|
|
7
|
+
start(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Stops the tracking process.
|
|
10
|
+
*/
|
|
11
|
+
stop(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if an account ID is being tracked.
|
|
14
|
+
*
|
|
15
|
+
* @param accountId - The account ID.
|
|
16
|
+
* @returns True if the account is being tracked, false otherwise.
|
|
17
|
+
*/
|
|
18
|
+
isTracked(accountId: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Asserts that an account ID is being tracked.
|
|
21
|
+
*
|
|
22
|
+
* @param accountId - The account ID.
|
|
23
|
+
* @throws If the account ID is not being tracked.
|
|
24
|
+
*/
|
|
25
|
+
assertBeingTracked(accountId: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Starts tracking a new account ID. This method has no effect on already tracked
|
|
28
|
+
* accounts.
|
|
29
|
+
*
|
|
30
|
+
* @param accountId - The account ID.
|
|
31
|
+
* @param blockTime - The block time (used when refreshing the account balances).
|
|
32
|
+
*/
|
|
33
|
+
track(accountId: string, blockTime: number): void;
|
|
34
|
+
/**
|
|
35
|
+
* Stops tracking a tracked account ID.
|
|
36
|
+
*
|
|
37
|
+
* @param accountId - The account ID.
|
|
38
|
+
* @throws If the account ID is not being tracked.
|
|
39
|
+
*/
|
|
40
|
+
untrack(accountId: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Update the balances for a tracked account ID.
|
|
43
|
+
*
|
|
44
|
+
* @param accountId - The account ID.
|
|
45
|
+
* @throws If the account ID is not being tracked.
|
|
46
|
+
*/
|
|
47
|
+
updateBalance(accountId: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Update the balances of all tracked accounts (only if the balances
|
|
50
|
+
* is considered outdated).
|
|
51
|
+
*/
|
|
52
|
+
updateBalances(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=BalancesTracker.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BalancesTracker.d.cts","sourceRoot":"","sources":["../../src/MultichainBalancesController/BalancesTracker.ts"],"names":[],"mappings":"AASA,qBAAa,eAAe;;gBAOd,qBAAqB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC;IASvE;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,IAAI,IAAI;IAIZ;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM;IAI3B;;;;;OAKG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM;IAMpC;;;;;;OAMG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAU1C;;;;;OAKG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM;IAKzB;;;;;OAKG;IACG,aAAa,CAAC,SAAS,EAAE,MAAM;IAerC;;;OAGG;IACG,cAAc;CAwBrB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export declare class BalancesTracker {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(updateBalanceCallback: (accountId: string) => Promise<void>);
|
|
4
|
+
/**
|
|
5
|
+
* Starts the tracking process.
|
|
6
|
+
*/
|
|
7
|
+
start(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Stops the tracking process.
|
|
10
|
+
*/
|
|
11
|
+
stop(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if an account ID is being tracked.
|
|
14
|
+
*
|
|
15
|
+
* @param accountId - The account ID.
|
|
16
|
+
* @returns True if the account is being tracked, false otherwise.
|
|
17
|
+
*/
|
|
18
|
+
isTracked(accountId: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Asserts that an account ID is being tracked.
|
|
21
|
+
*
|
|
22
|
+
* @param accountId - The account ID.
|
|
23
|
+
* @throws If the account ID is not being tracked.
|
|
24
|
+
*/
|
|
25
|
+
assertBeingTracked(accountId: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Starts tracking a new account ID. This method has no effect on already tracked
|
|
28
|
+
* accounts.
|
|
29
|
+
*
|
|
30
|
+
* @param accountId - The account ID.
|
|
31
|
+
* @param blockTime - The block time (used when refreshing the account balances).
|
|
32
|
+
*/
|
|
33
|
+
track(accountId: string, blockTime: number): void;
|
|
34
|
+
/**
|
|
35
|
+
* Stops tracking a tracked account ID.
|
|
36
|
+
*
|
|
37
|
+
* @param accountId - The account ID.
|
|
38
|
+
* @throws If the account ID is not being tracked.
|
|
39
|
+
*/
|
|
40
|
+
untrack(accountId: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Update the balances for a tracked account ID.
|
|
43
|
+
*
|
|
44
|
+
* @param accountId - The account ID.
|
|
45
|
+
* @throws If the account ID is not being tracked.
|
|
46
|
+
*/
|
|
47
|
+
updateBalance(accountId: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Update the balances of all tracked accounts (only if the balances
|
|
50
|
+
* is considered outdated).
|
|
51
|
+
*/
|
|
52
|
+
updateBalances(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=BalancesTracker.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BalancesTracker.d.mts","sourceRoot":"","sources":["../../src/MultichainBalancesController/BalancesTracker.ts"],"names":[],"mappings":"AASA,qBAAa,eAAe;;gBAOd,qBAAqB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC;IASvE;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,IAAI,IAAI;IAIZ;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM;IAI3B;;;;;OAKG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM;IAMpC;;;;;;OAMG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAU1C;;;;;OAKG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM;IAKzB;;;;;OAKG;IACG,aAAa,CAAC,SAAS,EAAE,MAAM;IAerC;;;OAGG;IACG,cAAc;CAwBrB"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _BalancesTracker_instances, _BalancesTracker_poller, _BalancesTracker_updateBalance, _BalancesTracker_balances, _BalancesTracker_isBalanceOutdated;
|
|
13
|
+
import { Poller } from "./Poller.mjs";
|
|
14
|
+
const BALANCES_TRACKING_INTERVAL = 5000; // Every 5s in milliseconds.
|
|
15
|
+
export class BalancesTracker {
|
|
16
|
+
constructor(updateBalanceCallback) {
|
|
17
|
+
_BalancesTracker_instances.add(this);
|
|
18
|
+
_BalancesTracker_poller.set(this, void 0);
|
|
19
|
+
_BalancesTracker_updateBalance.set(this, void 0);
|
|
20
|
+
_BalancesTracker_balances.set(this, {});
|
|
21
|
+
__classPrivateFieldSet(this, _BalancesTracker_updateBalance, updateBalanceCallback, "f");
|
|
22
|
+
__classPrivateFieldSet(this, _BalancesTracker_poller, new Poller(() => this.updateBalances(), BALANCES_TRACKING_INTERVAL), "f");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Starts the tracking process.
|
|
26
|
+
*/
|
|
27
|
+
start() {
|
|
28
|
+
__classPrivateFieldGet(this, _BalancesTracker_poller, "f").start();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Stops the tracking process.
|
|
32
|
+
*/
|
|
33
|
+
stop() {
|
|
34
|
+
__classPrivateFieldGet(this, _BalancesTracker_poller, "f").stop();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Checks if an account ID is being tracked.
|
|
38
|
+
*
|
|
39
|
+
* @param accountId - The account ID.
|
|
40
|
+
* @returns True if the account is being tracked, false otherwise.
|
|
41
|
+
*/
|
|
42
|
+
isTracked(accountId) {
|
|
43
|
+
return Object.prototype.hasOwnProperty.call(__classPrivateFieldGet(this, _BalancesTracker_balances, "f"), accountId);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Asserts that an account ID is being tracked.
|
|
47
|
+
*
|
|
48
|
+
* @param accountId - The account ID.
|
|
49
|
+
* @throws If the account ID is not being tracked.
|
|
50
|
+
*/
|
|
51
|
+
assertBeingTracked(accountId) {
|
|
52
|
+
if (!this.isTracked(accountId)) {
|
|
53
|
+
throw new Error(`Account is not being tracked: ${accountId}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Starts tracking a new account ID. This method has no effect on already tracked
|
|
58
|
+
* accounts.
|
|
59
|
+
*
|
|
60
|
+
* @param accountId - The account ID.
|
|
61
|
+
* @param blockTime - The block time (used when refreshing the account balances).
|
|
62
|
+
*/
|
|
63
|
+
track(accountId, blockTime) {
|
|
64
|
+
// Do not overwrite current info if already being tracked!
|
|
65
|
+
if (!this.isTracked(accountId)) {
|
|
66
|
+
__classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId] = {
|
|
67
|
+
lastUpdated: 0,
|
|
68
|
+
blockTime,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Stops tracking a tracked account ID.
|
|
74
|
+
*
|
|
75
|
+
* @param accountId - The account ID.
|
|
76
|
+
* @throws If the account ID is not being tracked.
|
|
77
|
+
*/
|
|
78
|
+
untrack(accountId) {
|
|
79
|
+
this.assertBeingTracked(accountId);
|
|
80
|
+
delete __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Update the balances for a tracked account ID.
|
|
84
|
+
*
|
|
85
|
+
* @param accountId - The account ID.
|
|
86
|
+
* @throws If the account ID is not being tracked.
|
|
87
|
+
*/
|
|
88
|
+
async updateBalance(accountId) {
|
|
89
|
+
this.assertBeingTracked(accountId);
|
|
90
|
+
// We check if the balance is outdated (by comparing to the block time associated
|
|
91
|
+
// with this kind of account).
|
|
92
|
+
//
|
|
93
|
+
// This might not be super accurate, but we could probably compute this differently
|
|
94
|
+
// and try to sync with the "real block time"!
|
|
95
|
+
const info = __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
|
|
96
|
+
if (__classPrivateFieldGet(this, _BalancesTracker_instances, "m", _BalancesTracker_isBalanceOutdated).call(this, info)) {
|
|
97
|
+
await __classPrivateFieldGet(this, _BalancesTracker_updateBalance, "f").call(this, accountId);
|
|
98
|
+
__classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId].lastUpdated = Date.now();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Update the balances of all tracked accounts (only if the balances
|
|
103
|
+
* is considered outdated).
|
|
104
|
+
*/
|
|
105
|
+
async updateBalances() {
|
|
106
|
+
await Promise.allSettled(Object.keys(__classPrivateFieldGet(this, _BalancesTracker_balances, "f")).map(async (accountId) => {
|
|
107
|
+
await this.updateBalance(accountId);
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
_BalancesTracker_poller = new WeakMap(), _BalancesTracker_updateBalance = new WeakMap(), _BalancesTracker_balances = new WeakMap(), _BalancesTracker_instances = new WeakSet(), _BalancesTracker_isBalanceOutdated = function _BalancesTracker_isBalanceOutdated({ lastUpdated, blockTime }) {
|
|
112
|
+
return (
|
|
113
|
+
// Never been updated:
|
|
114
|
+
lastUpdated === 0 ||
|
|
115
|
+
// Outdated:
|
|
116
|
+
Date.now() - lastUpdated >= blockTime);
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=BalancesTracker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BalancesTracker.mjs","sourceRoot":"","sources":["../../src/MultichainBalancesController/BalancesTracker.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAOlC,MAAM,0BAA0B,GAAG,IAAI,CAAC,CAAC,4BAA4B;AAErE,MAAM,OAAO,eAAe;IAO1B,YAAY,qBAA2D;;QANvE,0CAAgB;QAEhB,iDAAqD;QAErD,oCAAyC,EAAE,EAAC;QAG1C,uBAAA,IAAI,kCAAkB,qBAAqB,MAAA,CAAC;QAE5C,uBAAA,IAAI,2BAAW,IAAI,MAAM,CACvB,GAAG,EAAE,CAAC,IAAI,CAAC,cAAc,EAAE,EAC3B,0BAA0B,CAC3B,MAAA,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,+BAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,uBAAA,IAAI,+BAAQ,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,SAAiB;QACzB,OAAO,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,uBAAA,IAAI,iCAAU,EAAE,SAAS,CAAC,CAAC;IACzE,CAAC;IAED;;;;;OAKG;IACH,kBAAkB,CAAC,SAAiB;QAClC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,iCAAiC,SAAS,EAAE,CAAC,CAAC;SAC/D;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,SAAiB,EAAE,SAAiB;QACxC,0DAA0D;QAC1D,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;YAC9B,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,GAAG;gBAC1B,WAAW,EAAE,CAAC;gBACd,SAAS;aACV,CAAC;SACH;IACH,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,SAAiB;QACvB,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;QACnC,OAAO,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,aAAa,CAAC,SAAiB;QACnC,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;QAEnC,iFAAiF;QACjF,8BAA8B;QAC9B,EAAE;QACF,mFAAmF;QACnF,8CAA8C;QAC9C,MAAM,IAAI,GAAG,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC;QACvC,IAAI,uBAAA,IAAI,sEAAmB,MAAvB,IAAI,EAAoB,IAAI,CAAC,EAAE;YACjC,MAAM,uBAAA,IAAI,sCAAe,MAAnB,IAAI,EAAgB,SAAS,CAAC,CAAC;YACrC,uBAAA,IAAI,iCAAU,CAAC,SAAS,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;SACpD;IACH,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,cAAc;QAClB,MAAM,OAAO,CAAC,UAAU,CACtB,MAAM,CAAC,IAAI,CAAC,uBAAA,IAAI,iCAAU,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;YAClD,MAAM,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QACtC,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CAkBF;iQARoB,EAAE,WAAW,EAAE,SAAS,EAAe;IACxD,OAAO;IACL,sBAAsB;IACtB,WAAW,KAAK,CAAC;QACjB,YAAY;QACZ,IAAI,CAAC,GAAG,EAAE,GAAG,WAAW,IAAI,SAAS,CACtC,CAAC;AACJ,CAAC","sourcesContent":["import { Poller } from './Poller';\n\ntype BalanceInfo = {\n lastUpdated: number;\n blockTime: number;\n};\n\nconst BALANCES_TRACKING_INTERVAL = 5000; // Every 5s in milliseconds.\n\nexport class BalancesTracker {\n #poller: Poller;\n\n #updateBalance: (accountId: string) => Promise<void>;\n\n #balances: Record<string, BalanceInfo> = {};\n\n constructor(updateBalanceCallback: (accountId: string) => Promise<void>) {\n this.#updateBalance = updateBalanceCallback;\n\n this.#poller = new Poller(\n () => this.updateBalances(),\n BALANCES_TRACKING_INTERVAL,\n );\n }\n\n /**\n * Starts the tracking process.\n */\n start(): void {\n this.#poller.start();\n }\n\n /**\n * Stops the tracking process.\n */\n stop(): void {\n this.#poller.stop();\n }\n\n /**\n * Checks if an account ID is being tracked.\n *\n * @param accountId - The account ID.\n * @returns True if the account is being tracked, false otherwise.\n */\n isTracked(accountId: string) {\n return Object.prototype.hasOwnProperty.call(this.#balances, accountId);\n }\n\n /**\n * Asserts that an account ID is being tracked.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n assertBeingTracked(accountId: string) {\n if (!this.isTracked(accountId)) {\n throw new Error(`Account is not being tracked: ${accountId}`);\n }\n }\n\n /**\n * Starts tracking a new account ID. This method has no effect on already tracked\n * accounts.\n *\n * @param accountId - The account ID.\n * @param blockTime - The block time (used when refreshing the account balances).\n */\n track(accountId: string, blockTime: number) {\n // Do not overwrite current info if already being tracked!\n if (!this.isTracked(accountId)) {\n this.#balances[accountId] = {\n lastUpdated: 0,\n blockTime,\n };\n }\n }\n\n /**\n * Stops tracking a tracked account ID.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n untrack(accountId: string) {\n this.assertBeingTracked(accountId);\n delete this.#balances[accountId];\n }\n\n /**\n * Update the balances for a tracked account ID.\n *\n * @param accountId - The account ID.\n * @throws If the account ID is not being tracked.\n */\n async updateBalance(accountId: string) {\n this.assertBeingTracked(accountId);\n\n // We check if the balance is outdated (by comparing to the block time associated\n // with this kind of account).\n //\n // This might not be super accurate, but we could probably compute this differently\n // and try to sync with the \"real block time\"!\n const info = this.#balances[accountId];\n if (this.#isBalanceOutdated(info)) {\n await this.#updateBalance(accountId);\n this.#balances[accountId].lastUpdated = Date.now();\n }\n }\n\n /**\n * Update the balances of all tracked accounts (only if the balances\n * is considered outdated).\n */\n async updateBalances() {\n await Promise.allSettled(\n Object.keys(this.#balances).map(async (accountId) => {\n await this.updateBalance(accountId);\n }),\n );\n }\n\n /**\n * Checks if the balance is outdated according to the provided data.\n *\n * @param param - The balance info.\n * @param param.lastUpdated - The last updated timestamp.\n * @param param.blockTime - The block time.\n * @returns True if the balance is outdated, false otherwise.\n */\n #isBalanceOutdated({ lastUpdated, blockTime }: BalanceInfo): boolean {\n return (\n // Never been updated:\n lastUpdated === 0 ||\n // Outdated:\n Date.now() - lastUpdated >= blockTime\n );\n }\n}\n"]}
|