@@ -64,6 +64,35 @@ const fetchHistory = async ({ symbol, range, cluster, from, until }: { symbol: s
6464 return fetch ( url ) . then ( async ( data ) => historySchema . parse ( await data . json ( ) ) ) ;
6565}
6666
67+ // const checkPriceData = (data: {time: UTCTimestamp}[]) => {
68+ // const chartData = [...data].sort((a, b) => a.time - b.time);
69+ // if(chartData.length < 2) {
70+ // return;
71+ // }
72+ // const firstElement = chartData.at(-2);
73+ // const secondElement = chartData.at(-1);
74+ // if(!firstElement || !secondElement ) {
75+ // return;
76+ // }
77+ // const detectedInterval = secondElement.time - firstElement.time
78+ // for(let i = 0; i < chartData.length - 1; i++) {
79+ // const currentElement = chartData[i];
80+ // const nextElement = chartData[i + 1];
81+ // if(!currentElement || !nextElement) {
82+ // return;
83+ // }
84+ // const interval = nextElement.time - currentElement.time
85+ // if(interval !== detectedInterval) {
86+ // console.warn("Price chartData is not consistent", {
87+ // current: currentElement,
88+ // next: nextElement,
89+ // detectedInterval,
90+ // });
91+ // }
92+ // }
93+ // return detectedInterval;
94+ // }
95+
6796const useChartElem = ( symbol : string , feedId : string ) => {
6897 const logger = useLogger ( ) ;
6998 const { current } = useLivePriceData ( Cluster . Pythnet , feedId ) ;
@@ -80,9 +109,9 @@ const [interval] = useQueryState(
80109 if ( ! isBackfilling . current && earliestDateRef . current ) {
81110 isBackfilling . current = true ;
82111 // seconds to date
83- console . log ( "backfilling" , new Date ( Number ( earliestDateRef . current ) * 1000 ) ) ;
84112 const range = interval === "Live" ? "1H" : interval ;
85- fetchHistory ( { symbol, range, cluster : "pythnet" , from : earliestDateRef . current - 100n , until : earliestDateRef . current - 2n } )
113+ console . log ( "backfilling" , new Date ( Number ( earliestDateRef . current ) * 1000 ) , { from : earliestDateRef . current - 100n , until : earliestDateRef . current } ) ;
114+ fetchHistory ( { symbol, range, cluster : "pythnet" , from : earliestDateRef . current - 100n , until : earliestDateRef . current } )
86115 . then ( ( data ) => {
87116 const firstPoint = data [ 0 ] ;
88117 if ( firstPoint ) {
@@ -109,28 +138,28 @@ const [interval] = useQueryState(
109138 value : price ,
110139 } ) )
111140 } ) ;
112- chartRef . current . price . setData ( [
113- ...convertedData . map ( ( { time, price } ) => ( {
141+ const newPriceData = [ ...convertedData . map ( ( { time, price } ) => ( {
114142 time,
115143 value : price ,
116144 } ) ) ,
117- ...chartRef . current . price . data ( ) ,
118- ] ) ;
119-
120- chartRef . current . confidenceHigh . setData ( [
121- ...convertedData . map ( ( { time, price, confidence } ) => ( {
145+ ...chartRef . current . price . data ( ) , ]
146+ const newConfidenceHighData = [ ...convertedData . map ( ( { time, price, confidence } ) => ( {
122147 time,
123148 value : price + confidence ,
124149 } ) ) ,
125- ...chartRef . current . confidenceHigh . data ( ) ,
126- ] ) ;
127- chartRef . current . confidenceLow . setData ( [
128- ...convertedData . map ( ( { time, price, confidence } ) => ( {
150+ ...chartRef . current . confidenceHigh . data ( ) , ]
151+ const newConfidenceLowData = [ ...convertedData . map ( ( { time, price, confidence } ) => ( {
152+ time,
153+ value : price - confidence ,
154+ } ) ) , ...chartRef . current . confidenceLow . data ( ) , ]
155+ checkPriceData ( convertedData . map ( ( { time, price } ) => ( {
129156 time,
130- value : price - confidence ,
131- } ) ) ,
132- ...chartRef . current . confidenceLow . data ( ) ,
133- ] ) ;
157+ value : price ,
158+ } ) ) ) ;
159+ console . log ( newPriceData )
160+ chartRef . current . price . setData ( newPriceData ) ;
161+ chartRef . current . confidenceHigh . setData ( newConfidenceHighData ) ;
162+ chartRef . current . confidenceLow . setData ( newConfidenceLowData ) ;
134163 }
135164 isBackfilling . current = false ;
136165 } )
0 commit comments