Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch token rates across accounts #4759

Merged
merged 2 commits into from
Oct 4, 2024
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
46 changes: 28 additions & 18 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ describe('TokenRatesController', () => {
});

describe('when polling is active', () => {
it('should update exchange rates when selected address changes', async () => {
it('should not update exchange rates when selected address changes', async () => {
const alternateSelectedAddress =
'0x0000000000000000000000000000000000000002';
const alternateSelectedAccount = createMockInternalAccount({
Expand Down Expand Up @@ -1024,7 +1024,7 @@ describe('TokenRatesController', () => {
.mockResolvedValue();
triggerSelectedAccountChange(alternateSelectedAccount);

expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1);
expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
},
);
});
Expand Down Expand Up @@ -1581,31 +1581,18 @@ describe('TokenRatesController', () => {
);
});

it('does not update state if there are no tokens for the given chain and address', async () => {
it('does not update state if there are no tokens for the given chain', async () => {
await withController(
async ({
controller,
triggerTokensStateChange,
triggerNetworkStateChange,
}) => {
const tokenAddress = '0x0000000000000000000000000000000000000001';
const differentAccount =
'0x1000000000000000000000000000000000000000';
controller.enable();
await callUpdateExchangeRatesMethod({
allTokens: {
// These tokens are for the right chain but wrong account
[ChainId.mainnet]: {
[differentAccount]: [
{
address: tokenAddress,
decimals: 18,
symbol: 'TST',
aggregators: [],
},
],
},
// These tokens are for the right account but wrong chain
// These tokens are on a different chain
[toHex(2)]: {
[defaultSelectedAddress]: [
{
Expand Down Expand Up @@ -1713,7 +1700,10 @@ describe('TokenRatesController', () => {
await callUpdateExchangeRatesMethod({
allTokens: {
[chainId]: {
[defaultSelectedAddress]: tokens,
[defaultSelectedAddress]: tokens.slice(0, 100),
// Include tokens from non selected addresses
'0x0000000000000000000000000000000000000123':
tokens.slice(100),
},
},
chainId,
Expand Down Expand Up @@ -1748,6 +1738,7 @@ describe('TokenRatesController', () => {
const tokenAddresses = [
'0x0000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000002',
'0x0000000000000000000000000000000000000003',
];
const tokenPricesService = buildMockTokenPricesService({
fetchTokenPrices: jest.fn().mockResolvedValue({
Expand All @@ -1761,6 +1752,11 @@ describe('TokenRatesController', () => {
tokenAddress: tokenAddresses[1],
value: 0.002,
},
[tokenAddresses[2]]: {
currency: 'ETH',
tokenAddress: tokenAddresses[2],
value: 0.003,
},
}),
});
await withController(
Expand All @@ -1787,6 +1783,15 @@ describe('TokenRatesController', () => {
aggregators: [],
},
],
// Include tokens from non selected addresses
'0x0000000000000000000000000000000000000123': [
{
address: tokenAddresses[2],
decimals: 18,
symbol: 'TST1',
aggregators: [],
},
],
},
},
chainId: ChainId.mainnet,
Expand All @@ -1812,6 +1817,11 @@ describe('TokenRatesController', () => {
"tokenAddress": "0x0000000000000000000000000000000000000002",
"value": 0.002,
},
"0x0000000000000000000000000000000000000003": Object {
"currency": "ETH",
"tokenAddress": "0x0000000000000000000000000000000000000003",
"value": 0.003,
},
},
},
}
Expand Down
45 changes: 10 additions & 35 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,6 @@ export class TokenRatesController extends StaticIntervalPollingController<
this.#subscribeToTokensStateChange();

this.#subscribeToNetworkStateChange();

this.#subscribeToAccountChange();
}

#subscribeToTokensStateChange() {
Expand Down Expand Up @@ -356,45 +354,22 @@ export class TokenRatesController extends StaticIntervalPollingController<
);
}

#subscribeToAccountChange() {
this.messagingSystem.subscribe(
'AccountsController:selectedEvmAccountChange',
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (selectedAccount) => {
if (this.#selectedAccountId !== selectedAccount.id) {
this.#selectedAccountId = selectedAccount.id;
if (this.#pollState === PollState.Active) {
await this.updateExchangeRates();
}
}
},
);
}

/**
* Get the user's tokens for the given chain.
* Get the 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 selectedAccount = this.messagingSystem.call(
'AccountsController:getAccount',
this.#selectedAccountId,
);
const selectedAddress = selectedAccount?.address ?? '';
const tokens = this.#allTokens[chainId]?.[selectedAddress] || [];
const detectedTokens =
this.#allDetectedTokens[chainId]?.[selectedAddress] || [];

return [
...new Set(
[...tokens, ...detectedTokens].map((token) =>
toHex(toChecksumHexAddress(token.address)),
),
),
].sort();
const getTokens = (allTokens: Record<Hex, { address: string }[]>) =>
Object.values(allTokens ?? {}).flatMap((tokens) =>
tokens.map(({ address }) => toHex(toChecksumHexAddress(address))),
);

const tokenAddresses = getTokens(this.#allTokens[chainId]);
const detectedTokenAddresses = getTokens(this.#allDetectedTokens[chainId]);

return [...new Set([...tokenAddresses, ...detectedTokenAddresses])].sort();
}

/**
Expand Down
Loading