@@ -158,12 +158,14 @@ 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 ( "resolution" , mapResolutionToBenchmarksApi ( resolution ) ) ;
167169
168170 abortControllerRef . current = new AbortController ( ) ;
169171 abortControllerRef . current . signal . addEventListener ( "abort" , ( ) => {
@@ -177,7 +179,15 @@ const useChartElem = (symbol: string, feedId: string) => {
177179 return ;
178180 }
179181
180- const data = historicalDataSchema . parse ( jsonData ) ;
182+ const benchmarkData = benchmarksApiResponseSchema . parse ( jsonData ) ;
183+
184+ // Transform OHLC data to our format using close prices
185+ const data = benchmarkData . t . map ( ( timestamp , i ) => ( {
186+ time : timestamp as UTCTimestamp ,
187+ price : benchmarkData . c [ i ] , // Use close price
188+ confidence : 0 , // No confidence data from benchmarks API
189+ status : PriceStatus . Trading ,
190+ } ) ) ;
181191
182192 // Get the current historical price data
183193 // Note that .data() returns (WhitespaceData | LineData)[], hence the type cast.
@@ -194,16 +204,17 @@ const useChartElem = (symbol: string, feedId: string) => {
194204 value : d . price ,
195205 } ) ,
196206 } ) ) ;
207+ // we have no confidence data, set confidence bands to match the price
197208 const newHistoricalConfidenceHighData = data . map ( ( d ) => ( {
198209 time : d . time ,
199210 ...( d . status === PriceStatus . Trading && {
200- value : d . price + d . confidence ,
211+ value : d . price ,
201212 } ) ,
202213 } ) ) ;
203214 const newHistoricalConfidenceLowData = data . map ( ( d ) => ( {
204215 time : d . time ,
205216 ...( d . status === PriceStatus . Trading && {
206- value : d . price - d . confidence ,
217+ value : d . price ,
207218 } ) ,
208219 } ) ) ;
209220
@@ -381,21 +392,15 @@ type ChartRefContents = {
381392 price : ISeriesApi < "Line" > ;
382393} ;
383394
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- ) ;
395+ const benchmarksApiResponseSchema = z . object ( {
396+ s : z . string ( ) , // status
397+ t : z . array ( z . number ( ) ) , // timestamp
398+ o : z . array ( z . number ( ) ) , // open
399+ h : z . array ( z . number ( ) ) , // high
400+ l : z . array ( z . number ( ) ) , // low
401+ c : z . array ( z . number ( ) ) , // close
402+ v : z . array ( z . number ( ) ) , // volume
403+ } ) ;
399404const priceFormat = {
400405 type : "price" ,
401406 precision : 5 ,
@@ -408,6 +413,30 @@ const confidenceConfig = {
408413 lineWidth : 1 ,
409414} as const ;
410415
416+ /**
417+ * Map our internal resolution format to the benchmarks API resolution format
418+ */
419+ function mapResolutionToBenchmarksApi ( resolution : string ) : string {
420+ switch ( resolution ) {
421+ case "1s" :
422+ case "1m" : {
423+ return "1" ;
424+ }
425+ case "5m" : {
426+ return "5" ;
427+ }
428+ case "1H" : {
429+ return "60" ;
430+ }
431+ case "1D" : {
432+ return "1D" ;
433+ }
434+ default : {
435+ throw new Error ( `Unknown resolution: ${ resolution } ` ) ;
436+ }
437+ }
438+ }
439+
411440const useChartResize = (
412441 chartContainerRef : RefObject < HTMLDivElement | null > ,
413442 chartRef : RefObject < ChartRefContents | undefined > ,
0 commit comments