Skip to content

Commit 228b1b4

Browse files
committed
Update 'token contract address' -> 'token address'
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 1003274 commit 228b1b4

File tree

6 files changed

+97
-105
lines changed

6 files changed

+97
-105
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

2221
### Removed

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { advanceTime } from '../../../tests/helpers';
99
import type {
1010
AbstractTokenPricesService,
1111
TokenPrice,
12-
TokenPricesByTokenContractAddress,
12+
TokenPricesByTokenAddress,
1313
} from './token-prices-service/abstract-token-prices-service';
1414
import type { TokenBalancesState } from './TokenBalancesController';
1515
import { TokenRatesController } from './TokenRatesController';
@@ -1691,12 +1691,12 @@ describe('TokenRatesController', () => {
16911691
fetchTokenPrices: jest.fn().mockResolvedValue({
16921692
[tokenAddresses[0]]: {
16931693
currency: 'ETH',
1694-
tokenContractAddress: tokenAddresses[0],
1694+
tokenAddress: tokenAddresses[0],
16951695
value: 0.001,
16961696
},
16971697
[tokenAddresses[1]]: {
16981698
currency: 'ETH',
1699-
tokenContractAddress: tokenAddresses[1],
1699+
tokenAddress: tokenAddresses[1],
17001700
value: 0.002,
17011701
},
17021702
}),
@@ -1760,12 +1760,12 @@ describe('TokenRatesController', () => {
17601760
fetchTokenPrices: jest.fn().mockResolvedValue({
17611761
[tokenAddresses[0]]: {
17621762
currency: 'ETH',
1763-
tokenContractAddress: tokenAddresses[0],
1763+
tokenAddress: tokenAddresses[0],
17641764
value: 0.001,
17651765
},
17661766
[tokenAddresses[1]]: {
17671767
currency: 'ETH',
1768-
tokenContractAddress: tokenAddresses[1],
1768+
tokenAddress: tokenAddresses[1],
17691769
value: 0.002,
17701770
},
17711771
}),
@@ -1827,12 +1827,12 @@ describe('TokenRatesController', () => {
18271827
fetchTokenPrices: jest.fn().mockResolvedValue({
18281828
[tokenAddresses[0]]: {
18291829
currency: 'ETH',
1830-
tokenContractAddress: tokenAddresses[0],
1830+
tokenAddress: tokenAddresses[0],
18311831
value: 0.001,
18321832
},
18331833
[tokenAddresses[1]]: {
18341834
currency: 'ETH',
1835-
tokenContractAddress: tokenAddresses[1],
1835+
tokenAddress: tokenAddresses[1],
18361836
value: 0.002,
18371837
},
18381838
}),
@@ -1909,12 +1909,12 @@ describe('TokenRatesController', () => {
19091909
fetchTokenPrices: jest.fn().mockResolvedValue({
19101910
[tokenAddresses[0]]: {
19111911
currency: 'ETH',
1912-
tokenContractAddress: tokenAddresses[0],
1912+
tokenAddress: tokenAddresses[0],
19131913
value: 0.001,
19141914
},
19151915
[tokenAddresses[1]]: {
19161916
currency: 'ETH',
1917-
tokenContractAddress: tokenAddresses[1],
1917+
tokenAddress: tokenAddresses[1],
19181918
value: 0.002,
19191919
},
19201920
}),
@@ -2256,31 +2256,31 @@ function buildMockTokenPricesService(
22562256
* price of each given token is incremented by one.
22572257
*
22582258
* @param args - The arguments to this function.
2259-
* @param args.tokenContractAddresses - The token contract addresses.
2259+
* @param args.tokenAddresses - The token addresses.
22602260
* @param args.currency - The currency.
22612261
* @returns The token prices.
22622262
*/
22632263
async function fetchTokenPricesWithIncreasingPriceForEachToken<
22642264
TokenAddress extends Hex,
22652265
Currency extends string,
22662266
>({
2267-
tokenContractAddresses,
2267+
tokenAddresses,
22682268
currency,
22692269
}: {
2270-
tokenContractAddresses: TokenAddress[];
2270+
tokenAddresses: TokenAddress[];
22712271
currency: Currency;
22722272
}) {
2273-
return tokenContractAddresses.reduce<
2274-
Partial<TokenPricesByTokenContractAddress<TokenAddress, Currency>>
2275-
>((obj, tokenContractAddress, i) => {
2273+
return tokenAddresses.reduce<
2274+
Partial<TokenPricesByTokenAddress<TokenAddress, Currency>>
2275+
>((obj, tokenAddress, i) => {
22762276
const tokenPrice: TokenPrice<TokenAddress, Currency> = {
2277-
tokenContractAddress,
2277+
tokenAddress,
22782278
value: (i + 1) / 1000,
22792279
currency,
22802280
};
22812281
return {
22822282
...obj,
2283-
[tokenContractAddress]: tokenPrice,
2283+
[tokenAddress]: tokenPrice,
22842284
};
2285-
}, {}) as TokenPricesByTokenContractAddress<TokenAddress, Currency>;
2285+
}, {}) as TokenPricesByTokenAddress<TokenAddress, Currency>;
22862286
}

packages/assets-controllers/src/TokenRatesController.ts

+31-31
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ export class TokenRatesController extends PollingControllerV1<
357357
return;
358358
}
359359

360-
const tokenContractAddresses = this.#getTokenAddresses(chainId);
361-
if (tokenContractAddresses.length === 0) {
360+
const tokenAddresses = this.#getTokenAddresses(chainId);
361+
if (tokenAddresses.length === 0) {
362362
return;
363363
}
364364

@@ -380,7 +380,7 @@ export class TokenRatesController extends PollingControllerV1<
380380

381381
try {
382382
const newContractExchangeRates = await this.#fetchAndMapExchangeRates({
383-
tokenContractAddresses,
383+
tokenAddresses,
384384
chainId,
385385
nativeCurrency,
386386
});
@@ -430,42 +430,42 @@ export class TokenRatesController extends PollingControllerV1<
430430
* exchange rate between the known currency and desired currency.
431431
*
432432
* @param args - The arguments to this function.
433-
* @param args.tokenContractAddresses - Contract addresses for tokens.
433+
* @param args.tokenAddresses - Addresses for tokens.
434434
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
435435
* @param args.nativeCurrency - The native currency in which to request
436436
* exchange rates.
437-
* @returns A map from token contract address to its exchange rate in the
438-
* native currency, or an empty map if no exchange rates can be obtained for
439-
* the chain ID.
437+
* @returns A map from token address to its exchange rate in the native
438+
* currency, or an empty map if no exchange rates can be obtained for the
439+
* chain ID.
440440
*/
441441
async #fetchAndMapExchangeRates({
442-
tokenContractAddresses,
442+
tokenAddresses,
443443
chainId,
444444
nativeCurrency,
445445
}: {
446-
tokenContractAddresses: Hex[];
446+
tokenAddresses: Hex[];
447447
chainId: Hex;
448448
nativeCurrency: string;
449449
}): Promise<ContractExchangeRates> {
450450
if (!this.#tokenPricesService.validateChainIdSupported(chainId)) {
451-
return tokenContractAddresses.reduce((obj, tokenContractAddress) => {
451+
return tokenAddresses.reduce((obj, tokenAddress) => {
452452
return {
453453
...obj,
454-
[tokenContractAddress]: undefined,
454+
[tokenAddress]: undefined,
455455
};
456456
}, {});
457457
}
458458

459459
if (this.#tokenPricesService.validateCurrencySupported(nativeCurrency)) {
460460
return await this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
461-
tokenContractAddresses,
461+
tokenAddresses,
462462
chainId,
463463
nativeCurrency,
464464
});
465465
}
466466

467467
return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
468-
tokenContractAddresses,
468+
tokenAddresses,
469469
nativeCurrency,
470470
});
471471
}
@@ -489,34 +489,34 @@ export class TokenRatesController extends PollingControllerV1<
489489
* chain. Ensures that token addresses are checksum addresses.
490490
*
491491
* @param args - The arguments to this function.
492-
* @param args.tokenContractAddresses - Contract addresses for tokens.
492+
* @param args.tokenAddresses - Addresses for tokens.
493493
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
494494
* @param args.nativeCurrency - The native currency in which to request
495495
* prices.
496496
* @returns A map of the token addresses (as checksums) to their prices in the
497497
* native currency.
498498
*/
499499
async #fetchAndMapExchangeRatesForSupportedNativeCurrency({
500-
tokenContractAddresses,
500+
tokenAddresses,
501501
chainId,
502502
nativeCurrency,
503503
}: {
504-
tokenContractAddresses: Hex[];
504+
tokenAddresses: Hex[];
505505
chainId: Hex;
506506
nativeCurrency: string;
507507
}): Promise<ContractExchangeRates> {
508-
const tokenPricesByTokenContractAddress =
508+
const tokenPricesByTokenAddress =
509509
await this.#tokenPricesService.fetchTokenPrices({
510-
tokenContractAddresses,
510+
tokenAddresses,
511511
chainId,
512512
currency: nativeCurrency,
513513
});
514514

515-
return Object.entries(tokenPricesByTokenContractAddress).reduce(
516-
(obj, [tokenContractAddress, tokenPrice]) => {
515+
return Object.entries(tokenPricesByTokenAddress).reduce(
516+
(obj, [tokenAddress, tokenPrice]) => {
517517
return {
518518
...obj,
519-
[tokenContractAddress]: tokenPrice.value,
519+
[tokenAddress]: tokenPrice.value,
520520
};
521521
},
522522
{},
@@ -529,25 +529,25 @@ export class TokenRatesController extends PollingControllerV1<
529529
* API, then convert the prices to our desired native currency.
530530
*
531531
* @param args - The arguments to this function.
532-
* @param args.tokenContractAddresses - The contract addresses for the tokens you
533-
* want to retrieve prices for.
534-
* @param args.nativeCurrency - The currency you want the prices to be in.
532+
* @param args.tokenAddresses - Addresses for tokens.
533+
* @param args.nativeCurrency - The native currency in which to request
534+
* prices.
535535
* @returns A map of the token addresses (as checksums) to their prices in the
536536
* native currency.
537537
*/
538538
async #fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
539-
tokenContractAddresses,
539+
tokenAddresses,
540540
nativeCurrency,
541541
}: {
542-
tokenContractAddresses: Hex[];
542+
tokenAddresses: Hex[];
543543
nativeCurrency: string;
544544
}): Promise<ContractExchangeRates> {
545545
const [
546-
tokenPricesByTokenContractAddress,
546+
tokenPricesByTokenAddress,
547547
fallbackCurrencyToNativeCurrencyConversionRate,
548548
] = await Promise.all([
549549
this.#tokenPricesService.fetchTokenPrices({
550-
tokenContractAddresses,
550+
tokenAddresses,
551551
currency: FALL_BACK_VS_CURRENCY,
552552
chainId: this.config.chainId,
553553
}),
@@ -561,11 +561,11 @@ export class TokenRatesController extends PollingControllerV1<
561561
return {};
562562
}
563563

564-
return Object.entries(tokenPricesByTokenContractAddress).reduce(
565-
(obj, [tokenContractAddress, tokenPrice]) => {
564+
return Object.entries(tokenPricesByTokenAddress).reduce(
565+
(obj, [tokenAddress, tokenPrice]) => {
566566
return {
567567
...obj,
568-
[tokenContractAddress]:
568+
[tokenAddress]:
569569
tokenPrice.value * fallbackCurrencyToNativeCurrencyConversionRate,
570570
};
571571
},

packages/assets-controllers/src/token-prices-service/abstract-token-prices-service.ts

+16-22
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@ import type { Hex } from '@metamask/utils';
33
/**
44
* Represents the price of a token in a currency.
55
*/
6-
export type TokenPrice<
7-
TokenContractAddress extends Hex,
8-
Currency extends string,
9-
> = {
10-
tokenContractAddress: TokenContractAddress;
6+
export type TokenPrice<TokenAddress extends Hex, Currency extends string> = {
7+
tokenAddress: TokenAddress;
118
value: number;
129
currency: Currency;
1310
};
1411

1512
/**
16-
* A map of token contract address to its price.
13+
* A map of token address to its price.
1714
*/
18-
export type TokenPricesByTokenContractAddress<
19-
TokenContractAddress extends Hex,
15+
export type TokenPricesByTokenAddress<
16+
TokenAddress extends Hex,
2017
Currency extends string,
2118
> = {
22-
[A in TokenContractAddress]: TokenPrice<A, Currency>;
19+
[A in TokenAddress]: TokenPrice<A, Currency>;
2320
};
2421

2522
/**
@@ -28,40 +25,37 @@ export type TokenPricesByTokenContractAddress<
2825
*
2926
* @template ChainId - A type union of valid arguments for the `chainId`
3027
* argument to `fetchTokenPrices`.
31-
* @template TokenContractAddress - A type union of all token contract
32-
* addresses. The reason this type parameter exists is so that we can guarantee
33-
* that same addresses that `fetchTokenPrices` receives are the same addresses
34-
* that shown up in the return value.
28+
* @template TokenAddress - A type union of all token addresses. The reason this
29+
* type parameter exists is so that we can guarantee that same addresses that
30+
* `fetchTokenPrices` receives are the same addresses that shown up in the
31+
* return value.
3532
* @template Currency - A type union of valid arguments for the `currency`
3633
* argument to `fetchTokenPrices`.
3734
*/
3835
export type AbstractTokenPricesService<
3936
ChainId extends Hex = Hex,
40-
TokenContractAddress extends Hex = Hex,
37+
TokenAddress extends Hex = Hex,
4138
Currency extends string = string,
4239
> = {
4340
/**
4441
* Retrieves prices in the given currency for the tokens identified by the
45-
* given contract addresses which are expected to live on the given chain.
42+
* given addresses which are expected to live on the given chain.
4643
*
4744
* @param args - The arguments to this function.
4845
* @param args.chainId - An EIP-155 chain ID.
49-
* @param args.tokenContractAddresses - Contract addresses for tokens that
50-
* live on the chain.
46+
* @param args.tokenAddresses - Addresses for tokens that live on the chain.
5147
* @param args.currency - The desired currency of the token prices.
5248
* @returns The prices for the requested tokens.
5349
*/
5450
fetchTokenPrices({
5551
chainId,
56-
tokenContractAddresses,
52+
tokenAddresses,
5753
currency,
5854
}: {
5955
chainId: ChainId;
60-
tokenContractAddresses: TokenContractAddress[];
56+
tokenAddresses: TokenAddress[];
6157
currency: Currency;
62-
}): Promise<
63-
TokenPricesByTokenContractAddress<TokenContractAddress, Currency>
64-
>;
58+
}): Promise<TokenPricesByTokenAddress<TokenAddress, Currency>>;
6559

6660
/**
6761
* Type guard for whether the API can return token prices for the given chain

0 commit comments

Comments
 (0)