@metamask-previews/assets-controllers 45.1.1-preview-9cfbf21f → 45.1.1-preview-00ea290f

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.
Files changed (57) hide show
  1. package/dist/MultichainBalancesController/BalancesTracker.cjs +129 -0
  2. package/dist/MultichainBalancesController/BalancesTracker.cjs.map +1 -0
  3. package/dist/MultichainBalancesController/BalancesTracker.d.cts +54 -0
  4. package/dist/MultichainBalancesController/BalancesTracker.d.cts.map +1 -0
  5. package/dist/MultichainBalancesController/BalancesTracker.d.mts +54 -0
  6. package/dist/MultichainBalancesController/BalancesTracker.d.mts.map +1 -0
  7. package/dist/MultichainBalancesController/BalancesTracker.mjs +125 -0
  8. package/dist/MultichainBalancesController/BalancesTracker.mjs.map +1 -0
  9. package/dist/MultichainBalancesController/MultichainBalancesController.cjs +223 -0
  10. package/dist/MultichainBalancesController/MultichainBalancesController.cjs.map +1 -0
  11. package/dist/MultichainBalancesController/MultichainBalancesController.d.cts +89 -0
  12. package/dist/MultichainBalancesController/MultichainBalancesController.d.cts.map +1 -0
  13. package/dist/MultichainBalancesController/MultichainBalancesController.d.mts +89 -0
  14. package/dist/MultichainBalancesController/MultichainBalancesController.d.mts.map +1 -0
  15. package/dist/MultichainBalancesController/MultichainBalancesController.mjs +221 -0
  16. package/dist/MultichainBalancesController/MultichainBalancesController.mjs.map +1 -0
  17. package/dist/MultichainBalancesController/Poller.cjs +42 -0
  18. package/dist/MultichainBalancesController/Poller.cjs.map +1 -0
  19. package/dist/MultichainBalancesController/Poller.d.cts +7 -0
  20. package/dist/MultichainBalancesController/Poller.d.cts.map +1 -0
  21. package/dist/MultichainBalancesController/Poller.d.mts +7 -0
  22. package/dist/MultichainBalancesController/Poller.d.mts.map +1 -0
  23. package/dist/MultichainBalancesController/Poller.mjs +38 -0
  24. package/dist/MultichainBalancesController/Poller.mjs.map +1 -0
  25. package/dist/MultichainBalancesController/constants.cjs +45 -0
  26. package/dist/MultichainBalancesController/constants.cjs.map +1 -0
  27. package/dist/MultichainBalancesController/constants.d.cts +29 -0
  28. package/dist/MultichainBalancesController/constants.d.cts.map +1 -0
  29. package/dist/MultichainBalancesController/constants.d.mts +29 -0
  30. package/dist/MultichainBalancesController/constants.d.mts.map +1 -0
  31. package/dist/MultichainBalancesController/constants.mjs +43 -0
  32. package/dist/MultichainBalancesController/constants.mjs.map +1 -0
  33. package/dist/MultichainBalancesController/index.cjs +13 -0
  34. package/dist/MultichainBalancesController/index.cjs.map +1 -0
  35. package/dist/MultichainBalancesController/index.d.cts +5 -0
  36. package/dist/MultichainBalancesController/index.d.cts.map +1 -0
  37. package/dist/MultichainBalancesController/index.d.mts +5 -0
  38. package/dist/MultichainBalancesController/index.d.mts.map +1 -0
  39. package/dist/MultichainBalancesController/index.mjs +4 -0
  40. package/dist/MultichainBalancesController/index.mjs.map +1 -0
  41. package/dist/MultichainBalancesController/utils.cjs +60 -0
  42. package/dist/MultichainBalancesController/utils.cjs.map +1 -0
  43. package/dist/MultichainBalancesController/utils.d.cts +29 -0
  44. package/dist/MultichainBalancesController/utils.d.cts.map +1 -0
  45. package/dist/MultichainBalancesController/utils.d.mts +29 -0
  46. package/dist/MultichainBalancesController/utils.d.mts.map +1 -0
  47. package/dist/MultichainBalancesController/utils.mjs +55 -0
  48. package/dist/MultichainBalancesController/utils.mjs.map +1 -0
  49. package/dist/index.cjs +9 -1
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.cts +2 -0
  52. package/dist/index.d.cts.map +1 -1
  53. package/dist/index.d.mts +2 -0
  54. package/dist/index.d.mts.map +1 -1
  55. package/dist/index.mjs +3 -0
  56. package/dist/index.mjs.map +1 -1
  57. package/package.json +5 -1
@@ -0,0 +1,129 @@
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_poller, _BalancesTracker_updateBalance, _BalancesTracker_balances;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.BalancesTracker = void 0;
16
+ const Poller_1 = require("./Poller.cjs");
17
+ const BALANCES_TRACKING_INTERVAL = 30 * 1000; // Every 30s in milliseconds.
18
+ class BalancesTracker {
19
+ constructor(updateBalanceCallback) {
20
+ _BalancesTracker_poller.set(this, void 0);
21
+ _BalancesTracker_updateBalance.set(this, void 0);
22
+ _BalancesTracker_balances.set(this, {});
23
+ __classPrivateFieldSet(this, _BalancesTracker_updateBalance, updateBalanceCallback, "f");
24
+ __classPrivateFieldSet(this, _BalancesTracker_poller, new Poller_1.Poller(this.updateBalances, BALANCES_TRACKING_INTERVAL), "f");
25
+ }
26
+ /**
27
+ * Starts the tracking process.
28
+ */
29
+ start() {
30
+ __classPrivateFieldGet(this, _BalancesTracker_poller, "f").start();
31
+ }
32
+ /**
33
+ * Stops the tracking process.
34
+ */
35
+ stop() {
36
+ __classPrivateFieldGet(this, _BalancesTracker_poller, "f").stop();
37
+ }
38
+ /**
39
+ * Checks if an account ID is being tracked.
40
+ *
41
+ * @param accountId - The account ID.
42
+ * @returns True if the account is being tracker, false otherwise.
43
+ */
44
+ isTracked(accountId) {
45
+ console.log('MultichainBalancesController - BalancesTracker isTracked');
46
+ const result = Object.prototype.hasOwnProperty.call(__classPrivateFieldGet(this, _BalancesTracker_balances, "f"), accountId);
47
+ console.log('MultichainBalancesController - BalancesTracker isTracked', {
48
+ result,
49
+ });
50
+ return result;
51
+ }
52
+ /**
53
+ * Asserts that an account ID is being tracked.
54
+ *
55
+ * @param accountId - The account ID.
56
+ * @throws If the account ID is not being tracked.
57
+ */
58
+ assertBeingTracked(accountId) {
59
+ console.log('MultichainBalancesController - BalancesTracker assertBeingTracked', { accountId });
60
+ if (!this.isTracked(accountId)) {
61
+ throw new Error(`Account is not being tracked: ${accountId}`);
62
+ }
63
+ }
64
+ /**
65
+ * Starts tracking a new account ID. This method has no effect on already tracked
66
+ * accounts.
67
+ *
68
+ * @param accountId - The account ID.
69
+ * @param blockTime - The block time (used when refreshing the account balances).
70
+ */
71
+ track(accountId, blockTime) {
72
+ // Do not overwrite current info if already being tracked!
73
+ if (!this.isTracked(accountId)) {
74
+ __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId] = {
75
+ lastUpdated: 0,
76
+ blockTime,
77
+ };
78
+ }
79
+ }
80
+ /**
81
+ * Stops tracking a tracked account ID.
82
+ *
83
+ * @param accountId - The account ID.
84
+ * @throws If the account ID is not being tracked.
85
+ */
86
+ untrack(accountId) {
87
+ this.assertBeingTracked(accountId);
88
+ delete __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
89
+ }
90
+ /**
91
+ * Update the balances for a tracked account ID.
92
+ *
93
+ * @param accountId - The account ID.
94
+ * @throws If the account ID is not being tracked.
95
+ */
96
+ async updateBalance(accountId) {
97
+ console.log('MultichainBalancesController - BalancesTracker updateBalance start');
98
+ this.assertBeingTracked(accountId);
99
+ console.log('MultichainBalancesController - BalancesTracker updateBalance', { accountId });
100
+ // We check if the balance is outdated (by comparing to the block time associated
101
+ // with this kind of account).
102
+ //
103
+ // This might not be super accurate, but we could probably compute this differently
104
+ // and try to sync with the "real block time"!
105
+ const info = __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
106
+ console.log({ info });
107
+ const isOutdated = Date.now() - info.lastUpdated >= info.blockTime;
108
+ console.log({ isOutdated });
109
+ const hasNoBalanceYet = info.lastUpdated === 0;
110
+ console.log({ hasNoBalanceYet });
111
+ if (hasNoBalanceYet || isOutdated) {
112
+ await __classPrivateFieldGet(this, _BalancesTracker_updateBalance, "f").call(this, accountId);
113
+ __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId].lastUpdated = Date.now();
114
+ }
115
+ }
116
+ /**
117
+ * Update the balances of all tracked accounts (only if the balances
118
+ * is considered outdated).
119
+ */
120
+ async updateBalances() {
121
+ console.log('MultichainBalancesController - BalancesTracker updateBalances', { balances: __classPrivateFieldGet(this, _BalancesTracker_balances, "f") });
122
+ await Promise.allSettled(Object.keys(__classPrivateFieldGet(this, _BalancesTracker_balances, "f")).map(async (accountId) => {
123
+ await this.updateBalance(accountId);
124
+ }));
125
+ }
126
+ }
127
+ exports.BalancesTracker = BalancesTracker;
128
+ _BalancesTracker_poller = new WeakMap(), _BalancesTracker_updateBalance = new WeakMap(), _BalancesTracker_balances = new WeakMap();
129
+ //# 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,EAAE,GAAG,IAAI,CAAC,CAAC,6BAA6B;AAE3E,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,CAAC,IAAI,CAAC,cAAc,EAAE,0BAA0B,CAAC,MAAA,CAAC;IAC7E,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,+B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dated: number;\n blockTime: number;\n};\n\nconst BALANCES_TRACKING_INTERVAL = 30 * 1000; // Every 30s 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(this.updateBalances, BALANCES_TRACKING_INTERVAL);\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 tracker, false otherwise.\n */\n isTracked(accountId: string) {\n console.log('MultichainBalancesController - BalancesTracker isTracked');\n const result = Object.prototype.hasOwnProperty.call(\n this.#balances,\n accountId,\n );\n console.log('MultichainBalancesController - BalancesTracker isTracked', {\n result,\n });\n return result;\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 console.log(\n 'MultichainBalancesController - BalancesTracker assertBeingTracked',\n { accountId },\n );\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 console.log(\n 'MultichainBalancesController - BalancesTracker updateBalance start',\n );\n this.assertBeingTracked(accountId);\n console.log(\n 'MultichainBalancesController - BalancesTracker updateBalance',\n { accountId },\n );\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 console.log({ info });\n\n const isOutdated = Date.now() - info.lastUpdated >= info.blockTime;\n console.log({ isOutdated });\n const hasNoBalanceYet = info.lastUpdated === 0;\n console.log({ hasNoBalanceYet });\n if (hasNoBalanceYet || isOutdated) {\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 console.log(\n 'MultichainBalancesController - BalancesTracker updateBalances',\n { balances: this.#balances },\n );\n await Promise.allSettled(\n Object.keys(this.#balances).map(async (accountId) => {\n await this.updateBalance(accountId);\n }),\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 tracker, 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;IAMvE;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,IAAI,IAAI;IAIZ;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM;IAY3B;;;;;OAKG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM;IAUpC;;;;;;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;IA4BrC;;;OAGG;IACG,cAAc;CAWrB"}
@@ -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 tracker, 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;IAMvE;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,IAAI,IAAI,IAAI;IAIZ;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM;IAY3B;;;;;OAKG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM;IAUpC;;;;;;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;IA4BrC;;;OAGG;IACG,cAAc;CAWrB"}
@@ -0,0 +1,125 @@
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_poller, _BalancesTracker_updateBalance, _BalancesTracker_balances;
13
+ import { Poller } from "./Poller.mjs";
14
+ const BALANCES_TRACKING_INTERVAL = 30 * 1000; // Every 30s in milliseconds.
15
+ export class BalancesTracker {
16
+ constructor(updateBalanceCallback) {
17
+ _BalancesTracker_poller.set(this, void 0);
18
+ _BalancesTracker_updateBalance.set(this, void 0);
19
+ _BalancesTracker_balances.set(this, {});
20
+ __classPrivateFieldSet(this, _BalancesTracker_updateBalance, updateBalanceCallback, "f");
21
+ __classPrivateFieldSet(this, _BalancesTracker_poller, new Poller(this.updateBalances, BALANCES_TRACKING_INTERVAL), "f");
22
+ }
23
+ /**
24
+ * Starts the tracking process.
25
+ */
26
+ start() {
27
+ __classPrivateFieldGet(this, _BalancesTracker_poller, "f").start();
28
+ }
29
+ /**
30
+ * Stops the tracking process.
31
+ */
32
+ stop() {
33
+ __classPrivateFieldGet(this, _BalancesTracker_poller, "f").stop();
34
+ }
35
+ /**
36
+ * Checks if an account ID is being tracked.
37
+ *
38
+ * @param accountId - The account ID.
39
+ * @returns True if the account is being tracker, false otherwise.
40
+ */
41
+ isTracked(accountId) {
42
+ console.log('MultichainBalancesController - BalancesTracker isTracked');
43
+ const result = Object.prototype.hasOwnProperty.call(__classPrivateFieldGet(this, _BalancesTracker_balances, "f"), accountId);
44
+ console.log('MultichainBalancesController - BalancesTracker isTracked', {
45
+ result,
46
+ });
47
+ return result;
48
+ }
49
+ /**
50
+ * Asserts that an account ID is being tracked.
51
+ *
52
+ * @param accountId - The account ID.
53
+ * @throws If the account ID is not being tracked.
54
+ */
55
+ assertBeingTracked(accountId) {
56
+ console.log('MultichainBalancesController - BalancesTracker assertBeingTracked', { accountId });
57
+ if (!this.isTracked(accountId)) {
58
+ throw new Error(`Account is not being tracked: ${accountId}`);
59
+ }
60
+ }
61
+ /**
62
+ * Starts tracking a new account ID. This method has no effect on already tracked
63
+ * accounts.
64
+ *
65
+ * @param accountId - The account ID.
66
+ * @param blockTime - The block time (used when refreshing the account balances).
67
+ */
68
+ track(accountId, blockTime) {
69
+ // Do not overwrite current info if already being tracked!
70
+ if (!this.isTracked(accountId)) {
71
+ __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId] = {
72
+ lastUpdated: 0,
73
+ blockTime,
74
+ };
75
+ }
76
+ }
77
+ /**
78
+ * Stops tracking a tracked account ID.
79
+ *
80
+ * @param accountId - The account ID.
81
+ * @throws If the account ID is not being tracked.
82
+ */
83
+ untrack(accountId) {
84
+ this.assertBeingTracked(accountId);
85
+ delete __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
86
+ }
87
+ /**
88
+ * Update the balances for a tracked account ID.
89
+ *
90
+ * @param accountId - The account ID.
91
+ * @throws If the account ID is not being tracked.
92
+ */
93
+ async updateBalance(accountId) {
94
+ console.log('MultichainBalancesController - BalancesTracker updateBalance start');
95
+ this.assertBeingTracked(accountId);
96
+ console.log('MultichainBalancesController - BalancesTracker updateBalance', { accountId });
97
+ // We check if the balance is outdated (by comparing to the block time associated
98
+ // with this kind of account).
99
+ //
100
+ // This might not be super accurate, but we could probably compute this differently
101
+ // and try to sync with the "real block time"!
102
+ const info = __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId];
103
+ console.log({ info });
104
+ const isOutdated = Date.now() - info.lastUpdated >= info.blockTime;
105
+ console.log({ isOutdated });
106
+ const hasNoBalanceYet = info.lastUpdated === 0;
107
+ console.log({ hasNoBalanceYet });
108
+ if (hasNoBalanceYet || isOutdated) {
109
+ await __classPrivateFieldGet(this, _BalancesTracker_updateBalance, "f").call(this, accountId);
110
+ __classPrivateFieldGet(this, _BalancesTracker_balances, "f")[accountId].lastUpdated = Date.now();
111
+ }
112
+ }
113
+ /**
114
+ * Update the balances of all tracked accounts (only if the balances
115
+ * is considered outdated).
116
+ */
117
+ async updateBalances() {
118
+ console.log('MultichainBalancesController - BalancesTracker updateBalances', { balances: __classPrivateFieldGet(this, _BalancesTracker_balances, "f") });
119
+ await Promise.allSettled(Object.keys(__classPrivateFieldGet(this, _BalancesTracker_balances, "f")).map(async (accountId) => {
120
+ await this.updateBalance(accountId);
121
+ }));
122
+ }
123
+ }
124
+ _BalancesTracker_poller = new WeakMap(), _BalancesTracker_updateBalance = new WeakMap(), _BalancesTracker_balances = new WeakMap();
125
+ //# 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,EAAE,GAAG,IAAI,CAAC,CAAC,6BAA6B;AAE3E,MAAM,OAAO,eAAe;IAO1B,YAAY,qBAA2D;QANvE,0CAAgB;QAEhB,iDAAqD;QAErD,oCAAyC,EAAE,EAAC;QAG1C,uBAAA,IAAI,kCAAkB,qBAAqB,MAAA,CAAC;QAE5C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CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QACtC,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { Poller } from './Poller';\n\ntype BalanceInfo = {\n lastUpdated: number;\n blockTime: number;\n};\n\nconst BALANCES_TRACKING_INTERVAL = 30 * 1000; // Every 30s 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(this.updateBalances, BALANCES_TRACKING_INTERVAL);\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 tracker, false otherwise.\n */\n isTracked(accountId: string) {\n console.log('MultichainBalancesController - BalancesTracker isTracked');\n const result = Object.prototype.hasOwnProperty.call(\n this.#balances,\n accountId,\n );\n console.log('MultichainBalancesController - BalancesTracker isTracked', {\n result,\n });\n return result;\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 console.log(\n 'MultichainBalancesController - BalancesTracker assertBeingTracked',\n { accountId },\n );\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 console.log(\n 'MultichainBalancesController - BalancesTracker updateBalance start',\n );\n this.assertBeingTracked(accountId);\n console.log(\n 'MultichainBalancesController - BalancesTracker updateBalance',\n { accountId },\n );\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 console.log({ info });\n\n const isOutdated = Date.now() - info.lastUpdated >= info.blockTime;\n console.log({ isOutdated });\n const hasNoBalanceYet = info.lastUpdated === 0;\n console.log({ hasNoBalanceYet });\n if (hasNoBalanceYet || isOutdated) {\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 console.log(\n 'MultichainBalancesController - BalancesTracker updateBalances',\n { balances: this.#balances },\n );\n await Promise.allSettled(\n Object.keys(this.#balances).map(async (accountId) => {\n await this.updateBalance(accountId);\n }),\n );\n }\n}\n"]}
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ 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");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
+ if (kind === "m") throw new TypeError("Private method is not writable");
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
+ 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");
11
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
+ };
13
+ var _MultichainBalancesController_instances, _MultichainBalancesController_tracker, _MultichainBalancesController_networkAssetsMap, _MultichainBalancesController_trackAccount, _MultichainBalancesController_listMultichainAccounts, _MultichainBalancesController_listAccounts, _MultichainBalancesController_getAccount, _MultichainBalancesController_updateBalance, _MultichainBalancesController_isNonEvmAccount, _MultichainBalancesController_handleOnAccountAdded, _MultichainBalancesController_handleOnAccountRemoved, _MultichainBalancesController_getBalances, _MultichainBalancesController_getClient;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MultichainBalancesController = exports.defaultState = void 0;
16
+ const base_controller_1 = require("@metamask/base-controller");
17
+ const keyring_api_1 = require("@metamask/keyring-api");
18
+ const snaps_utils_1 = require("@metamask/snaps-utils");
19
+ const _1 = require("./index.cjs");
20
+ const utils_1 = require("./utils.cjs");
21
+ const controllerName = 'MultichainBalancesController';
22
+ /**
23
+ * Default state of the {@link MultichainBalancesController}.
24
+ */
25
+ exports.defaultState = { balances: {} };
26
+ /**
27
+ * {@link multichainBalancesController}'s metadata.
28
+ *
29
+ * This allows us to choose if fields of the state should be persisted or not
30
+ * using the `persist` flag; and if they can be sent to Sentry or not, using
31
+ * the `anonymous` flag.
32
+ */
33
+ const multichainBalancesControllerMetadata = {
34
+ balances: {
35
+ persist: true,
36
+ anonymous: false,
37
+ },
38
+ };
39
+ /**
40
+ * The MultichainBalancesController is responsible for fetching and caching account
41
+ * balances.
42
+ */
43
+ class MultichainBalancesController extends base_controller_1.BaseController {
44
+ constructor({ messenger, state, }) {
45
+ super({
46
+ messenger,
47
+ name: controllerName,
48
+ metadata: multichainBalancesControllerMetadata,
49
+ state: {
50
+ ...exports.defaultState,
51
+ ...state,
52
+ },
53
+ });
54
+ _MultichainBalancesController_instances.add(this);
55
+ _MultichainBalancesController_tracker.set(this, void 0);
56
+ // As a temporary solution, we are using a map to store the assets
57
+ // that are hardcoded in the module. In the future, this mapping
58
+ // should be dynamic to allow a client to register and unregister assets
59
+ _MultichainBalancesController_networkAssetsMap.set(this, _1.NETWORK_ASSETS_MAP);
60
+ __classPrivateFieldSet(this, _MultichainBalancesController_tracker, new _1.BalancesTracker(async (accountId) => await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_updateBalance).call(this, accountId)), "f");
61
+ // Register all non-EVM accounts into the tracker
62
+ for (const account of __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_listAccounts).call(this)) {
63
+ __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_trackAccount).call(this, account);
64
+ }
65
+ this.messagingSystem.subscribe('AccountsController:accountAdded', (account) => __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_handleOnAccountAdded).call(this, account));
66
+ this.messagingSystem.subscribe('AccountsController:accountRemoved', (account) => __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_handleOnAccountRemoved).call(this, account));
67
+ }
68
+ /**
69
+ * Starts the polling process.
70
+ */
71
+ start() {
72
+ __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").start();
73
+ }
74
+ /**
75
+ * Stops the polling process.
76
+ */
77
+ stop() {
78
+ __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").stop();
79
+ }
80
+ /**
81
+ * Updates the balances of one account. This method doesn't return
82
+ * anything, but it updates the state of the controller.
83
+ *
84
+ * @param accountId - The account ID.
85
+ */
86
+ async updateBalance(accountId) {
87
+ console.log('MultichainBalancesController updateBalance', { accountId });
88
+ // NOTE: No need to track the account here, since we start tracking those when
89
+ // the "AccountsController:accountAdded" is fired.
90
+ await __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").updateBalance(accountId);
91
+ }
92
+ /**
93
+ * Updates the balances of all supported accounts. This method doesn't return
94
+ * anything, but it updates the state of the controller.
95
+ */
96
+ async updateBalances() {
97
+ console.log('MultichainBalancesController updateBalances');
98
+ await __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").updateBalances();
99
+ }
100
+ }
101
+ exports.MultichainBalancesController = MultichainBalancesController;
102
+ _MultichainBalancesController_tracker = new WeakMap(), _MultichainBalancesController_networkAssetsMap = new WeakMap(), _MultichainBalancesController_instances = new WeakSet(), _MultichainBalancesController_trackAccount = function _MultichainBalancesController_trackAccount(account) {
103
+ if (!__classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_isNonEvmAccount).call(this, account)) {
104
+ // Nothing to do here for EVM accounts
105
+ console.log('MultichainBalancesController - early return in #trackAccount', account);
106
+ return;
107
+ }
108
+ const updateTime =
109
+ // @ts-expect-error - For the moment we are only tracking non-EVM accounts and this is
110
+ // checked with the method `#isNonEvmAccount`. We can ignore this error since
111
+ // eip155:eoa and eip155:erc4337 account type balances are not tracked here.
112
+ _1.BALANCE_UPDATE_INTERVALS[__classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getAccount).call(this, account.id).type];
113
+ __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").track(account.id, updateTime);
114
+ }, _MultichainBalancesController_listMultichainAccounts = function _MultichainBalancesController_listMultichainAccounts() {
115
+ return this.messagingSystem.call('AccountsController:listMultichainAccounts');
116
+ }, _MultichainBalancesController_listAccounts = function _MultichainBalancesController_listAccounts() {
117
+ const accounts = __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_listMultichainAccounts).call(this);
118
+ return accounts.filter((account) => __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_isNonEvmAccount).call(this, account));
119
+ }, _MultichainBalancesController_getAccount = function _MultichainBalancesController_getAccount(accountId) {
120
+ const account = __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_listMultichainAccounts).call(this).find((multichainAccount) => multichainAccount.id === accountId);
121
+ if (!account) {
122
+ throw new Error(`Unknown account: ${accountId}`);
123
+ }
124
+ if (!__classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_isNonEvmAccount).call(this, account)) {
125
+ throw new Error(`Account is not a non-EVM account: ${accountId}`);
126
+ }
127
+ return account;
128
+ }, _MultichainBalancesController_updateBalance =
129
+ /**
130
+ * Updates the balances of one account. This method doesn't return
131
+ * anything, but it updates the state of the controller.
132
+ *
133
+ * @param accountId - The account ID.
134
+ */
135
+ async function _MultichainBalancesController_updateBalance(accountId) {
136
+ const account = __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getAccount).call(this, accountId);
137
+ console.log('MultichainBalancesController updateBalance', { account });
138
+ const partialState = { balances: {} };
139
+ console.log('MultichainBalancesController updateBalance', { partialState });
140
+ const scope = (0, utils_1.getScopeForAddress)(account);
141
+ console.log('MultichainBalancesController updateBalance', { scope });
142
+ const assetsList = __classPrivateFieldGet(this, _MultichainBalancesController_networkAssetsMap, "f")[scope];
143
+ console.log('MultichainBalancesController updateBalance', { assetsList });
144
+ if (account.metadata.snap) {
145
+ partialState.balances[account.id] = await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getBalances).call(this, account.id, account.metadata.snap.id, assetsList);
146
+ }
147
+ console.log('MultichainBalancesController update state', { partialState });
148
+ this.update((state) => {
149
+ state.balances = {
150
+ ...state.balances,
151
+ ...partialState.balances,
152
+ };
153
+ });
154
+ }, _MultichainBalancesController_isNonEvmAccount = function _MultichainBalancesController_isNonEvmAccount(account) {
155
+ return (!(0, keyring_api_1.isEvmAccountType)(account.type) &&
156
+ // Non-EVM accounts are backed by a Snap for now
157
+ account.metadata.snap !== undefined);
158
+ }, _MultichainBalancesController_handleOnAccountAdded =
159
+ /**
160
+ * Handles changes when a new account has been added.
161
+ *
162
+ * @param account - The new account being added.
163
+ */
164
+ async function _MultichainBalancesController_handleOnAccountAdded(account) {
165
+ console.log('MultichainBalancesController handleOnAccountAdded', {
166
+ account,
167
+ });
168
+ __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_trackAccount).call(this, account);
169
+ // NOTE: Unfortunately, we cannot update the balance right away here, because
170
+ // messenger's events are running synchronously and fetching the balance is
171
+ // asynchronous.
172
+ // Updating the balance here would resume at some point but the event emitter
173
+ // will not `await` this (so we have no real control "when" the balance will
174
+ // really be updated), see:
175
+ // - https://github.com/MetaMask/core/blob/v213.0.0/packages/accounts-controller/src/AccountsController.ts#L1036-L1039
176
+ }, _MultichainBalancesController_handleOnAccountRemoved =
177
+ /**
178
+ * Handles changes when a new account has been removed.
179
+ *
180
+ * @param accountId - The account ID being removed.
181
+ */
182
+ async function _MultichainBalancesController_handleOnAccountRemoved(accountId) {
183
+ console.log('MultichainBalancesController handleOnAccountAdded', {
184
+ accountId,
185
+ });
186
+ if (__classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").isTracked(accountId)) {
187
+ __classPrivateFieldGet(this, _MultichainBalancesController_tracker, "f").untrack(accountId);
188
+ }
189
+ if (accountId in this.state.balances) {
190
+ this.update((state) => {
191
+ delete state.balances[accountId];
192
+ return state;
193
+ });
194
+ }
195
+ }, _MultichainBalancesController_getBalances =
196
+ /**
197
+ * Get the balances for an account.
198
+ *
199
+ * @param accountId - ID of the account to get balances for.
200
+ * @param snapId - ID of the Snap which manages the account.
201
+ * @param assetTypes - Array of asset types to get balances for.
202
+ * @returns A map of asset types to balances.
203
+ */
204
+ async function _MultichainBalancesController_getBalances(accountId, snapId, assetTypes) {
205
+ console.log('MultichainBalancesController - getBalances', {
206
+ accountId,
207
+ snapId,
208
+ assetTypes,
209
+ });
210
+ const keyringClient = __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getClient).call(this, snapId);
211
+ console.log('MultichainBalancesController', { keyringClient });
212
+ return await keyringClient.getAccountBalances(accountId, assetTypes);
213
+ }, _MultichainBalancesController_getClient = function _MultichainBalancesController_getClient(snapId) {
214
+ return new keyring_api_1.KeyringClient({
215
+ send: async (request) => (await this.messagingSystem.call('SnapController:handleRequest', {
216
+ snapId: snapId,
217
+ origin: 'metamask',
218
+ handler: snaps_utils_1.HandlerType.OnKeyringRequest,
219
+ request,
220
+ })),
221
+ });
222
+ };
223
+ //# sourceMappingURL=MultichainBalancesController.cjs.map