-
Notifications
You must be signed in to change notification settings - Fork 308
feat(insights): pyth metadata caching #2921
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
Merged
Merged
Changes from 35 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
2e2f8bb
feat: add cached methods
6e33dd2
feat: add more cached methods
alexcambose bc390f6
feat(insights): add more cached methods 2
alexcambose 70ba6f9
feat(insights): testing in memory data
alexcambose 80a0429
chore: test with partial cache
alexcambose a3d71ee
perf: reduce partial cache
alexcambose 092cdd5
chore: test with max memory setting
alexcambose 5ca1c24
chore: bump next version
alexcambose 7c69871
chore: add logs
alexcambose 5a0694a
chore: add cache test function
alexcambose c8f9edb
chore: test with unstable cache
alexcambose 970fec1
chore: test with function tags
alexcambose 90b3668
perf: chunk cache
alexcambose ca16ed8
fix: add cache buffer
alexcambose f7c4e0d
fix: lint
alexcambose f226d34
feat: chunk cache
alexcambose f6ffcf7
feat: replaced more cached functions
alexcambose 022793b
fix: use in memory cache
alexcambose 3823e64
fix: cache key issue
alexcambose b06ac66
chore: added logging
alexcambose b5ac3f7
chore: revalidate false
alexcambose effbc28
feat: migrated to redis
alexcambose 0682c73
feat: added fetch routes
alexcambose 8b2719c
feat: added all endpoints
alexcambose fcbda8a
feat: more endpoint fetching
alexcambose 8daffbb
fix: types
alexcambose 9e29e54
refactor: cleanup
alexcambose b684cbe
fix: publishers metadata
alexcambose 2744ee0
fix: imports
alexcambose a3cfcda
refactor: minor fixes
alexcambose d9456ed
Merge branch 'main' into feat/perf-caching
alexcambose 5da02aa
Merge branch 'main' into feat/perf-caching
alexcambose 038d370
chore: updated pnpm
alexcambose 1872162
chore: updated pnpm
alexcambose 70e5bd4
chore: revert with fetch again
alexcambose 43ff345
refactor: addressed PR comments
alexcambose 9966636
feat: use zod for validation
alexcambose 498dbd0
fix: use origin headers
alexcambose 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
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
30 changes: 30 additions & 0 deletions
30
apps/insights/src/app/api/pyth/get-feeds-for-publisher/[publisher]/route.ts
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { stringify } from "superjson"; | ||
|
|
||
| import { getFeedsCached } from '../../../../../server/pyth/get-feeds'; | ||
| import { Cluster } from "../../../../../services/pyth"; | ||
|
|
||
| export const GET = async (request: Request, { params }: { params: Promise<{ publisher: string }> }) => { | ||
| const { publisher } = await params; | ||
| const { searchParams } = new URL(request.url); | ||
alexcambose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster; | ||
alexcambose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // check if cluster is valid | ||
| if (cluster && !Object.values(Cluster).includes(cluster)) { | ||
| return NextResponse.json({ error: "Invalid cluster" }, { status: 400 }); | ||
| } | ||
|
|
||
| if (!publisher) { | ||
| return NextResponse.json({ error: "Publisher is required" }, { status: 400 }); | ||
| } | ||
|
|
||
| const feeds = await getFeedsCached(cluster); | ||
|
|
||
| const filteredFeeds = feeds.filter((feed) => feed.price.priceComponents.some((c) => c.publisher === publisher)); | ||
|
|
||
| return new Response(stringify(filteredFeeds), { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
alexcambose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
25 changes: 25 additions & 0 deletions
25
apps/insights/src/app/api/pyth/get-feeds/[symbol]/route.ts
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { stringify } from 'superjson'; | ||
|
|
||
| import { getFeedsCached } from "../../../../../server/pyth/get-feeds"; | ||
| import { Cluster } from "../../../../../services/pyth"; | ||
|
|
||
| export const GET = async (request: Request, { params }: { params: Promise<{ symbol: string }> }) => { | ||
| const { symbol } = await params; | ||
| const { searchParams } = new URL(request.url); | ||
| const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster; | ||
|
|
||
| // check if cluster is valid | ||
| if (cluster && !Object.values(Cluster).includes(cluster)) { | ||
| return NextResponse.json({ error: "Invalid cluster" }, { status: 400 }); | ||
| } | ||
|
|
||
| const feeds = await getFeedsCached(cluster); | ||
| const feed = feeds.find((feed) => feed.symbol === symbol); | ||
|
|
||
| return new Response(stringify(feed), { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| } | ||
| }); | ||
| }; |
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { stringify } from 'superjson'; | ||
| import type { z } from 'zod'; | ||
|
|
||
| import { getFeedsCached } from "../../../../server/pyth/get-feeds"; | ||
| import { Cluster, priceFeedsSchema } from "../../../../services/pyth"; | ||
|
|
||
| export const GET = async (request: Request) => { | ||
| // get cluster from query params | ||
| const { searchParams } = new URL(request.url); | ||
| const excludePriceComponents = searchParams.get("excludePriceComponents") === "true"; | ||
| const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster; | ||
alexcambose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // check if cluster is valid | ||
| if (cluster && !Object.values(Cluster).includes(cluster)) { | ||
| return NextResponse.json({ error: "Invalid cluster" }, { status: 400 }); | ||
| } | ||
|
|
||
| let feeds = await getFeedsCached(cluster) as Omit<z.infer<typeof priceFeedsSchema>[number], 'price'>[]; | ||
|
||
|
|
||
| if(excludePriceComponents) { | ||
| feeds = feeds.map((feed) => ({ | ||
| ...feed, | ||
| price: undefined, | ||
| })); | ||
| } | ||
|
|
||
| return new Response(stringify(feeds), { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }; | ||
24 changes: 24 additions & 0 deletions
24
apps/insights/src/app/api/pyth/get-publishers/[symbol]/route.ts
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| import { getPublishersForClusterCached } from "../../../../../server/pyth/get-publishers-for-cluster"; | ||
| import { Cluster } from "../../../../../services/pyth"; | ||
|
|
||
| export const GET = async (request: Request, { params }: { params: Promise<{ symbol: string }> }) => { | ||
| const { symbol } = await params; | ||
| // get cluster from query params | ||
| const { searchParams } = new URL(request.url); | ||
| const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster; | ||
|
|
||
| // check if cluster is valid | ||
| if (cluster && !Object.values(Cluster).includes(cluster)) { | ||
| return NextResponse.json({ error: "Invalid cluster" }, { status: 400 }); | ||
| } | ||
|
|
||
| if (!symbol) { | ||
| return NextResponse.json({ error: "Symbol is required" }, { status: 400 }); | ||
| } | ||
|
|
||
| const map = await getPublishersForClusterCached(cluster); | ||
|
|
||
| return NextResponse.json(map[symbol] ?? []); | ||
| }; |
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
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
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 |
|---|---|---|
| @@ -1,20 +1,31 @@ | ||
| import { notFound } from "next/navigation"; | ||
| import { parse } from "superjson"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { Cluster, getFeeds } from "../../services/pyth"; | ||
| import { PUBLIC_URL, VERCEL_AUTOMATION_BYPASS_SECRET } from '../../config/server'; | ||
| import { getFeedForSymbolCached } from '../../server/pyth'; | ||
| import { Cluster, priceFeedsSchema } from "../../services/pyth"; | ||
| import { DEFAULT_CACHE_TTL } from '../../utils/cache'; | ||
|
|
||
| export const getFeed = async (params: Promise<{ slug: string }>) => { | ||
| "use cache"; | ||
| const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${Cluster.Pythnet.toString()}&excludePriceComponents=true`, { | ||
| next: { | ||
| revalidate: DEFAULT_CACHE_TTL, | ||
| }, | ||
| headers: { | ||
| 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET, | ||
alexcambose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }); | ||
| const dataJson = await data.text(); | ||
| const feeds: z.infer<typeof priceFeedsSchema> = parse(dataJson); | ||
|
|
||
| const [{ slug }, feeds] = await Promise.all([params, getPythnetFeeds()]); | ||
| const { slug } = await params; | ||
| const symbol = decodeURIComponent(slug); | ||
| const feed = await getFeedForSymbolCached({symbol, cluster: Cluster.Pythnet}); | ||
|
|
||
| return { | ||
| feeds, | ||
| feed: feeds.find((item) => item.symbol === symbol) ?? notFound(), | ||
| feed: feed ?? notFound(), | ||
| symbol, | ||
| } as const; | ||
| }; | ||
|
|
||
| const getPythnetFeeds = async () => { | ||
| "use cache"; | ||
| return getFeeds(Cluster.Pythnet); | ||
| }; | ||
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
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
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
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
Oops, something went wrong.
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.