Skip to content

Commit

Permalink
fix: historical price poller check to timestamp (#4664)
Browse files Browse the repository at this point in the history
* fix: historical price poller check to timestamp

* fix: remove duplicated
  • Loading branch information
liu-zhipeng authored Jul 7, 2023
1 parent 1fc8a52 commit b4eccab
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions packages/agents/cartographer/poller/src/lib/operations/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,41 +92,43 @@ export const updateHistoricAssetPrices = async () => {

const latestTimestamp =
(await database.getCheckPoint("asset_price_timestamp_" + canonicalDomain)) || ASSET_PRICE_START_TIMESTAMP;
const toTimestamp = latestTimestamp + 90 * 24 * 3600;
const toTimestamp = Math.min(latestTimestamp + 90 * 24 * 3600, getNtpTimeSeconds());

for (const asset of assetsOnCanonical) {
let response: any;
try {
response = await axiosGet(`https://api.coingecko.com/api/v3/coins/${asset.coingeckoId}/market_chart/range`, {
params: { from: latestTimestamp, to: toTimestamp, vs_currency: "usd" },
});
if (response && response.data) {
logger.debug("Got historical prices from Coingecko API", requestContext, methodContext, {
coingeckoId: asset.coingeckoId,
from: latestTimestamp,
to: toTimestamp,
length: response.data.prices.length,
if (latestTimestamp < toTimestamp) {
for (const asset of assetsOnCanonical) {
let response: any;
try {
response = await axiosGet(`https://api.coingecko.com/api/v3/coins/${asset.coingeckoId}/market_chart/range`, {
params: { from: latestTimestamp, to: toTimestamp, vs_currency: "usd" },
});
if (response && response.data) {
logger.debug("Got historical prices from Coingecko API", requestContext, methodContext, {
coingeckoId: asset.coingeckoId,
from: latestTimestamp,
to: toTimestamp,
length: response.data.prices.length,
});

const prices = response.data.prices as unknown as [number, number][];
const assetPrices: AssetPrice[] = prices.map(([timestamp, price]) => {
return {
canonicalId: asset.canonicalId,
canonicalDomain: asset.canonicalDomain,
timestamp: Math.floor(timestamp / 1000),
price: price,
};
});
const prices = response.data.prices as unknown as [number, number][];
const assetPrices: AssetPrice[] = prices.map(([timestamp, price]) => {
return {
canonicalId: asset.canonicalId,
canonicalDomain: asset.canonicalDomain,
timestamp: Math.floor(timestamp / 1000),
price: price,
};
});

await database.saveAssetPrice(assetPrices);
await database.saveCheckPoint("asset_price_timestamp_" + canonicalDomain, toTimestamp);
logger.debug("Saved Asset Prices", requestContext, methodContext, { assetPrices });
await database.saveAssetPrice(assetPrices);
await database.saveCheckPoint("asset_price_timestamp_" + canonicalDomain, toTimestamp);
logger.debug("Saved Asset Prices", requestContext, methodContext, { assetPrices });
}
} catch (e: unknown) {
logger.debug("Coingecko API not responding correctly", requestContext, methodContext, {
res: response ? (response?.data ? response.data : response) : undefined,
error: jsonifyError(e as NxtpError),
});
}
} catch (e: unknown) {
logger.debug("Coingecko API not responding correctly", requestContext, methodContext, {
res: response ? (response?.data ? response.data : response) : undefined,
error: jsonifyError(e as NxtpError),
});
}
}
};

0 comments on commit b4eccab

Please sign in to comment.