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

fix: should not reset market data after switch network #4832

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 70 additions & 8 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ describe('TokenRatesController', () => {
);
});

it('should update exchange rates when chain ID changes', async () => {
it('should not update exchange rates when chain ID changes', async () => {
await withController(
{
options: {
Expand Down Expand Up @@ -722,12 +722,18 @@ describe('TokenRatesController', () => {
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
expect(controller.state.marketData).toStrictEqual({
'0x1': {
'0x0000000000000000000000000000000000000000': {
currency: 'ETH',
},
},
});
},
);
});

it('should clear marketData state when chain ID changes', async () => {
it('should not clear marketData state when chain ID changes', async () => {
await withController(
{
options: {
Expand Down Expand Up @@ -776,7 +782,13 @@ describe('TokenRatesController', () => {
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
expect(controller.state.marketData).toStrictEqual({
'0x1': {
'0x0000000000000000000000000000000000000000': {
currency: 'ETH',
},
},
});
},
);
});
Expand Down Expand Up @@ -865,7 +877,7 @@ describe('TokenRatesController', () => {
);
});

it('should clear marketData state when ticker changes', async () => {
it('should not clear marketData state when ticker changes', async () => {
await withController(
{
options: {
Expand Down Expand Up @@ -913,12 +925,37 @@ describe('TokenRatesController', () => {
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
expect(controller.state.marketData).toStrictEqual({
'0x1': {
'0x02': {
currency: 'ETH',
priceChange1d: 0,
pricePercentChange1d: 0,
tokenAddress: '0x02',
allTimeHigh: 4000,
allTimeLow: 900,
circulatingSupply: 2000,
dilutedMarketCap: 100,
high1d: 200,
low1d: 100,
marketCap: 1000,
marketCapPercentChange1d: 100,
price: 0.001,
pricePercentChange14d: 100,
pricePercentChange1h: 1,
pricePercentChange1y: 200,
pricePercentChange200d: 300,
pricePercentChange30d: 200,
pricePercentChange7d: 100,
totalVolume: 100,
},
},
});
},
);
});

it('should clear marketData state when chain ID changes', async () => {
it('should not clear marketData state when chain ID changes', async () => {
await withController(
{
options: {
Expand Down Expand Up @@ -966,7 +1003,32 @@ describe('TokenRatesController', () => {
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
expect(controller.state.marketData).toStrictEqual({
'0x1': {
'0x02': {
currency: 'ETH',
priceChange1d: 0,
pricePercentChange1d: 0,
tokenAddress: '0x02',
allTimeHigh: 4000,
allTimeLow: 900,
circulatingSupply: 2000,
dilutedMarketCap: 100,
high1d: 200,
low1d: 100,
marketCap: 1000,
marketCapPercentChange1d: 100,
price: 0.001,
pricePercentChange14d: 100,
pricePercentChange1h: 1,
pricePercentChange1y: 200,
pricePercentChange200d: 300,
pricePercentChange30d: 200,
pricePercentChange7d: 100,
totalVolume: 100,
},
},
});
},
);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,6 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
);

if (this.#chainId !== chainId || this.#ticker !== ticker) {
this.update((state) => {
state.marketData = {};
});
this.#chainId = chainId;
Copy link
Contributor

@bergeron bergeron Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we move to the new polling API (startPolling from the base class), this entire subscription and concept of a 'current chain id' will go away. The caller will become responsible for passing in the chain id(s) to poll, rather than the controller listening to the current one.

this.#ticker = ticker;
if (this.#pollState === PollState.Active) {
Expand Down Expand Up @@ -531,7 +528,10 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
};

this.update((state) => {
state.marketData = marketData;
state.marketData = {
...state.marketData,
...marketData,
};
});
updateSucceeded();
} catch (error: unknown) {
Expand Down
Loading