Skip to content

Commit aea9bac

Browse files
authored
Update 'token contract address' -> 'token address' (#3653)
A recent change to update TokenRatesController from the CoinGecko API to our own API added code which refers to "token contract addresses". While this is a more accurate name for the hexadecimal string you can use to refer to a token, we consistently use the name "token address" in the code rather than "token contract address", so update these references to be consistent with existing code.
1 parent e1ff173 commit aea9bac

File tree

6 files changed

+106
-114
lines changed

6 files changed

+106
-114
lines changed

packages/assets-controllers/CHANGELOG.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313
- **BREAKING:** TokenRatesController now takes a required argument `tokenPricesService` ([#3600](https://github.com/MetaMask/core/pull/3600))
1414
- This object is responsible for fetching the prices for tokens held by this controller.
15-
- **BREAKING:** Update signature of `TokenRatesController.updateExchangeRatesByChainId` ([#3600](https://github.com/MetaMask/core/pull/3600))
16-
- Rename `tokenAddresses` argument to `tokenContractAddresses`
17-
- Change the type of `tokenContractAddresses` from `string[]` to `Hex[]`
18-
- **BREAKING:** Change signature of `TokenRatesController.fetchAndMapExchangeRates` ([#3600](https://github.com/MetaMask/core/pull/3600))
19-
- This method now takes an object with shape `{ tokenContractAddresses: Hex[]; chainId: Hex; nativeCurrency: string; }` rather than positional arguments
15+
- **BREAKING:** Update signature of `TokenRatesController.updateExchangeRatesByChainId` ([#3600](https://github.com/MetaMask/core/pull/3600), [#3653](https://github.com/MetaMask/core/pull/3653))
16+
- Change the type of `tokenAddresses` from `string[]` to `Hex[]`
17+
- **BREAKING:** Change signature of `TokenRatesController.fetchAndMapExchangeRates` ([#3600](https://github.com/MetaMask/core/pull/3600), [#3653](https://github.com/MetaMask/core/pull/3653))
18+
- This method now takes an object with shape `{ tokenAddresses: Hex[]; chainId: Hex; nativeCurrency: string; }` rather than positional arguments
2019
- Update TokenListController to fetch prefiltered set of tokens from the API, reducing response data and removing the need for filtering logic ([#2054](https://github.com/MetaMask/core/pull/2054))
2120
- Update TokenRatesController to request token rates from the Price API in batches of 100 ([#3650](https://github.com/MetaMask/core/pull/3650))
2221

packages/assets-controllers/src/TokenRatesController.test.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { advanceTime } from '../../../tests/helpers';
1414
import type {
1515
AbstractTokenPricesService,
1616
TokenPrice,
17-
TokenPricesByTokenContractAddress,
17+
TokenPricesByTokenAddress,
1818
} from './token-prices-service/abstract-token-prices-service';
1919
import type { TokenBalancesState } from './TokenBalancesController';
2020
import { TokenRatesController } from './TokenRatesController';
@@ -1727,12 +1727,12 @@ describe('TokenRatesController', () => {
17271727
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(2);
17281728
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(1, {
17291729
chainId,
1730-
tokenContractAddresses: tokenAddresses.slice(0, 100),
1730+
tokenAddresses: tokenAddresses.slice(0, 100),
17311731
currency: ticker,
17321732
});
17331733
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(2, {
17341734
chainId,
1735-
tokenContractAddresses: tokenAddresses.slice(100),
1735+
tokenAddresses: tokenAddresses.slice(100),
17361736
currency: ticker,
17371737
});
17381738
},
@@ -1748,12 +1748,12 @@ describe('TokenRatesController', () => {
17481748
fetchTokenPrices: jest.fn().mockResolvedValue({
17491749
[tokenAddresses[0]]: {
17501750
currency: 'ETH',
1751-
tokenContractAddress: tokenAddresses[0],
1751+
tokenAddress: tokenAddresses[0],
17521752
value: 0.001,
17531753
},
17541754
[tokenAddresses[1]]: {
17551755
currency: 'ETH',
1756-
tokenContractAddress: tokenAddresses[1],
1756+
tokenAddress: tokenAddresses[1],
17571757
value: 0.002,
17581758
},
17591759
}),
@@ -1817,12 +1817,12 @@ describe('TokenRatesController', () => {
18171817
fetchTokenPrices: jest.fn().mockResolvedValue({
18181818
[tokenAddresses[0]]: {
18191819
currency: 'ETH',
1820-
tokenContractAddress: tokenAddresses[0],
1820+
tokenAddress: tokenAddresses[0],
18211821
value: 0.001,
18221822
},
18231823
[tokenAddresses[1]]: {
18241824
currency: 'ETH',
1825-
tokenContractAddress: tokenAddresses[1],
1825+
tokenAddress: tokenAddresses[1],
18261826
value: 0.002,
18271827
},
18281828
}),
@@ -1884,12 +1884,12 @@ describe('TokenRatesController', () => {
18841884
fetchTokenPrices: jest.fn().mockResolvedValue({
18851885
[tokenAddresses[0]]: {
18861886
currency: 'ETH',
1887-
tokenContractAddress: tokenAddresses[0],
1887+
tokenAddress: tokenAddresses[0],
18881888
value: 0.001,
18891889
},
18901890
[tokenAddresses[1]]: {
18911891
currency: 'ETH',
1892-
tokenContractAddress: tokenAddresses[1],
1892+
tokenAddress: tokenAddresses[1],
18931893
value: 0.002,
18941894
},
18951895
}),
@@ -2007,12 +2007,12 @@ describe('TokenRatesController', () => {
20072007
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(2);
20082008
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(1, {
20092009
chainId,
2010-
tokenContractAddresses: tokenAddresses.slice(0, 100),
2010+
tokenAddresses: tokenAddresses.slice(0, 100),
20112011
currency: 'ETH',
20122012
});
20132013
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(2, {
20142014
chainId,
2015-
tokenContractAddresses: tokenAddresses.slice(100),
2015+
tokenAddresses: tokenAddresses.slice(100),
20162016
currency: 'ETH',
20172017
});
20182018
},
@@ -2028,12 +2028,12 @@ describe('TokenRatesController', () => {
20282028
fetchTokenPrices: jest.fn().mockResolvedValue({
20292029
[tokenAddresses[0]]: {
20302030
currency: 'ETH',
2031-
tokenContractAddress: tokenAddresses[0],
2031+
tokenAddress: tokenAddresses[0],
20322032
value: 0.001,
20332033
},
20342034
[tokenAddresses[1]]: {
20352035
currency: 'ETH',
2036-
tokenContractAddress: tokenAddresses[1],
2036+
tokenAddress: tokenAddresses[1],
20372037
value: 0.002,
20382038
},
20392039
}),
@@ -2100,12 +2100,12 @@ describe('TokenRatesController', () => {
21002100
const fetchTokenPricesMock = jest.fn().mockResolvedValue({
21012101
[tokenAddresses[0]]: {
21022102
currency: 'ETH',
2103-
tokenContractAddress: tokenAddresses[0],
2103+
tokenAddress: tokenAddresses[0],
21042104
value: 0.001,
21052105
},
21062106
[tokenAddresses[1]]: {
21072107
currency: 'ETH',
2108-
tokenContractAddress: tokenAddresses[1],
2108+
tokenAddress: tokenAddresses[1],
21092109
value: 0.002,
21102110
},
21112111
});
@@ -2375,33 +2375,33 @@ function buildMockTokenPricesService(
23752375
* price of each given token is incremented by one.
23762376
*
23772377
* @param args - The arguments to this function.
2378-
* @param args.tokenContractAddresses - The token contract addresses.
2378+
* @param args.tokenAddresses - The token addresses.
23792379
* @param args.currency - The currency.
23802380
* @returns The token prices.
23812381
*/
23822382
async function fetchTokenPricesWithIncreasingPriceForEachToken<
23832383
TokenAddress extends Hex,
23842384
Currency extends string,
23852385
>({
2386-
tokenContractAddresses,
2386+
tokenAddresses,
23872387
currency,
23882388
}: {
2389-
tokenContractAddresses: TokenAddress[];
2389+
tokenAddresses: TokenAddress[];
23902390
currency: Currency;
23912391
}) {
2392-
return tokenContractAddresses.reduce<
2393-
Partial<TokenPricesByTokenContractAddress<TokenAddress, Currency>>
2394-
>((obj, tokenContractAddress, i) => {
2392+
return tokenAddresses.reduce<
2393+
Partial<TokenPricesByTokenAddress<TokenAddress, Currency>>
2394+
>((obj, tokenAddress, i) => {
23952395
const tokenPrice: TokenPrice<TokenAddress, Currency> = {
2396-
tokenContractAddress,
2396+
tokenAddress,
23972397
value: (i + 1) / 1000,
23982398
currency,
23992399
};
24002400
return {
24012401
...obj,
2402-
[tokenContractAddress]: tokenPrice,
2402+
[tokenAddress]: tokenPrice,
24032403
};
2404-
}, {}) as TokenPricesByTokenContractAddress<TokenAddress, Currency>;
2404+
}, {}) as TokenPricesByTokenAddress<TokenAddress, Currency>;
24052405
}
24062406

24072407
/**

packages/assets-controllers/src/TokenRatesController.ts

+34-34
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
364364
return;
365365
}
366366

367-
const tokenContractAddresses = this.#getTokenAddresses(chainId);
368-
if (tokenContractAddresses.length === 0) {
367+
const tokenAddresses = this.#getTokenAddresses(chainId);
368+
if (tokenAddresses.length === 0) {
369369
return;
370370
}
371371

@@ -387,7 +387,7 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
387387

388388
try {
389389
const newContractExchangeRates = await this.#fetchAndMapExchangeRates({
390-
tokenContractAddresses,
390+
tokenAddresses,
391391
chainId,
392392
nativeCurrency,
393393
});
@@ -437,42 +437,42 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
437437
* exchange rate between the known currency and desired currency.
438438
*
439439
* @param args - The arguments to this function.
440-
* @param args.tokenContractAddresses - Contract addresses for tokens.
440+
* @param args.tokenAddresses - Addresses for tokens.
441441
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
442442
* @param args.nativeCurrency - The native currency in which to request
443443
* exchange rates.
444-
* @returns A map from token contract address to its exchange rate in the
445-
* native currency, or an empty map if no exchange rates can be obtained for
446-
* the chain ID.
444+
* @returns A map from token address to its exchange rate in the native
445+
* currency, or an empty map if no exchange rates can be obtained for the
446+
* chain ID.
447447
*/
448448
async #fetchAndMapExchangeRates({
449-
tokenContractAddresses,
449+
tokenAddresses,
450450
chainId,
451451
nativeCurrency,
452452
}: {
453-
tokenContractAddresses: Hex[];
453+
tokenAddresses: Hex[];
454454
chainId: Hex;
455455
nativeCurrency: string;
456456
}): Promise<ContractExchangeRates> {
457457
if (!this.#tokenPricesService.validateChainIdSupported(chainId)) {
458-
return tokenContractAddresses.reduce((obj, tokenContractAddress) => {
458+
return tokenAddresses.reduce((obj, tokenAddress) => {
459459
return {
460460
...obj,
461-
[tokenContractAddress]: undefined,
461+
[tokenAddress]: undefined,
462462
};
463463
}, {});
464464
}
465465

466466
if (this.#tokenPricesService.validateCurrencySupported(nativeCurrency)) {
467467
return await this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
468-
tokenContractAddresses,
468+
tokenAddresses,
469469
chainId,
470470
nativeCurrency,
471471
});
472472
}
473473

474474
return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
475-
tokenContractAddresses,
475+
tokenAddresses,
476476
nativeCurrency,
477477
});
478478
}
@@ -496,49 +496,49 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
496496
* chain. Ensures that token addresses are checksum addresses.
497497
*
498498
* @param args - The arguments to this function.
499-
* @param args.tokenContractAddresses - Contract addresses for tokens.
499+
* @param args.tokenAddresses - Addresses for tokens.
500500
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
501501
* @param args.nativeCurrency - The native currency in which to request
502502
* prices.
503503
* @returns A map of the token addresses (as checksums) to their prices in the
504504
* native currency.
505505
*/
506506
async #fetchAndMapExchangeRatesForSupportedNativeCurrency({
507-
tokenContractAddresses,
507+
tokenAddresses,
508508
chainId,
509509
nativeCurrency,
510510
}: {
511-
tokenContractAddresses: Hex[];
511+
tokenAddresses: Hex[];
512512
chainId: Hex;
513513
nativeCurrency: string;
514514
}): Promise<ContractExchangeRates> {
515-
const tokenPricesByTokenContractAddress = await reduceInBatchesSerially<
515+
const tokenPricesByTokenAddress = await reduceInBatchesSerially<
516516
Hex,
517517
Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>
518518
>({
519-
values: tokenContractAddresses,
519+
values: tokenAddresses,
520520
batchSize: TOKEN_PRICES_BATCH_SIZE,
521-
eachBatch: async (allTokenPricesByTokenContractAddress, batch) => {
522-
const tokenPricesByTokenContractAddressForBatch =
521+
eachBatch: async (allTokenPricesByTokenAddress, batch) => {
522+
const tokenPricesByTokenAddressForBatch =
523523
await this.#tokenPricesService.fetchTokenPrices({
524-
tokenContractAddresses: batch,
524+
tokenAddresses: batch,
525525
chainId,
526526
currency: nativeCurrency,
527527
});
528528

529529
return {
530-
...allTokenPricesByTokenContractAddress,
531-
...tokenPricesByTokenContractAddressForBatch,
530+
...allTokenPricesByTokenAddress,
531+
...tokenPricesByTokenAddressForBatch,
532532
};
533533
},
534534
initialResult: {},
535535
});
536536

537-
return Object.entries(tokenPricesByTokenContractAddress).reduce(
538-
(obj, [tokenContractAddress, tokenPrice]) => {
537+
return Object.entries(tokenPricesByTokenAddress).reduce(
538+
(obj, [tokenAddress, tokenPrice]) => {
539539
return {
540540
...obj,
541-
[tokenContractAddress]: tokenPrice.value,
541+
[tokenAddress]: tokenPrice.value,
542542
};
543543
},
544544
{},
@@ -551,25 +551,25 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
551551
* API, then convert the prices to our desired native currency.
552552
*
553553
* @param args - The arguments to this function.
554-
* @param args.tokenContractAddresses - The contract addresses for the tokens you
555-
* want to retrieve prices for.
556-
* @param args.nativeCurrency - The currency you want the prices to be in.
554+
* @param args.tokenAddresses - Addresses for tokens.
555+
* @param args.nativeCurrency - The native currency in which to request
556+
* prices.
557557
* @returns A map of the token addresses (as checksums) to their prices in the
558558
* native currency.
559559
*/
560560
async #fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
561-
tokenContractAddresses,
561+
tokenAddresses,
562562
nativeCurrency,
563563
}: {
564-
tokenContractAddresses: Hex[];
564+
tokenAddresses: Hex[];
565565
nativeCurrency: string;
566566
}): Promise<ContractExchangeRates> {
567567
const [
568568
contractExchangeRates,
569569
fallbackCurrencyToNativeCurrencyConversionRate,
570570
] = await Promise.all([
571571
this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
572-
tokenContractAddresses,
572+
tokenAddresses,
573573
chainId: this.config.chainId,
574574
nativeCurrency: FALL_BACK_VS_CURRENCY,
575575
}),
@@ -584,10 +584,10 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
584584
}
585585

586586
return Object.entries(contractExchangeRates).reduce(
587-
(obj, [tokenContractAddress, tokenValue]) => {
587+
(obj, [tokenAddress, tokenValue]) => {
588588
return {
589589
...obj,
590-
[tokenContractAddress]: tokenValue
590+
[tokenAddress]: tokenValue
591591
? tokenValue * fallbackCurrencyToNativeCurrencyConversionRate
592592
: undefined,
593593
};

0 commit comments

Comments
 (0)