timelock-sdk 0.0.41 → 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 CHANGED
@@ -550,23 +550,43 @@ const getResolutionConfig = (resolution) => {
550
550
  }
551
551
  }[resolution];
552
552
  };
553
- const getPriceHistory = async (poolAddress, resolution, startTimestamp, endTimestamp) => {
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 (endTimestamp.getTime() > Date.now()) endTimestamp = new Date(Date.now());
557
- const startSecs = Math.floor(startTimestamp.getTime() / 1e3);
558
- const endSecs = Math.floor(endTimestamp.getTime() / 1e3);
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
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&currency=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(({ 0: timestamp, 4: price }) => ({
564
- timestamp,
565
- price
566
- })).filter((point) => point.timestamp >= startSecs && point.timestamp <= endSecs).sort((a, b) => a.timestamp - b.timestamp).map((a) => ({
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