Skip to content

Commit 1c6ddcd

Browse files
committed
feat: Use benchmarks API for historical data
1 parent 7df0697 commit 1c6ddcd

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
@@ -160,12 +160,17 @@ const useChartElem = (symbol: string, feedId: string) => {
160160
return;
161161
}
162162
isBackfilling.current = true;
163-
const url = new URL("/historical-prices", globalThis.location.origin);
163+
164+
const url = new URL(
165+
"https://benchmarks.pyth.network/v1/shims/tradingview/history",
166+
);
164167
url.searchParams.set("symbol", symbol);
165168
url.searchParams.set("from", from.toString());
166169
url.searchParams.set("to", to.toString());
167-
url.searchParams.set("resolution", resolution);
168-
url.searchParams.set("cluster", "pythnet");
170+
url.searchParams.set(
171+
"resolution",
172+
mapResolutionToBenchmarksApi(resolution),
173+
);
169174

170175
abortControllerRef.current = new AbortController();
171176
abortControllerRef.current.signal.addEventListener("abort", () => {
@@ -179,7 +184,15 @@ const useChartElem = (symbol: string, feedId: string) => {
179184
return;
180185
}
181186

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

184197
// Get the current historical price data
185198
// Note that .data() returns (WhitespaceData | LineData)[], hence the type cast.
@@ -196,16 +209,17 @@ const useChartElem = (symbol: string, feedId: string) => {
196209
value: d.price,
197210
}),
198211
}));
212+
// we have no confidence data, set confidence bands to match the price
199213
const newHistoricalConfidenceHighData = data.map((d) => ({
200214
time: d.time,
201215
...(d.status === PriceStatus.Trading && {
202-
value: d.price + d.confidence,
216+
value: d.price,
203217
}),
204218
}));
205219
const newHistoricalConfidenceLowData = data.map((d) => ({
206220
time: d.time,
207221
...(d.status === PriceStatus.Trading && {
208-
value: d.price - d.confidence,
222+
value: d.price,
209223
}),
210224
}));
211225

@@ -394,21 +408,15 @@ type ChartRefContents = {
394408
price: ISeriesApi<"Line">;
395409
};
396410

397-
const historicalDataSchema = z.array(
398-
z
399-
.strictObject({
400-
timestamp: z.number(),
401-
price: z.number(),
402-
confidence: z.number(),
403-
status: z.nativeEnum(PriceStatus),
404-
})
405-
.transform((d) => ({
406-
time: Number(d.timestamp) as UTCTimestamp,
407-
price: d.price,
408-
confidence: d.confidence,
409-
status: d.status,
410-
})),
411-
);
411+
const benchmarksApiResponseSchema = z.object({
412+
s: z.string(), // status
413+
t: z.array(z.number()), // timestamp
414+
o: z.array(z.number()), // open
415+
h: z.array(z.number()), // high
416+
l: z.array(z.number()), // low
417+
c: z.array(z.number()), // close
418+
v: z.array(z.number()), // volume
419+
});
412420
const priceFormat = {
413421
type: "price",
414422
precision: 5,
@@ -421,6 +429,30 @@ const confidenceConfig = {
421429
lineWidth: 1,
422430
} as const;
423431

432+
/**
433+
* Map our internal resolution format to the benchmarks API resolution format
434+
*/
435+
function mapResolutionToBenchmarksApi(resolution: string): string {
436+
switch (resolution) {
437+
case "1s":
438+
case "1m": {
439+
return "1";
440+
}
441+
case "5m": {
442+
return "5";
443+
}
444+
case "1H": {
445+
return "60";
446+
}
447+
case "1D": {
448+
return "1D";
449+
}
450+
default: {
451+
throw new Error(`Unknown resolution: ${resolution}`);
452+
}
453+
}
454+
}
455+
424456
const useChartResize = (
425457
chartContainerRef: RefObject<HTMLDivElement | null>,
426458
chartRef: RefObject<ChartRefContents | undefined>,

0 commit comments

Comments
 (0)