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,63 @@ 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+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10_000 ) ) ;
44+ const feeds = await _getFeedsCached ( cluster ) as Omit < z . infer < typeof priceFeedsSchema > [ number ] , 'price' > [ ] ;
45+ const end = Date . now ( ) ;
46+ // eslint-disable-next-line no-console
47+ console . log ( "internal getFeedsCached" , cluster , end - start ) ;
48+ return feeds . map ( ( feed ) => ( {
49+ ...feed ,
50+ price : undefined ,
51+ } ) ) ;
52+ // const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, {
53+ // next: {
54+ // revalidate: DEFAULT_CACHE_TTL,
55+ // },
56+ // headers: {
57+ // 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET,
58+ // },
59+ // });
60+ // const dataJson = await data.text();
61+ // const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson);
62+ // return feeds;
63+ } , [ 'getFeedsCached__' ] , {
64+ revalidate : DEFAULT_CACHE_TTL ,
65+ } ) ;
66+
4067export 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 ;
68+ const start = Date . now ( ) ;
69+ const data = await _privateGetFeeds ( cluster ) ;
70+ const end = Date . now ( ) ;
71+ // eslint-disable-next-line no-console
72+ console . log ( "getFeedsCached" , end - start ) ;
73+ return data ;
74+ } ;
75+
76+ const _getFeedsForSymbol = async ( symbol : string , cluster : Cluster ) => {
77+ const feeds = await _getFeedsCached ( cluster ) ;
78+ const feed = feeds . find ( ( feed ) => feed . symbol === symbol ) ;
79+ return feed ;
5280}
5381
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- }
6782
68- const dataJson = await data . text ( ) ;
69- const feed : z . infer < typeof priceFeedsSchema > [ 0 ] = parse ( dataJson ) ;
70- return feed ;
71- }
83+
84+ export const getFeedForSymbol = unstable_cache ( async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
85+ const data = await _getFeedsForSymbol ( symbol , cluster ) ;
86+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10_000 ) ) ;
87+
88+ return stringify ( data ) ;
89+ } , [ 'getFeedForSymbolCached__' ] , {
90+ revalidate : DEFAULT_CACHE_TTL ,
91+ } ) ;
92+
93+ export const getFeedForSymbolCached = async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
94+ const start = Date . now ( ) ;
95+ const data = await getFeedForSymbol ( { symbol, cluster} ) ;
96+ const end = Date . now ( ) ;
97+ // eslint-disable-next-line no-console
98+ console . log ( "getFeedForSymbolCached" , symbol , cluster , end - start ) ;
99+ return parse < ReturnType < typeof _getFeedsForSymbol > > ( data ) ;
100+ } ;
0 commit comments