forked from MetaMask/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTokenRatesController.ts
More file actions
624 lines (572 loc) · 18.6 KB
/
TokenRatesController.ts
File metadata and controls
624 lines (572 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import type { BaseConfig, BaseState } from '@metamask/base-controller';
import {
safelyExecute,
toChecksumHexAddress,
FALL_BACK_VS_CURRENCY,
toHex,
} from '@metamask/controller-utils';
import type {
NetworkClientId,
NetworkController,
NetworkState,
} from '@metamask/network-controller';
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import { createDeferredPromise, type Hex } from '@metamask/utils';
import { isEqual } from 'lodash';
import { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';
import { fetchExchangeRate as fetchNativeCurrencyExchangeRate } from './crypto-compare-service';
import type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';
import { ZERO_ADDRESS } from './token-prices-service/codefi-v2';
import type { TokensControllerState } from './TokensController';
/**
* @type Token
*
* Token representation
* @property address - Hex address of the token contract
* @property decimals - Number of decimals the token uses
* @property symbol - Symbol of the token
* @property image - Image of the token, url or bit32 image
*/
export type Token = {
address: string;
decimals: number;
symbol: string;
aggregators?: string[];
image?: string;
hasBalanceError?: boolean;
isERC721?: boolean;
name?: string;
};
/**
* @type TokenRatesConfig
*
* Token rates controller configuration
* @property interval - Polling interval used to fetch new token rates
* @property nativeCurrency - Current native currency selected to use base of rates
* @property chainId - Current network chainId
* @property tokens - List of tokens to track exchange rates for
* @property threshold - Threshold to invalidate the supportedChains
*/
// This interface was created before this ESLint rule was added.
// Convert to a `type` in a future major version.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface TokenRatesConfig extends BaseConfig {
interval: number;
nativeCurrency: string;
chainId: Hex;
selectedAddress: string;
allTokens: { [chainId: Hex]: { [key: string]: Token[] } };
allDetectedTokens: { [chainId: Hex]: { [key: string]: Token[] } };
threshold: number;
}
// This interface was created before this ESLint rule was added.
// Convert to a `type` in a future major version.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface ContractExchangeRates {
[address: string]: number | undefined;
}
type MarketDataDetails = {
tokenAddress: `0x${string}`;
currency: string;
allTimeHigh: number;
allTimeLow: number;
circulatingSupply: number;
dilutedMarketCap: number;
high1d: number;
low1d: number;
marketCap: number;
marketCapPercentChange1d: number;
price: number;
priceChange1d: number;
pricePercentChange1d: number;
pricePercentChange1h: number;
pricePercentChange1y: number;
pricePercentChange7d: number;
pricePercentChange14d: number;
pricePercentChange30d: number;
pricePercentChange200d: number;
totalVolume: number;
};
export type ContractMarketData = Record<Hex, MarketDataDetails>;
enum PollState {
Active = 'Active',
Inactive = 'Inactive',
}
/**
* @type TokenRatesState
*
* Token rates controller state
* @property marketData - Market data for tokens, keyed by chain ID and then token contract address.
*/
// This interface was created before this ESLint rule was added.
// Convert to a `type` in a future major version.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface TokenRatesState extends BaseState {
marketData: Record<Hex, Record<Hex, MarketDataDetails>>;
}
/**
* Uses the CryptoCompare API to fetch the exchange rate between one currency
* and another, i.e., the multiplier to apply the amount of one currency in
* order to convert it to another.
*
* @param args - The arguments to this function.
* @param args.from - The currency to convert from.
* @param args.to - The currency to convert to.
* @returns The exchange rate between `fromCurrency` to `toCurrency` if one
* exists, or null if one does not.
*/
async function getCurrencyConversionRate({
from,
to,
}: {
from: string;
to: string;
}) {
const includeUSDRate = false;
try {
const result = await fetchNativeCurrencyExchangeRate(
to,
from,
includeUSDRate,
);
return result.conversionRate;
} catch (error) {
if (
error instanceof Error &&
error.message.includes('market does not exist for this coin pair')
) {
return null;
}
throw error;
}
}
/**
* Controller that passively polls on a set interval for token-to-fiat exchange rates
* for tokens stored in the TokensController
*/
export class TokenRatesController extends StaticIntervalPollingControllerV1<
TokenRatesConfig,
TokenRatesState
> {
private handle?: ReturnType<typeof setTimeout>;
#pollState = PollState.Inactive;
#tokenPricesService: AbstractTokenPricesService;
#inProcessExchangeRateUpdates: Record<`${Hex}:${string}`, Promise<void>> = {};
/**
* Name of this controller used during composition
*/
override name = 'TokenRatesController' as const;
private readonly getNetworkClientById: NetworkController['getNetworkClientById'];
/**
* Creates a TokenRatesController instance.
*
* @param options - The controller options.
* @param options.interval - The polling interval in ms
* @param options.threshold - The duration in ms before metadata fetched from CoinGecko is considered stale
* @param options.getNetworkClientById - Gets the network client with the given id from the NetworkController.
* @param options.chainId - The chain ID of the current network.
* @param options.ticker - The ticker for the current network.
* @param options.selectedAddress - The current selected address.
* @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
* @param options.onTokensStateChange - Allows subscribing to token controller state changes.
* @param options.onNetworkStateChange - Allows subscribing to network state changes.
* @param options.tokenPricesService - An object in charge of retrieving token prices.
* @param config - Initial options used to configure this controller.
* @param state - Initial state to set on this controller.
*/
constructor(
{
interval = 3 * 60 * 1000,
threshold = 6 * 60 * 60 * 1000,
getNetworkClientById,
chainId: initialChainId,
ticker: initialTicker,
selectedAddress: initialSelectedAddress,
onPreferencesStateChange,
onTokensStateChange,
onNetworkStateChange,
tokenPricesService,
}: {
interval?: number;
threshold?: number;
getNetworkClientById: NetworkController['getNetworkClientById'];
chainId: Hex;
ticker: string;
selectedAddress: string;
onPreferencesStateChange: (
listener: (preferencesState: PreferencesState) => void,
) => void;
onTokensStateChange: (
listener: (tokensState: TokensControllerState) => void,
) => void;
onNetworkStateChange: (
listener: (networkState: NetworkState) => void,
) => void;
tokenPricesService: AbstractTokenPricesService;
},
config?: Partial<TokenRatesConfig>,
state?: Partial<TokenRatesState>,
) {
super(config, state);
this.defaultConfig = {
interval,
threshold,
disabled: false,
nativeCurrency: initialTicker,
chainId: initialChainId,
selectedAddress: initialSelectedAddress,
allTokens: {}, // TODO: initialize these correctly, maybe as part of BaseControllerV2 migration
allDetectedTokens: {},
};
this.defaultState = {
marketData: {},
};
this.initialize();
this.setIntervalLength(interval);
this.getNetworkClientById = getNetworkClientById;
this.#tokenPricesService = tokenPricesService;
if (config?.disabled) {
this.configure({ disabled: true }, false, false);
}
onPreferencesStateChange(async ({ selectedAddress }) => {
if (this.config.selectedAddress !== selectedAddress) {
this.configure({ selectedAddress });
if (this.#pollState === PollState.Active) {
await this.updateExchangeRates();
}
}
});
onTokensStateChange(async ({ allTokens, allDetectedTokens }) => {
const previousTokenAddresses = this.#getTokenAddresses(
this.config.chainId,
);
this.configure({ allTokens, allDetectedTokens });
const newTokenAddresses = this.#getTokenAddresses(this.config.chainId);
if (
!isEqual(previousTokenAddresses, newTokenAddresses) &&
this.#pollState === PollState.Active
) {
await this.updateExchangeRates();
}
});
onNetworkStateChange(async ({ selectedNetworkClientId }) => {
const selectedNetworkClient = getNetworkClientById(
selectedNetworkClientId,
);
const { chainId, ticker } = selectedNetworkClient.configuration;
if (
this.config.chainId !== chainId ||
this.config.nativeCurrency !== ticker
) {
this.update({ ...this.defaultState });
this.configure({ chainId, nativeCurrency: ticker });
if (this.#pollState === PollState.Active) {
await this.updateExchangeRates();
}
}
});
}
/**
* Get the user's tokens for the given chain.
*
* @param chainId - The chain ID.
* @returns The list of tokens addresses for the current chain
*/
#getTokenAddresses(chainId: Hex): Hex[] {
const { allTokens, allDetectedTokens } = this.config;
const tokens = allTokens[chainId]?.[this.config.selectedAddress] || [];
const detectedTokens =
allDetectedTokens[chainId]?.[this.config.selectedAddress] || [];
return [
...new Set(
[...tokens, ...detectedTokens].map((token) =>
toHex(toChecksumHexAddress(token.address)),
),
),
].sort();
}
/**
* Start (or restart) polling.
*/
async start() {
this.#stopPoll();
this.#pollState = PollState.Active;
await this.#poll();
}
/**
* Stop polling.
*/
stop() {
this.#stopPoll();
this.#pollState = PollState.Inactive;
}
/**
* Clear the active polling timer, if present.
*/
#stopPoll() {
if (this.handle) {
clearTimeout(this.handle);
}
}
/**
* Poll for exchange rate updates.
*/
async #poll() {
await safelyExecute(() => this.updateExchangeRates());
// Poll using recursive `setTimeout` instead of `setInterval` so that
// requests don't stack if they take longer than the polling interval
this.handle = setTimeout(() => {
this.#poll();
}, this.config.interval);
}
/**
* Updates exchange rates for all tokens.
*/
async updateExchangeRates() {
const { chainId, nativeCurrency } = this.config;
await this.updateExchangeRatesByChainId({
chainId,
nativeCurrency,
});
}
/**
* Updates exchange rates for all tokens.
*
* @param options - The options to fetch exchange rates.
* @param options.chainId - The chain ID.
* @param options.nativeCurrency - The ticker for the chain.
*/
async updateExchangeRatesByChainId({
chainId,
nativeCurrency,
}: {
chainId: Hex;
nativeCurrency: string;
}) {
if (this.disabled) {
return;
}
const tokenAddresses = this.#getTokenAddresses(chainId);
const updateKey: `${Hex}:${string}` = `${chainId}:${nativeCurrency}`;
if (updateKey in this.#inProcessExchangeRateUpdates) {
// This prevents redundant updates
// This promise is resolved after the in-progress update has finished,
// and state has been updated.
await this.#inProcessExchangeRateUpdates[updateKey];
return;
}
const {
promise: inProgressUpdate,
resolve: updateSucceeded,
reject: updateFailed,
} = createDeferredPromise({ suppressUnhandledRejection: true });
this.#inProcessExchangeRateUpdates[updateKey] = inProgressUpdate;
try {
const contractInformations = await this.#fetchAndMapExchangeRates({
tokenAddresses,
chainId,
nativeCurrency,
});
const marketData = {
[chainId]: {
...(contractInformations ?? {}),
},
};
this.update({
marketData,
});
updateSucceeded();
} catch (error: unknown) {
updateFailed(error);
throw error;
} finally {
delete this.#inProcessExchangeRateUpdates[updateKey];
}
}
/**
* Uses the token prices service to retrieve exchange rates for tokens in a
* particular currency.
*
* If the price API does not support the given chain ID, returns an empty
* object.
*
* If the price API does not support the given currency, retrieves exchange
* rates in a known currency instead, then converts those rates using the
* exchange rate between the known currency and desired currency.
*
* @param args - The arguments to this function.
* @param args.tokenAddresses - Addresses for tokens.
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
* @param args.nativeCurrency - The native currency in which to request
* exchange rates.
* @returns A map from token address to its exchange rate in the native
* currency, or an empty map if no exchange rates can be obtained for the
* chain ID.
*/
async #fetchAndMapExchangeRates({
tokenAddresses,
chainId,
nativeCurrency,
}: {
tokenAddresses: Hex[];
chainId: Hex;
nativeCurrency: string;
}): Promise<ContractMarketData> {
if (!this.#tokenPricesService.validateChainIdSupported(chainId)) {
return tokenAddresses.reduce((obj, tokenAddress) => {
obj = {
...obj,
[tokenAddress]: undefined,
};
return obj;
}, {});
}
if (this.#tokenPricesService.validateCurrencySupported(nativeCurrency)) {
return await this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenAddresses,
chainId,
nativeCurrency,
});
}
return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
tokenAddresses,
nativeCurrency,
});
}
/**
* Updates token rates for the given networkClientId
*
* @param networkClientId - The network client ID used to get a ticker value.
* @returns The controller state.
*/
async _executePoll(networkClientId: NetworkClientId): Promise<void> {
const networkClient = this.getNetworkClientById(networkClientId);
await this.updateExchangeRatesByChainId({
chainId: networkClient.configuration.chainId,
nativeCurrency: networkClient.configuration.ticker,
});
}
/**
* Retrieves prices in the given currency for the given tokens on the given
* chain. Ensures that token addresses are checksum addresses.
*
* @param args - The arguments to this function.
* @param args.tokenAddresses - Addresses for tokens.
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
* @param args.nativeCurrency - The native currency in which to request
* prices.
* @returns A map of the token addresses (as checksums) to their prices in the
* native currency.
*/
async #fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenAddresses,
chainId,
nativeCurrency,
}: {
tokenAddresses: Hex[];
chainId: Hex;
nativeCurrency: string;
}): Promise<ContractMarketData> {
let contractNativeInformations;
const tokenPricesByTokenAddress = await reduceInBatchesSerially<
Hex,
Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>
>({
values: [...tokenAddresses].sort(),
batchSize: TOKEN_PRICES_BATCH_SIZE,
eachBatch: async (allTokenPricesByTokenAddress, batch) => {
const tokenPricesByTokenAddressForBatch =
await this.#tokenPricesService.fetchTokenPrices({
tokenAddresses: batch,
chainId,
currency: nativeCurrency,
});
return {
...allTokenPricesByTokenAddress,
...tokenPricesByTokenAddressForBatch,
};
},
initialResult: {},
});
contractNativeInformations = tokenPricesByTokenAddress;
// fetch for native token
if (tokenAddresses.length === 0) {
const contractNativeInformationsNative =
await this.#tokenPricesService.fetchTokenPrices({
tokenAddresses: [],
chainId,
currency: nativeCurrency,
});
contractNativeInformations = {
[ZERO_ADDRESS]: {
currency: nativeCurrency,
...contractNativeInformationsNative[ZERO_ADDRESS],
},
};
}
return Object.entries(contractNativeInformations).reduce(
(obj, [tokenAddress, token]) => {
obj = {
...obj,
[tokenAddress]: { ...token },
};
return obj;
},
{},
);
}
/**
* If the price API does not support a given native currency, then we need to
* convert it to a fallback currency and feed that currency into the price
* API, then convert the prices to our desired native currency.
*
* @param args - The arguments to this function.
* @param args.tokenAddresses - Addresses for tokens.
* @param args.nativeCurrency - The native currency in which to request
* prices.
* @returns A map of the token addresses (as checksums) to their prices in the
* native currency.
*/
async #fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
tokenAddresses,
nativeCurrency,
}: {
tokenAddresses: Hex[];
nativeCurrency: string;
}): Promise<ContractMarketData> {
const [
contractExchangeInformations,
fallbackCurrencyToNativeCurrencyConversionRate,
] = await Promise.all([
this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenAddresses,
chainId: this.config.chainId,
nativeCurrency: FALL_BACK_VS_CURRENCY,
}),
getCurrencyConversionRate({
from: FALL_BACK_VS_CURRENCY,
to: nativeCurrency,
}),
]);
if (fallbackCurrencyToNativeCurrencyConversionRate === null) {
return {};
}
const updatedContractExchangeRates = Object.entries(
contractExchangeInformations,
).reduce((acc, [tokenAddress, token]) => {
acc = {
...acc,
[tokenAddress]: {
...token,
price: token.price
? token.price * fallbackCurrencyToNativeCurrencyConversionRate
: undefined,
},
};
return acc;
}, {});
return updatedContractExchangeRates;
}
}
export default TokenRatesController;