timelock-sdk 0.0.42 → 0.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.cjs +32 -12
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +56 -56
- package/dist/client.d.ts +55 -55
- package/dist/client.js +32 -12
- package/dist/client.js.map +1 -1
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -550,23 +550,43 @@ const getResolutionConfig = (resolution) => {
|
|
|
550
550
|
}
|
|
551
551
|
}[resolution];
|
|
552
552
|
};
|
|
553
|
-
const
|
|
553
|
+
const fillGaps = (prices, start, end, intervalMs) => {
|
|
554
|
+
if (prices.length === 0) return [];
|
|
555
|
+
const priceMap = /* @__PURE__ */ new Map();
|
|
556
|
+
for (const point of prices) {
|
|
557
|
+
const alignedTime = Math.floor(point.timestamp.getTime() / intervalMs) * intervalMs;
|
|
558
|
+
priceMap.set(alignedTime, point);
|
|
559
|
+
}
|
|
560
|
+
const filled = [];
|
|
561
|
+
let currentTime = Math.floor(prices[0].timestamp.getTime() / intervalMs) * intervalMs;
|
|
562
|
+
let lastKnownPrice = null;
|
|
563
|
+
while (currentTime <= end.getTime()) {
|
|
564
|
+
const existing = priceMap.get(currentTime);
|
|
565
|
+
if (existing) {
|
|
566
|
+
filled.push(existing);
|
|
567
|
+
lastKnownPrice = existing;
|
|
568
|
+
} else if (lastKnownPrice) filled.push({
|
|
569
|
+
timestamp: new Date(currentTime),
|
|
570
|
+
price: lastKnownPrice.price
|
|
571
|
+
});
|
|
572
|
+
currentTime += intervalMs;
|
|
573
|
+
}
|
|
574
|
+
return filled;
|
|
575
|
+
};
|
|
576
|
+
const getPriceHistory = async (poolAddress, resolution, start, end) => {
|
|
554
577
|
const network = "monad-testnet";
|
|
555
578
|
const { timeframe, aggregate, seconds } = getResolutionConfig(resolution);
|
|
556
|
-
if (
|
|
557
|
-
const startSecs = Math.floor(
|
|
558
|
-
const endSecs = Math.floor(
|
|
579
|
+
if (end.getTime() > Date.now()) end = new Date(Date.now());
|
|
580
|
+
const startSecs = Math.floor(start.getTime() / 1e3);
|
|
581
|
+
const endSecs = Math.floor(end.getTime() / 1e3);
|
|
559
582
|
const diffSeconds = endSecs - startSecs;
|
|
560
|
-
const url = `https://api.geckoterminal.com/api/v2/networks/${network}/pools/${poolAddress}/ohlcv/${timeframe}?aggregate=${aggregate}&limit=${Math.min(Math.ceil(diffSeconds / seconds), 1e3)}&
|
|
583
|
+
const url = `https://api.geckoterminal.com/api/v2/networks/${network}/pools/${poolAddress}/ohlcv/${timeframe}?aggregate=${aggregate}&limit=${Math.min(Math.ceil(diffSeconds / seconds), 1e3)}&token=quote¤cy=usd&before_timestamp=${endSecs}`;
|
|
561
584
|
const res = await fetch(url, { headers: { Accept: "application/json" } });
|
|
562
585
|
if (!res.ok) throw new Error(`Failed to fetch price history: ${res.statusText}`);
|
|
563
|
-
return (await res.json()).data.attributes.ohlcv_list.map((
|
|
564
|
-
timestamp,
|
|
565
|
-
price
|
|
566
|
-
})).filter((point) => point.timestamp >= startSecs && point.timestamp <= endSecs).sort((a, b) => a.timestamp - b.timestamp
|
|
567
|
-
timestamp: /* @__PURE__ */ new Date(a.timestamp * 1e3),
|
|
568
|
-
price: a.price
|
|
569
|
-
}));
|
|
586
|
+
return fillGaps((await res.json()).data.attributes.ohlcv_list.map(([timestamp, , , , close]) => ({
|
|
587
|
+
timestamp: /* @__PURE__ */ new Date(timestamp * 1e3),
|
|
588
|
+
price: close
|
|
589
|
+
})).filter((point) => point.timestamp.getTime() / 1e3 >= startSecs && point.timestamp.getTime() / 1e3 <= endSecs).sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()), start, end, seconds * 1e3);
|
|
570
590
|
};
|
|
571
591
|
|
|
572
592
|
//#endregion
|