Skip to content

Commit 80a0429

Browse files
committed
chore: test with partial cache
1 parent 70ba6f9 commit 80a0429

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

apps/insights/src/server/pyth.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
import { Cluster, getFeeds, getFeedsForPublisher, getPublishersForFeed } from "../services/pyth";
1+
import type { PriceData, Product } from '@pythnetwork/client';
2+
import superjson from "superjson";
23

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+
) => {
49
"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;
617
};
718

8-
export const getFeedsForPublisherCached = async (cluster: Cluster, key: string) => {
19+
const getAllFeeds = async (cluster: Cluster) => {
920
"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+
));
1142
};
1243

13-
export const getPublishersForFeedCached = async (cluster: Cluster, symbol: string) => {
44+
export const getFeedsCached = async (cluster: Cluster) => {
1445
"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];
1652
};

apps/insights/src/services/pyth.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ export const toCluster = (name: (typeof CLUSTER_NAMES)[number]): Cluster => {
3737
}
3838
};
3939

40-
let inMemoryData: Awaited<ReturnType<typeof clients[Cluster]["getData"]>> | undefined;
41-
42-
const getData = async (cluster: Cluster) => {
43-
inMemoryData ??= await clients[cluster].getData();
44-
return inMemoryData;
45-
};
46-
4740
export const parseCluster = (name: string): Cluster | undefined =>
4841
(CLUSTER_NAMES as readonly string[]).includes(name)
4942
? toCluster(name as (typeof CLUSTER_NAMES)[number])
@@ -63,23 +56,13 @@ const mkClient = (cluster: Cluster) =>
6356
getPythProgramKeyForCluster(ClusterToName[cluster]),
6457
);
6558

66-
const clients = {
59+
export const clients = {
6760
[Cluster.Pythnet]: mkClient(Cluster.Pythnet),
6861
[Cluster.PythtestConformance]: mkClient(Cluster.PythtestConformance),
6962
} as const;
7063

71-
export const getPublishersForFeed = async (
72-
cluster: Cluster,
73-
symbol: string,
74-
) => {
75-
const data = await getData(cluster);
76-
return data.productPrice
77-
.get(symbol)
78-
?.priceComponents.map(({ publisher }) => publisher.toBase58());
79-
};
80-
8164
export const getFeeds = async (cluster: Cluster) => {
82-
const data = await getData(cluster);
65+
const data = await clients[cluster].getData();
8366
return priceFeedsSchema.parse(
8467
data.symbols.map((symbol) => ({
8568
symbol,
@@ -93,7 +76,7 @@ export const getFeedsForPublisher = async (
9376
cluster: Cluster,
9477
publisher: string,
9578
) => {
96-
const data = await getData(cluster);
79+
const data = await clients[cluster].getData();
9780
return priceFeedsSchema.parse(
9881
data.symbols
9982
.filter(
@@ -113,7 +96,7 @@ export const getFeedsForPublisher = async (
11396
);
11497
};
11598

116-
const priceFeedsSchema = z.array(
99+
export const priceFeedsSchema = z.array(
117100
z.object({
118101
symbol: z.string(),
119102
product: z.object({

0 commit comments

Comments
 (0)