@metamask-previews/assets-controllers 19.0.0-preview.5736db0 → 20.0.0-preview.3fbb6b41
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 +49 -1
- package/dist/AccountTrackerController.d.ts +2 -2
- package/dist/AccountTrackerController.d.ts.map +1 -1
- package/dist/AccountTrackerController.js +1 -1
- package/dist/AccountTrackerController.js.map +1 -1
- package/dist/AssetsContractController.d.ts +5 -5
- package/dist/AssetsContractController.d.ts.map +1 -1
- package/dist/AssetsContractController.js +6 -4
- package/dist/AssetsContractController.js.map +1 -1
- package/dist/NftController.d.ts +2 -2
- package/dist/NftController.d.ts.map +1 -1
- package/dist/NftController.js +1 -1
- package/dist/NftController.js.map +1 -1
- package/dist/TokenBalancesController.d.ts +2 -2
- package/dist/TokenBalancesController.d.ts.map +1 -1
- package/dist/TokenBalancesController.js +1 -1
- package/dist/TokenBalancesController.js.map +1 -1
- package/dist/TokenListController.d.ts.map +1 -1
- package/dist/TokenListController.js +1 -11
- package/dist/TokenListController.js.map +1 -1
- package/dist/TokenRatesController.d.ts +30 -64
- package/dist/TokenRatesController.d.ts.map +1 -1
- package/dist/TokenRatesController.js +110 -170
- package/dist/TokenRatesController.js.map +1 -1
- package/dist/TokensController.d.ts +5 -5
- package/dist/TokensController.d.ts.map +1 -1
- package/dist/TokensController.js +4 -4
- package/dist/TokensController.js.map +1 -1
- package/dist/assetsUtil.d.ts +3 -1
- package/dist/assetsUtil.d.ts.map +1 -1
- package/dist/assetsUtil.js +2 -0
- package/dist/assetsUtil.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.d.ts +63 -0
- package/dist/token-prices-service/abstract-token-prices-service.d.ts.map +1 -0
- package/dist/token-prices-service/abstract-token-prices-service.js +3 -0
- package/dist/token-prices-service/abstract-token-prices-service.js.map +1 -0
- package/dist/token-prices-service/codefi-v2.d.ts +31 -0
- package/dist/token-prices-service/codefi-v2.d.ts.map +1 -0
- package/dist/token-prices-service/codefi-v2.js +286 -0
- package/dist/token-prices-service/codefi-v2.js.map +1 -0
- package/dist/token-prices-service/index.d.ts +3 -0
- package/dist/token-prices-service/index.d.ts.map +1 -0
- package/dist/token-prices-service/index.js +6 -0
- package/dist/token-prices-service/index.js.map +1 -0
- package/dist/token-service.d.ts.map +1 -1
- package/dist/token-service.js +1 -1
- package/dist/token-service.js.map +1 -1
- package/package.json +10 -10
|
@@ -3,28 +3,8 @@ import type { NetworkClientId, NetworkController, NetworkState } from '@metamask
|
|
|
3
3
|
import { PollingControllerV1 } from '@metamask/polling-controller';
|
|
4
4
|
import type { PreferencesState } from '@metamask/preferences-controller';
|
|
5
5
|
import type { Hex } from '@metamask/utils';
|
|
6
|
+
import type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';
|
|
6
7
|
import type { TokensState } from './TokensController';
|
|
7
|
-
/**
|
|
8
|
-
* @type CoinGeckoResponse
|
|
9
|
-
*
|
|
10
|
-
* CoinGecko API response representation
|
|
11
|
-
*/
|
|
12
|
-
export interface CoinGeckoResponse {
|
|
13
|
-
[address: string]: {
|
|
14
|
-
[currency: string]: number;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @type CoinGeckoPlatform
|
|
19
|
-
*
|
|
20
|
-
* CoinGecko supported platform API representation
|
|
21
|
-
*/
|
|
22
|
-
export interface CoinGeckoPlatform {
|
|
23
|
-
id: string;
|
|
24
|
-
chain_identifier: null | number;
|
|
25
|
-
name: string;
|
|
26
|
-
shortname: string;
|
|
27
|
-
}
|
|
28
8
|
/**
|
|
29
9
|
* @type Token
|
|
30
10
|
*
|
|
@@ -93,8 +73,6 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
93
73
|
#private;
|
|
94
74
|
private handle?;
|
|
95
75
|
private tokenList;
|
|
96
|
-
private supportedChains;
|
|
97
|
-
private supportedVsCurrencies;
|
|
98
76
|
/**
|
|
99
77
|
* Name of this controller used during composition
|
|
100
78
|
*/
|
|
@@ -113,10 +91,11 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
113
91
|
* @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
|
|
114
92
|
* @param options.onTokensStateChange - Allows subscribing to token controller state changes.
|
|
115
93
|
* @param options.onNetworkStateChange - Allows subscribing to network state changes.
|
|
94
|
+
* @param options.tokenPricesService - An object in charge of retrieving token prices.
|
|
116
95
|
* @param config - Initial options used to configure this controller.
|
|
117
96
|
* @param state - Initial state to set on this controller.
|
|
118
97
|
*/
|
|
119
|
-
constructor({ interval, threshold, getNetworkClientById, chainId: initialChainId, ticker: initialTicker, selectedAddress: initialSelectedAddress, onPreferencesStateChange, onTokensStateChange, onNetworkStateChange, }: {
|
|
98
|
+
constructor({ interval, threshold, getNetworkClientById, chainId: initialChainId, ticker: initialTicker, selectedAddress: initialSelectedAddress, onPreferencesStateChange, onTokensStateChange, onNetworkStateChange, tokenPricesService, }: {
|
|
120
99
|
interval?: number;
|
|
121
100
|
threshold?: number;
|
|
122
101
|
getNetworkClientById: NetworkController['getNetworkClientById'];
|
|
@@ -126,6 +105,7 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
126
105
|
onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
|
|
127
106
|
onTokensStateChange: (listener: (tokensState: TokensState) => void) => void;
|
|
128
107
|
onNetworkStateChange: (listener: (networkState: NetworkState) => void) => void;
|
|
108
|
+
tokenPricesService: AbstractTokenPricesService;
|
|
129
109
|
}, config?: Partial<TokenRatesConfig>, state?: Partial<TokenRatesState>);
|
|
130
110
|
/**
|
|
131
111
|
* Start (or restart) polling.
|
|
@@ -135,31 +115,6 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
135
115
|
* Stop polling.
|
|
136
116
|
*/
|
|
137
117
|
stop(): void;
|
|
138
|
-
/**
|
|
139
|
-
* Fetches a pairs of token address and native currency.
|
|
140
|
-
*
|
|
141
|
-
* @param chainSlug - The chain string identifier.
|
|
142
|
-
* @param vsCurrency - The currency used to generate pairs against the tokens.
|
|
143
|
-
* @param tokenAddresses - The addresses for the token contracts.
|
|
144
|
-
* @returns The exchange rates for the given pairs.
|
|
145
|
-
*/
|
|
146
|
-
fetchExchangeRate(chainSlug: string, vsCurrency: string, tokenAddresses?: string[]): Promise<CoinGeckoResponse>;
|
|
147
|
-
/**
|
|
148
|
-
* Checks if the current native currency is a supported vs currency to use
|
|
149
|
-
* to query for token exchange rates.
|
|
150
|
-
*
|
|
151
|
-
* @param nativeCurrency - The native currency to check.
|
|
152
|
-
* @returns A boolean indicating whether it's a supported vsCurrency.
|
|
153
|
-
*/
|
|
154
|
-
private checkIsSupportedVsCurrency;
|
|
155
|
-
/**
|
|
156
|
-
* Gets the slug from cached supported platforms CoinGecko API response for the chain ID.
|
|
157
|
-
* If cached supported platforms response is stale, fetches and updates it.
|
|
158
|
-
*
|
|
159
|
-
* @param chainId - The chain ID.
|
|
160
|
-
* @returns The CoinGecko slug for the chain ID.
|
|
161
|
-
*/
|
|
162
|
-
getChainSlug(chainId?: Hex): Promise<string | null>;
|
|
163
118
|
/**
|
|
164
119
|
* Updates exchange rates for all tokens.
|
|
165
120
|
*/
|
|
@@ -170,27 +125,38 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
170
125
|
* @param options - The options to fetch exchange rates.
|
|
171
126
|
* @param options.chainId - The chain ID.
|
|
172
127
|
* @param options.nativeCurrency - The ticker for the chain.
|
|
173
|
-
* @param options.
|
|
128
|
+
* @param options.tokenContractAddresses - The addresses for the token contracts.
|
|
174
129
|
*/
|
|
175
|
-
updateExchangeRatesByChainId({ chainId, nativeCurrency,
|
|
130
|
+
updateExchangeRatesByChainId({ chainId, nativeCurrency, tokenContractAddresses, }: {
|
|
176
131
|
chainId: Hex;
|
|
177
132
|
nativeCurrency: string;
|
|
178
|
-
|
|
133
|
+
tokenContractAddresses: Hex[];
|
|
179
134
|
}): Promise<void>;
|
|
180
135
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
136
|
+
* Uses the token prices service to retrieve exchange rates for tokens in a
|
|
137
|
+
* particular currency.
|
|
138
|
+
*
|
|
139
|
+
* If the price API does not support the given chain ID, returns an empty
|
|
140
|
+
* object.
|
|
185
141
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* @
|
|
191
|
-
*
|
|
142
|
+
* If the price API does not support the given currency, retrieves exchange
|
|
143
|
+
* rates in a known currency instead, then converts those rates using the
|
|
144
|
+
* exchange rate between the known currency and desired currency.
|
|
145
|
+
*
|
|
146
|
+
* @param args - The arguments to this function.
|
|
147
|
+
* @param args.tokenContractAddresses - Contract addresses for tokens.
|
|
148
|
+
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
|
|
149
|
+
* @param args.nativeCurrency - The native currency in which to request
|
|
150
|
+
* exchange rates.
|
|
151
|
+
* @returns A map from token contract address to its exchange rate in the
|
|
152
|
+
* native currency, or an empty map if no exchange rates can be obtained for
|
|
153
|
+
* the chain ID.
|
|
192
154
|
*/
|
|
193
|
-
fetchAndMapExchangeRates(
|
|
155
|
+
fetchAndMapExchangeRates({ tokenContractAddresses, chainId, nativeCurrency, }: {
|
|
156
|
+
tokenContractAddresses: Hex[];
|
|
157
|
+
chainId: Hex;
|
|
158
|
+
nativeCurrency: string;
|
|
159
|
+
}): Promise<ContractExchangeRates>;
|
|
194
160
|
/**
|
|
195
161
|
* Updates token rates for the given networkClientId and contract addresses
|
|
196
162
|
*
|
|
@@ -200,7 +166,7 @@ export declare class TokenRatesController extends PollingControllerV1<TokenRates
|
|
|
200
166
|
* @returns The controller state.
|
|
201
167
|
*/
|
|
202
168
|
_executePoll(networkClientId: NetworkClientId, options: {
|
|
203
|
-
tokenAddresses:
|
|
169
|
+
tokenAddresses: Hex[];
|
|
204
170
|
}): Promise<void>;
|
|
205
171
|
}
|
|
206
172
|
export default TokenRatesController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenRatesController.d.ts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"TokenRatesController.d.ts","sourceRoot":"","sources":["../src/TokenRatesController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAMvE,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EACjB,YAAY,EACb,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,sDAAsD,CAAC;AACvG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD;;;;;;;;GAQG;AAIH,MAAM,WAAW,KAAK;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;GASG;AAIH,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAClD,QAAQ,EAAE,MAAM,CAAC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
|
|
@@ -8,18 +8,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
-
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");
|
|
14
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
-
};
|
|
16
11
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
17
12
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
18
13
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
19
14
|
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");
|
|
20
15
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
21
16
|
};
|
|
22
|
-
var
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
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");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _TokenRatesController_instances, _TokenRatesController_pollState, _TokenRatesController_tokenPricesService, _TokenRatesController_updateTokenList, _TokenRatesController_stopPoll, _TokenRatesController_poll, _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency, _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency;
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.TokenRatesController = void 0;
|
|
25
25
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
@@ -30,32 +30,32 @@ var PollState;
|
|
|
30
30
|
PollState["Active"] = "Active";
|
|
31
31
|
PollState["Inactive"] = "Inactive";
|
|
32
32
|
})(PollState || (PollState = {}));
|
|
33
|
-
const CoinGeckoApi = {
|
|
34
|
-
BASE_URL: 'https://api.coingecko.com/api/v3',
|
|
35
|
-
getTokenPriceURL(chainSlug, query) {
|
|
36
|
-
return `${this.BASE_URL}/simple/token_price/${chainSlug}?${query}`;
|
|
37
|
-
},
|
|
38
|
-
getPlatformsURL() {
|
|
39
|
-
return `${this.BASE_URL}/asset_platforms`;
|
|
40
|
-
},
|
|
41
|
-
getSupportedVsCurrencies() {
|
|
42
|
-
return `${this.BASE_URL}/simple/supported_vs_currencies`;
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
33
|
/**
|
|
46
|
-
*
|
|
34
|
+
* Uses the CryptoCompare API to fetch the exchange rate between one currency
|
|
35
|
+
* and another, i.e., the multiplier to apply the amount of one currency in
|
|
36
|
+
* order to convert it to another.
|
|
47
37
|
*
|
|
48
|
-
* @param
|
|
49
|
-
* @param
|
|
50
|
-
* @
|
|
38
|
+
* @param args - The arguments to this function.
|
|
39
|
+
* @param args.from - The currency to convert from.
|
|
40
|
+
* @param args.to - The currency to convert to.
|
|
41
|
+
* @returns The exchange rate between `fromCurrency` to `toCurrency` if one
|
|
42
|
+
* exists, or null if one does not.
|
|
51
43
|
*/
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
44
|
+
function getCurrencyConversionRate({ from, to, }) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const includeUSDRate = false;
|
|
47
|
+
try {
|
|
48
|
+
const result = yield (0, crypto_compare_1.fetchExchangeRate)(to, from, includeUSDRate);
|
|
49
|
+
return result.conversionRate;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof Error &&
|
|
53
|
+
error.message.includes('market does not exist for this coin pair')) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* Controller that passively polls on a set interval for token-to-fiat exchange rates
|
|
@@ -75,22 +75,16 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
75
75
|
* @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
|
|
76
76
|
* @param options.onTokensStateChange - Allows subscribing to token controller state changes.
|
|
77
77
|
* @param options.onNetworkStateChange - Allows subscribing to network state changes.
|
|
78
|
+
* @param options.tokenPricesService - An object in charge of retrieving token prices.
|
|
78
79
|
* @param config - Initial options used to configure this controller.
|
|
79
80
|
* @param state - Initial state to set on this controller.
|
|
80
81
|
*/
|
|
81
|
-
constructor({ interval = 3 * 60 * 1000, threshold = 6 * 60 * 60 * 1000, getNetworkClientById, chainId: initialChainId, ticker: initialTicker, selectedAddress: initialSelectedAddress, onPreferencesStateChange, onTokensStateChange, onNetworkStateChange, }, config, state) {
|
|
82
|
+
constructor({ interval = 3 * 60 * 1000, threshold = 6 * 60 * 60 * 1000, getNetworkClientById, chainId: initialChainId, ticker: initialTicker, selectedAddress: initialSelectedAddress, onPreferencesStateChange, onTokensStateChange, onNetworkStateChange, tokenPricesService, }, config, state) {
|
|
82
83
|
super(config, state);
|
|
83
84
|
_TokenRatesController_instances.add(this);
|
|
84
85
|
this.tokenList = [];
|
|
85
|
-
this.supportedChains = {
|
|
86
|
-
timestamp: 0,
|
|
87
|
-
data: null,
|
|
88
|
-
};
|
|
89
|
-
this.supportedVsCurrencies = {
|
|
90
|
-
timestamp: 0,
|
|
91
|
-
data: [],
|
|
92
|
-
};
|
|
93
86
|
_TokenRatesController_pollState.set(this, PollState.Inactive);
|
|
87
|
+
_TokenRatesController_tokenPricesService.set(this, void 0);
|
|
94
88
|
/**
|
|
95
89
|
* Name of this controller used during composition
|
|
96
90
|
*/
|
|
@@ -112,6 +106,7 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
112
106
|
this.initialize();
|
|
113
107
|
this.setIntervalLength(interval);
|
|
114
108
|
this.getNetworkClientById = getNetworkClientById;
|
|
109
|
+
__classPrivateFieldSet(this, _TokenRatesController_tokenPricesService, tokenPricesService, "f");
|
|
115
110
|
if (config === null || config === void 0 ? void 0 : config.disabled) {
|
|
116
111
|
this.configure({ disabled: true }, false, false);
|
|
117
112
|
}
|
|
@@ -166,67 +161,6 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
166
161
|
__classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_stopPoll).call(this);
|
|
167
162
|
__classPrivateFieldSet(this, _TokenRatesController_pollState, PollState.Inactive, "f");
|
|
168
163
|
}
|
|
169
|
-
/**
|
|
170
|
-
* Fetches a pairs of token address and native currency.
|
|
171
|
-
*
|
|
172
|
-
* @param chainSlug - The chain string identifier.
|
|
173
|
-
* @param vsCurrency - The currency used to generate pairs against the tokens.
|
|
174
|
-
* @param tokenAddresses - The addresses for the token contracts.
|
|
175
|
-
* @returns The exchange rates for the given pairs.
|
|
176
|
-
*/
|
|
177
|
-
fetchExchangeRate(chainSlug, vsCurrency, tokenAddresses = this.tokenList.map((token) => token.address)) {
|
|
178
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
-
const tokenPairs = tokenAddresses.join(',');
|
|
180
|
-
const query = `contract_addresses=${tokenPairs}&vs_currencies=${vsCurrency.toLowerCase()}`;
|
|
181
|
-
return (0, controller_utils_1.handleFetch)(CoinGeckoApi.getTokenPriceURL(chainSlug, query));
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Checks if the current native currency is a supported vs currency to use
|
|
186
|
-
* to query for token exchange rates.
|
|
187
|
-
*
|
|
188
|
-
* @param nativeCurrency - The native currency to check.
|
|
189
|
-
* @returns A boolean indicating whether it's a supported vsCurrency.
|
|
190
|
-
*/
|
|
191
|
-
checkIsSupportedVsCurrency(nativeCurrency) {
|
|
192
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
-
const { threshold } = this.config;
|
|
194
|
-
const { timestamp, data } = this.supportedVsCurrencies;
|
|
195
|
-
const now = Date.now();
|
|
196
|
-
if (now - timestamp > threshold) {
|
|
197
|
-
const currencies = yield (0, controller_utils_1.handleFetch)(CoinGeckoApi.getSupportedVsCurrencies());
|
|
198
|
-
this.supportedVsCurrencies = {
|
|
199
|
-
data: currencies,
|
|
200
|
-
timestamp: Date.now(),
|
|
201
|
-
};
|
|
202
|
-
return currencies.includes(nativeCurrency.toLowerCase());
|
|
203
|
-
}
|
|
204
|
-
return data.includes(nativeCurrency.toLowerCase());
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Gets the slug from cached supported platforms CoinGecko API response for the chain ID.
|
|
209
|
-
* If cached supported platforms response is stale, fetches and updates it.
|
|
210
|
-
*
|
|
211
|
-
* @param chainId - The chain ID.
|
|
212
|
-
* @returns The CoinGecko slug for the chain ID.
|
|
213
|
-
*/
|
|
214
|
-
getChainSlug(chainId = this.config.chainId) {
|
|
215
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
const { threshold } = this.config;
|
|
217
|
-
const { data, timestamp } = this.supportedChains;
|
|
218
|
-
const now = Date.now();
|
|
219
|
-
if (now - timestamp > threshold) {
|
|
220
|
-
const platforms = yield (0, controller_utils_1.handleFetch)(CoinGeckoApi.getPlatformsURL());
|
|
221
|
-
this.supportedChains = {
|
|
222
|
-
data: platforms,
|
|
223
|
-
timestamp: Date.now(),
|
|
224
|
-
};
|
|
225
|
-
return findChainSlug(chainId, platforms);
|
|
226
|
-
}
|
|
227
|
-
return findChainSlug(chainId, data);
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
164
|
/**
|
|
231
165
|
* Updates exchange rates for all tokens.
|
|
232
166
|
*/
|
|
@@ -236,11 +170,11 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
236
170
|
return;
|
|
237
171
|
}
|
|
238
172
|
const { chainId, nativeCurrency } = this.config;
|
|
239
|
-
const
|
|
173
|
+
const tokenContractAddresses = this.tokenList.map((token) => token.address);
|
|
240
174
|
yield this.updateExchangeRatesByChainId({
|
|
241
175
|
chainId,
|
|
242
176
|
nativeCurrency,
|
|
243
|
-
|
|
177
|
+
tokenContractAddresses,
|
|
244
178
|
});
|
|
245
179
|
this.update({
|
|
246
180
|
contractExchangeRates: this.state.contractExchangeRatesByChainId[chainId][nativeCurrency],
|
|
@@ -253,88 +187,63 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
253
187
|
* @param options - The options to fetch exchange rates.
|
|
254
188
|
* @param options.chainId - The chain ID.
|
|
255
189
|
* @param options.nativeCurrency - The ticker for the chain.
|
|
256
|
-
* @param options.
|
|
190
|
+
* @param options.tokenContractAddresses - The addresses for the token contracts.
|
|
257
191
|
*/
|
|
258
|
-
updateExchangeRatesByChainId({ chainId, nativeCurrency,
|
|
259
|
-
var _a
|
|
192
|
+
updateExchangeRatesByChainId({ chainId, nativeCurrency, tokenContractAddresses, }) {
|
|
193
|
+
var _a;
|
|
260
194
|
return __awaiter(this, void 0, void 0, function* () {
|
|
261
|
-
if (
|
|
195
|
+
if (tokenContractAddresses.length === 0 || this.disabled) {
|
|
262
196
|
return;
|
|
263
197
|
}
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
newContractExchangeRates[address] = undefined;
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
newContractExchangeRates = yield this.fetchAndMapExchangeRates(nativeCurrency, chainSlug, tokenAddresses);
|
|
274
|
-
}
|
|
198
|
+
const newContractExchangeRates = yield this.fetchAndMapExchangeRates({
|
|
199
|
+
tokenContractAddresses,
|
|
200
|
+
chainId,
|
|
201
|
+
nativeCurrency,
|
|
202
|
+
});
|
|
275
203
|
const existingContractExchangeRatesForChainId = (_a = this.state.contractExchangeRatesByChainId[chainId]) !== null && _a !== void 0 ? _a : {};
|
|
276
204
|
this.update({
|
|
277
|
-
contractExchangeRatesByChainId: Object.assign(Object.assign({}, this.state.contractExchangeRatesByChainId), { [chainId]: Object.assign(Object.assign({}, existingContractExchangeRatesForChainId), { [nativeCurrency]: Object.assign(Object.assign({},
|
|
205
|
+
contractExchangeRatesByChainId: Object.assign(Object.assign({}, this.state.contractExchangeRatesByChainId), { [chainId]: Object.assign(Object.assign({}, existingContractExchangeRatesForChainId), { [nativeCurrency]: Object.assign(Object.assign({}, existingContractExchangeRatesForChainId[nativeCurrency]), newContractExchangeRates) }) }),
|
|
278
206
|
});
|
|
279
207
|
});
|
|
280
208
|
}
|
|
281
209
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* If not supported, it fetches contractExchange rates and maps them from token/fallback-currency
|
|
285
|
-
* to token/nativeCurrency.
|
|
210
|
+
* Uses the token prices service to retrieve exchange rates for tokens in a
|
|
211
|
+
* particular currency.
|
|
286
212
|
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
213
|
+
* If the price API does not support the given chain ID, returns an empty
|
|
214
|
+
* object.
|
|
215
|
+
*
|
|
216
|
+
* If the price API does not support the given currency, retrieves exchange
|
|
217
|
+
* rates in a known currency instead, then converts those rates using the
|
|
218
|
+
* exchange rate between the known currency and desired currency.
|
|
219
|
+
*
|
|
220
|
+
* @param args - The arguments to this function.
|
|
221
|
+
* @param args.tokenContractAddresses - Contract addresses for tokens.
|
|
222
|
+
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
|
|
223
|
+
* @param args.nativeCurrency - The native currency in which to request
|
|
224
|
+
* exchange rates.
|
|
225
|
+
* @returns A map from token contract address to its exchange rate in the
|
|
226
|
+
* native currency, or an empty map if no exchange rates can be obtained for
|
|
227
|
+
* the chain ID.
|
|
293
228
|
*/
|
|
294
|
-
fetchAndMapExchangeRates(
|
|
229
|
+
fetchAndMapExchangeRates({ tokenContractAddresses, chainId, nativeCurrency, }) {
|
|
295
230
|
return __awaiter(this, void 0, void 0, function* () {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
// If it is we can do a simple fetch against the CoinGecko API
|
|
301
|
-
const prices = yield this.fetchExchangeRate(chainSlug, nativeCurrency, tokenAddresses);
|
|
302
|
-
tokenAddresses.forEach((tokenAddress) => {
|
|
303
|
-
const price = prices[tokenAddress.toLowerCase()];
|
|
304
|
-
contractExchangeRates[(0, controller_utils_1.toChecksumHexAddress)(tokenAddress)] = price
|
|
305
|
-
? price[nativeCurrency.toLowerCase()]
|
|
306
|
-
: 0;
|
|
307
|
-
});
|
|
231
|
+
if (!__classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").validateChainIdSupported(chainId)) {
|
|
232
|
+
return tokenContractAddresses.reduce((obj, tokenContractAddress) => {
|
|
233
|
+
return Object.assign(Object.assign({}, obj), { [tokenContractAddress]: undefined });
|
|
234
|
+
}, {});
|
|
308
235
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
[
|
|
316
|
-
tokenExchangeRates,
|
|
317
|
-
{ conversionRate: vsCurrencyToNativeCurrencyConversionRate },
|
|
318
|
-
] = yield Promise.all([
|
|
319
|
-
this.fetchExchangeRate(chainSlug, controller_utils_1.FALL_BACK_VS_CURRENCY, tokenAddresses),
|
|
320
|
-
(0, crypto_compare_1.fetchExchangeRate)(nativeCurrency, controller_utils_1.FALL_BACK_VS_CURRENCY, false),
|
|
321
|
-
]);
|
|
322
|
-
}
|
|
323
|
-
catch (error) {
|
|
324
|
-
if (error instanceof Error &&
|
|
325
|
-
error.message.includes('market does not exist for this coin pair')) {
|
|
326
|
-
return {};
|
|
327
|
-
}
|
|
328
|
-
throw error;
|
|
329
|
-
}
|
|
330
|
-
for (const [tokenAddress, conversion] of Object.entries(tokenExchangeRates)) {
|
|
331
|
-
const tokenToVsCurrencyConversionRate = conversion[controller_utils_1.FALL_BACK_VS_CURRENCY.toLowerCase()];
|
|
332
|
-
contractExchangeRates[(0, controller_utils_1.toChecksumHexAddress)(tokenAddress)] =
|
|
333
|
-
tokenToVsCurrencyConversionRate *
|
|
334
|
-
vsCurrencyToNativeCurrencyConversionRate;
|
|
335
|
-
}
|
|
236
|
+
if (__classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").validateCurrencySupported(nativeCurrency)) {
|
|
237
|
+
return yield __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency).call(this, {
|
|
238
|
+
tokenContractAddresses,
|
|
239
|
+
chainId,
|
|
240
|
+
nativeCurrency,
|
|
241
|
+
});
|
|
336
242
|
}
|
|
337
|
-
return
|
|
243
|
+
return yield __classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency).call(this, {
|
|
244
|
+
tokenContractAddresses,
|
|
245
|
+
nativeCurrency,
|
|
246
|
+
});
|
|
338
247
|
});
|
|
339
248
|
}
|
|
340
249
|
/**
|
|
@@ -351,13 +260,13 @@ class TokenRatesController extends polling_controller_1.PollingControllerV1 {
|
|
|
351
260
|
yield this.updateExchangeRatesByChainId({
|
|
352
261
|
chainId: networkClient.configuration.chainId,
|
|
353
262
|
nativeCurrency: networkClient.configuration.ticker,
|
|
354
|
-
|
|
263
|
+
tokenContractAddresses: options.tokenAddresses,
|
|
355
264
|
});
|
|
356
265
|
});
|
|
357
266
|
}
|
|
358
267
|
}
|
|
359
268
|
exports.TokenRatesController = TokenRatesController;
|
|
360
|
-
_TokenRatesController_pollState = new WeakMap(), _TokenRatesController_instances = new WeakSet(), _TokenRatesController_updateTokenList = function _TokenRatesController_updateTokenList() {
|
|
269
|
+
_TokenRatesController_pollState = new WeakMap(), _TokenRatesController_tokenPricesService = new WeakMap(), _TokenRatesController_instances = new WeakSet(), _TokenRatesController_updateTokenList = function _TokenRatesController_updateTokenList() {
|
|
361
270
|
var _a, _b;
|
|
362
271
|
const { allTokens, allDetectedTokens } = this.config;
|
|
363
272
|
const tokens = ((_a = allTokens[this.config.chainId]) === null || _a === void 0 ? void 0 : _a[this.config.selectedAddress]) || [];
|
|
@@ -377,6 +286,37 @@ _TokenRatesController_pollState = new WeakMap(), _TokenRatesController_instances
|
|
|
377
286
|
__classPrivateFieldGet(this, _TokenRatesController_instances, "m", _TokenRatesController_poll).call(this);
|
|
378
287
|
}, this.config.interval);
|
|
379
288
|
});
|
|
289
|
+
}, _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency = function _TokenRatesController_fetchAndMapExchangeRatesForSupportedNativeCurrency({ tokenContractAddresses, chainId, nativeCurrency, }) {
|
|
290
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
const tokenPricesByTokenContractAddress = yield __classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").fetchTokenPrices({
|
|
292
|
+
tokenContractAddresses,
|
|
293
|
+
chainId,
|
|
294
|
+
currency: nativeCurrency,
|
|
295
|
+
});
|
|
296
|
+
return Object.entries(tokenPricesByTokenContractAddress).reduce((obj, [tokenContractAddress, tokenPrice]) => {
|
|
297
|
+
return Object.assign(Object.assign({}, obj), { [(0, controller_utils_1.toChecksumHexAddress)(tokenContractAddress)]: tokenPrice.value });
|
|
298
|
+
}, {});
|
|
299
|
+
});
|
|
300
|
+
}, _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency = function _TokenRatesController_fetchAndMapExchangeRatesForUnsupportedNativeCurrency({ tokenContractAddresses, nativeCurrency, }) {
|
|
301
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
const [tokenPricesByTokenContractAddress, fallbackCurrencyToNativeCurrencyConversionRate,] = yield Promise.all([
|
|
303
|
+
__classPrivateFieldGet(this, _TokenRatesController_tokenPricesService, "f").fetchTokenPrices({
|
|
304
|
+
tokenContractAddresses,
|
|
305
|
+
currency: controller_utils_1.FALL_BACK_VS_CURRENCY,
|
|
306
|
+
chainId: this.config.chainId,
|
|
307
|
+
}),
|
|
308
|
+
getCurrencyConversionRate({
|
|
309
|
+
from: controller_utils_1.FALL_BACK_VS_CURRENCY,
|
|
310
|
+
to: nativeCurrency,
|
|
311
|
+
}),
|
|
312
|
+
]);
|
|
313
|
+
if (fallbackCurrencyToNativeCurrencyConversionRate === null) {
|
|
314
|
+
return {};
|
|
315
|
+
}
|
|
316
|
+
return Object.entries(tokenPricesByTokenContractAddress).reduce((obj, [tokenContractAddress, tokenPrice]) => {
|
|
317
|
+
return Object.assign(Object.assign({}, obj), { [(0, controller_utils_1.toChecksumHexAddress)(tokenContractAddress)]: tokenPrice.value * fallbackCurrencyToNativeCurrencyConversionRate });
|
|
318
|
+
}, {});
|
|
319
|
+
});
|
|
380
320
|
};
|
|
381
321
|
exports.default = TokenRatesController;
|
|
382
322
|
//# sourceMappingURL=TokenRatesController.js.map
|