@metamask/assets-controller 0.2.0 → 2.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 +39 -1
- package/dist/AssetsController-method-action-types.cjs +7 -0
- package/dist/AssetsController-method-action-types.cjs.map +1 -0
- package/dist/AssetsController-method-action-types.d.cts +78 -0
- package/dist/AssetsController-method-action-types.d.cts.map +1 -0
- package/dist/AssetsController-method-action-types.d.mts +78 -0
- package/dist/AssetsController-method-action-types.d.mts.map +1 -0
- package/dist/AssetsController-method-action-types.mjs +6 -0
- package/dist/AssetsController-method-action-types.mjs.map +1 -0
- package/dist/AssetsController.cjs +346 -271
- package/dist/AssetsController.cjs.map +1 -1
- package/dist/AssetsController.d.cts +92 -96
- package/dist/AssetsController.d.cts.map +1 -1
- package/dist/AssetsController.d.mts +92 -96
- package/dist/AssetsController.d.mts.map +1 -1
- package/dist/AssetsController.mjs +347 -272
- package/dist/AssetsController.mjs.map +1 -1
- package/dist/data-sources/AbstractDataSource.cjs +8 -0
- package/dist/data-sources/AbstractDataSource.cjs.map +1 -1
- package/dist/data-sources/AbstractDataSource.d.cts +16 -1
- package/dist/data-sources/AbstractDataSource.d.cts.map +1 -1
- package/dist/data-sources/AbstractDataSource.d.mts +16 -1
- package/dist/data-sources/AbstractDataSource.d.mts.map +1 -1
- package/dist/data-sources/AbstractDataSource.mjs +8 -0
- package/dist/data-sources/AbstractDataSource.mjs.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.cjs +92 -111
- package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.cts +27 -70
- package/dist/data-sources/AccountsApiDataSource.d.cts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.mts +27 -70
- package/dist/data-sources/AccountsApiDataSource.d.mts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.mjs +88 -107
- package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.cjs +141 -46
- package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.cts +19 -66
- package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.mts +19 -66
- package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.mjs +141 -46
- package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
- package/dist/data-sources/PriceDataSource.cjs +39 -48
- package/dist/data-sources/PriceDataSource.cjs.map +1 -1
- package/dist/data-sources/PriceDataSource.d.cts +13 -92
- package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
- package/dist/data-sources/PriceDataSource.d.mts +13 -92
- package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
- package/dist/data-sources/PriceDataSource.mjs +39 -48
- package/dist/data-sources/PriceDataSource.mjs.map +1 -1
- package/dist/data-sources/RpcDataSource.cjs +184 -142
- package/dist/data-sources/RpcDataSource.cjs.map +1 -1
- package/dist/data-sources/RpcDataSource.d.cts +29 -114
- package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
- package/dist/data-sources/RpcDataSource.d.mts +29 -114
- package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
- package/dist/data-sources/RpcDataSource.mjs +185 -143
- package/dist/data-sources/RpcDataSource.mjs.map +1 -1
- package/dist/data-sources/SnapDataSource.cjs +53 -39
- package/dist/data-sources/SnapDataSource.cjs.map +1 -1
- package/dist/data-sources/SnapDataSource.d.cts +9 -45
- package/dist/data-sources/SnapDataSource.d.cts.map +1 -1
- package/dist/data-sources/SnapDataSource.d.mts +9 -45
- package/dist/data-sources/SnapDataSource.d.mts.map +1 -1
- package/dist/data-sources/SnapDataSource.mjs +53 -39
- package/dist/data-sources/SnapDataSource.mjs.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.cjs +609 -0
- package/dist/data-sources/StakedBalanceDataSource.cjs.map +1 -0
- package/dist/data-sources/StakedBalanceDataSource.d.cts +72 -0
- package/dist/data-sources/StakedBalanceDataSource.d.cts.map +1 -0
- package/dist/data-sources/StakedBalanceDataSource.d.mts +72 -0
- package/dist/data-sources/StakedBalanceDataSource.d.mts.map +1 -0
- package/dist/data-sources/StakedBalanceDataSource.mjs +605 -0
- package/dist/data-sources/StakedBalanceDataSource.mjs.map +1 -0
- package/dist/data-sources/TokenDataSource.cjs +15 -21
- package/dist/data-sources/TokenDataSource.cjs.map +1 -1
- package/dist/data-sources/TokenDataSource.d.cts +3 -25
- package/dist/data-sources/TokenDataSource.d.cts.map +1 -1
- package/dist/data-sources/TokenDataSource.d.mts +3 -25
- package/dist/data-sources/TokenDataSource.d.mts.map +1 -1
- package/dist/data-sources/TokenDataSource.mjs +15 -21
- package/dist/data-sources/TokenDataSource.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.cjs +5 -1
- package/dist/data-sources/evm-rpc-services/index.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.cts +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.mts +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.mjs +1 -1
- package/dist/data-sources/evm-rpc-services/index.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.cjs +132 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.cjs.map +1 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.d.cts +78 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.d.cts.map +1 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.d.mts +78 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.d.mts.map +1 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.mjs +126 -0
- package/dist/data-sources/evm-rpc-services/services/StakedBalanceFetcher.mjs.map +1 -0
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.cjs +16 -0
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.cts +5 -1
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.mts +5 -1
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.mjs +16 -0
- package/dist/data-sources/evm-rpc-services/services/TokenDetector.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/index.cjs +6 -1
- package/dist/data-sources/evm-rpc-services/services/index.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/index.d.cts +1 -0
- package/dist/data-sources/evm-rpc-services/services/index.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/index.d.mts +1 -0
- package/dist/data-sources/evm-rpc-services/services/index.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/index.mjs +1 -0
- package/dist/data-sources/evm-rpc-services/services/index.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.d.cts +4 -0
- package/dist/data-sources/evm-rpc-services/types/services.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.d.mts +4 -0
- package/dist/data-sources/evm-rpc-services/types/services.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/state.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/state.d.cts +3 -3
- package/dist/data-sources/evm-rpc-services/types/state.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/state.d.mts +3 -3
- package/dist/data-sources/evm-rpc-services/types/state.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/state.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/utils/index.cjs +9 -1
- package/dist/data-sources/evm-rpc-services/utils/index.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/utils/index.d.cts +2 -0
- package/dist/data-sources/evm-rpc-services/utils/index.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/utils/index.d.mts +2 -0
- package/dist/data-sources/evm-rpc-services/utils/index.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/utils/index.mjs +2 -0
- package/dist/data-sources/evm-rpc-services/utils/index.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/utils/parsing.cjs +47 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.cjs.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.d.cts +25 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.d.cts.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.d.mts +25 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.d.mts.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.mjs +39 -0
- package/dist/data-sources/evm-rpc-services/utils/parsing.mjs.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.cjs +68 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.cjs.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.d.cts +25 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.d.cts.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.d.mts +25 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.d.mts.map +1 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.mjs +62 -0
- package/dist/data-sources/evm-rpc-services/utils/staking-contracts.mjs.map +1 -0
- package/dist/data-sources/index.cjs +3 -6
- package/dist/data-sources/index.cjs.map +1 -1
- package/dist/data-sources/index.d.cts +7 -7
- package/dist/data-sources/index.d.cts.map +1 -1
- package/dist/data-sources/index.d.mts +7 -7
- package/dist/data-sources/index.d.mts.map +1 -1
- package/dist/data-sources/index.mjs +2 -3
- package/dist/data-sources/index.mjs.map +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -11
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +11 -11
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/middlewares/DetectionMiddleware.cjs +8 -28
- package/dist/middlewares/DetectionMiddleware.cjs.map +1 -1
- package/dist/middlewares/DetectionMiddleware.d.cts +4 -26
- package/dist/middlewares/DetectionMiddleware.d.cts.map +1 -1
- package/dist/middlewares/DetectionMiddleware.d.mts +4 -26
- package/dist/middlewares/DetectionMiddleware.d.mts.map +1 -1
- package/dist/middlewares/DetectionMiddleware.mjs +8 -28
- package/dist/middlewares/DetectionMiddleware.mjs.map +1 -1
- package/dist/middlewares/index.cjs.map +1 -1
- package/dist/middlewares/index.d.cts +1 -1
- package/dist/middlewares/index.d.cts.map +1 -1
- package/dist/middlewares/index.d.mts +1 -1
- package/dist/middlewares/index.d.mts.map +1 -1
- package/dist/middlewares/index.mjs.map +1 -1
- package/dist/selectors/balance.cjs +306 -0
- package/dist/selectors/balance.cjs.map +1 -0
- package/dist/selectors/balance.d.cts +22 -0
- package/dist/selectors/balance.d.cts.map +1 -0
- package/dist/selectors/balance.d.mts +22 -0
- package/dist/selectors/balance.d.mts.map +1 -0
- package/dist/selectors/balance.mjs +297 -0
- package/dist/selectors/balance.mjs.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +56 -21
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +56 -21
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +13 -10
- package/dist/data-sources/initDataSources.cjs +0 -215
- package/dist/data-sources/initDataSources.cjs.map +0 -1
- package/dist/data-sources/initDataSources.d.cts +0 -140
- package/dist/data-sources/initDataSources.d.cts.map +0 -1
- package/dist/data-sources/initDataSources.d.mts +0 -140
- package/dist/data-sources/initDataSources.d.mts.map +0 -1
- package/dist/data-sources/initDataSources.mjs +0 -210
- package/dist/data-sources/initDataSources.mjs.map +0 -1
|
@@ -9,12 +9,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
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
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _RpcDataSource_instances, _RpcDataSource_timeout, _RpcDataSource_tokenDetectionEnabled, _RpcDataSource_activeChains, _RpcDataSource_chainStatuses, _RpcDataSource_providerCache, _RpcDataSource_activeSubscriptions, _RpcDataSource_multicallClient, _RpcDataSource_balanceFetcher, _RpcDataSource_tokenDetector, _RpcDataSource_convertToHumanReadable, _RpcDataSource_collectMetadataForBalances, _RpcDataSource_handleBalanceUpdate, _RpcDataSource_handleDetectionUpdate,
|
|
12
|
+
var _RpcDataSource_instances, _RpcDataSource_messenger, _RpcDataSource_onActiveChainsUpdated, _RpcDataSource_timeout, _RpcDataSource_tokenDetectionEnabled, _RpcDataSource_useExternalService, _RpcDataSource_activeChains, _RpcDataSource_chainStatuses, _RpcDataSource_providerCache, _RpcDataSource_activeSubscriptions, _RpcDataSource_unsubscribeTransactionConfirmed, _RpcDataSource_unsubscribeIncomingTransactions, _RpcDataSource_multicallClient, _RpcDataSource_balanceFetcher, _RpcDataSource_tokenDetector, _RpcDataSource_convertToHumanReadable, _RpcDataSource_collectMetadataForBalances, _RpcDataSource_handleBalanceUpdate, _RpcDataSource_handleDetectionUpdate, _RpcDataSource_subscribeToNetworkController, _RpcDataSource_subscribeToTransactionEvents, _RpcDataSource_onTransactionConfirmed, _RpcDataSource_onIncomingTransactions, _RpcDataSource_refreshBalanceForChains, _RpcDataSource_initializeFromNetworkController, _RpcDataSource_updateFromNetworkState, _RpcDataSource_getProvider, _RpcDataSource_getMulticallProvider, _RpcDataSource_clearProviderCache, _RpcDataSource_buildNativeAssetId, _RpcDataSource_getExistingAssetsMetadata, _RpcDataSource_getTokenMetadataFromTokenList;
|
|
13
13
|
import { Web3Provider } from "@ethersproject/providers";
|
|
14
|
-
import { BaseController } from "@metamask/base-controller";
|
|
15
14
|
import { toHex } from "@metamask/controller-utils";
|
|
16
|
-
import { isStrictHexString, isCaipChainId, parseCaipChainId } from "@metamask/utils";
|
|
15
|
+
import { isStrictHexString, isCaipChainId, numberToHex, parseCaipAssetType, parseCaipChainId } from "@metamask/utils";
|
|
17
16
|
import BigNumberJS from "bignumber.js";
|
|
17
|
+
import { AbstractDataSource } from "./AbstractDataSource.mjs";
|
|
18
18
|
import { BalanceFetcher, MulticallClient, TokenDetector } from "./evm-rpc-services/index.mjs";
|
|
19
19
|
import { projectLogger, createModuleLogger } from "../logger.mjs";
|
|
20
20
|
const CONTROLLER_NAME = 'RpcDataSource';
|
|
@@ -54,17 +54,15 @@ export const caipChainIdToHex = (chainId) => {
|
|
|
54
54
|
* - RpcDataSource:activeChainsUpdated
|
|
55
55
|
* - RpcDataSource:assetsUpdated
|
|
56
56
|
*/
|
|
57
|
-
export class RpcDataSource extends
|
|
57
|
+
export class RpcDataSource extends AbstractDataSource {
|
|
58
58
|
constructor(options) {
|
|
59
|
-
super({
|
|
60
|
-
name: CONTROLLER_NAME,
|
|
61
|
-
metadata: {},
|
|
62
|
-
state: {},
|
|
63
|
-
messenger: options.messenger,
|
|
64
|
-
});
|
|
59
|
+
super(CONTROLLER_NAME, { activeChains: [] });
|
|
65
60
|
_RpcDataSource_instances.add(this);
|
|
61
|
+
_RpcDataSource_messenger.set(this, void 0);
|
|
62
|
+
_RpcDataSource_onActiveChainsUpdated.set(this, void 0);
|
|
66
63
|
_RpcDataSource_timeout.set(this, void 0);
|
|
67
64
|
_RpcDataSource_tokenDetectionEnabled.set(this, void 0);
|
|
65
|
+
_RpcDataSource_useExternalService.set(this, void 0);
|
|
68
66
|
/** Currently active chains */
|
|
69
67
|
_RpcDataSource_activeChains.set(this, []);
|
|
70
68
|
/** Network status for each active chain */
|
|
@@ -73,19 +71,25 @@ export class RpcDataSource extends BaseController {
|
|
|
73
71
|
_RpcDataSource_providerCache.set(this, new Map());
|
|
74
72
|
/** Active subscriptions by ID */
|
|
75
73
|
_RpcDataSource_activeSubscriptions.set(this, new Map());
|
|
74
|
+
_RpcDataSource_unsubscribeTransactionConfirmed.set(this, undefined);
|
|
75
|
+
_RpcDataSource_unsubscribeIncomingTransactions.set(this, undefined);
|
|
76
76
|
// Rpc-datasource components
|
|
77
77
|
_RpcDataSource_multicallClient.set(this, void 0);
|
|
78
78
|
_RpcDataSource_balanceFetcher.set(this, void 0);
|
|
79
79
|
_RpcDataSource_tokenDetector.set(this, void 0);
|
|
80
|
+
__classPrivateFieldSet(this, _RpcDataSource_messenger, options.messenger, "f");
|
|
81
|
+
__classPrivateFieldSet(this, _RpcDataSource_onActiveChainsUpdated, options.onActiveChainsUpdated, "f");
|
|
80
82
|
__classPrivateFieldSet(this, _RpcDataSource_timeout, options.timeout ?? 10000, "f");
|
|
81
|
-
__classPrivateFieldSet(this, _RpcDataSource_tokenDetectionEnabled, options.tokenDetectionEnabled ??
|
|
83
|
+
__classPrivateFieldSet(this, _RpcDataSource_tokenDetectionEnabled, options.tokenDetectionEnabled ?? (() => true), "f");
|
|
84
|
+
__classPrivateFieldSet(this, _RpcDataSource_useExternalService, options.useExternalService ?? (() => true), "f");
|
|
82
85
|
const balanceInterval = options.balanceInterval ?? DEFAULT_BALANCE_INTERVAL;
|
|
83
86
|
const detectionInterval = options.detectionInterval ?? DEFAULT_DETECTION_INTERVAL;
|
|
84
87
|
log('Initializing RpcDataSource', {
|
|
85
88
|
timeout: __classPrivateFieldGet(this, _RpcDataSource_timeout, "f"),
|
|
86
89
|
balanceInterval,
|
|
87
90
|
detectionInterval,
|
|
88
|
-
tokenDetectionEnabled: __classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f"),
|
|
91
|
+
tokenDetectionEnabled: __classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f").call(this),
|
|
92
|
+
useExternalService: __classPrivateFieldGet(this, _RpcDataSource_useExternalService, "f").call(this),
|
|
89
93
|
});
|
|
90
94
|
// Initialize MulticallClient with a provider getter
|
|
91
95
|
__classPrivateFieldSet(this, _RpcDataSource_multicallClient, new MulticallClient((hexChainId) => {
|
|
@@ -94,7 +98,7 @@ export class RpcDataSource extends BaseController {
|
|
|
94
98
|
// Create messenger adapters for BalanceFetcher and TokenDetector
|
|
95
99
|
const balanceFetcherMessenger = {
|
|
96
100
|
call: (_action) => {
|
|
97
|
-
const state = this.
|
|
101
|
+
const state = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('AssetsController:getState');
|
|
98
102
|
return {
|
|
99
103
|
assetsBalance: (state.assetsBalance ?? {}),
|
|
100
104
|
};
|
|
@@ -102,17 +106,21 @@ export class RpcDataSource extends BaseController {
|
|
|
102
106
|
};
|
|
103
107
|
const tokenDetectorMessenger = {
|
|
104
108
|
call: (_action) => {
|
|
105
|
-
return this.
|
|
109
|
+
return __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('TokenListController:getState');
|
|
106
110
|
},
|
|
107
111
|
};
|
|
108
112
|
// Initialize BalanceFetcher with polling interval
|
|
109
113
|
__classPrivateFieldSet(this, _RpcDataSource_balanceFetcher, new BalanceFetcher(__classPrivateFieldGet(this, _RpcDataSource_multicallClient, "f"), balanceFetcherMessenger, { pollingInterval: balanceInterval }), "f");
|
|
110
114
|
__classPrivateFieldGet(this, _RpcDataSource_balanceFetcher, "f").setOnBalanceUpdate(__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_handleBalanceUpdate).bind(this));
|
|
111
115
|
// Initialize TokenDetector with polling interval
|
|
112
|
-
__classPrivateFieldSet(this, _RpcDataSource_tokenDetector, new TokenDetector(__classPrivateFieldGet(this, _RpcDataSource_multicallClient, "f"), tokenDetectorMessenger, {
|
|
116
|
+
__classPrivateFieldSet(this, _RpcDataSource_tokenDetector, new TokenDetector(__classPrivateFieldGet(this, _RpcDataSource_multicallClient, "f"), tokenDetectorMessenger, {
|
|
117
|
+
pollingInterval: detectionInterval,
|
|
118
|
+
tokenDetectionEnabled: __classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f"),
|
|
119
|
+
useExternalService: __classPrivateFieldGet(this, _RpcDataSource_useExternalService, "f"),
|
|
120
|
+
}), "f");
|
|
113
121
|
__classPrivateFieldGet(this, _RpcDataSource_tokenDetector, "f").setOnDetectionUpdate(__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_handleDetectionUpdate).bind(this));
|
|
114
|
-
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_registerActionHandlers).call(this);
|
|
115
122
|
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_subscribeToNetworkController).call(this);
|
|
123
|
+
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_subscribeToTransactionEvents).call(this);
|
|
116
124
|
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_initializeFromNetworkController).call(this);
|
|
117
125
|
}
|
|
118
126
|
/**
|
|
@@ -120,17 +128,6 @@ export class RpcDataSource extends BaseController {
|
|
|
120
128
|
*
|
|
121
129
|
* @returns The name of this data source.
|
|
122
130
|
*/
|
|
123
|
-
getName() {
|
|
124
|
-
return this.name;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Get currently active chains.
|
|
128
|
-
*
|
|
129
|
-
* @returns Promise resolving to array of active chain IDs.
|
|
130
|
-
*/
|
|
131
|
-
async getActiveChains() {
|
|
132
|
-
return __classPrivateFieldGet(this, _RpcDataSource_activeChains, "f");
|
|
133
|
-
}
|
|
134
131
|
/**
|
|
135
132
|
* Get the status of all configured chains.
|
|
136
133
|
*
|
|
@@ -186,7 +183,7 @@ export class RpcDataSource extends BaseController {
|
|
|
186
183
|
const response = {};
|
|
187
184
|
const chainsToFetch = request.chainIds.filter((chainId) => __classPrivateFieldGet(this, _RpcDataSource_activeChains, "f").includes(chainId));
|
|
188
185
|
log('Fetch requested', {
|
|
189
|
-
accounts: request.
|
|
186
|
+
accounts: request.accountsWithSupportedChains.map((a) => a.account.id),
|
|
190
187
|
requestedChains: request.chainIds,
|
|
191
188
|
chainsToFetch,
|
|
192
189
|
});
|
|
@@ -195,16 +192,17 @@ export class RpcDataSource extends BaseController {
|
|
|
195
192
|
return response;
|
|
196
193
|
}
|
|
197
194
|
const assetsBalance = {};
|
|
198
|
-
const
|
|
195
|
+
const assetsInfo = {};
|
|
199
196
|
const failedChains = [];
|
|
200
|
-
// Fetch balances for each
|
|
201
|
-
for (const
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
197
|
+
// Fetch balances for each account and its supported chains (pre-computed in request)
|
|
198
|
+
for (const { account, supportedChains, } of request.accountsWithSupportedChains) {
|
|
199
|
+
const chainsForAccount = chainsToFetch.filter((chain) => supportedChains.includes(chain));
|
|
200
|
+
if (chainsForAccount.length === 0) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const { address, id: accountId } = account;
|
|
204
|
+
for (const chainId of chainsForAccount) {
|
|
205
|
+
const hexChainId = caipChainIdToHex(chainId);
|
|
208
206
|
try {
|
|
209
207
|
// Use BalanceFetcher for batched balance fetching
|
|
210
208
|
const result = await __classPrivateFieldGet(this, _RpcDataSource_balanceFetcher, "f").fetchBalancesForTokens(hexChainId, accountId, address, [], // Empty array means just native token
|
|
@@ -214,10 +212,10 @@ export class RpcDataSource extends BaseController {
|
|
|
214
212
|
}
|
|
215
213
|
// Collect metadata for all balances
|
|
216
214
|
const balanceMetadata = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_collectMetadataForBalances).call(this, result.balances, chainId);
|
|
217
|
-
Object.assign(
|
|
215
|
+
Object.assign(assetsInfo, balanceMetadata);
|
|
218
216
|
// Convert balances to human-readable format
|
|
219
217
|
for (const balance of result.balances) {
|
|
220
|
-
const metadata =
|
|
218
|
+
const metadata = assetsInfo[balance.assetId];
|
|
221
219
|
// Default to 18 decimals (ERC20 standard) for consistent human-readable format
|
|
222
220
|
const decimals = metadata?.decimals ?? 18;
|
|
223
221
|
const humanReadableAmount = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_convertToHumanReadable).call(this, balance.balance, decimals);
|
|
@@ -236,7 +234,7 @@ export class RpcDataSource extends BaseController {
|
|
|
236
234
|
// Even on error, include native token metadata
|
|
237
235
|
const chainStatus = __classPrivateFieldGet(this, _RpcDataSource_chainStatuses, "f")[chainId];
|
|
238
236
|
if (chainStatus) {
|
|
239
|
-
|
|
237
|
+
assetsInfo[nativeAssetId] = {
|
|
240
238
|
type: 'native',
|
|
241
239
|
symbol: chainStatus.nativeCurrency,
|
|
242
240
|
name: chainStatus.nativeCurrency,
|
|
@@ -267,8 +265,8 @@ export class RpcDataSource extends BaseController {
|
|
|
267
265
|
}
|
|
268
266
|
response.assetsBalance = assetsBalance;
|
|
269
267
|
// Include metadata for native tokens if we have any
|
|
270
|
-
if (Object.keys(
|
|
271
|
-
response.
|
|
268
|
+
if (Object.keys(assetsInfo).length > 0) {
|
|
269
|
+
response.assetsInfo = assetsInfo;
|
|
272
270
|
}
|
|
273
271
|
return response;
|
|
274
272
|
}
|
|
@@ -280,14 +278,17 @@ export class RpcDataSource extends BaseController {
|
|
|
280
278
|
* @returns Promise resolving to a DataResponse with detected assets.
|
|
281
279
|
*/
|
|
282
280
|
async detectTokens(chainId, account) {
|
|
283
|
-
if (!__classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f")) {
|
|
281
|
+
if (!__classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f").call(this) || !__classPrivateFieldGet(this, _RpcDataSource_useExternalService, "f").call(this)) {
|
|
284
282
|
return {};
|
|
285
283
|
}
|
|
286
284
|
const hexChainId = caipChainIdToHex(chainId);
|
|
287
285
|
const { address, id: accountId } = account;
|
|
288
286
|
log('Running token detection', { chainId, accountId });
|
|
289
287
|
try {
|
|
290
|
-
const result = await __classPrivateFieldGet(this, _RpcDataSource_tokenDetector, "f").detectTokens(hexChainId, accountId, address
|
|
288
|
+
const result = await __classPrivateFieldGet(this, _RpcDataSource_tokenDetector, "f").detectTokens(hexChainId, accountId, address, {
|
|
289
|
+
tokenDetectionEnabled: __classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f").call(this),
|
|
290
|
+
useExternalService: __classPrivateFieldGet(this, _RpcDataSource_useExternalService, "f").call(this),
|
|
291
|
+
});
|
|
291
292
|
if (result.detectedAssets.length === 0) {
|
|
292
293
|
log('No new tokens detected');
|
|
293
294
|
return {};
|
|
@@ -299,11 +300,11 @@ export class RpcDataSource extends BaseController {
|
|
|
299
300
|
});
|
|
300
301
|
// Convert detected assets to DataResponse format
|
|
301
302
|
const balances = {};
|
|
302
|
-
const
|
|
303
|
+
const assetsInfo = {};
|
|
303
304
|
// Build metadata from detected assets
|
|
304
305
|
for (const asset of result.detectedAssets) {
|
|
305
306
|
if (asset.symbol && asset.decimals !== undefined) {
|
|
306
|
-
|
|
307
|
+
assetsInfo[asset.assetId] = {
|
|
307
308
|
type: 'erc20',
|
|
308
309
|
symbol: asset.symbol,
|
|
309
310
|
name: asset.name ?? asset.symbol,
|
|
@@ -331,8 +332,8 @@ export class RpcDataSource extends BaseController {
|
|
|
331
332
|
},
|
|
332
333
|
};
|
|
333
334
|
// Include metadata if we have any
|
|
334
|
-
if (Object.keys(
|
|
335
|
-
response.
|
|
335
|
+
if (Object.keys(assetsInfo).length > 0) {
|
|
336
|
+
response.assetsInfo = assetsInfo;
|
|
336
337
|
}
|
|
337
338
|
return response;
|
|
338
339
|
}
|
|
@@ -352,7 +353,7 @@ export class RpcDataSource extends BaseController {
|
|
|
352
353
|
let successfullyHandledChains = [];
|
|
353
354
|
log('Middleware fetching', {
|
|
354
355
|
chains: supportedChains,
|
|
355
|
-
accounts: request.
|
|
356
|
+
accounts: request.accountsWithSupportedChains.map((a) => a.account.id),
|
|
356
357
|
});
|
|
357
358
|
const response = await this.fetch({
|
|
358
359
|
...request,
|
|
@@ -368,11 +369,11 @@ export class RpcDataSource extends BaseController {
|
|
|
368
369
|
};
|
|
369
370
|
}
|
|
370
371
|
}
|
|
371
|
-
if (response.
|
|
372
|
-
(_c = context.response).
|
|
373
|
-
context.response.
|
|
374
|
-
...context.response.
|
|
375
|
-
...response.
|
|
372
|
+
if (response.assetsInfo) {
|
|
373
|
+
(_c = context.response).assetsInfo ?? (_c.assetsInfo = {});
|
|
374
|
+
context.response.assetsInfo = {
|
|
375
|
+
...context.response.assetsInfo,
|
|
376
|
+
...response.assetsInfo,
|
|
376
377
|
};
|
|
377
378
|
}
|
|
378
379
|
const failedChains = new Set(Object.keys(response.errors ?? {}));
|
|
@@ -402,7 +403,7 @@ export class RpcDataSource extends BaseController {
|
|
|
402
403
|
log('Subscribe requested', {
|
|
403
404
|
subscriptionId,
|
|
404
405
|
isUpdate,
|
|
405
|
-
accounts: request.
|
|
406
|
+
accounts: request.accountsWithSupportedChains.map((a) => a.account.id),
|
|
406
407
|
chainsToSubscribe,
|
|
407
408
|
});
|
|
408
409
|
if (chainsToSubscribe.length === 0) {
|
|
@@ -423,16 +424,17 @@ export class RpcDataSource extends BaseController {
|
|
|
423
424
|
}
|
|
424
425
|
// Clean up existing subscription (stops old polling)
|
|
425
426
|
await this.unsubscribe(subscriptionId);
|
|
426
|
-
// Start polling through BalanceFetcher and TokenDetector
|
|
427
|
+
// Start polling through BalanceFetcher and TokenDetector (use pre-computed supportedChains per account)
|
|
427
428
|
const balancePollingTokens = [];
|
|
428
429
|
const detectionPollingTokens = [];
|
|
429
|
-
for (const
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
430
|
+
for (const { account, supportedChains, } of request.accountsWithSupportedChains) {
|
|
431
|
+
const chainsForAccount = chainsToSubscribe.filter((chain) => supportedChains.includes(chain));
|
|
432
|
+
if (chainsForAccount.length === 0) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const { address, id: accountId } = account;
|
|
436
|
+
for (const chainId of chainsForAccount) {
|
|
437
|
+
const hexChainId = caipChainIdToHex(chainId);
|
|
436
438
|
// Start balance polling
|
|
437
439
|
const balanceInput = {
|
|
438
440
|
chainId: hexChainId,
|
|
@@ -441,8 +443,8 @@ export class RpcDataSource extends BaseController {
|
|
|
441
443
|
};
|
|
442
444
|
const balanceToken = __classPrivateFieldGet(this, _RpcDataSource_balanceFetcher, "f").startPolling(balanceInput);
|
|
443
445
|
balancePollingTokens.push(balanceToken);
|
|
444
|
-
// Start detection polling if enabled
|
|
445
|
-
if (__classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f")) {
|
|
446
|
+
// Start detection polling if enabled and external services allowed
|
|
447
|
+
if (__classPrivateFieldGet(this, _RpcDataSource_tokenDetectionEnabled, "f").call(this) && __classPrivateFieldGet(this, _RpcDataSource_useExternalService, "f").call(this)) {
|
|
446
448
|
const detectionInput = {
|
|
447
449
|
chainId: hexChainId,
|
|
448
450
|
accountId,
|
|
@@ -454,11 +456,13 @@ export class RpcDataSource extends BaseController {
|
|
|
454
456
|
}
|
|
455
457
|
}
|
|
456
458
|
// Store subscription data
|
|
459
|
+
const accounts = request.accountsWithSupportedChains.map((entry) => entry.account);
|
|
457
460
|
__classPrivateFieldGet(this, _RpcDataSource_activeSubscriptions, "f").set(subscriptionId, {
|
|
458
461
|
balancePollingTokens,
|
|
459
462
|
detectionPollingTokens,
|
|
460
463
|
chains: chainsToSubscribe,
|
|
461
|
-
accounts
|
|
464
|
+
accounts,
|
|
465
|
+
onAssetsUpdate: subscriptionRequest.onAssetsUpdate,
|
|
462
466
|
});
|
|
463
467
|
log('Subscription SUCCESS', {
|
|
464
468
|
subscriptionId,
|
|
@@ -492,6 +496,8 @@ export class RpcDataSource extends BaseController {
|
|
|
492
496
|
*/
|
|
493
497
|
destroy() {
|
|
494
498
|
log('Destroying RpcDataSource');
|
|
499
|
+
__classPrivateFieldGet(this, _RpcDataSource_unsubscribeTransactionConfirmed, "f")?.call(this);
|
|
500
|
+
__classPrivateFieldGet(this, _RpcDataSource_unsubscribeIncomingTransactions, "f")?.call(this);
|
|
495
501
|
// Stop all polling
|
|
496
502
|
__classPrivateFieldGet(this, _RpcDataSource_balanceFetcher, "f").stopAllPolling();
|
|
497
503
|
__classPrivateFieldGet(this, _RpcDataSource_tokenDetector, "f").stopAllPolling();
|
|
@@ -501,19 +507,19 @@ export class RpcDataSource extends BaseController {
|
|
|
501
507
|
__classPrivateFieldGet(this, _RpcDataSource_providerCache, "f").clear();
|
|
502
508
|
}
|
|
503
509
|
}
|
|
504
|
-
_RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = new WeakMap(), _RpcDataSource_activeChains = new WeakMap(), _RpcDataSource_chainStatuses = new WeakMap(), _RpcDataSource_providerCache = new WeakMap(), _RpcDataSource_activeSubscriptions = new WeakMap(), _RpcDataSource_multicallClient = new WeakMap(), _RpcDataSource_balanceFetcher = new WeakMap(), _RpcDataSource_tokenDetector = new WeakMap(), _RpcDataSource_instances = new WeakSet(), _RpcDataSource_convertToHumanReadable = function _RpcDataSource_convertToHumanReadable(rawBalance, decimals) {
|
|
510
|
+
_RpcDataSource_messenger = new WeakMap(), _RpcDataSource_onActiveChainsUpdated = new WeakMap(), _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = new WeakMap(), _RpcDataSource_useExternalService = new WeakMap(), _RpcDataSource_activeChains = new WeakMap(), _RpcDataSource_chainStatuses = new WeakMap(), _RpcDataSource_providerCache = new WeakMap(), _RpcDataSource_activeSubscriptions = new WeakMap(), _RpcDataSource_unsubscribeTransactionConfirmed = new WeakMap(), _RpcDataSource_unsubscribeIncomingTransactions = new WeakMap(), _RpcDataSource_multicallClient = new WeakMap(), _RpcDataSource_balanceFetcher = new WeakMap(), _RpcDataSource_tokenDetector = new WeakMap(), _RpcDataSource_instances = new WeakSet(), _RpcDataSource_convertToHumanReadable = function _RpcDataSource_convertToHumanReadable(rawBalance, decimals) {
|
|
505
511
|
const rawAmount = new BigNumberJS(rawBalance);
|
|
506
512
|
const divisor = new BigNumberJS(10).pow(decimals);
|
|
507
513
|
return rawAmount.dividedBy(divisor).toString();
|
|
508
514
|
}, _RpcDataSource_collectMetadataForBalances = function _RpcDataSource_collectMetadataForBalances(balances, chainId) {
|
|
509
|
-
const
|
|
515
|
+
const assetsInfo = {};
|
|
510
516
|
const existingMetadata = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_getExistingAssetsMetadata).call(this);
|
|
511
517
|
for (const balance of balances) {
|
|
512
518
|
const isNative = balance.assetId.includes('/slip44:');
|
|
513
519
|
if (isNative) {
|
|
514
520
|
const chainStatus = __classPrivateFieldGet(this, _RpcDataSource_chainStatuses, "f")[chainId];
|
|
515
521
|
if (chainStatus) {
|
|
516
|
-
|
|
522
|
+
assetsInfo[balance.assetId] = {
|
|
517
523
|
type: 'native',
|
|
518
524
|
symbol: chainStatus.nativeCurrency,
|
|
519
525
|
name: chainStatus.nativeCurrency,
|
|
@@ -525,19 +531,19 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
525
531
|
// For ERC20 tokens, try existing metadata from state first
|
|
526
532
|
const existingMeta = existingMetadata[balance.assetId];
|
|
527
533
|
if (existingMeta) {
|
|
528
|
-
|
|
534
|
+
assetsInfo[balance.assetId] = existingMeta;
|
|
529
535
|
}
|
|
530
536
|
else {
|
|
531
537
|
// Fallback to token list if not in state
|
|
532
538
|
const tokenListMeta = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_getTokenMetadataFromTokenList).call(this, balance.assetId);
|
|
533
539
|
if (tokenListMeta) {
|
|
534
|
-
|
|
540
|
+
assetsInfo[balance.assetId] = tokenListMeta;
|
|
535
541
|
}
|
|
536
542
|
else {
|
|
537
543
|
// Default metadata for unknown ERC20 tokens.
|
|
538
544
|
// Use 18 decimals (the standard for most ERC20 tokens)
|
|
539
545
|
// to ensure consistent human-readable balance format.
|
|
540
|
-
|
|
546
|
+
assetsInfo[balance.assetId] = {
|
|
541
547
|
type: 'erc20',
|
|
542
548
|
symbol: '',
|
|
543
549
|
name: '',
|
|
@@ -547,17 +553,17 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
547
553
|
}
|
|
548
554
|
}
|
|
549
555
|
}
|
|
550
|
-
return
|
|
556
|
+
return assetsInfo;
|
|
551
557
|
}, _RpcDataSource_handleBalanceUpdate = function _RpcDataSource_handleBalanceUpdate(result) {
|
|
552
558
|
const newBalances = {};
|
|
553
559
|
// Convert hex chain ID to CAIP-2 format
|
|
554
560
|
const chainIdDecimal = parseInt(result.chainId, 16);
|
|
555
561
|
const caipChainId = `eip155:${chainIdDecimal}`;
|
|
556
562
|
// Collect metadata for all balances
|
|
557
|
-
const
|
|
563
|
+
const assetsInfo = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_collectMetadataForBalances).call(this, result.balances, caipChainId);
|
|
558
564
|
// Convert balances to human-readable format using metadata
|
|
559
565
|
for (const balance of result.balances) {
|
|
560
|
-
const metadata =
|
|
566
|
+
const metadata = assetsInfo[balance.assetId];
|
|
561
567
|
// Default to 18 decimals (ERC20 standard) for consistent human-readable format
|
|
562
568
|
const decimals = metadata?.decimals ?? 18;
|
|
563
569
|
const humanReadableAmount = __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_convertToHumanReadable).call(this, balance.balance, decimals);
|
|
@@ -571,17 +577,17 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
571
577
|
assetsBalance: {
|
|
572
578
|
[result.accountId]: newBalances,
|
|
573
579
|
},
|
|
574
|
-
|
|
580
|
+
assetsInfo,
|
|
575
581
|
};
|
|
576
582
|
log('Balance update response', {
|
|
577
583
|
accountId: result.accountId,
|
|
578
584
|
newBalanceCount: Object.keys(newBalances).length,
|
|
579
585
|
});
|
|
580
|
-
this.
|
|
581
|
-
.
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
}
|
|
586
|
+
for (const subscription of __classPrivateFieldGet(this, _RpcDataSource_activeSubscriptions, "f").values()) {
|
|
587
|
+
subscription.onAssetsUpdate(response)?.catch((error) => {
|
|
588
|
+
log('Failed to update assets', { error });
|
|
589
|
+
});
|
|
590
|
+
}
|
|
585
591
|
}, _RpcDataSource_handleDetectionUpdate = function _RpcDataSource_handleDetectionUpdate(result) {
|
|
586
592
|
log('Detected new tokens', {
|
|
587
593
|
count: result.detectedAssets.length,
|
|
@@ -622,37 +628,93 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
622
628
|
detectedAssets: {
|
|
623
629
|
[result.accountId]: result.detectedAssets.map((asset) => asset.assetId),
|
|
624
630
|
},
|
|
625
|
-
|
|
631
|
+
assetsInfo: newMetadata,
|
|
626
632
|
assetsBalance: {
|
|
627
633
|
[result.accountId]: newBalances,
|
|
628
634
|
},
|
|
629
635
|
};
|
|
630
|
-
this.
|
|
631
|
-
.
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
}
|
|
635
|
-
}, _RpcDataSource_registerActionHandlers = function _RpcDataSource_registerActionHandlers() {
|
|
636
|
-
const getAssetsMiddlewareHandler = () => this.assetsMiddleware;
|
|
637
|
-
const getActiveChainsHandler = async () => this.getActiveChains();
|
|
638
|
-
const fetchHandler = async (request) => this.fetch(request);
|
|
639
|
-
const subscribeHandler = async (request) => this.subscribe(request);
|
|
640
|
-
const unsubscribeHandler = async (subscriptionId) => this.unsubscribe(subscriptionId);
|
|
641
|
-
this.messenger.registerActionHandler('RpcDataSource:getAssetsMiddleware', getAssetsMiddlewareHandler);
|
|
642
|
-
this.messenger.registerActionHandler('RpcDataSource:getActiveChains', getActiveChainsHandler);
|
|
643
|
-
this.messenger.registerActionHandler('RpcDataSource:fetch', fetchHandler);
|
|
644
|
-
this.messenger.registerActionHandler('RpcDataSource:subscribe', subscribeHandler);
|
|
645
|
-
this.messenger.registerActionHandler('RpcDataSource:unsubscribe', unsubscribeHandler);
|
|
636
|
+
for (const subscription of __classPrivateFieldGet(this, _RpcDataSource_activeSubscriptions, "f").values()) {
|
|
637
|
+
subscription.onAssetsUpdate(response)?.catch((error) => {
|
|
638
|
+
log('Failed to update detected assets', { error });
|
|
639
|
+
});
|
|
640
|
+
}
|
|
646
641
|
}, _RpcDataSource_subscribeToNetworkController = function _RpcDataSource_subscribeToNetworkController() {
|
|
647
|
-
this.
|
|
642
|
+
__classPrivateFieldGet(this, _RpcDataSource_messenger, "f").subscribe('NetworkController:stateChange', (networkState) => {
|
|
648
643
|
log('NetworkController state changed');
|
|
649
644
|
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_clearProviderCache).call(this);
|
|
650
645
|
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_updateFromNetworkState).call(this, networkState);
|
|
651
646
|
});
|
|
647
|
+
}, _RpcDataSource_subscribeToTransactionEvents = function _RpcDataSource_subscribeToTransactionEvents() {
|
|
648
|
+
const unsubConfirmed = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").subscribe('TransactionController:transactionConfirmed', __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_onTransactionConfirmed).bind(this));
|
|
649
|
+
__classPrivateFieldSet(this, _RpcDataSource_unsubscribeTransactionConfirmed, typeof unsubConfirmed === 'function' ? unsubConfirmed : undefined, "f");
|
|
650
|
+
const unsubIncoming = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").subscribe('TransactionController:incomingTransactionsReceived', __classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_onIncomingTransactions).bind(this));
|
|
651
|
+
__classPrivateFieldSet(this, _RpcDataSource_unsubscribeIncomingTransactions, typeof unsubIncoming === 'function' ? unsubIncoming : undefined, "f");
|
|
652
|
+
}, _RpcDataSource_onTransactionConfirmed = function _RpcDataSource_onTransactionConfirmed(payload) {
|
|
653
|
+
const hexChainId = payload?.chainId;
|
|
654
|
+
if (!hexChainId) {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
const caipChainId = `eip155:${parseInt(hexChainId, 16)}`;
|
|
658
|
+
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_refreshBalanceForChains).call(this, [caipChainId]).catch((error) => {
|
|
659
|
+
log('Failed to refresh balance after transaction confirmed', { error });
|
|
660
|
+
});
|
|
661
|
+
}, _RpcDataSource_onIncomingTransactions = function _RpcDataSource_onIncomingTransactions(payload) {
|
|
662
|
+
const chainIds = Array.from(new Set((payload ?? [])
|
|
663
|
+
.map((item) => item?.chainId)
|
|
664
|
+
.filter((id) => Boolean(id))));
|
|
665
|
+
const caipChainIds = chainIds.map((hexChainId) => `eip155:${parseInt(hexChainId, 16)}`);
|
|
666
|
+
const toRefresh = caipChainIds.length > 0 ? caipChainIds : [...__classPrivateFieldGet(this, _RpcDataSource_activeChains, "f")];
|
|
667
|
+
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_refreshBalanceForChains).call(this, toRefresh).catch((error) => {
|
|
668
|
+
log('Failed to refresh balance after incoming transactions', { error });
|
|
669
|
+
});
|
|
670
|
+
}, _RpcDataSource_refreshBalanceForChains =
|
|
671
|
+
/**
|
|
672
|
+
* Fetch balances for the given chains across all active subscriptions and
|
|
673
|
+
* push updates to the controller.
|
|
674
|
+
*
|
|
675
|
+
* @param chainIds - CAIP-2 chain IDs to refresh.
|
|
676
|
+
*/
|
|
677
|
+
async function _RpcDataSource_refreshBalanceForChains(chainIds) {
|
|
678
|
+
const chainIdsSet = new Set(chainIds);
|
|
679
|
+
const chainsToFetch = chainIds.filter((chainId) => __classPrivateFieldGet(this, _RpcDataSource_activeChains, "f").includes(chainId));
|
|
680
|
+
if (chainsToFetch.length === 0) {
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
for (const subscription of __classPrivateFieldGet(this, _RpcDataSource_activeSubscriptions, "f").values()) {
|
|
684
|
+
const subscriptionChains = subscription.chains.filter((chainId) => chainIdsSet.has(chainId));
|
|
685
|
+
if (subscriptionChains.length === 0) {
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
const request = {
|
|
689
|
+
accountsWithSupportedChains: subscription.accounts.map((account) => ({
|
|
690
|
+
account,
|
|
691
|
+
supportedChains: subscriptionChains,
|
|
692
|
+
})),
|
|
693
|
+
chainIds: subscriptionChains,
|
|
694
|
+
dataTypes: ['balance'],
|
|
695
|
+
};
|
|
696
|
+
try {
|
|
697
|
+
const response = await this.fetch(request);
|
|
698
|
+
if (response.assetsBalance &&
|
|
699
|
+
Object.keys(response.assetsBalance).length > 0) {
|
|
700
|
+
subscription.onAssetsUpdate(response)?.catch((error) => {
|
|
701
|
+
log('Failed to report balance update after transaction', {
|
|
702
|
+
error,
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
catch (error) {
|
|
708
|
+
log('Failed to fetch balance after transaction', {
|
|
709
|
+
chains: subscriptionChains,
|
|
710
|
+
error,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
}
|
|
652
714
|
}, _RpcDataSource_initializeFromNetworkController = function _RpcDataSource_initializeFromNetworkController() {
|
|
653
715
|
log('Initializing from NetworkController');
|
|
654
716
|
try {
|
|
655
|
-
const networkState = this.
|
|
717
|
+
const networkState = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('NetworkController:getState');
|
|
656
718
|
__classPrivateFieldGet(this, _RpcDataSource_instances, "m", _RpcDataSource_updateFromNetworkState).call(this, networkState);
|
|
657
719
|
}
|
|
658
720
|
catch (error) {
|
|
@@ -688,14 +750,18 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
688
750
|
activeChains,
|
|
689
751
|
});
|
|
690
752
|
// Check if chains changed
|
|
691
|
-
const previousChains =
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
|
|
753
|
+
const previousChains = [...__classPrivateFieldGet(this, _RpcDataSource_activeChains, "f")];
|
|
754
|
+
const previousSet = new Set(previousChains);
|
|
755
|
+
const hasChanges = previousChains.length !== activeChains.length ||
|
|
756
|
+
activeChains.some((chain) => !previousSet.has(chain));
|
|
757
|
+
// Update internal state and data source state before notifying, so that
|
|
758
|
+
// when the controller handles the callback and calls getActiveChainsSync(),
|
|
759
|
+
// it receives the updated chains (same order as AbstractDataSource.updateActiveChains).
|
|
695
760
|
__classPrivateFieldSet(this, _RpcDataSource_chainStatuses, chainStatuses, "f");
|
|
696
761
|
__classPrivateFieldSet(this, _RpcDataSource_activeChains, activeChains, "f");
|
|
762
|
+
this.state.activeChains = activeChains;
|
|
697
763
|
if (hasChanges) {
|
|
698
|
-
this.
|
|
764
|
+
__classPrivateFieldGet(this, _RpcDataSource_onActiveChainsUpdated, "f").call(this, this.getName(), activeChains, previousChains);
|
|
699
765
|
}
|
|
700
766
|
}, _RpcDataSource_getProvider = function _RpcDataSource_getProvider(chainId) {
|
|
701
767
|
const cached = __classPrivateFieldGet(this, _RpcDataSource_providerCache, "f").get(chainId);
|
|
@@ -707,7 +773,10 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
707
773
|
return undefined;
|
|
708
774
|
}
|
|
709
775
|
try {
|
|
710
|
-
const networkClient = this.
|
|
776
|
+
const networkClient = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('NetworkController:getNetworkClientById', chainStatus.networkClientId);
|
|
777
|
+
if (!networkClient?.provider) {
|
|
778
|
+
return undefined;
|
|
779
|
+
}
|
|
711
780
|
const web3Provider = new Web3Provider(networkClient.provider);
|
|
712
781
|
__classPrivateFieldGet(this, _RpcDataSource_providerCache, "f").set(chainId, web3Provider);
|
|
713
782
|
return web3Provider;
|
|
@@ -737,39 +806,14 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
737
806
|
};
|
|
738
807
|
}, _RpcDataSource_clearProviderCache = function _RpcDataSource_clearProviderCache() {
|
|
739
808
|
__classPrivateFieldGet(this, _RpcDataSource_providerCache, "f").clear();
|
|
740
|
-
}, _RpcDataSource_accountSupportsChain = function _RpcDataSource_accountSupportsChain(account, chainId) {
|
|
741
|
-
const scopes = account.scopes ?? [];
|
|
742
|
-
if (scopes.length === 0) {
|
|
743
|
-
return true;
|
|
744
|
-
}
|
|
745
|
-
const [chainNamespace, chainReference] = chainId.split(':');
|
|
746
|
-
for (const scope of scopes) {
|
|
747
|
-
const [scopeNamespace, scopeReference] = scope.split(':');
|
|
748
|
-
if (scopeNamespace !== chainNamespace) {
|
|
749
|
-
continue;
|
|
750
|
-
}
|
|
751
|
-
// Wildcard scope (e.g., eip155:0) matches all chains in the namespace
|
|
752
|
-
if (scopeReference === '0') {
|
|
753
|
-
return true;
|
|
754
|
-
}
|
|
755
|
-
// RpcDataSource only handles eip155 (EVM) chains
|
|
756
|
-
// Normalize hex chain references (e.g., 0x1 -> 1) for comparison
|
|
757
|
-
const normalizedScopeRef = scopeReference?.startsWith('0x')
|
|
758
|
-
? parseInt(scopeReference, 16).toString()
|
|
759
|
-
: scopeReference;
|
|
760
|
-
if (normalizedScopeRef === chainReference) {
|
|
761
|
-
return true;
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
return false;
|
|
765
809
|
}, _RpcDataSource_buildNativeAssetId = function _RpcDataSource_buildNativeAssetId(chainId) {
|
|
766
|
-
const { nativeAssetIdentifiers } = this.
|
|
810
|
+
const { nativeAssetIdentifiers } = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('NetworkEnablementController:getState');
|
|
767
811
|
return (nativeAssetIdentifiers[chainId] ??
|
|
768
812
|
`${chainId}/slip44:60`);
|
|
769
813
|
}, _RpcDataSource_getExistingAssetsMetadata = function _RpcDataSource_getExistingAssetsMetadata() {
|
|
770
814
|
try {
|
|
771
|
-
const state = this.
|
|
772
|
-
return
|
|
815
|
+
const state = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('AssetsController:getState');
|
|
816
|
+
return state.assetsInfo ?? {};
|
|
773
817
|
}
|
|
774
818
|
catch {
|
|
775
819
|
// If AssetsController:getState fails, return empty metadata
|
|
@@ -777,17 +821,15 @@ _RpcDataSource_timeout = new WeakMap(), _RpcDataSource_tokenDetectionEnabled = n
|
|
|
777
821
|
}
|
|
778
822
|
}, _RpcDataSource_getTokenMetadataFromTokenList = function _RpcDataSource_getTokenMetadataFromTokenList(assetId) {
|
|
779
823
|
try {
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
const [chainPart, assetPart] = assetId.split('/');
|
|
783
|
-
if (!assetPart?.startsWith('erc20:')) {
|
|
824
|
+
const parsed = parseCaipAssetType(assetId);
|
|
825
|
+
if (parsed.assetNamespace !== 'erc20') {
|
|
784
826
|
return undefined;
|
|
785
827
|
}
|
|
786
|
-
const tokenAddress =
|
|
787
|
-
const
|
|
788
|
-
const hexChainId =
|
|
789
|
-
const tokenListState = this.
|
|
790
|
-
const chainCacheEntry = tokenListState
|
|
828
|
+
const tokenAddress = parsed.assetReference;
|
|
829
|
+
const { reference } = parseCaipChainId(parsed.chainId);
|
|
830
|
+
const hexChainId = numberToHex(parseInt(reference, 10));
|
|
831
|
+
const tokenListState = __classPrivateFieldGet(this, _RpcDataSource_messenger, "f").call('TokenListController:getState');
|
|
832
|
+
const chainCacheEntry = tokenListState?.tokensChainsCache?.[hexChainId];
|
|
791
833
|
const chainTokenList = chainCacheEntry?.data;
|
|
792
834
|
if (!chainTokenList) {
|
|
793
835
|
return undefined;
|