Skip to content

Commit 1e01def

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 4fbf54d commit 1e01def

File tree

5 files changed

+93
-100
lines changed

5 files changed

+93
-100
lines changed

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
}),
@@ -2170,31 +2170,31 @@ function buildMockTokenPricesService(
21702170
* price of each given token is incremented by one.
21712171
*
21722172
* @param args - The arguments to this function.
2173-
* @param args.tokenContractAddresses - The token contract addresses.
2173+
* @param args.tokenAddresses - The token addresses.
21742174
* @param args.currency - The currency.
21752175
* @returns The token prices.
21762176
*/
21772177
async function fetchTokenPricesWithIncreasingPriceForEachToken<
21782178
TokenAddress extends Hex,
21792179
Currency extends string,
21802180
>({
2181-
tokenContractAddresses,
2181+
tokenAddresses,
21822182
currency,
21832183
}: {
2184-
tokenContractAddresses: TokenAddress[];
2184+
tokenAddresses: TokenAddress[];
21852185
currency: Currency;
21862186
}) {
2187-
return tokenContractAddresses.reduce<
2188-
Partial<TokenPricesByTokenContractAddress<TokenAddress, Currency>>
2189-
>((obj, tokenContractAddress, i) => {
2187+
return tokenAddresses.reduce<
2188+
Partial<TokenPricesByTokenAddress<TokenAddress, Currency>>
2189+
>((obj, tokenAddress, i) => {
21902190
const tokenPrice: TokenPrice<TokenAddress, Currency> = {
2191-
tokenContractAddress,
2191+
tokenAddress,
21922192
value: (i + 1) / 1000,
21932193
currency,
21942194
};
21952195
return {
21962196
...obj,
2197-
[tokenContractAddress]: tokenPrice,
2197+
[tokenAddress]: tokenPrice,
21982198
};
2199-
}, {}) as TokenPricesByTokenContractAddress<TokenAddress, Currency>;
2199+
}, {}) as TokenPricesByTokenAddress<TokenAddress, Currency>;
22002200
}

packages/assets-controllers/src/TokenRatesController.ts

+31-31
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ export class TokenRatesController extends PollingControllerV1<
355355
return;
356356
}
357357

358-
const tokenContractAddresses = this.#getTokenAddresses(chainId);
359-
if (tokenContractAddresses.length === 0) {
358+
const tokenAddresses = this.#getTokenAddresses(chainId);
359+
if (tokenAddresses.length === 0) {
360360
return;
361361
}
362362

363363
const newContractExchangeRates = await this.#fetchAndMapExchangeRates({
364-
tokenContractAddresses,
364+
tokenAddresses,
365365
chainId,
366366
nativeCurrency,
367367
});
@@ -404,42 +404,42 @@ export class TokenRatesController extends PollingControllerV1<
404404
* exchange rate between the known currency and desired currency.
405405
*
406406
* @param args - The arguments to this function.
407-
* @param args.tokenContractAddresses - Contract addresses for tokens.
407+
* @param args.tokenAddresses - Addresses for tokens.
408408
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
409409
* @param args.nativeCurrency - The native currency in which to request
410410
* exchange rates.
411-
* @returns A map from token contract address to its exchange rate in the
412-
* native currency, or an empty map if no exchange rates can be obtained for
413-
* the chain ID.
411+
* @returns A map from token address to its exchange rate in the native
412+
* currency, or an empty map if no exchange rates can be obtained for the
413+
* chain ID.
414414
*/
415415
async #fetchAndMapExchangeRates({
416-
tokenContractAddresses,
416+
tokenAddresses,
417417
chainId,
418418
nativeCurrency,
419419
}: {
420-
tokenContractAddresses: Hex[];
420+
tokenAddresses: Hex[];
421421
chainId: Hex;
422422
nativeCurrency: string;
423423
}): Promise<ContractExchangeRates> {
424424
if (!this.#tokenPricesService.validateChainIdSupported(chainId)) {
425-
return tokenContractAddresses.reduce((obj, tokenContractAddress) => {
425+
return tokenAddresses.reduce((obj, tokenAddress) => {
426426
return {
427427
...obj,
428-
[tokenContractAddress]: undefined,
428+
[tokenAddress]: undefined,
429429
};
430430
}, {});
431431
}
432432

433433
if (this.#tokenPricesService.validateCurrencySupported(nativeCurrency)) {
434434
return await this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
435-
tokenContractAddresses,
435+
tokenAddresses,
436436
chainId,
437437
nativeCurrency,
438438
});
439439
}
440440

441441
return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
442-
tokenContractAddresses,
442+
tokenAddresses,
443443
nativeCurrency,
444444
});
445445
}
@@ -463,34 +463,34 @@ export class TokenRatesController extends PollingControllerV1<
463463
* chain. Ensures that token addresses are checksum addresses.
464464
*
465465
* @param args - The arguments to this function.
466-
* @param args.tokenContractAddresses - Contract addresses for tokens.
466+
* @param args.tokenAddresses - Addresses for tokens.
467467
* @param args.chainId - The EIP-155 ID of the chain where the tokens live.
468468
* @param args.nativeCurrency - The native currency in which to request
469469
* prices.
470470
* @returns A map of the token addresses (as checksums) to their prices in the
471471
* native currency.
472472
*/
473473
async #fetchAndMapExchangeRatesForSupportedNativeCurrency({
474-
tokenContractAddresses,
474+
tokenAddresses,
475475
chainId,
476476
nativeCurrency,
477477
}: {
478-
tokenContractAddresses: Hex[];
478+
tokenAddresses: Hex[];
479479
chainId: Hex;
480480
nativeCurrency: string;
481481
}): Promise<ContractExchangeRates> {
482-
const tokenPricesByTokenContractAddress =
482+
const tokenPricesByTokenAddress =
483483
await this.#tokenPricesService.fetchTokenPrices({
484-
tokenContractAddresses,
484+
tokenAddresses,
485485
chainId,
486486
currency: nativeCurrency,
487487
});
488488

489-
return Object.entries(tokenPricesByTokenContractAddress).reduce(
490-
(obj, [tokenContractAddress, tokenPrice]) => {
489+
return Object.entries(tokenPricesByTokenAddress).reduce(
490+
(obj, [tokenAddress, tokenPrice]) => {
491491
return {
492492
...obj,
493-
[tokenContractAddress]: tokenPrice.value,
493+
[tokenAddress]: tokenPrice.value,
494494
};
495495
},
496496
{},
@@ -503,25 +503,25 @@ export class TokenRatesController extends PollingControllerV1<
503503
* API, then convert the prices to our desired native currency.
504504
*
505505
* @param args - The arguments to this function.
506-
* @param args.tokenContractAddresses - The contract addresses for the tokens you
507-
* want to retrieve prices for.
508-
* @param args.nativeCurrency - The currency you want the prices to be in.
506+
* @param args.tokenAddresses - Addresses for tokens.
507+
* @param args.nativeCurrency - The native currency in which to request
508+
* prices.
509509
* @returns A map of the token addresses (as checksums) to their prices in the
510510
* native currency.
511511
*/
512512
async #fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
513-
tokenContractAddresses,
513+
tokenAddresses,
514514
nativeCurrency,
515515
}: {
516-
tokenContractAddresses: Hex[];
516+
tokenAddresses: Hex[];
517517
nativeCurrency: string;
518518
}): Promise<ContractExchangeRates> {
519519
const [
520-
tokenPricesByTokenContractAddress,
520+
tokenPricesByTokenAddress,
521521
fallbackCurrencyToNativeCurrencyConversionRate,
522522
] = await Promise.all([
523523
this.#tokenPricesService.fetchTokenPrices({
524-
tokenContractAddresses,
524+
tokenAddresses,
525525
currency: FALL_BACK_VS_CURRENCY,
526526
chainId: this.config.chainId,
527527
}),
@@ -535,11 +535,11 @@ export class TokenRatesController extends PollingControllerV1<
535535
return {};
536536
}
537537

538-
return Object.entries(tokenPricesByTokenContractAddress).reduce(
539-
(obj, [tokenContractAddress, tokenPrice]) => {
538+
return Object.entries(tokenPricesByTokenAddress).reduce(
539+
(obj, [tokenAddress, tokenPrice]) => {
540540
return {
541541
...obj,
542-
[tokenContractAddress]:
542+
[tokenAddress]:
543543
tokenPrice.value * fallbackCurrencyToNativeCurrencyConversionRate,
544544
};
545545
},

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

packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ describe('codefiTokenPricesServiceV2', () => {
3030
const pricedTokensByAddress =
3131
await codefiTokenPricesServiceV2.fetchTokenPrices({
3232
chainId: '0x1',
33-
tokenContractAddresses: ['0xAAA', '0xBBB', '0xCCC'],
33+
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
3434
currency: 'ETH',
3535
});
3636

3737
expect(pricedTokensByAddress).toStrictEqual({
3838
'0xAAA': {
39-
tokenContractAddress: '0xAAA',
39+
tokenAddress: '0xAAA',
4040
value: 148.17205755299946,
4141
currency: 'ETH',
4242
},
4343
'0xBBB': {
44-
tokenContractAddress: '0xBBB',
44+
tokenAddress: '0xBBB',
4545
value: 33689.98134554716,
4646
currency: 'ETH',
4747
},
4848
'0xCCC': {
49-
tokenContractAddress: '0xCCC',
49+
tokenAddress: '0xCCC',
5050
value: 148.1344197578456,
5151
currency: 'ETH',
5252
},
@@ -72,7 +72,7 @@ describe('codefiTokenPricesServiceV2', () => {
7272
await expect(
7373
codefiTokenPricesServiceV2.fetchTokenPrices({
7474
chainId: '0x1',
75-
tokenContractAddresses: ['0xAAA', '0xBBB', '0xCCC'],
75+
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
7676
currency: 'ETH',
7777
}),
7878
).rejects.toThrow('Could not find price for "0xAAA" in "ETH"');
@@ -98,7 +98,7 @@ describe('codefiTokenPricesServiceV2', () => {
9898
await expect(
9999
codefiTokenPricesServiceV2.fetchTokenPrices({
100100
chainId: '0x1',
101-
tokenContractAddresses: ['0xAAA', '0xBBB', '0xCCC'],
101+
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
102102
currency: 'ETH',
103103
}),
104104
).rejects.toThrow('Could not find price for "0xAAA" in "ETH"');

0 commit comments

Comments
 (0)