Skip to content

Commit a3d71ee

Browse files
committed
perf: reduce partial cache
1 parent 80a0429 commit a3d71ee

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

apps/insights/src/server/pyth.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { PriceData, Product } from '@pythnetwork/client';
21
import superjson from "superjson";
2+
import { z } from 'zod';
33

44
import { Cluster, clients, getFeeds, priceFeedsSchema } from "../services/pyth";
55

@@ -19,21 +19,28 @@ export const getPublishersForFeed = async (
1919
const getAllFeeds = async (cluster: Cluster) => {
2020
"use cache";
2121
const data = await clients[cluster].getData();
22-
return superjson.stringify(data.symbols.filter(
22+
23+
return superjson.stringify(priceFeedsSchema.parse(data.symbols.filter(
2324
(symbol) =>
2425
data.productFromSymbol.get(symbol)?.display_symbol !== undefined,
2526
).map((symbol) => ({
2627
symbol,
2728
product: data.productFromSymbol.get(symbol),
28-
price: data.productPrice.get(symbol),
29-
})))
29+
price: {
30+
...data.productPrice.get(symbol),
31+
priceComponents: data.productPrice.get(symbol)?.priceComponents.map(({ publisher }) => ({
32+
publisher: publisher.toBase58(),
33+
})) ?? [],
34+
},
35+
}))))
3036
}
3137

3238
export const getFeedsForPublisherCached = async (
3339
cluster: Cluster,
3440
publisher: string,
3541
) => {
36-
const feeds = superjson.parse<{ symbol: string, product: Product, price: PriceData }[]>(await getAllFeeds(cluster));
42+
const rawFeeds = await getAllFeeds(cluster);
43+
const feeds = superjson.parse<z.infer<typeof priceFeedsSchema>>(rawFeeds);
3744
return priceFeedsSchema.parse(feeds.filter(({ price }) =>
3845
price.priceComponents.some(
3946
(component) => component.publisher.toString() === publisher,

apps/insights/src/services/pyth.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ export const getFeeds = async (cluster: Cluster) => {
6767
data.symbols.map((symbol) => ({
6868
symbol,
6969
product: data.productFromSymbol.get(symbol),
70-
price: data.productPrice.get(symbol),
70+
price: {
71+
...data.productPrice.get(symbol),
72+
priceComponents: data.productPrice.get(symbol)?.priceComponents.map(({ publisher }) => ({
73+
publisher: publisher.toBase58(),
74+
})) ?? [],
75+
},
7176
})),
7277
);
7378
};
@@ -123,6 +128,9 @@ export const priceFeedsSchema = z.array(
123128
minPublishers: z.number(),
124129
lastSlot: z.bigint(),
125130
validSlot: z.bigint(),
131+
priceComponents: z.array(z.object({
132+
publisher: z.string(),
133+
})),
126134
}),
127135
}),
128136
);

0 commit comments

Comments
 (0)