1- import type { PythHttpClientResult } from '@pythnetwork/client/lib/PythHttpClient' ;
2- import type { z } from 'zod' ;
1+ import { Cluster , priceFeedsSchema } from "../services/pyth" ;
2+ import { getFeeds } from './pyth/get-feeds' ;
3+ import { getPublishersForCluster } from './pyth/get-publishers-for-cluster' ;
34
4- import { Cluster , clients , priceFeedsSchema } from "../services/pyth" ;
5- import { createChunkedCacheFetcher , fetchAllChunks , timeFunction } from '../utils/cache' ;
6-
7-
8- type CacheEntry = {
9- data ?: PythHttpClientResult ;
10- timestamp ?: number ;
11- promise ?: Promise < PythHttpClientResult > ;
12- } ;
13-
14- const dataCache = new Map < Cluster , CacheEntry > ( ) ;
15- const CACHE_EXPIRY_MS = 24 * 60 * 60 ; // 1 day in seconds
16-
17- const getDataCached = async ( cluster : Cluster ) => {
18- const now = Date . now ( ) ;
19- const cached = dataCache . get ( cluster ) ;
20-
21- // Check if cache exists and is not expired
22- if ( cached ?. data && cached . timestamp && ( now - cached . timestamp ) < CACHE_EXPIRY_MS * 1000 ) {
23- return cached . data ;
24- }
25-
26- // Check if there's already a pending request
27- if ( cached ?. promise ) {
28- return cached . promise ;
29- }
30-
31- // eslint-disable-next-line no-console
32- console . log ( 'fetching fresh FULL data' ) ;
33-
34- // Create a new promise for the request
35- const promise = clients [ cluster ] . getData ( ) . then ( ( data ) => {
36- // Store the result in cache
37- dataCache . set ( cluster , { data, timestamp : now } ) ;
38- return data ;
39- } ) ;
40-
41- // Store the promise in cache to prevent duplicate requests
42- dataCache . set ( cluster , { promise } ) ;
43-
44- return promise ;
45- } ;
46-
47- const fetchFeeds = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
48- const unfilteredData = await getDataCached ( cluster ) ;
49- // eslint-disable-next-line no-console
50- console . log ( 'fetchFeeds called' ) ;
51- const filteredData = unfilteredData . symbols
52- . filter (
53- ( symbol ) =>
54- unfilteredData . productFromSymbol . get ( symbol ) ?. display_symbol !== undefined
55- )
56- . map ( ( symbol ) => ( {
57- symbol,
58- product : unfilteredData . productFromSymbol . get ( symbol ) ,
59- price : {
60- ...unfilteredData . productPrice . get ( symbol ) ,
61- priceComponents :
62- unfilteredData . productPrice
63- . get ( symbol )
64- ?. priceComponents . map ( ( { publisher } ) => ( {
65- publisher : publisher . toBase58 ( ) ,
66- } ) ) ?? [ ] ,
67- } ,
68- } ) ) ;
69- const parsedData = priceFeedsSchema . parse ( filteredData ) ;
70- return parsedData ;
71- } , 'getFeeds' ) ;
72-
73- const fetchPublishers = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
74- const data = await getDataCached ( cluster ) ;
75- // eslint-disable-next-line no-console
76- console . log ( 'fetchPublishers called' ) ;
77- const result : Record < string , string [ ] > = { } ;
78- for ( const key of data . productPrice . keys ( ) ) {
79- const price = data . productPrice . get ( key ) ;
80- result [ key ] = price ?. priceComponents . map ( ( { publisher } ) => publisher . toBase58 ( ) ) ?? [ ] ;
81- }
82- return result ;
83- } , 'fetchPublishers' ) ;
84-
85- export const getFeedsCached = async ( cluster : Cluster ) => {
86- return timeFunction ( async ( ) => {
87- return fetchAllChunks < z . infer < typeof priceFeedsSchema > , [ Cluster ] > ( fetchFeeds , cluster ) ;
88- } , 'getFeedsCached' ) ;
89- } ;
90-
91- export const getPublishersForFeedCached = async ( cluster : Cluster , symbol : string ) => {
92- const data = await timeFunction ( async ( ) => {
93- return fetchAllChunks < Record < string , string [ ] > , [ Cluster ] > ( fetchPublishers , cluster ) ;
94- } , 'getPublishersForFeedCached' ) ;
95- return data [ symbol ]
96- } ;
5+ // Convenience helpers matching your previous functions
6+ export async function getPublishersForFeedCached (
7+ cluster : Cluster ,
8+ symbol : string
9+ ) {
10+ const map = await getPublishersForCluster ( cluster ) ;
11+ return map [ symbol ] ?? [ ] ;
12+ }
9713
98- export const getFeedsForPublisherCached = async (
14+ export async function getFeedsForPublisherCached (
9915 cluster : Cluster ,
10016 publisher : string
101- ) => {
102- const data = await timeFunction ( async ( ) => {
103- return getFeedsCached ( cluster ) ;
104- } , 'getFeedsForPublisherCached' ) ;
17+ ) {
18+ const data = await getFeeds ( cluster ) ;
10519 return priceFeedsSchema . parse (
10620 data . filter ( ( { price } ) =>
107- price . priceComponents . some (
108- ( component ) => component . publisher . toString ( ) === publisher
109- )
21+ price . priceComponents . some ( ( c ) => c . publisher === publisher )
11022 )
11123 ) ;
112- } ;
24+ }
0 commit comments