@metamask-previews/assets-controllers 73.0.1-preview-bf50b46b → 73.0.1-preview-e4e5ca5c
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 +1 -13
- package/dist/AccountTrackerController.cjs +5 -167
- package/dist/AccountTrackerController.cjs.map +1 -1
- package/dist/AccountTrackerController.d.cts +2 -14
- package/dist/AccountTrackerController.d.cts.map +1 -1
- package/dist/AccountTrackerController.d.mts +2 -14
- package/dist/AccountTrackerController.d.mts.map +1 -1
- package/dist/AccountTrackerController.mjs +5 -167
- package/dist/AccountTrackerController.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +321 -267
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +93 -51
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +93 -51
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +320 -270
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +1 -13
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +0 -8
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +0 -8
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +1 -12
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/constants.cjs +1 -12
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +0 -1
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +0 -1
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +0 -11
- package/dist/constants.mjs.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs +1 -35
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts +0 -16
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts +0 -16
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs +0 -33
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.cts +0 -8
- package/dist/multi-chain-accounts-service/types.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.mts +0 -8
- package/dist/multi-chain-accounts-service/types.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/types.mjs.map +1 -1
- package/dist/multicall.cjs +22 -397
- package/dist/multicall.cjs.map +1 -1
- package/dist/multicall.d.cts +0 -39
- package/dist/multicall.d.cts.map +1 -1
- package/dist/multicall.d.mts +0 -39
- package/dist/multicall.d.mts.map +1 -1
- package/dist/multicall.mjs +21 -398
- package/dist/multicall.mjs.map +1 -1
- package/dist/selectors/balanceSelectors.cjs +275 -0
- package/dist/selectors/balanceSelectors.cjs.map +1 -0
- package/dist/selectors/balanceSelectors.d.cts +248 -0
- package/dist/selectors/balanceSelectors.d.cts.map +1 -0
- package/dist/selectors/balanceSelectors.d.mts +248 -0
- package/dist/selectors/balanceSelectors.d.mts.map +1 -0
- package/dist/selectors/balanceSelectors.mjs +268 -0
- package/dist/selectors/balanceSelectors.mjs.map +1 -0
- package/package.json +6 -3
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs +0 -98
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts +0 -28
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts +0 -28
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs +0 -98
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.cjs +0 -128
- package/dist/rpc-service/rpc-balance-fetcher.cjs.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.cts +0 -34
- package/dist/rpc-service/rpc-balance-fetcher.d.cts.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.mts +0 -34
- package/dist/rpc-service/rpc-balance-fetcher.d.mts.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.mjs +0 -124
- package/dist/rpc-service/rpc-balance-fetcher.mjs.map +0 -1
- package/dist/selectors.cjs +0 -64
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -51
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -51
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -61
- package/dist/selectors.mjs.map +0 -1
|
@@ -1,310 +1,360 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
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");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
1
6
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
7
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
8
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
9
|
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
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
11
|
};
|
|
7
|
-
var
|
|
8
|
-
|
|
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 _TokenBalancesController_instances, _TokenBalancesController_log, _TokenBalancesController_queryAllAccounts, _TokenBalancesController_balanceFetchers, _TokenBalancesController_allTokens, _TokenBalancesController_detectedTokens, _TokenBalancesController_chainIdsWithTokens_get, _TokenBalancesController_getProvider, _TokenBalancesController_getNetworkClient, _TokenBalancesController_onTokensChanged, _TokenBalancesController_onNetworkChanged, _TokenBalancesController_onAccountRemoved;
|
|
13
|
-
function $importDefault(module) {
|
|
14
|
-
if (module?.__esModule) {
|
|
15
|
-
return module.default;
|
|
16
|
-
}
|
|
17
|
-
return module;
|
|
18
|
-
}
|
|
12
|
+
var _TokenBalancesController_instances, _TokenBalancesController_queryMultipleAccounts, _TokenBalancesController_allTokens, _TokenBalancesController_allDetectedTokens, _TokenBalancesController_calculateQueryMultipleAccounts, _TokenBalancesController_onPreferencesStateChange, _TokenBalancesController_onTokensStateChange, _TokenBalancesController_onNetworkStateChange, _TokenBalancesController_handleOnAccountRemoved, _TokenBalancesController_getChainIds, _TokenBalancesController_handleTokensControllerStateChange, _TokenBalancesController_getProvider, _TokenBalancesController_ensureFreshBlockData, _TokenBalancesController_batchBalanceOf, _TokenBalancesController_getNetworkClient;
|
|
13
|
+
import { Contract } from "@ethersproject/contracts";
|
|
19
14
|
import { Web3Provider } from "@ethersproject/providers";
|
|
20
|
-
import { isValidHexAddress, toHex } from "@metamask/controller-utils";
|
|
15
|
+
import { isValidHexAddress, toChecksumHexAddress, toHex } from "@metamask/controller-utils";
|
|
16
|
+
import { abiERC20 } from "@metamask/metamask-eth-abis";
|
|
21
17
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
22
18
|
import { isStrictHexString } from "@metamask/utils";
|
|
23
|
-
import
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const CONTROLLER = 'TokenBalancesController';
|
|
29
|
-
const DEFAULT_INTERVAL_MS = 180000; // 3 minutes
|
|
19
|
+
import $lodash from "lodash";
|
|
20
|
+
const { isEqual } = $lodash;
|
|
21
|
+
import { multicallOrFallback } from "./multicall.mjs";
|
|
22
|
+
const DEFAULT_INTERVAL = 180000;
|
|
23
|
+
const controllerName = 'TokenBalancesController';
|
|
30
24
|
const metadata = {
|
|
31
25
|
tokenBalances: { persist: true, anonymous: false },
|
|
32
26
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Get the default TokenBalancesController state.
|
|
29
|
+
*
|
|
30
|
+
* @returns The default TokenBalancesController state.
|
|
31
|
+
*/
|
|
32
|
+
export function getDefaultTokenBalancesState() {
|
|
33
|
+
return {
|
|
34
|
+
tokenBalances: {},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Controller that passively polls on a set interval token balances
|
|
39
|
+
* for tokens stored in the TokensController
|
|
40
|
+
*/
|
|
41
41
|
export class TokenBalancesController extends StaticIntervalPollingController() {
|
|
42
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Construct a Token Balances Controller.
|
|
44
|
+
*
|
|
45
|
+
* @param options - The controller options.
|
|
46
|
+
* @param options.interval - Polling interval used to fetch new token balances.
|
|
47
|
+
* @param options.state - Initial state to set on this controller.
|
|
48
|
+
* @param options.messenger - The controller restricted messenger.
|
|
49
|
+
*/
|
|
50
|
+
constructor({ interval = DEFAULT_INTERVAL, messenger, state = {}, }) {
|
|
51
|
+
var _a, _b;
|
|
43
52
|
super({
|
|
44
|
-
name:
|
|
45
|
-
messenger,
|
|
53
|
+
name: controllerName,
|
|
46
54
|
metadata,
|
|
47
|
-
|
|
55
|
+
messenger,
|
|
56
|
+
state: {
|
|
57
|
+
...getDefaultTokenBalancesState(),
|
|
58
|
+
...state,
|
|
59
|
+
},
|
|
48
60
|
});
|
|
49
61
|
_TokenBalancesController_instances.add(this);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const cfg = networkConfigurationsByChainId[chainId];
|
|
65
|
-
const { networkClientId } = cfg.rpcEndpoints[cfg.defaultRpcEndpointIndex];
|
|
66
|
-
return this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
|
62
|
+
_TokenBalancesController_queryMultipleAccounts.set(this, void 0);
|
|
63
|
+
_TokenBalancesController_allTokens.set(this, void 0);
|
|
64
|
+
_TokenBalancesController_allDetectedTokens.set(this, void 0);
|
|
65
|
+
/**
|
|
66
|
+
* Determines whether to query all accounts, or just the selected account.
|
|
67
|
+
* @param preferences - The preferences state.
|
|
68
|
+
* @param preferences.isMultiAccountBalancesEnabled - whether to query all accounts (mobile).
|
|
69
|
+
* @param preferences.useMultiAccountBalanceChecker - whether to query all accounts (extension).
|
|
70
|
+
* @returns true if all accounts should be queried.
|
|
71
|
+
*/
|
|
72
|
+
_TokenBalancesController_calculateQueryMultipleAccounts.set(this, ({ isMultiAccountBalancesEnabled, useMultiAccountBalanceChecker, }) => {
|
|
73
|
+
return Boolean(
|
|
74
|
+
// Note: These settings have different names on extension vs mobile
|
|
75
|
+
isMultiAccountBalancesEnabled || useMultiAccountBalanceChecker);
|
|
67
76
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
// Only process chains that are explicitly mentioned in the incoming state change
|
|
81
|
-
const incomingChainIds = new Set([
|
|
82
|
-
...Object.keys(state.allTokens),
|
|
83
|
-
...Object.keys(state.allDetectedTokens),
|
|
84
|
-
]);
|
|
85
|
-
// Only proceed if there are actual changes to chains that have balances or are being added
|
|
86
|
-
const relevantChainIds = Array.from(incomingChainIds).filter((chainId) => {
|
|
87
|
-
const id = chainId;
|
|
88
|
-
const hasTokensNow = (state.allTokens[id] && Object.keys(state.allTokens[id]).length > 0) ||
|
|
89
|
-
(state.allDetectedTokens[id] &&
|
|
90
|
-
Object.keys(state.allDetectedTokens[id]).length > 0);
|
|
91
|
-
const hadTokensBefore = (__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id] && Object.keys(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id]).length > 0) ||
|
|
92
|
-
(__classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id] &&
|
|
93
|
-
Object.keys(__classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id]).length > 0);
|
|
94
|
-
// Check if there's an actual change in token state
|
|
95
|
-
const hasTokenChange = !isEqual(state.allTokens[id], __classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id]) ||
|
|
96
|
-
!isEqual(state.allDetectedTokens[id], __classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id]);
|
|
97
|
-
// Process chains that have actual changes OR are new chains getting tokens
|
|
98
|
-
return hasTokenChange || (!hadTokensBefore && hasTokensNow);
|
|
99
|
-
});
|
|
100
|
-
if (relevantChainIds.length === 0) {
|
|
101
|
-
// No relevant changes, just update internal state
|
|
102
|
-
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, state.allTokens, "f");
|
|
103
|
-
__classPrivateFieldSet(this, _TokenBalancesController_detectedTokens, state.allDetectedTokens, "f");
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
// Handle both cleanup and updates in a single state update
|
|
107
|
-
this.update((s) => {
|
|
108
|
-
for (const chainId of relevantChainIds) {
|
|
109
|
-
const id = chainId;
|
|
110
|
-
const hasTokensNow = (state.allTokens[id] &&
|
|
111
|
-
Object.keys(state.allTokens[id]).length > 0) ||
|
|
112
|
-
(state.allDetectedTokens[id] &&
|
|
113
|
-
Object.keys(state.allDetectedTokens[id]).length > 0);
|
|
114
|
-
const hadTokensBefore = (__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id] &&
|
|
115
|
-
Object.keys(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id]).length > 0) ||
|
|
116
|
-
(__classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id] &&
|
|
117
|
-
Object.keys(__classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id]).length > 0);
|
|
118
|
-
if (!isEqual(state.allTokens[id], __classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[id]) ||
|
|
119
|
-
!isEqual(state.allDetectedTokens[id], __classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[id])) {
|
|
120
|
-
if (hasTokensNow) {
|
|
121
|
-
// Chain still has tokens - mark for async balance update
|
|
122
|
-
changed.push(id);
|
|
123
|
-
}
|
|
124
|
-
else if (hadTokensBefore) {
|
|
125
|
-
// Chain had tokens before but doesn't now - clean up balances immediately
|
|
126
|
-
for (const address of Object.keys(s.tokenBalances)) {
|
|
127
|
-
const addressKey = address;
|
|
128
|
-
if (s.tokenBalances[addressKey]?.[id]) {
|
|
129
|
-
s.tokenBalances[addressKey][id] = {};
|
|
130
|
-
hasChanges = true;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, state.allTokens, "f");
|
|
138
|
-
__classPrivateFieldSet(this, _TokenBalancesController_detectedTokens, state.allDetectedTokens, "f");
|
|
139
|
-
// Only update balances for chains that still have tokens (and only if we haven't already updated state)
|
|
140
|
-
if (changed.length && !hasChanges) {
|
|
141
|
-
this.updateBalances({ chainIds: changed }).catch((error) => {
|
|
142
|
-
__classPrivateFieldGet(this, _TokenBalancesController_log, "f").call(this, 'Error updating balances after token change:', error);
|
|
143
|
-
});
|
|
77
|
+
/**
|
|
78
|
+
* Handles the event for preferences state changes.
|
|
79
|
+
* @param preferences - The preferences state.
|
|
80
|
+
*/
|
|
81
|
+
_TokenBalancesController_onPreferencesStateChange.set(this, (preferences) => {
|
|
82
|
+
// Update the user preference for whether to query multiple accounts.
|
|
83
|
+
const queryMultipleAccounts = __classPrivateFieldGet(this, _TokenBalancesController_calculateQueryMultipleAccounts, "f").call(this, preferences);
|
|
84
|
+
// Refresh when flipped off -> on
|
|
85
|
+
const refresh = queryMultipleAccounts && !__classPrivateFieldGet(this, _TokenBalancesController_queryMultipleAccounts, "f");
|
|
86
|
+
__classPrivateFieldSet(this, _TokenBalancesController_queryMultipleAccounts, queryMultipleAccounts, "f");
|
|
87
|
+
if (refresh) {
|
|
88
|
+
this.updateBalances().catch(console.error);
|
|
144
89
|
}
|
|
145
90
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
for (const address of Object.keys(s.tokenBalances)) {
|
|
163
|
-
const addressKey = address;
|
|
164
|
-
for (const removedNetwork of removedNetworks) {
|
|
165
|
-
const networkKey = removedNetwork;
|
|
166
|
-
if (s.tokenBalances[addressKey]?.[networkKey]) {
|
|
167
|
-
delete s.tokenBalances[addressKey][networkKey];
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
_TokenBalancesController_onAccountRemoved.set(this, (addr) => {
|
|
175
|
-
if (!isStrictHexString(addr) || !isValidHexAddress(addr)) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
this.update((s) => {
|
|
179
|
-
delete s.tokenBalances[addr];
|
|
180
|
-
});
|
|
91
|
+
/**
|
|
92
|
+
* Handles the event for tokens state changes.
|
|
93
|
+
* @param state - The token state.
|
|
94
|
+
* @param state.allTokens - The state for imported tokens across all chains.
|
|
95
|
+
* @param state.allDetectedTokens - The state for detected tokens across all chains.
|
|
96
|
+
*/
|
|
97
|
+
_TokenBalancesController_onTokensStateChange.set(this, ({ allTokens, allDetectedTokens, }) => {
|
|
98
|
+
// Refresh token balances on chains whose tokens have changed.
|
|
99
|
+
const chainIds = __classPrivateFieldGet(this, _TokenBalancesController_getChainIds, "f").call(this, allTokens, allDetectedTokens);
|
|
100
|
+
const chainIdsToUpdate = chainIds.filter((chainId) => !isEqual(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[chainId], allTokens[chainId]) ||
|
|
101
|
+
!isEqual(__classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")[chainId], allDetectedTokens[chainId]));
|
|
102
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, allTokens, "f");
|
|
103
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allDetectedTokens, allDetectedTokens, "f");
|
|
104
|
+
__classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_handleTokensControllerStateChange).call(this, {
|
|
105
|
+
chainIds: chainIdsToUpdate,
|
|
106
|
+
}).catch(console.error);
|
|
181
107
|
});
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
new
|
|
190
|
-
allTokens
|
|
191
|
-
allDetectedTokens
|
|
192
|
-
|
|
193
|
-
]
|
|
108
|
+
/**
|
|
109
|
+
* Returns an array of chain ids that have tokens.
|
|
110
|
+
* @param allTokens - The state for imported tokens across all chains.
|
|
111
|
+
* @param allDetectedTokens - The state for detected tokens across all chains.
|
|
112
|
+
* @returns An array of chain ids that have tokens.
|
|
113
|
+
*/
|
|
114
|
+
_TokenBalancesController_getChainIds.set(this, (allTokens, allDetectedTokens) => [
|
|
115
|
+
...new Set([
|
|
116
|
+
...Object.keys(allTokens),
|
|
117
|
+
...Object.keys(allDetectedTokens),
|
|
118
|
+
]),
|
|
119
|
+
]);
|
|
194
120
|
this.setIntervalLength(interval);
|
|
195
|
-
// initial
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
121
|
+
// Set initial preference for querying multiple accounts, and subscribe to changes
|
|
122
|
+
__classPrivateFieldSet(this, _TokenBalancesController_queryMultipleAccounts, __classPrivateFieldGet(this, _TokenBalancesController_calculateQueryMultipleAccounts, "f").call(this, this.messagingSystem.call('PreferencesController:getState')), "f");
|
|
123
|
+
this.messagingSystem.subscribe('PreferencesController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_onPreferencesStateChange, "f").bind(this));
|
|
124
|
+
// Set initial tokens, and subscribe to changes
|
|
125
|
+
(_a = this, _b = this, {
|
|
126
|
+
allTokens: ({ set value(_c) { __classPrivateFieldSet(_a, _TokenBalancesController_allTokens, _c, "f"); } }).value,
|
|
127
|
+
allDetectedTokens: ({ set value(_c) { __classPrivateFieldSet(_b, _TokenBalancesController_allDetectedTokens, _c, "f"); } }).value,
|
|
128
|
+
} = this.messagingSystem.call('TokensController:getState'));
|
|
129
|
+
this.messagingSystem.subscribe('TokensController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_onTokensStateChange, "f").bind(this));
|
|
130
|
+
// Subscribe to network state changes
|
|
131
|
+
this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_onNetworkStateChange).bind(this));
|
|
132
|
+
// subscribe to account removed event to cleanup stale balances
|
|
133
|
+
this.messagingSystem.subscribe('KeyringController:accountRemoved', (accountAddress) => __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_handleOnAccountRemoved).call(this, accountAddress));
|
|
202
134
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Polls for erc20 token balances.
|
|
137
|
+
* @param input - The input for the poll.
|
|
138
|
+
* @param input.chainId - The chain id to poll token balances on.
|
|
139
|
+
*/
|
|
140
|
+
async _executePoll({ chainId }) {
|
|
141
|
+
await this.updateBalancesByChainId({ chainId });
|
|
206
142
|
}
|
|
207
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Updates the token balances for the given chain ids.
|
|
145
|
+
* @param input - The input for the update.
|
|
146
|
+
* @param input.chainIds - The chain ids to update token balances for.
|
|
147
|
+
* Or omitted to update all chains that contain tokens.
|
|
148
|
+
*/
|
|
208
149
|
async updateBalances({ chainIds } = {}) {
|
|
209
|
-
|
|
210
|
-
|
|
150
|
+
chainIds ?? (chainIds = __classPrivateFieldGet(this, _TokenBalancesController_getChainIds, "f").call(this, __classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f"), __classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")));
|
|
151
|
+
await Promise.allSettled(chainIds.map((chainId) => this.updateBalancesByChainId({ chainId })));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Returns ERC-20 balances for a single account on a single chain.
|
|
155
|
+
*
|
|
156
|
+
* @param params - The parameters for the balance fetch.
|
|
157
|
+
* @param params.chainId - The chain id to fetch balances on.
|
|
158
|
+
* @param params.accountAddress - The account address to fetch balances for.
|
|
159
|
+
* @param params.tokenAddresses - The token addresses to fetch balances for.
|
|
160
|
+
* @returns A mapping from token address to balance (hex) | null.
|
|
161
|
+
*/
|
|
162
|
+
async getErc20Balances({ chainId, accountAddress, tokenAddresses, }) {
|
|
163
|
+
// Return early if no token addresses provided
|
|
164
|
+
if (tokenAddresses.length === 0) {
|
|
165
|
+
return {};
|
|
166
|
+
}
|
|
167
|
+
const pairs = tokenAddresses.map((tokenAddress) => ({
|
|
168
|
+
accountAddress,
|
|
169
|
+
tokenAddress,
|
|
170
|
+
}));
|
|
171
|
+
const results = await __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_batchBalanceOf).call(this, { chainId, pairs });
|
|
172
|
+
const balances = {};
|
|
173
|
+
tokenAddresses.forEach((tokenAddress, i) => {
|
|
174
|
+
balances[tokenAddress] = results[i]?.success
|
|
175
|
+
? toHex(results[i].value)
|
|
176
|
+
: null;
|
|
177
|
+
});
|
|
178
|
+
return balances;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Updates token balances for the given chain id.
|
|
182
|
+
* @param input - The input for the update.
|
|
183
|
+
* @param input.chainId - The chain id to update token balances on.
|
|
184
|
+
*/
|
|
185
|
+
async updateBalancesByChainId({ chainId }) {
|
|
186
|
+
const { address: selectedAccountAddress } = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
|
187
|
+
const isSelectedAccount = (accountAddress) => toChecksumHexAddress(accountAddress) ===
|
|
188
|
+
toChecksumHexAddress(selectedAccountAddress);
|
|
189
|
+
const accountTokenPairs = [];
|
|
190
|
+
const addTokens = ([accountAddress, tokens]) => __classPrivateFieldGet(this, _TokenBalancesController_queryMultipleAccounts, "f") || isSelectedAccount(accountAddress)
|
|
191
|
+
? tokens.forEach((t) => accountTokenPairs.push({
|
|
192
|
+
accountAddress: accountAddress,
|
|
193
|
+
tokenAddress: t.address,
|
|
194
|
+
}))
|
|
195
|
+
: undefined;
|
|
196
|
+
// Balances will be updated for both imported and detected tokens
|
|
197
|
+
Object.entries(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")[chainId] ?? {}).forEach(addTokens);
|
|
198
|
+
Object.entries(__classPrivateFieldGet(this, _TokenBalancesController_allDetectedTokens, "f")[chainId] ?? {}).forEach(addTokens);
|
|
199
|
+
let results = [];
|
|
200
|
+
const currentTokenBalances = this.messagingSystem.call('TokenBalancesController:getState');
|
|
201
|
+
if (accountTokenPairs.length > 0) {
|
|
202
|
+
results = await __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_batchBalanceOf).call(this, {
|
|
203
|
+
chainId,
|
|
204
|
+
pairs: accountTokenPairs,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
const updatedResults = results.map((res, i) => {
|
|
208
|
+
const { value } = res;
|
|
209
|
+
const { accountAddress, tokenAddress } = accountTokenPairs[i];
|
|
210
|
+
const currentTokenBalanceValueForAccount = currentTokenBalances.tokenBalances?.[accountAddress]?.[chainId]?.[tokenAddress];
|
|
211
|
+
// `value` can be null or undefined if the multicall failed due to RPC issue.
|
|
212
|
+
// Please see packages/assets-controllers/src/multicall.ts#L365.
|
|
213
|
+
// Hence we should not update the balance in that case.
|
|
214
|
+
const isTokenBalanceValueChanged = res.success && value !== undefined && value !== null
|
|
215
|
+
? currentTokenBalanceValueForAccount !== toHex(value)
|
|
216
|
+
: false;
|
|
217
|
+
return {
|
|
218
|
+
...res,
|
|
219
|
+
isTokenBalanceValueChanged,
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
// if all values of isTokenBalanceValueChanged are false, return
|
|
223
|
+
if (updatedResults.every((result) => !result.isTokenBalanceValueChanged)) {
|
|
211
224
|
return;
|
|
212
225
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
if (!supportedChains.length) {
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
const balances = await fetcher.fetch({
|
|
225
|
-
chainIds: supportedChains,
|
|
226
|
-
queryAllAccounts: __classPrivateFieldGet(this, _TokenBalancesController_queryAllAccounts, "f"),
|
|
227
|
-
selectedAccount: selected,
|
|
228
|
-
allAccounts,
|
|
229
|
-
});
|
|
230
|
-
if (balances.length > 0) {
|
|
231
|
-
aggregated.push(...balances);
|
|
232
|
-
// Remove chains that were successfully processed
|
|
233
|
-
const processedChains = new Set(balances.map((b) => b.chainId));
|
|
234
|
-
remainingChains = remainingChains.filter((chain) => !processedChains.has(chain));
|
|
226
|
+
this.update((state) => {
|
|
227
|
+
var _a, _b;
|
|
228
|
+
for (let i = 0; i < updatedResults.length; i++) {
|
|
229
|
+
const { success, value, isTokenBalanceValueChanged } = updatedResults[i];
|
|
230
|
+
const { accountAddress, tokenAddress } = accountTokenPairs[i];
|
|
231
|
+
if (success && isTokenBalanceValueChanged) {
|
|
232
|
+
((_b = ((_a = state.tokenBalances)[accountAddress] ?? (_a[accountAddress] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = toHex(value);
|
|
235
233
|
}
|
|
236
234
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Reset the controller state to the default state.
|
|
239
|
+
*/
|
|
240
|
+
resetState() {
|
|
241
|
+
this.update(() => {
|
|
242
|
+
return getDefaultTokenBalancesState();
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
_TokenBalancesController_queryMultipleAccounts = new WeakMap(), _TokenBalancesController_allTokens = new WeakMap(), _TokenBalancesController_allDetectedTokens = new WeakMap(), _TokenBalancesController_calculateQueryMultipleAccounts = new WeakMap(), _TokenBalancesController_onPreferencesStateChange = new WeakMap(), _TokenBalancesController_onTokensStateChange = new WeakMap(), _TokenBalancesController_getChainIds = new WeakMap(), _TokenBalancesController_instances = new WeakSet(), _TokenBalancesController_onNetworkStateChange = function _TokenBalancesController_onNetworkStateChange(_, patches) {
|
|
247
|
+
// Remove state for deleted networks
|
|
248
|
+
for (const patch of patches) {
|
|
249
|
+
if (patch.op === 'remove' &&
|
|
250
|
+
patch.path[0] === 'networkConfigurationsByChainId') {
|
|
251
|
+
const removedChainId = patch.path[1];
|
|
252
|
+
this.update((state) => {
|
|
253
|
+
for (const accountAddress of Object.keys(state.tokenBalances)) {
|
|
254
|
+
delete state.tokenBalances[accountAddress][removedChainId];
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}, _TokenBalancesController_handleOnAccountRemoved = function _TokenBalancesController_handleOnAccountRemoved(accountAddress) {
|
|
260
|
+
const isEthAddress = isStrictHexString(accountAddress.toLowerCase()) &&
|
|
261
|
+
isValidHexAddress(accountAddress);
|
|
262
|
+
if (!isEthAddress) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
this.update((state) => {
|
|
266
|
+
delete state.tokenBalances[accountAddress];
|
|
267
|
+
});
|
|
268
|
+
}, _TokenBalancesController_handleTokensControllerStateChange = async function _TokenBalancesController_handleTokensControllerStateChange({ chainIds, } = {}) {
|
|
269
|
+
const currentTokenBalancesState = this.messagingSystem.call('TokenBalancesController:getState');
|
|
270
|
+
const currentTokenBalances = currentTokenBalancesState.tokenBalances;
|
|
271
|
+
const currentAllTokens = __classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f");
|
|
272
|
+
const chainIdsSet = new Set(chainIds);
|
|
273
|
+
// first we check if the state change was due to a token being removed
|
|
274
|
+
for (const currentAccount of Object.keys(currentTokenBalances)) {
|
|
275
|
+
const allChains = currentTokenBalances[currentAccount];
|
|
276
|
+
for (const currentChain of Object.keys(allChains)) {
|
|
277
|
+
if (chainIds?.length && !chainIdsSet.has(currentChain)) {
|
|
278
|
+
continue;
|
|
240
279
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
280
|
+
const tokensObject = allChains[currentChain];
|
|
281
|
+
const allCurrentTokens = Object.keys(tokensObject);
|
|
282
|
+
const existingTokensInState = currentAllTokens[currentChain]?.[currentAccount] || [];
|
|
283
|
+
const existingSet = new Set(existingTokensInState.map((elm) => elm.address));
|
|
284
|
+
for (const singleToken of allCurrentTokens) {
|
|
285
|
+
if (!existingSet.has(singleToken)) {
|
|
286
|
+
this.update((state) => {
|
|
287
|
+
delete state.tokenBalances[currentAccount][currentChain][singleToken];
|
|
288
|
+
});
|
|
289
|
+
}
|
|
244
290
|
}
|
|
245
291
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const tokenAddress = token.address;
|
|
262
|
-
((_b = ((_a = d.tokenBalances)[account] ?? (_a[account] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = '0x0';
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
// Initialize tokens from allDetectedTokens
|
|
266
|
-
const detectedChainTokens = __classPrivateFieldGet(this, _TokenBalancesController_detectedTokens, "f")[chainId];
|
|
267
|
-
if (detectedChainTokens?.[account]) {
|
|
268
|
-
Object.values(detectedChainTokens[account]).forEach((token) => {
|
|
269
|
-
var _a, _b;
|
|
270
|
-
const tokenAddress = token.address;
|
|
271
|
-
((_b = ((_a = d.tokenBalances)[account] ?? (_a[account] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = '0x0';
|
|
272
|
-
});
|
|
273
|
-
}
|
|
292
|
+
}
|
|
293
|
+
// then we check if the state change was due to a token being added
|
|
294
|
+
let shouldUpdate = false;
|
|
295
|
+
for (const currentChain of Object.keys(currentAllTokens)) {
|
|
296
|
+
if (chainIds?.length && !chainIdsSet.has(currentChain)) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const accountsPerChain = currentAllTokens[currentChain];
|
|
300
|
+
for (const currentAccount of Object.keys(accountsPerChain)) {
|
|
301
|
+
const tokensList = accountsPerChain[currentAccount];
|
|
302
|
+
const tokenBalancesObject = currentTokenBalances[currentAccount]?.[currentChain] || {};
|
|
303
|
+
for (const singleToken of tokensList) {
|
|
304
|
+
if (!tokenBalancesObject?.[singleToken.address]) {
|
|
305
|
+
shouldUpdate = true;
|
|
306
|
+
break;
|
|
274
307
|
}
|
|
275
308
|
}
|
|
276
|
-
// Then update with actual fetched balances where available
|
|
277
|
-
aggregated.forEach(({ success, value, account, token, chainId }) => {
|
|
278
|
-
var _a, _b;
|
|
279
|
-
if (success && value !== undefined) {
|
|
280
|
-
((_b = ((_a = d.tokenBalances)[account] ?? (_a[account] = {})))[chainId] ?? (_b[chainId] = {}))[token] =
|
|
281
|
-
toHex(value);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
if (!isEqual(prev, next)) {
|
|
286
|
-
this.update(() => next);
|
|
287
|
-
aggregated.forEach((r) => {
|
|
288
|
-
if (r.success && r.token === ZERO_ADDRESS) {
|
|
289
|
-
this.messagingSystem.call('AccountTrackerController:updateNativeToken', r.account, r.chainId, r.value?.toString() ?? '0');
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
__classPrivateFieldGet(this, _TokenBalancesController_log, "f").call(this, 'No balance changes detected');
|
|
295
309
|
}
|
|
296
310
|
}
|
|
297
|
-
|
|
298
|
-
this.
|
|
311
|
+
if (shouldUpdate) {
|
|
312
|
+
await this.updateBalances({ chainIds }).catch(console.error);
|
|
299
313
|
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
314
|
+
}, _TokenBalancesController_getProvider = function _TokenBalancesController_getProvider(chainId) {
|
|
315
|
+
return new Web3Provider(__classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_getNetworkClient).call(this, chainId).provider);
|
|
316
|
+
}, _TokenBalancesController_ensureFreshBlockData =
|
|
317
|
+
/**
|
|
318
|
+
* Ensures that the block tracker has the latest block data before performing multicall operations.
|
|
319
|
+
* This is a temporary fix to ensure that the block number is up to date.
|
|
320
|
+
*
|
|
321
|
+
* @param chainId - The chain id to update block data for.
|
|
322
|
+
*/
|
|
323
|
+
async function _TokenBalancesController_ensureFreshBlockData(chainId) {
|
|
324
|
+
// Force fresh block data before multicall
|
|
325
|
+
// TODO: This is a temporary fix to ensure that the block number is up to date.
|
|
326
|
+
// We should remove this once we have a better solution for this on the block tracker controller.
|
|
327
|
+
const networkClient = __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_getNetworkClient).call(this, chainId);
|
|
328
|
+
await networkClient.blockTracker?.checkForLatestBlock?.();
|
|
329
|
+
}, _TokenBalancesController_batchBalanceOf =
|
|
330
|
+
/**
|
|
331
|
+
* Internal util: run `balanceOf` for an arbitrary set of account/token pairs.
|
|
332
|
+
*
|
|
333
|
+
* @param params - The parameters for the balance fetch.
|
|
334
|
+
* @param params.chainId - The chain id to fetch balances on.
|
|
335
|
+
* @param params.pairs - The account/token pairs to fetch balances for.
|
|
336
|
+
* @returns The balances for the given token addresses.
|
|
337
|
+
*/
|
|
338
|
+
async function _TokenBalancesController_batchBalanceOf({ chainId, pairs, }) {
|
|
339
|
+
if (!pairs.length) {
|
|
340
|
+
return [];
|
|
341
|
+
}
|
|
342
|
+
const provider = __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_getProvider).call(this, chainId);
|
|
343
|
+
const calls = pairs.map(({ accountAddress, tokenAddress }) => ({
|
|
344
|
+
contract: new Contract(tokenAddress, abiERC20, provider),
|
|
345
|
+
functionSignature: 'balanceOf(address)',
|
|
346
|
+
arguments: [accountAddress],
|
|
347
|
+
}));
|
|
348
|
+
await __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_ensureFreshBlockData).call(this, chainId);
|
|
349
|
+
return multicallOrFallback(calls, chainId, provider);
|
|
350
|
+
}, _TokenBalancesController_getNetworkClient = function _TokenBalancesController_getNetworkClient(chainId) {
|
|
351
|
+
const { networkConfigurationsByChainId } = this.messagingSystem.call('NetworkController:getState');
|
|
352
|
+
const networkConfiguration = networkConfigurationsByChainId[chainId];
|
|
353
|
+
if (!networkConfiguration) {
|
|
354
|
+
throw new Error(`TokenBalancesController: No network configuration found for chainId ${chainId}`);
|
|
355
|
+
}
|
|
356
|
+
const { networkClientId } = networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex];
|
|
357
|
+
return this.messagingSystem.call(`NetworkController:getNetworkClientById`, networkClientId);
|
|
308
358
|
};
|
|
309
359
|
export default TokenBalancesController;
|
|
310
360
|
//# sourceMappingURL=TokenBalancesController.mjs.map
|