Skip to content

Commit a697c35

Browse files
committed
feat: Use benchmarks API for historical data
1 parent b914d46 commit a697c35

File tree

1 file changed

+53
-21
lines changed
  • apps/insights/src/components/PriceFeed/Chart

1 file changed

+53
-21
lines changed

apps/insights/src/components/PriceFeed/Chart/chart.tsx

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,17 @@ const useChartElem = (symbol: string, feedId: string) => {
158158
return;
159159
}
160160
isBackfilling.current = true;
161-
const url = new URL("/historical-prices", globalThis.location.origin);
161+
162+
const url = new URL(
163+
"https://benchmarks.pyth.network/v1/shims/tradingview/history",
164+
);
162165
url.searchParams.set("symbol", symbol);
163166
url.searchParams.set("from", from.toString());
164167
url.searchParams.set("to", to.toString());
165-
url.searchParams.set("resolution", resolution);
166-
url.searchParams.set("cluster", "pythnet");
168+
url.searchParams.set(
169+
"resolution",
170+
mapResolutionToBenchmarksApi(resolution),
171+
);
167172

168173
abortControllerRef.current = new AbortController();
169174
abortControllerRef.current.signal.addEventListener("abort", () => {
@@ -177,7 +182,15 @@ const useChartElem = (symbol: string, feedId: string) => {
177182
return;
178183
}
179184

180-
const data = historicalDataSchema.parse(jsonData);
185+
const benchmarkData = benchmarksApiResponseSchema.parse(jsonData);
186+
187+
// Transform OHLC data to our format using close prices
188+
const data = benchmarkData.t.map((timestamp, i) => ({
189+
time: timestamp as UTCTimestamp,
190+
price: benchmarkData.c[i], // Use close price
191+
confidence: 0, // No confidence data from benchmarks API
192+
status: PriceStatus.Trading,
193+
}));
181194

182195
// Get the current historical price data
183196
// Note that .data() returns (WhitespaceData | LineData)[], hence the type cast.
@@ -194,16 +207,17 @@ const useChartElem = (symbol: string, feedId: string) => {
194207
value: d.price,
195208
}),
196209
}));
210+
// we have no confidence data, set confidence bands to match the price
197211
const newHistoricalConfidenceHighData = data.map((d) => ({
198212
time: d.time,
199213
...(d.status === PriceStatus.Trading && {
200-
value: d.price + d.confidence,
214+
value: d.price,
201215
}),
202216
}));
203217
const newHistoricalConfidenceLowData = data.map((d) => ({
204218
time: d.time,
205219
...(d.status === PriceStatus.Trading && {
206-
value: d.price - d.confidence,
220+
value: d.price,
207221
}),
208222
}));
209223

@@ -381,21 +395,15 @@ type ChartRefContents = {
381395
price: ISeriesApi<"Line">;
382396
};
383397

384-
const historicalDataSchema = z.array(
385-
z
386-
.strictObject({
387-
timestamp: z.number(),
388-
price: z.number(),
389-
confidence: z.number(),
390-
status: z.nativeEnum(PriceStatus),
391-
})
392-
.transform((d) => ({
393-
time: Number(d.timestamp) as UTCTimestamp,
394-
price: d.price,
395-
confidence: d.confidence,
396-
status: d.status,
397-
})),
398-
);
398+
const benchmarksApiResponseSchema = z.object({
399+
s: z.string(), // status
400+
t: z.array(z.number()), // timestamp
401+
o: z.array(z.number()), // open
402+
h: z.array(z.number()), // high
403+
l: z.array(z.number()), // low
404+
c: z.array(z.number()), // close
405+
v: z.array(z.number()), // volume
406+
});
399407
const priceFormat = {
400408
type: "price",
401409
precision: 5,
@@ -408,6 +416,30 @@ const confidenceConfig = {
408416
lineWidth: 1,
409417
} as const;
410418

419+
/**
420+
* Map our internal resolution format to the benchmarks API resolution format
421+
*/
422+
function mapResolutionToBenchmarksApi(resolution: string): string {
423+
switch (resolution) {
424+
case "1s":
425+
case "1m": {
426+
return "1";
427+
}
428+
case "5m": {
429+
return "5";
430+
}
431+
case "1H": {
432+
return "60";
433+
}
434+
case "1D": {
435+
return "1D";
436+
}
437+
default: {
438+
throw new Error(`Unknown resolution: ${resolution}`);
439+
}
440+
}
441+
}
442+
411443
const useChartResize = (
412444
chartContainerRef: RefObject<HTMLDivElement | null>,
413445
chartRef: RefObject<ChartRefContents | undefined>,

0 commit comments

Comments
 (0)