Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/assets-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- **BREAKING:** TokenRatesController now takes a required argument `tokenPricesService` ([#3600](https://github.com/MetaMask/core/pull/3600))
- This object is responsible for fetching the prices for tokens held by this controller.
- **BREAKING:** Update signature of `TokenRatesController.updateExchangeRatesByChainId` ([#3600](https://github.com/MetaMask/core/pull/3600))
- Rename `tokenAddresses` argument to `tokenContractAddresses`
- Change the type of `tokenContractAddresses` from `string[]` to `Hex[]`
- **BREAKING:** Change signature of `TokenRatesController.fetchAndMapExchangeRates` ([#3600](https://github.com/MetaMask/core/pull/3600))
- This method now takes an object with shape `{ tokenContractAddresses: Hex[]; chainId: Hex; nativeCurrency: string; }` rather than positional arguments
- **BREAKING:** Update signature of `TokenRatesController.updateExchangeRatesByChainId` ([#3600](https://github.com/MetaMask/core/pull/3600), [#3653](https://github.com/MetaMask/core/pull/3653))
- Change the type of `tokenAddresses` from `string[]` to `Hex[]`
- **BREAKING:** Change signature of `TokenRatesController.fetchAndMapExchangeRates` ([#3600](https://github.com/MetaMask/core/pull/3600), [#3653](https://github.com/MetaMask/core/pull/3653))
- This method now takes an object with shape `{ tokenAddresses: Hex[]; chainId: Hex; nativeCurrency: string; }` rather than positional arguments
- 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))
- Update TokenRatesController to request token rates from the Price API in batches of 100 ([#3650](https://github.com/MetaMask/core/pull/3650))

Expand Down
48 changes: 24 additions & 24 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { advanceTime } from '../../../tests/helpers';
import type {
AbstractTokenPricesService,
TokenPrice,
TokenPricesByTokenContractAddress,
TokenPricesByTokenAddress,
} from './token-prices-service/abstract-token-prices-service';
import type { TokenBalancesState } from './TokenBalancesController';
import { TokenRatesController } from './TokenRatesController';
Expand Down Expand Up @@ -1727,12 +1727,12 @@ describe('TokenRatesController', () => {
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(2);
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(1, {
chainId,
tokenContractAddresses: tokenAddresses.slice(0, 100),
tokenAddresses: tokenAddresses.slice(0, 100),
currency: ticker,
});
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(2, {
chainId,
tokenContractAddresses: tokenAddresses.slice(100),
tokenAddresses: tokenAddresses.slice(100),
currency: ticker,
});
},
Expand All @@ -1748,12 +1748,12 @@ describe('TokenRatesController', () => {
fetchTokenPrices: jest.fn().mockResolvedValue({
[tokenAddresses[0]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[0],
tokenAddress: tokenAddresses[0],
value: 0.001,
},
[tokenAddresses[1]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[1],
tokenAddress: tokenAddresses[1],
value: 0.002,
},
}),
Expand Down Expand Up @@ -1817,12 +1817,12 @@ describe('TokenRatesController', () => {
fetchTokenPrices: jest.fn().mockResolvedValue({
[tokenAddresses[0]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[0],
tokenAddress: tokenAddresses[0],
value: 0.001,
},
[tokenAddresses[1]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[1],
tokenAddress: tokenAddresses[1],
value: 0.002,
},
}),
Expand Down Expand Up @@ -1884,12 +1884,12 @@ describe('TokenRatesController', () => {
fetchTokenPrices: jest.fn().mockResolvedValue({
[tokenAddresses[0]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[0],
tokenAddress: tokenAddresses[0],
value: 0.001,
},
[tokenAddresses[1]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[1],
tokenAddress: tokenAddresses[1],
value: 0.002,
},
}),
Expand Down Expand Up @@ -2007,12 +2007,12 @@ describe('TokenRatesController', () => {
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(2);
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(1, {
chainId,
tokenContractAddresses: tokenAddresses.slice(0, 100),
tokenAddresses: tokenAddresses.slice(0, 100),
currency: 'ETH',
});
expect(fetchTokenPricesSpy).toHaveBeenNthCalledWith(2, {
chainId,
tokenContractAddresses: tokenAddresses.slice(100),
tokenAddresses: tokenAddresses.slice(100),
currency: 'ETH',
});
},
Expand All @@ -2028,12 +2028,12 @@ describe('TokenRatesController', () => {
fetchTokenPrices: jest.fn().mockResolvedValue({
[tokenAddresses[0]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[0],
tokenAddress: tokenAddresses[0],
value: 0.001,
},
[tokenAddresses[1]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[1],
tokenAddress: tokenAddresses[1],
value: 0.002,
},
}),
Expand Down Expand Up @@ -2100,12 +2100,12 @@ describe('TokenRatesController', () => {
const fetchTokenPricesMock = jest.fn().mockResolvedValue({
[tokenAddresses[0]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[0],
tokenAddress: tokenAddresses[0],
value: 0.001,
},
[tokenAddresses[1]]: {
currency: 'ETH',
tokenContractAddress: tokenAddresses[1],
tokenAddress: tokenAddresses[1],
value: 0.002,
},
});
Expand Down Expand Up @@ -2375,33 +2375,33 @@ function buildMockTokenPricesService(
* price of each given token is incremented by one.
*
* @param args - The arguments to this function.
* @param args.tokenContractAddresses - The token contract addresses.
* @param args.tokenAddresses - The token addresses.
* @param args.currency - The currency.
* @returns The token prices.
*/
async function fetchTokenPricesWithIncreasingPriceForEachToken<
TokenAddress extends Hex,
Currency extends string,
>({
tokenContractAddresses,
tokenAddresses,
currency,
}: {
tokenContractAddresses: TokenAddress[];
tokenAddresses: TokenAddress[];
currency: Currency;
}) {
return tokenContractAddresses.reduce<
Partial<TokenPricesByTokenContractAddress<TokenAddress, Currency>>
>((obj, tokenContractAddress, i) => {
return tokenAddresses.reduce<
Partial<TokenPricesByTokenAddress<TokenAddress, Currency>>
>((obj, tokenAddress, i) => {
const tokenPrice: TokenPrice<TokenAddress, Currency> = {
tokenContractAddress,
tokenAddress,
value: (i + 1) / 1000,
currency,
};
return {
...obj,
[tokenContractAddress]: tokenPrice,
[tokenAddress]: tokenPrice,
};
}, {}) as TokenPricesByTokenContractAddress<TokenAddress, Currency>;
}, {}) as TokenPricesByTokenAddress<TokenAddress, Currency>;
}

/**
Expand Down
68 changes: 34 additions & 34 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
return;
}

const tokenContractAddresses = this.#getTokenAddresses(chainId);
if (tokenContractAddresses.length === 0) {
const tokenAddresses = this.#getTokenAddresses(chainId);
if (tokenAddresses.length === 0) {
return;
}

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

try {
const newContractExchangeRates = await this.#fetchAndMapExchangeRates({
tokenContractAddresses,
tokenAddresses,
chainId,
nativeCurrency,
});
Expand Down Expand Up @@ -437,42 +437,42 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
* exchange rate between the known currency and desired currency.
*
* @param args - The arguments to this function.
* @param args.tokenContractAddresses - Contract addresses for tokens.
* @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 contract address to its exchange rate in the
* native currency, or an empty map if no exchange rates can be obtained for
* the chain ID.
* @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({
tokenContractAddresses,
tokenAddresses,
chainId,
nativeCurrency,
}: {
tokenContractAddresses: Hex[];
tokenAddresses: Hex[];
chainId: Hex;
nativeCurrency: string;
}): Promise<ContractExchangeRates> {
if (!this.#tokenPricesService.validateChainIdSupported(chainId)) {
return tokenContractAddresses.reduce((obj, tokenContractAddress) => {
return tokenAddresses.reduce((obj, tokenAddress) => {
return {
...obj,
[tokenContractAddress]: undefined,
[tokenAddress]: undefined,
};
}, {});
}

if (this.#tokenPricesService.validateCurrencySupported(nativeCurrency)) {
return await this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenContractAddresses,
tokenAddresses,
chainId,
nativeCurrency,
});
}

return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
tokenContractAddresses,
tokenAddresses,
nativeCurrency,
});
}
Expand All @@ -496,49 +496,49 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
* chain. Ensures that token addresses are checksum addresses.
*
* @param args - The arguments to this function.
* @param args.tokenContractAddresses - Contract addresses for tokens.
* @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({
tokenContractAddresses,
tokenAddresses,
chainId,
nativeCurrency,
}: {
tokenContractAddresses: Hex[];
tokenAddresses: Hex[];
chainId: Hex;
nativeCurrency: string;
}): Promise<ContractExchangeRates> {
const tokenPricesByTokenContractAddress = await reduceInBatchesSerially<
const tokenPricesByTokenAddress = await reduceInBatchesSerially<
Hex,
Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>
>({
values: tokenContractAddresses,
values: tokenAddresses,
batchSize: TOKEN_PRICES_BATCH_SIZE,
eachBatch: async (allTokenPricesByTokenContractAddress, batch) => {
const tokenPricesByTokenContractAddressForBatch =
eachBatch: async (allTokenPricesByTokenAddress, batch) => {
const tokenPricesByTokenAddressForBatch =
await this.#tokenPricesService.fetchTokenPrices({
tokenContractAddresses: batch,
tokenAddresses: batch,
chainId,
currency: nativeCurrency,
});

return {
...allTokenPricesByTokenContractAddress,
...tokenPricesByTokenContractAddressForBatch,
...allTokenPricesByTokenAddress,
...tokenPricesByTokenAddressForBatch,
};
},
initialResult: {},
});

return Object.entries(tokenPricesByTokenContractAddress).reduce(
(obj, [tokenContractAddress, tokenPrice]) => {
return Object.entries(tokenPricesByTokenAddress).reduce(
(obj, [tokenAddress, tokenPrice]) => {
return {
...obj,
[tokenContractAddress]: tokenPrice.value,
[tokenAddress]: tokenPrice.value,
};
},
{},
Expand All @@ -551,25 +551,25 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
* API, then convert the prices to our desired native currency.
*
* @param args - The arguments to this function.
* @param args.tokenContractAddresses - The contract addresses for the tokens you
* want to retrieve prices for.
* @param args.nativeCurrency - The currency you want the prices to be in.
* @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({
tokenContractAddresses,
tokenAddresses,
nativeCurrency,
}: {
tokenContractAddresses: Hex[];
tokenAddresses: Hex[];
nativeCurrency: string;
}): Promise<ContractExchangeRates> {
const [
contractExchangeRates,
fallbackCurrencyToNativeCurrencyConversionRate,
] = await Promise.all([
this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenContractAddresses,
tokenAddresses,
chainId: this.config.chainId,
nativeCurrency: FALL_BACK_VS_CURRENCY,
}),
Expand All @@ -584,10 +584,10 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
}

return Object.entries(contractExchangeRates).reduce(
(obj, [tokenContractAddress, tokenValue]) => {
(obj, [tokenAddress, tokenValue]) => {
return {
...obj,
[tokenContractAddress]: tokenValue
[tokenAddress]: tokenValue
? tokenValue * fallbackCurrencyToNativeCurrencyConversionRate
: undefined,
};
Expand Down
Loading