@metamask-previews/assets-controllers 89.0.0-preview-d356e047 → 89.0.0-preview-168b280
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 +2 -6
- package/dist/CurrencyRateController.cjs +4 -3
- package/dist/CurrencyRateController.cjs.map +1 -1
- package/dist/CurrencyRateController.d.cts.map +1 -1
- package/dist/CurrencyRateController.d.mts.map +1 -1
- package/dist/CurrencyRateController.mjs +4 -3
- package/dist/CurrencyRateController.mjs.map +1 -1
- package/dist/TokenRatesController.cjs +278 -82
- package/dist/TokenRatesController.cjs.map +1 -1
- package/dist/TokenRatesController.d.cts +35 -9
- package/dist/TokenRatesController.d.cts.map +1 -1
- package/dist/TokenRatesController.d.mts +35 -9
- package/dist/TokenRatesController.d.mts.map +1 -1
- package/dist/TokenRatesController.mjs +279 -83
- package/dist/TokenRatesController.mjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.cjs +3 -2
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.cjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.cts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.mts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.mjs +3 -2
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.mjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/types.cjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/types.d.cts +2 -2
- package/dist/TokenSearchDiscoveryDataController/types.d.cts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/types.d.mts +2 -2
- package/dist/TokenSearchDiscoveryDataController/types.d.mts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/types.mjs.map +1 -1
- package/dist/assetsUtil.cjs +5 -11
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +2 -2
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +2 -2
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +5 -11
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.cjs.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.d.cts +43 -17
- package/dist/token-prices-service/abstract-token-prices-service.d.cts.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.d.mts +43 -17
- package/dist/token-prices-service/abstract-token-prices-service.d.mts.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.mjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.cjs +98 -121
- package/dist/token-prices-service/codefi-v2.cjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.cts +10 -52
- package/dist/token-prices-service/codefi-v2.d.cts.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.mts +10 -52
- package/dist/token-prices-service/codefi-v2.d.mts.map +1 -1
- package/dist/token-prices-service/codefi-v2.mjs +98 -121
- package/dist/token-prices-service/codefi-v2.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { AccountsControllerGetAccountAction, AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedEvmAccountChangeEvent } from "@metamask/accounts-controller";
|
|
1
2
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
3
|
import type { Messenger } from "@metamask/messenger";
|
|
3
|
-
import type { NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
4
|
-
import type
|
|
4
|
+
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
5
|
+
import { type Hex } from "@metamask/utils";
|
|
5
6
|
import type { AbstractTokenPricesService } from "./token-prices-service/abstract-token-prices-service.cjs";
|
|
6
7
|
import type { TokensControllerGetStateAction, TokensControllerStateChangeEvent } from "./TokensController.cjs";
|
|
7
8
|
/**
|
|
@@ -57,18 +58,14 @@ export type MarketDataDetails = {
|
|
|
57
58
|
* Represents a mapping of token contract addresses to their market data.
|
|
58
59
|
*/
|
|
59
60
|
export type ContractMarketData = Record<Hex, MarketDataDetails>;
|
|
60
|
-
type ChainIdAndNativeCurrency = {
|
|
61
|
-
chainId: Hex;
|
|
62
|
-
nativeCurrency: string;
|
|
63
|
-
};
|
|
64
61
|
/**
|
|
65
62
|
* The external actions available to the {@link TokenRatesController}.
|
|
66
63
|
*/
|
|
67
|
-
export type AllowedActions = TokensControllerGetStateAction | NetworkControllerGetStateAction;
|
|
64
|
+
export type AllowedActions = TokensControllerGetStateAction | NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetStateAction | AccountsControllerGetAccountAction | AccountsControllerGetSelectedAccountAction;
|
|
68
65
|
/**
|
|
69
66
|
* The external events available to the {@link TokenRatesController}.
|
|
70
67
|
*/
|
|
71
|
-
export type AllowedEvents = TokensControllerStateChangeEvent | NetworkControllerStateChangeEvent;
|
|
68
|
+
export type AllowedEvents = TokensControllerStateChangeEvent | NetworkControllerStateChangeEvent | AccountsControllerSelectedEvmAccountChangeEvent;
|
|
72
69
|
/**
|
|
73
70
|
* The name of the {@link TokenRatesController}.
|
|
74
71
|
*/
|
|
@@ -159,12 +156,41 @@ export declare class TokenRatesController extends TokenRatesController_base<type
|
|
|
159
156
|
* Blocks controller from making network calls
|
|
160
157
|
*/
|
|
161
158
|
disable(): void;
|
|
159
|
+
/**
|
|
160
|
+
* Start (or restart) polling.
|
|
161
|
+
*
|
|
162
|
+
* @param chainId - The chain ID.
|
|
163
|
+
* @param nativeCurrency - The native currency.
|
|
164
|
+
*/
|
|
165
|
+
start(chainId: Hex, nativeCurrency: string): Promise<void>;
|
|
166
|
+
/**
|
|
167
|
+
* Stop polling.
|
|
168
|
+
*/
|
|
169
|
+
stop(): void;
|
|
170
|
+
/**
|
|
171
|
+
* Updates exchange rates for all tokens.
|
|
172
|
+
*
|
|
173
|
+
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
174
|
+
*/
|
|
175
|
+
updateExchangeRates(chainIdAndNativeCurrency: {
|
|
176
|
+
chainId: Hex;
|
|
177
|
+
nativeCurrency: string;
|
|
178
|
+
}[]): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Updates exchange rates for all tokens.
|
|
181
|
+
*
|
|
182
|
+
* @param chainIds - The chain IDs.
|
|
183
|
+
* @returns A promise that resolves when all chain updates complete.
|
|
184
|
+
*/
|
|
162
185
|
/**
|
|
163
186
|
* Updates exchange rates for all tokens.
|
|
164
187
|
*
|
|
165
188
|
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
166
189
|
*/
|
|
167
|
-
|
|
190
|
+
updateExchangeRatesByChainId(chainIdAndNativeCurrency: {
|
|
191
|
+
chainId: Hex;
|
|
192
|
+
nativeCurrency: string;
|
|
193
|
+
}[]): Promise<void>;
|
|
168
194
|
/**
|
|
169
195
|
* Updates token rates for the given networkClientId
|
|
170
196
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenRatesController.d.cts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;
|
|
1
|
+
{"version":3,"file":"TokenRatesController.d.cts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kCAAkC,EAClC,0CAA0C,EAC1C,+CAA+C,EAChD,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAC/B,iCAAiC,EAClC,qCAAqC;AAEtC,OAAO,EAAy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OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;;IAiBC;;;;;;;;;OASG;gBACS,EACV,QAA2B,EAC3B,QAAgB,EAChB,kBAAkB,EAClB,SAAS,EACT,KAAK,GACN,EAAE;QACD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,kBAAkB,EAAE,0BAA0B,CAAC;QAC/C,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IAwID;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,OAAO,IAAI,IAAI;IAIf;;;;;OAKG;IACG,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM;IAMhD;;OAEG;IACH,IAAI;IAgDJ;;;;OAIG;IACG,mBAAmB,CACvB,wBAAwB,EAAE;QACxB,OAAO,EAAE,GAAG,CAAC;QACb,cAAc,EAAE,MAAM,CAAC;KACxB,EAAE;IAKL;;;;;OAKG;IACH;;;;OAIG;IACG,4BAA4B,CAChC,wBAAwB,EAAE;QACxB,OAAO,EAAE,GAAG,CAAC;QACb,cAAc,EAAE,MAAM,CAAC;KACxB,EAAE,GACF,OAAO,CAAC,IAAI,CAAC;IAiIhB;;;;;OAKG;IACG,YAAY,CAAC,EAAE,QAAQ,EAAE,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAqLvE;;OAEG;IACH,UAAU;CAKX;AAED,eAAe,oBAAoB,CAAC"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { AccountsControllerGetAccountAction, AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedEvmAccountChangeEvent } from "@metamask/accounts-controller";
|
|
1
2
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
3
|
import type { Messenger } from "@metamask/messenger";
|
|
3
|
-
import type { NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
4
|
-
import type
|
|
4
|
+
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
5
|
+
import { type Hex } from "@metamask/utils";
|
|
5
6
|
import type { AbstractTokenPricesService } from "./token-prices-service/abstract-token-prices-service.mjs";
|
|
6
7
|
import type { TokensControllerGetStateAction, TokensControllerStateChangeEvent } from "./TokensController.mjs";
|
|
7
8
|
/**
|
|
@@ -57,18 +58,14 @@ export type MarketDataDetails = {
|
|
|
57
58
|
* Represents a mapping of token contract addresses to their market data.
|
|
58
59
|
*/
|
|
59
60
|
export type ContractMarketData = Record<Hex, MarketDataDetails>;
|
|
60
|
-
type ChainIdAndNativeCurrency = {
|
|
61
|
-
chainId: Hex;
|
|
62
|
-
nativeCurrency: string;
|
|
63
|
-
};
|
|
64
61
|
/**
|
|
65
62
|
* The external actions available to the {@link TokenRatesController}.
|
|
66
63
|
*/
|
|
67
|
-
export type AllowedActions = TokensControllerGetStateAction | NetworkControllerGetStateAction;
|
|
64
|
+
export type AllowedActions = TokensControllerGetStateAction | NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetStateAction | AccountsControllerGetAccountAction | AccountsControllerGetSelectedAccountAction;
|
|
68
65
|
/**
|
|
69
66
|
* The external events available to the {@link TokenRatesController}.
|
|
70
67
|
*/
|
|
71
|
-
export type AllowedEvents = TokensControllerStateChangeEvent | NetworkControllerStateChangeEvent;
|
|
68
|
+
export type AllowedEvents = TokensControllerStateChangeEvent | NetworkControllerStateChangeEvent | AccountsControllerSelectedEvmAccountChangeEvent;
|
|
72
69
|
/**
|
|
73
70
|
* The name of the {@link TokenRatesController}.
|
|
74
71
|
*/
|
|
@@ -159,12 +156,41 @@ export declare class TokenRatesController extends TokenRatesController_base<type
|
|
|
159
156
|
* Blocks controller from making network calls
|
|
160
157
|
*/
|
|
161
158
|
disable(): void;
|
|
159
|
+
/**
|
|
160
|
+
* Start (or restart) polling.
|
|
161
|
+
*
|
|
162
|
+
* @param chainId - The chain ID.
|
|
163
|
+
* @param nativeCurrency - The native currency.
|
|
164
|
+
*/
|
|
165
|
+
start(chainId: Hex, nativeCurrency: string): Promise<void>;
|
|
166
|
+
/**
|
|
167
|
+
* Stop polling.
|
|
168
|
+
*/
|
|
169
|
+
stop(): void;
|
|
170
|
+
/**
|
|
171
|
+
* Updates exchange rates for all tokens.
|
|
172
|
+
*
|
|
173
|
+
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
174
|
+
*/
|
|
175
|
+
updateExchangeRates(chainIdAndNativeCurrency: {
|
|
176
|
+
chainId: Hex;
|
|
177
|
+
nativeCurrency: string;
|
|
178
|
+
}[]): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Updates exchange rates for all tokens.
|
|
181
|
+
*
|
|
182
|
+
* @param chainIds - The chain IDs.
|
|
183
|
+
* @returns A promise that resolves when all chain updates complete.
|
|
184
|
+
*/
|
|
162
185
|
/**
|
|
163
186
|
* Updates exchange rates for all tokens.
|
|
164
187
|
*
|
|
165
188
|
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
166
189
|
*/
|
|
167
|
-
|
|
190
|
+
updateExchangeRatesByChainId(chainIdAndNativeCurrency: {
|
|
191
|
+
chainId: Hex;
|
|
192
|
+
nativeCurrency: string;
|
|
193
|
+
}[]): Promise<void>;
|
|
168
194
|
/**
|
|
169
195
|
* Updates token rates for the given networkClientId
|
|
170
196
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenRatesController.d.mts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;
|
|
1
|
+
{"version":3,"file":"TokenRatesController.d.mts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kCAAkC,EAClC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 +9,20 @@ 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 _TokenRatesController_instances, _TokenRatesController_tokenPricesService, _TokenRatesController_disabled, _TokenRatesController_allTokens, _TokenRatesController_allDetectedTokens, _TokenRatesController_subscribeToTokensStateChange, _TokenRatesController_subscribeToNetworkStateChange, _TokenRatesController_getTokenAddresses, _TokenRatesController_getTokensControllerState, _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency, _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency;
|
|
13
|
-
import { toChecksumHexAddress } from "@metamask/controller-utils";
|
|
12
|
+
var _TokenRatesController_instances, _TokenRatesController_handle, _TokenRatesController_pollState, _TokenRatesController_tokenPricesService, _TokenRatesController_inProcessExchangeRateUpdates, _TokenRatesController_disabled, _TokenRatesController_interval, _TokenRatesController_allTokens, _TokenRatesController_allDetectedTokens, _TokenRatesController_subscribeToTokensStateChange, _TokenRatesController_subscribeToNetworkStateChange, _TokenRatesController_getTokenAddresses, _TokenRatesController_getTokensControllerState, _TokenRatesController_stopPoll, _TokenRatesController_poll, _TokenRatesController_fetchAndMapExchangeRates, _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency, _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency;
|
|
13
|
+
import { safelyExecute, toChecksumHexAddress } from "@metamask/controller-utils";
|
|
14
14
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
15
|
+
import { createDeferredPromise } from "@metamask/utils";
|
|
15
16
|
import $lodash from "lodash";
|
|
16
17
|
const { isEqual } = $lodash;
|
|
17
18
|
import { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from "./assetsUtil.mjs";
|
|
18
19
|
import { getNativeTokenAddress } from "./token-prices-service/codefi-v2.mjs";
|
|
19
20
|
const DEFAULT_INTERVAL = 180000;
|
|
21
|
+
var PollState;
|
|
22
|
+
(function (PollState) {
|
|
23
|
+
PollState["Active"] = "Active";
|
|
24
|
+
PollState["Inactive"] = "Inactive";
|
|
25
|
+
})(PollState || (PollState = {}));
|
|
20
26
|
/**
|
|
21
27
|
* The name of the {@link TokenRatesController}.
|
|
22
28
|
*/
|
|
@@ -62,13 +68,18 @@ export class TokenRatesController extends StaticIntervalPollingController() {
|
|
|
62
68
|
metadata: tokenRatesControllerMetadata,
|
|
63
69
|
});
|
|
64
70
|
_TokenRatesController_instances.add(this);
|
|
71
|
+
_TokenRatesController_handle.set(this, void 0);
|
|
72
|
+
_TokenRatesController_pollState.set(this, PollState.Inactive);
|
|
65
73
|
_TokenRatesController_tokenPricesService.set(this, void 0);
|
|
74
|
+
_TokenRatesController_inProcessExchangeRateUpdates.set(this, {});
|
|
66
75
|
_TokenRatesController_disabled.set(this, void 0);
|
|
76
|
+
_TokenRatesController_interval.set(this, void 0);
|
|
67
77
|
_TokenRatesController_allTokens.set(this, void 0);
|
|
68
78
|
_TokenRatesController_allDetectedTokens.set(this, void 0);
|
|
69
79
|
this.setIntervalLength(interval);
|
|
70
80
|
__classPrivateFieldSet(this, _TokenRatesController_tokenPricesService, tokenPricesService, "f");
|
|
71
81
|
__classPrivateFieldSet(this, _TokenRatesController_disabled, disabled, "f");
|
|
82
|
+
__classPrivateFieldSet(this, _TokenRatesController_interval, interval, "f");
|
|
72
83
|
const { allTokens, allDetectedTokens } = __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_getTokensControllerState).call(this);
|
|
73
84
|
__classPrivateFieldSet(this, _TokenRatesController_allTokens, allTokens, "f");
|
|
74
85
|
__classPrivateFieldSet(this, _TokenRatesController_allDetectedTokens, allDetectedTokens, "f");
|
|
@@ -87,52 +98,99 @@ export class TokenRatesController extends StaticIntervalPollingController() {
|
|
|
87
98
|
disable() {
|
|
88
99
|
__classPrivateFieldSet(this, _TokenRatesController_disabled, true, "f");
|
|
89
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Start (or restart) polling.
|
|
103
|
+
*
|
|
104
|
+
* @param chainId - The chain ID.
|
|
105
|
+
* @param nativeCurrency - The native currency.
|
|
106
|
+
*/
|
|
107
|
+
async start(chainId, nativeCurrency) {
|
|
108
|
+
__classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_stopPoll).call(this);
|
|
109
|
+
__classPrivateFieldSet(this, _TokenRatesController_pollState, PollState.Active, "f");
|
|
110
|
+
await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_poll).call(this, chainId, nativeCurrency);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Stop polling.
|
|
114
|
+
*/
|
|
115
|
+
stop() {
|
|
116
|
+
__classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_stopPoll).call(this);
|
|
117
|
+
__classPrivateFieldSet(this, _TokenRatesController_pollState, PollState.Inactive, "f");
|
|
118
|
+
}
|
|
90
119
|
/**
|
|
91
120
|
* Updates exchange rates for all tokens.
|
|
92
121
|
*
|
|
93
122
|
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
94
123
|
*/
|
|
95
124
|
async updateExchangeRates(chainIdAndNativeCurrency) {
|
|
125
|
+
await this.updateExchangeRatesByChainId(chainIdAndNativeCurrency);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Updates exchange rates for all tokens.
|
|
129
|
+
*
|
|
130
|
+
* @param chainIds - The chain IDs.
|
|
131
|
+
* @returns A promise that resolves when all chain updates complete.
|
|
132
|
+
*/
|
|
133
|
+
/**
|
|
134
|
+
* Updates exchange rates for all tokens.
|
|
135
|
+
*
|
|
136
|
+
* @param chainIdAndNativeCurrency - The chain ID and native currency.
|
|
137
|
+
*/
|
|
138
|
+
async updateExchangeRatesByChainId(chainIdAndNativeCurrency) {
|
|
96
139
|
if (__classPrivateFieldGet(this, _TokenRatesController_disabled, "f")) {
|
|
97
140
|
return;
|
|
98
141
|
}
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
tokenAddress,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
(unsupportedAssetsByNativeCurrency[nativeCurrency] ?? (unsupportedAssetsByNativeCurrency[nativeCurrency] = [])).push({
|
|
113
|
-
chainId,
|
|
114
|
-
tokenAddress,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
142
|
+
// Create a promise for each chainId to fetch exchange rates.
|
|
143
|
+
const updatePromises = chainIdAndNativeCurrency.map(async ({ chainId, nativeCurrency }) => {
|
|
144
|
+
const tokenAddresses = __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_getTokenAddresses).call(this, chainId);
|
|
145
|
+
// Build a unique key based on chainId, nativeCurrency, and the number of token addresses.
|
|
146
|
+
const updateKey = `${chainId}:${nativeCurrency}:${tokenAddresses.length}`;
|
|
147
|
+
if (updateKey in __classPrivateFieldGet(this, _TokenRatesController_inProcessExchangeRateUpdates, "f")) {
|
|
148
|
+
// Await any ongoing update to avoid redundant work.
|
|
149
|
+
await __classPrivateFieldGet(this, _TokenRatesController_inProcessExchangeRateUpdates, "f")[updateKey];
|
|
150
|
+
return null;
|
|
118
151
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
152
|
+
// Create a deferred promise to track this update.
|
|
153
|
+
const { promise: inProgressUpdate, resolve: updateSucceeded, reject: updateFailed, } = createDeferredPromise({ suppressUnhandledRejection: true });
|
|
154
|
+
__classPrivateFieldGet(this, _TokenRatesController_inProcessExchangeRateUpdates, "f")[updateKey] = inProgressUpdate;
|
|
155
|
+
try {
|
|
156
|
+
const contractInformations = await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRates).call(this, {
|
|
157
|
+
tokenAddresses,
|
|
158
|
+
chainId,
|
|
159
|
+
nativeCurrency,
|
|
160
|
+
});
|
|
161
|
+
// Each promise returns an object with the market data for the chain.
|
|
162
|
+
const marketData = {
|
|
163
|
+
[chainId]: {
|
|
164
|
+
...(contractInformations ?? {}),
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
updateSucceeded();
|
|
168
|
+
return marketData;
|
|
129
169
|
}
|
|
130
|
-
|
|
131
|
-
|
|
170
|
+
catch (error) {
|
|
171
|
+
updateFailed(error);
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
// Cleanup the tracking for this update.
|
|
176
|
+
delete __classPrivateFieldGet(this, _TokenRatesController_inProcessExchangeRateUpdates, "f")[updateKey];
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
// Wait for all update promises to settle.
|
|
180
|
+
const results = await Promise.allSettled(updatePromises);
|
|
181
|
+
// Merge all successful market data updates into one object.
|
|
182
|
+
const combinedMarketData = results.reduce((acc, result) => {
|
|
183
|
+
if (result.status === 'fulfilled' && result.value) {
|
|
184
|
+
acc = { ...acc, ...result.value };
|
|
185
|
+
}
|
|
186
|
+
return acc;
|
|
187
|
+
}, {});
|
|
188
|
+
// Call this.update only once with the combined market data to reduce the number of state changes and re-renders
|
|
189
|
+
if (Object.keys(combinedMarketData).length > 0) {
|
|
132
190
|
this.update((state) => {
|
|
133
191
|
state.marketData = {
|
|
134
192
|
...state.marketData,
|
|
135
|
-
...
|
|
193
|
+
...combinedMarketData,
|
|
136
194
|
};
|
|
137
195
|
});
|
|
138
196
|
}
|
|
@@ -157,7 +215,7 @@ export class TokenRatesController extends StaticIntervalPollingController() {
|
|
|
157
215
|
});
|
|
158
216
|
return acc;
|
|
159
217
|
}, []);
|
|
160
|
-
await this.
|
|
218
|
+
await this.updateExchangeRatesByChainId(chainIdAndNativeCurrency);
|
|
161
219
|
}
|
|
162
220
|
/**
|
|
163
221
|
* Reset the controller state to the default state.
|
|
@@ -168,7 +226,7 @@ export class TokenRatesController extends StaticIntervalPollingController() {
|
|
|
168
226
|
});
|
|
169
227
|
}
|
|
170
228
|
}
|
|
171
|
-
_TokenRatesController_tokenPricesService = new WeakMap(), _TokenRatesController_disabled = new WeakMap(), _TokenRatesController_allTokens = new WeakMap(), _TokenRatesController_allDetectedTokens = new WeakMap(), _TokenRatesController_instances = new WeakSet(), _TokenRatesController_subscribeToTokensStateChange = function _TokenRatesController_subscribeToTokensStateChange() {
|
|
229
|
+
_TokenRatesController_handle = new WeakMap(), _TokenRatesController_pollState = new WeakMap(), _TokenRatesController_tokenPricesService = new WeakMap(), _TokenRatesController_inProcessExchangeRateUpdates = new WeakMap(), _TokenRatesController_disabled = new WeakMap(), _TokenRatesController_interval = new WeakMap(), _TokenRatesController_allTokens = new WeakMap(), _TokenRatesController_allDetectedTokens = new WeakMap(), _TokenRatesController_instances = new WeakSet(), _TokenRatesController_subscribeToTokensStateChange = function _TokenRatesController_subscribeToTokensStateChange() {
|
|
172
230
|
this.messenger.subscribe('TokensController:stateChange',
|
|
173
231
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
174
232
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
@@ -199,12 +257,24 @@ _TokenRatesController_tokenPricesService = new WeakMap(), _TokenRatesController_
|
|
|
199
257
|
});
|
|
200
258
|
return acc;
|
|
201
259
|
}, []);
|
|
202
|
-
await this.
|
|
260
|
+
await this.updateExchangeRatesByChainId(chainIdAndNativeCurrency);
|
|
203
261
|
}, ({ allTokens, allDetectedTokens }) => {
|
|
204
262
|
return { allTokens, allDetectedTokens };
|
|
205
263
|
});
|
|
206
264
|
}, _TokenRatesController_subscribeToNetworkStateChange = function _TokenRatesController_subscribeToNetworkStateChange() {
|
|
207
|
-
this.messenger.subscribe('NetworkController:stateChange',
|
|
265
|
+
this.messenger.subscribe('NetworkController:stateChange',
|
|
266
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
268
|
+
async ({ networkConfigurationsByChainId }, patches) => {
|
|
269
|
+
const chainIdAndNativeCurrency = Object.values(networkConfigurationsByChainId).map(({ chainId, nativeCurrency }) => {
|
|
270
|
+
return {
|
|
271
|
+
chainId: chainId,
|
|
272
|
+
nativeCurrency,
|
|
273
|
+
};
|
|
274
|
+
});
|
|
275
|
+
if (__classPrivateFieldGet(this, _TokenRatesController_pollState, "f") === PollState.Active) {
|
|
276
|
+
await this.updateExchangeRates(chainIdAndNativeCurrency);
|
|
277
|
+
}
|
|
208
278
|
// Remove state for deleted networks
|
|
209
279
|
for (const patch of patches) {
|
|
210
280
|
if (patch.op === 'remove' &&
|
|
@@ -220,63 +290,189 @@ _TokenRatesController_tokenPricesService = new WeakMap(), _TokenRatesController_
|
|
|
220
290
|
const getTokens = (allTokens) => Object.values(allTokens ?? {}).flatMap((tokens) => tokens.map(({ address }) => toChecksumHexAddress(address)));
|
|
221
291
|
const tokenAddresses = getTokens(__classPrivateFieldGet(this, _TokenRatesController_allTokens, "f")[chainId]);
|
|
222
292
|
const detectedTokenAddresses = getTokens(__classPrivateFieldGet(this, _TokenRatesController_allDetectedTokens, "f")[chainId]);
|
|
223
|
-
return [
|
|
224
|
-
...new Set([
|
|
225
|
-
...tokenAddresses,
|
|
226
|
-
...detectedTokenAddresses,
|
|
227
|
-
getNativeTokenAddress(chainId),
|
|
228
|
-
]),
|
|
229
|
-
].sort();
|
|
293
|
+
return [...new Set([...tokenAddresses, ...detectedTokenAddresses])].sort();
|
|
230
294
|
}, _TokenRatesController_getTokensControllerState = function _TokenRatesController_getTokensControllerState() {
|
|
231
295
|
const { allTokens, allDetectedTokens } = this.messenger.call('TokensController:getState');
|
|
232
296
|
return {
|
|
233
297
|
allTokens,
|
|
234
298
|
allDetectedTokens,
|
|
235
299
|
};
|
|
236
|
-
},
|
|
237
|
-
|
|
238
|
-
|
|
300
|
+
}, _TokenRatesController_stopPoll = function _TokenRatesController_stopPoll() {
|
|
301
|
+
if (__classPrivateFieldGet(this, _TokenRatesController_handle, "f")) {
|
|
302
|
+
clearTimeout(__classPrivateFieldGet(this, _TokenRatesController_handle, "f"));
|
|
303
|
+
}
|
|
304
|
+
}, _TokenRatesController_poll =
|
|
305
|
+
/**
|
|
306
|
+
* Poll for exchange rate updates.
|
|
307
|
+
*
|
|
308
|
+
* @param chainId - The chain ID.
|
|
309
|
+
* @param nativeCurrency - The native currency.
|
|
310
|
+
*/
|
|
311
|
+
async function _TokenRatesController_poll(chainId, nativeCurrency) {
|
|
312
|
+
await safelyExecute(() => this.updateExchangeRates([{ chainId, nativeCurrency }]));
|
|
313
|
+
// Poll using recursive `setTimeout` instead of `setInterval` so that
|
|
314
|
+
// requests don't stack if they take longer than the polling interval
|
|
315
|
+
__classPrivateFieldSet(this, _TokenRatesController_handle, setTimeout(() => {
|
|
316
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
318
|
+
__classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_poll).call(this, chainId, nativeCurrency);
|
|
319
|
+
}, __classPrivateFieldGet(this, _TokenRatesController_interval, "f")), "f");
|
|
320
|
+
}, _TokenRatesController_fetchAndMapExchangeRates =
|
|
321
|
+
/**
|
|
322
|
+
* Uses the token prices service to retrieve exchange rates for tokens in a
|
|
323
|
+
* particular currency.
|
|
324
|
+
*
|
|
325
|
+
* If the price API does not support the given chain ID, returns an empty
|
|
326
|
+
* object.
|
|
327
|
+
*
|
|
328
|
+
* If the price API does not support the given currency, retrieves exchange
|
|
329
|
+
* rates in a known currency instead, then converts those rates using the
|
|
330
|
+
* exchange rate between the known currency and desired currency.
|
|
331
|
+
*
|
|
332
|
+
* @param args - The arguments to this function.
|
|
333
|
+
* @param args.tokenAddresses - Addresses for tokens.
|
|
334
|
+
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
|
|
335
|
+
* @param args.nativeCurrency - The native currency in which to request
|
|
336
|
+
* exchange rates.
|
|
337
|
+
* @returns A map from token address to its exchange rate in the native
|
|
338
|
+
* currency, or an empty map if no exchange rates can be obtained for the
|
|
339
|
+
* chain ID.
|
|
340
|
+
*/
|
|
341
|
+
async function _TokenRatesController_fetchAndMapExchangeRates({ tokenAddresses, chainId, nativeCurrency, }) {
|
|
342
|
+
if (!__classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").validateChainIdSupported(chainId)) {
|
|
343
|
+
return tokenAddresses.reduce((obj, tokenAddress) => {
|
|
344
|
+
obj = {
|
|
345
|
+
...obj,
|
|
346
|
+
[tokenAddress]: undefined,
|
|
347
|
+
};
|
|
348
|
+
return obj;
|
|
349
|
+
}, {});
|
|
350
|
+
}
|
|
351
|
+
if (__classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").validateCurrencySupported(nativeCurrency)) {
|
|
352
|
+
return await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency).call(this, {
|
|
353
|
+
tokenAddresses,
|
|
354
|
+
chainId,
|
|
355
|
+
nativeCurrency,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency).call(this, {
|
|
359
|
+
chainId,
|
|
360
|
+
tokenAddresses,
|
|
361
|
+
nativeCurrency,
|
|
362
|
+
});
|
|
363
|
+
}, _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency =
|
|
364
|
+
/**
|
|
365
|
+
* Retrieves prices in the given currency for the given tokens on the given
|
|
366
|
+
* chain. Ensures that token addresses are checksum addresses.
|
|
367
|
+
*
|
|
368
|
+
* @param args - The arguments to this function.
|
|
369
|
+
* @param args.tokenAddresses - Addresses for tokens.
|
|
370
|
+
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
|
|
371
|
+
* @param args.nativeCurrency - The native currency in which to request
|
|
372
|
+
* prices.
|
|
373
|
+
* @returns A map of the token addresses (as checksums) to their prices in the
|
|
374
|
+
* native currency.
|
|
375
|
+
*/
|
|
376
|
+
async function _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency({ tokenAddresses, chainId, nativeCurrency, }) {
|
|
377
|
+
let contractNativeInformations;
|
|
378
|
+
const tokenPricesByTokenAddress = await reduceInBatchesSerially({
|
|
379
|
+
values: [...tokenAddresses].sort(),
|
|
239
380
|
batchSize: TOKEN_PRICES_BATCH_SIZE,
|
|
240
|
-
eachBatch: async (
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
currency,
|
|
381
|
+
eachBatch: async (allTokenPricesByTokenAddress, batch) => {
|
|
382
|
+
const tokenPricesByTokenAddressForBatch = await __classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").fetchTokenPrices({
|
|
383
|
+
tokenAddresses: batch,
|
|
384
|
+
chainId,
|
|
385
|
+
currency: nativeCurrency,
|
|
245
386
|
});
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
387
|
+
return {
|
|
388
|
+
...allTokenPricesByTokenAddress,
|
|
389
|
+
...tokenPricesByTokenAddressForBatch,
|
|
390
|
+
};
|
|
250
391
|
},
|
|
251
|
-
initialResult:
|
|
392
|
+
initialResult: {},
|
|
393
|
+
});
|
|
394
|
+
contractNativeInformations = tokenPricesByTokenAddress;
|
|
395
|
+
// fetch for native token
|
|
396
|
+
if (tokenAddresses.length === 0) {
|
|
397
|
+
const contractNativeInformationsNative = await __classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").fetchTokenPrices({
|
|
398
|
+
tokenAddresses: [],
|
|
399
|
+
chainId,
|
|
400
|
+
currency: nativeCurrency,
|
|
401
|
+
});
|
|
402
|
+
contractNativeInformations = {
|
|
403
|
+
[getNativeTokenAddress(chainId)]: {
|
|
404
|
+
currency: nativeCurrency,
|
|
405
|
+
...contractNativeInformationsNative[getNativeTokenAddress(chainId)],
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
return Object.entries(contractNativeInformations).reduce((obj, [tokenAddress, token]) => {
|
|
410
|
+
obj = {
|
|
411
|
+
...obj,
|
|
412
|
+
[tokenAddress]: { ...token },
|
|
413
|
+
};
|
|
414
|
+
return obj;
|
|
415
|
+
}, {});
|
|
416
|
+
}, _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency =
|
|
417
|
+
/**
|
|
418
|
+
* If the price API does not support a given native currency, then we need to
|
|
419
|
+
* convert it to a fallback currency and feed that currency into the price
|
|
420
|
+
* API, then convert the prices to our desired native currency.
|
|
421
|
+
*
|
|
422
|
+
* @param args - The arguments to this function.
|
|
423
|
+
* @param args.chainId - The chain id to fetch prices for.
|
|
424
|
+
* @param args.tokenAddresses - Addresses for tokens.
|
|
425
|
+
* @param args.nativeCurrency - The native currency in which to request
|
|
426
|
+
* prices.
|
|
427
|
+
* @returns A map of the token addresses (as checksums) to their prices in the
|
|
428
|
+
* native currency.
|
|
429
|
+
*/
|
|
430
|
+
async function _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency({ chainId, tokenAddresses, nativeCurrency, }) {
|
|
431
|
+
const nativeTokenAddress = getNativeTokenAddress(chainId);
|
|
432
|
+
// Step -1: First fetch native token priced in USD
|
|
433
|
+
const nativeTokenPriceMap = await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency).call(this, {
|
|
434
|
+
tokenAddresses: [], // special-case: returns only native token
|
|
435
|
+
chainId,
|
|
436
|
+
nativeCurrency: 'usd',
|
|
252
437
|
});
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
438
|
+
// Step -2: Then fetch all tracked tokens priced in USD
|
|
439
|
+
const tokenPricesInUSD = await __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency).call(this, {
|
|
440
|
+
tokenAddresses,
|
|
441
|
+
chainId,
|
|
442
|
+
nativeCurrency: 'usd',
|
|
443
|
+
});
|
|
444
|
+
const nativeTokenInfo = nativeTokenPriceMap[nativeTokenAddress];
|
|
445
|
+
const nativeTokenPriceInUSD = nativeTokenInfo?.price;
|
|
446
|
+
if (!nativeTokenPriceInUSD || nativeTokenPriceInUSD === 0) {
|
|
447
|
+
// If we can't price the native token in the fallback currency,
|
|
448
|
+
// we can't safely convert; return empty so callers know there is no data.
|
|
449
|
+
return {};
|
|
450
|
+
}
|
|
451
|
+
// Step -3: Convert USD prices to native currency
|
|
256
452
|
// Formula: price_in_native = token_usd / native_usd
|
|
257
|
-
const convertUSDToNative = (valueInUSD
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
for (const [tokenAddress, tokenData] of Object.entries(marketDataByTokenAddress)) {
|
|
266
|
-
(marketData[chainId] ?? (marketData[chainId] = {}))[tokenAddress] = {
|
|
453
|
+
const convertUSDToNative = (valueInUSD) => valueInUSD !== undefined && valueInUSD !== null
|
|
454
|
+
? valueInUSD / nativeTokenPriceInUSD
|
|
455
|
+
: undefined;
|
|
456
|
+
// Step -4 & -5: Apply conversion to all token fields and return
|
|
457
|
+
const tokenPricesInNative = Object.entries(tokenPricesInUSD).reduce((acc, [tokenAddress, tokenData]) => {
|
|
458
|
+
acc = {
|
|
459
|
+
...acc,
|
|
460
|
+
[tokenAddress]: {
|
|
267
461
|
...tokenData,
|
|
268
|
-
currency,
|
|
269
|
-
price: convertUSDToNative(tokenData.price
|
|
270
|
-
marketCap: convertUSDToNative(tokenData.marketCap
|
|
271
|
-
allTimeHigh: convertUSDToNative(tokenData.allTimeHigh
|
|
272
|
-
allTimeLow: convertUSDToNative(tokenData.allTimeLow
|
|
273
|
-
totalVolume: convertUSDToNative(tokenData.totalVolume
|
|
274
|
-
high1d: convertUSDToNative(tokenData.high1d
|
|
275
|
-
low1d: convertUSDToNative(tokenData.low1d
|
|
276
|
-
dilutedMarketCap: convertUSDToNative(tokenData.dilutedMarketCap
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
462
|
+
currency: nativeCurrency,
|
|
463
|
+
price: convertUSDToNative(tokenData.price),
|
|
464
|
+
marketCap: convertUSDToNative(tokenData.marketCap),
|
|
465
|
+
allTimeHigh: convertUSDToNative(tokenData.allTimeHigh),
|
|
466
|
+
allTimeLow: convertUSDToNative(tokenData.allTimeLow),
|
|
467
|
+
totalVolume: convertUSDToNative(tokenData.totalVolume),
|
|
468
|
+
high1d: convertUSDToNative(tokenData.high1d),
|
|
469
|
+
low1d: convertUSDToNative(tokenData.low1d),
|
|
470
|
+
dilutedMarketCap: convertUSDToNative(tokenData.dilutedMarketCap),
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
return acc;
|
|
474
|
+
}, {});
|
|
475
|
+
return tokenPricesInNative;
|
|
280
476
|
};
|
|
281
477
|
export default TokenRatesController;
|
|
282
478
|
//# sourceMappingURL=TokenRatesController.mjs.map
|