-
Notifications
You must be signed in to change notification settings - Fork 302
feat: Use benchmarks API for historical data #3138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fhqvst
wants to merge
1
commit into
main
Choose a base branch
from
fhqvst/ui-276-use-benchmarks-api-for-historical-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,12 +158,17 @@ const useChartElem = (symbol: string, feedId: string) => { | |
| return; | ||
| } | ||
| isBackfilling.current = true; | ||
| const url = new URL("/historical-prices", globalThis.location.origin); | ||
|
|
||
| const url = new URL( | ||
| "https://benchmarks.pyth.network/v1/shims/tradingview/history", | ||
| ); | ||
| url.searchParams.set("symbol", symbol); | ||
| url.searchParams.set("from", from.toString()); | ||
| url.searchParams.set("to", to.toString()); | ||
| url.searchParams.set("resolution", resolution); | ||
| url.searchParams.set("cluster", "pythnet"); | ||
| url.searchParams.set( | ||
| "resolution", | ||
| mapResolutionToBenchmarksApi(resolution), | ||
| ); | ||
|
|
||
| abortControllerRef.current = new AbortController(); | ||
| abortControllerRef.current.signal.addEventListener("abort", () => { | ||
|
|
@@ -177,7 +182,15 @@ const useChartElem = (symbol: string, feedId: string) => { | |
| return; | ||
| } | ||
|
|
||
| const data = historicalDataSchema.parse(jsonData); | ||
| const benchmarkData = benchmarksApiResponseSchema.parse(jsonData); | ||
|
|
||
| // Transform OHLC data to our format using close prices | ||
| const data = benchmarkData.t.map((timestamp, i) => ({ | ||
| time: timestamp as UTCTimestamp, | ||
| price: benchmarkData.c[i], // Use close price | ||
| confidence: 0, // No confidence data from benchmarks API | ||
| status: PriceStatus.Trading, | ||
| })); | ||
|
Comment on lines
+188
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd do this using a zod transform |
||
|
|
||
| // Get the current historical price data | ||
| // Note that .data() returns (WhitespaceData | LineData)[], hence the type cast. | ||
|
|
@@ -194,16 +207,17 @@ const useChartElem = (symbol: string, feedId: string) => { | |
| value: d.price, | ||
| }), | ||
| })); | ||
| // we have no confidence data, set confidence bands to match the price | ||
| const newHistoricalConfidenceHighData = data.map((d) => ({ | ||
| time: d.time, | ||
| ...(d.status === PriceStatus.Trading && { | ||
| value: d.price + d.confidence, | ||
| value: d.price, | ||
| }), | ||
| })); | ||
| const newHistoricalConfidenceLowData = data.map((d) => ({ | ||
| time: d.time, | ||
| ...(d.status === PriceStatus.Trading && { | ||
| value: d.price - d.confidence, | ||
| value: d.price, | ||
| }), | ||
| })); | ||
|
|
||
|
|
@@ -381,21 +395,15 @@ type ChartRefContents = { | |
| price: ISeriesApi<"Line">; | ||
| }; | ||
|
|
||
| const historicalDataSchema = z.array( | ||
| z | ||
| .strictObject({ | ||
| timestamp: z.number(), | ||
| price: z.number(), | ||
| confidence: z.number(), | ||
| status: z.nativeEnum(PriceStatus), | ||
| }) | ||
| .transform((d) => ({ | ||
| time: Number(d.timestamp) as UTCTimestamp, | ||
| price: d.price, | ||
| confidence: d.confidence, | ||
| status: d.status, | ||
| })), | ||
| ); | ||
| const benchmarksApiResponseSchema = z.object({ | ||
| s: z.string(), // status | ||
| t: z.array(z.number()), // timestamp | ||
| o: z.array(z.number()), // open | ||
| h: z.array(z.number()), // high | ||
| l: z.array(z.number()), // low | ||
| c: z.array(z.number()), // close | ||
| v: z.array(z.number()), // volume | ||
| }); | ||
| const priceFormat = { | ||
| type: "price", | ||
| precision: 5, | ||
|
|
@@ -408,6 +416,30 @@ const confidenceConfig = { | |
| lineWidth: 1, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Map our internal resolution format to the benchmarks API resolution format | ||
| */ | ||
| function mapResolutionToBenchmarksApi(resolution: string): string { | ||
| switch (resolution) { | ||
| case "1s": | ||
| case "1m": { | ||
| return "1"; | ||
| } | ||
| case "5m": { | ||
| return "5"; | ||
| } | ||
| case "1H": { | ||
| return "60"; | ||
| } | ||
| case "1D": { | ||
| return "1D"; | ||
| } | ||
| default: { | ||
| throw new Error(`Unknown resolution: ${resolution}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const useChartResize = ( | ||
| chartContainerRef: RefObject<HTMLDivElement | null>, | ||
| chartRef: RefObject<ChartRefContents | undefined>, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this -- I think we will need to put more thought in here. If we aren't showing OHLC candles, I think the chart will be misleading. Plus, we should have full resolution (individual points) when zoomed all the way in, so OHLC doesn't really make sense at that resolution. Let's grab some time to chat through this