1
- import { Cluster , getFeeds , getFeedsForPublisher , getPublishersForFeed } from "../services/pyth" ;
1
+ import type { PriceData , Product } from '@pythnetwork/client' ;
2
+ import superjson from "superjson" ;
2
3
3
- export const getFeedsCached = async ( cluster : Cluster ) => {
4
+ import { Cluster , clients , getFeeds , priceFeedsSchema } from "../services/pyth" ;
5
+
6
+ export const getPublishersForFeed = async (
7
+ cluster : Cluster ,
8
+ ) => {
4
9
"use cache" ;
5
- return getFeeds ( cluster ) ;
10
+ const data = await clients [ cluster ] . getData ( ) ;
11
+ const result : Record < string , string [ ] > = { } ;
12
+ for ( const key of data . productPrice . keys ( ) ) {
13
+ const price = data . productPrice . get ( key ) ;
14
+ result [ key ] = price ?. priceComponents . map ( ( { publisher } ) => publisher . toBase58 ( ) ) ?? [ ] ;
15
+ }
16
+ return result ;
6
17
} ;
7
18
8
- export const getFeedsForPublisherCached = async ( cluster : Cluster , key : string ) => {
19
+ const getAllFeeds = async ( cluster : Cluster ) => {
9
20
"use cache" ;
10
- return getFeedsForPublisher ( cluster , key ) ;
21
+ const data = await clients [ cluster ] . getData ( ) ;
22
+ return superjson . stringify ( data . symbols . filter (
23
+ ( symbol ) =>
24
+ data . productFromSymbol . get ( symbol ) ?. display_symbol !== undefined ,
25
+ ) . map ( ( symbol ) => ( {
26
+ symbol,
27
+ product : data . productFromSymbol . get ( symbol ) ,
28
+ price : data . productPrice . get ( symbol ) ,
29
+ } ) ) )
30
+ }
31
+
32
+ export const getFeedsForPublisherCached = async (
33
+ cluster : Cluster ,
34
+ publisher : string ,
35
+ ) => {
36
+ const feeds = superjson . parse < { symbol : string , product : Product , price : PriceData } [ ] > ( await getAllFeeds ( cluster ) ) ;
37
+ return priceFeedsSchema . parse ( feeds . filter ( ( { price } ) =>
38
+ price . priceComponents . some (
39
+ ( component ) => component . publisher . toString ( ) === publisher ,
40
+ ) ,
41
+ ) ) ;
11
42
} ;
12
43
13
- export const getPublishersForFeedCached = async ( cluster : Cluster , symbol : string ) => {
44
+ export const getFeedsCached = async ( cluster : Cluster ) => {
14
45
"use cache" ;
15
- return getPublishersForFeed ( cluster , symbol ) ;
46
+ return getFeeds ( cluster ) ;
47
+ } ;
48
+
49
+ export const getPublishersForFeedCached = async ( cluster : Cluster , symbol : string ) => {
50
+ const data = await getPublishersForFeed ( cluster ) ;
51
+ return data [ symbol ] ;
16
52
} ;
0 commit comments