Skip to content

chore: add cleanup for expired non evm historical prices #5689

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ const fakeHistoricalPrices: OnAssetHistoricalPriceResponse = {
],
},
updateTime: 1737542312,
expirationTime: 1737542312,
// expirationTime is in 1Hour based on current Date.now()
expirationTime: Date.now() + 1000 * 60 * 60,
},
};

Expand Down Expand Up @@ -627,5 +628,97 @@ describe('MultichainAssetsRatesController', () => {

expect(snapHandler).toHaveBeenCalledTimes(1);
});

it('does not clean up any of the prices if none of them have expired', async () => {
const testCurrency = 'EUR';
const testNativeAssetPrices = {
intervals: {},
updateTime: Date.now(),
expirationTime: Date.now() + 1000, // not expired
};
const testTokenAssetPrices = {
intervals: {},
updateTime: Date.now(),
expirationTime: Date.now() + 1000, // not expired
};
const { controller, messenger } = setupController({
config: {
state: {
historicalPrices: {
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501': {
[testCurrency]: testNativeAssetPrices,
},
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:testToken1': {
[testCurrency]: testTokenAssetPrices,
},
},
},
},
});

const snapHandler = jest.fn().mockResolvedValue(fakeHistoricalPrices);
messenger.registerActionHandler(
'SnapController:handleRequest',
snapHandler,
);

await controller.fetchHistoricalPricesForAsset(
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501',
);

expect(snapHandler).toHaveBeenCalledTimes(1);
expect(controller.state.historicalPrices).toStrictEqual({
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501': {
USD: fakeHistoricalPrices.historicalPrice,
EUR: testNativeAssetPrices,
},
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:testToken1': {
EUR: testTokenAssetPrices,
},
});
});

it('cleans up all historical prices that have expired', async () => {
const testCurrency = 'EUR';
const { controller, messenger } = setupController({
config: {
state: {
historicalPrices: {
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501': {
[testCurrency]: {
intervals: {},
updateTime: Date.now(),
expirationTime: Date.now() - 1000, // expired
},
},
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:testToken1': {
[testCurrency]: {
intervals: {},
updateTime: Date.now(),
expirationTime: Date.now() - 1000, // expired
},
},
},
},
},
});

const snapHandler = jest.fn().mockResolvedValue(fakeHistoricalPrices);
messenger.registerActionHandler(
'SnapController:handleRequest',
snapHandler,
);

await controller.fetchHistoricalPricesForAsset(
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501',
);

expect(snapHandler).toHaveBeenCalledTimes(1);
expect(controller.state.historicalPrices).toStrictEqual({
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501': {
USD: fakeHistoricalPrices.historicalPrice,
},
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
import { HandlerType } from '@metamask/snaps-utils';
import { Mutex } from 'async-mutex';
import type { Draft } from 'immer';
import { isEqual } from 'lodash';

import { MAP_CAIP_CURRENCIES } from './constant';
import type {
Expand Down Expand Up @@ -389,6 +390,8 @@ export class MultichainAssetsRatesController extends StaticIntervalPollingContro
},
};
});
// cleanup all historical prices that have expired
this.#cleanupHistoricalPrices();
} catch {
throw new Error(
`Failed to fetch historical prices for asset: ${asset}`,
Expand All @@ -399,6 +402,45 @@ export class MultichainAssetsRatesController extends StaticIntervalPollingContro
});
}

#cleanupHistoricalPrices() {
const allHistoricalPrices = this.state.historicalPrices;
const cleanedHistoricalPrices =
this.#removeExpiredEntries(allHistoricalPrices);

// do not update state if no changes
if (isEqual(allHistoricalPrices, cleanedHistoricalPrices)) {
return;
}

this.update((state) => {
state.historicalPrices = cleanedHistoricalPrices;
});
}

#removeExpiredEntries(
data: Record<CaipAssetType, Record<string, HistoricalPrice>>,
): Record<CaipAssetType, Record<string, HistoricalPrice>> {
const now = Date.now();
const result: Record<CaipAssetType, Record<string, HistoricalPrice>> = {};

Object.entries(data).forEach(([assetId, currencies]) => {
const validCurrencies: Record<string, HistoricalPrice> = {};

Object.entries(currencies).forEach(([currency, details]) => {
const exp = details.expirationTime;
if (exp === undefined || exp > now) {
validCurrencies[currency] = details;
}
});

if (Object.keys(validCurrencies).length > 0) {
result[assetId as CaipAssetType] = validCurrencies;
}
});

return result;
}

/**
* Returns the array of CAIP-19 assets for the given account ID.
* If none are found, returns an empty array.
Expand Down
Loading