1- import { parse , } from "superjson" ;
1+ import { unstable_cache } from "next/cache" ;
2+ import { parse , stringify } from "superjson" ;
23import { z } from "zod" ;
34
45import { PUBLIC_URL , VERCEL_AUTOMATION_BYPASS_SECRET } from '../config/server' ;
56import { Cluster , priceFeedsSchema } from "../services/pyth" ;
67import { DEFAULT_CACHE_TTL } from "../utils/cache" ;
8+ import { getFeedsCached as _getFeedsCached } from "./pyth/get-feeds" ;
79
8- // Convenience helpers matching your previous functions
910export async function getPublishersForFeedCached (
1011 cluster : Cluster ,
1112 symbol : string
@@ -37,35 +38,60 @@ export async function getFeedsForPublisherCached(
3738 return parse < z . infer < typeof priceFeedsSchema > > ( rawData ) ;
3839}
3940
41+ const _privateGetFeeds = unstable_cache ( async ( cluster : Cluster ) => {
42+ const start = Date . now ( ) ;
43+ const feeds = await _getFeedsCached ( cluster ) as Omit < z . infer < typeof priceFeedsSchema > [ number ] , 'price' > [ ] ;
44+ const end = Date . now ( ) ;
45+ // eslint-disable-next-line no-console
46+ console . log ( "internal getFeedsCached" , cluster , end - start ) ;
47+ return feeds . map ( ( feed ) => ( {
48+ ...feed ,
49+ price : undefined ,
50+ } ) ) ;
51+ // const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, {
52+ // next: {
53+ // revalidate: DEFAULT_CACHE_TTL,
54+ // },
55+ // headers: {
56+ // 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET,
57+ // },
58+ // });
59+ // const dataJson = await data.text();
60+ // const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson);
61+ // return feeds;
62+ } , [ 'getFeedsCached__' ] , {
63+ revalidate : DEFAULT_CACHE_TTL ,
64+ } ) ;
65+
4066export const getFeedsCached = async ( cluster : Cluster ) => {
41- const data = await fetch ( `${ PUBLIC_URL } /api/pyth/get-feeds?cluster=${ cluster . toString ( ) } &excludePriceComponents=true` , {
42- next : {
43- revalidate : DEFAULT_CACHE_TTL ,
44- } ,
45- headers : {
46- 'x-vercel-protection-bypass' : VERCEL_AUTOMATION_BYPASS_SECRET ,
47- } ,
48- } ) ;
49- const dataJson = await data . text ( ) ;
50- const feeds : Omit < z . infer < typeof priceFeedsSchema > [ 0 ] , "price" > [ ] = parse ( dataJson ) ;
51- return feeds ;
67+ const start = Date . now ( ) ;
68+ const data = await _privateGetFeeds ( cluster ) ;
69+ const end = Date . now ( ) ;
70+ // eslint-disable-next-line no-console
71+ console . log ( "getFeedsCached" , end - start ) ;
72+ return data ;
73+ } ;
74+
75+ const _getFeedsForSymbol = async ( symbol : string , cluster : Cluster ) => {
76+ const feeds = await _getFeedsCached ( cluster ) ;
77+ const feed = feeds . find ( ( feed ) => feed . symbol === symbol ) ;
78+ return feed ;
5279}
5380
54- export const getFeedForSymbolCached = async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) : Promise < z . infer < typeof priceFeedsSchema > [ 0 ] | undefined > => {
55- const data = await fetch ( `${ PUBLIC_URL } /api/pyth/get-feeds/${ encodeURIComponent ( symbol ) } ?cluster=${ cluster . toString ( ) } ` , {
56- next : {
57- revalidate : DEFAULT_CACHE_TTL ,
58- } ,
59- headers : {
60- 'x-vercel-protection-bypass' : VERCEL_AUTOMATION_BYPASS_SECRET ,
61- } ,
62- } ) ;
63-
64- if ( ! data . ok ) {
65- return undefined ;
66- }
6781
68- const dataJson = await data . text ( ) ;
69- const feed : z . infer < typeof priceFeedsSchema > [ 0 ] = parse ( dataJson ) ;
70- return feed ;
71- }
82+
83+ export const getFeedForSymbol = unstable_cache ( async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
84+ const data = await _getFeedsForSymbol ( symbol , cluster ) ;
85+ return stringify ( data ) ;
86+ } , [ 'getFeedForSymbolCached__' ] , {
87+ revalidate : DEFAULT_CACHE_TTL ,
88+ } ) ;
89+
90+ export const getFeedForSymbolCached = async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
91+ const start = Date . now ( ) ;
92+ const data = await getFeedForSymbol ( { symbol, cluster} ) ;
93+ const end = Date . now ( ) ;
94+ // eslint-disable-next-line no-console
95+ console . log ( "getFeedForSymbolCached" , symbol , cluster , end - start ) ;
96+ return parse < ReturnType < typeof _getFeedsForSymbol > > ( data ) ;
97+ } ;
0 commit comments