Skip to content

Commit 43ff345

Browse files
committed
refactor: addressed PR comments
1 parent 70e5bd4 commit 43ff345

File tree

28 files changed

+571
-301
lines changed

28 files changed

+571
-301
lines changed

apps/insights/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@pythnetwork/known-publishers": "workspace:*",
2929
"@react-hookz/web": "catalog:",
3030
"@solana/web3.js": "catalog:",
31-
"async-cache-dedupe": "^3.0.0",
31+
"async-cache-dedupe": "catalog:",
3232
"bs58": "catalog:",
3333
"clsx": "catalog:",
3434
"cryptocurrency-icons": "catalog:",
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
import { NextResponse } from "next/server";
1+
import { NextRequest, NextResponse } from "next/server";
22
import { stringify } from "superjson";
33

4-
import { getFeedsCached } from '../../../../../server/pyth/get-feeds';
5-
import { Cluster } from "../../../../../services/pyth";
4+
import { Cluster, parseCluster } from "../../../../../services/pyth";
5+
import { getFeeds } from "../../../../../services/pyth/get-feeds";
66

7-
export const GET = async (request: Request, { params }: { params: Promise<{ publisher: string }> }) => {
7+
export const GET = async (
8+
request: NextRequest,
9+
{ params }: { params: Promise<{ publisher: string }> },
10+
) => {
811
const { publisher } = await params;
9-
const { searchParams } = new URL(request.url);
10-
const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
12+
const clusterName = request.nextUrl.searchParams.get("cluster");
1113

12-
// check if cluster is valid
13-
if (cluster && !Object.values(Cluster).includes(cluster)) {
14-
return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
14+
const cluster =
15+
clusterName === null ? Cluster.Pythnet : parseCluster(clusterName);
16+
17+
if (cluster === undefined) {
18+
return NextResponse.json({ error: "Invalid Cluster" }, { status: 400 });
1519
}
1620

1721
if (!publisher) {
18-
return NextResponse.json({ error: "Publisher is required" }, { status: 400 });
19-
}
22+
return NextResponse.json(
23+
{ error: "Publisher is required" },
24+
{ status: 400 },
25+
);
26+
}
27+
28+
const feeds = await getFeeds(cluster);
2029

21-
const feeds = await getFeedsCached(cluster);
30+
const filteredFeeds = feeds.filter((feed) =>
31+
feed.price.priceComponents.some((c) => c.publisher === publisher),
32+
);
2233

23-
const filteredFeeds = feeds.filter((feed) => feed.price.priceComponents.some((c) => c.publisher === publisher));
24-
2534
return new Response(stringify(filteredFeeds), {
2635
headers: {
27-
'Content-Type': 'application/json',
36+
"Content-Type": "application/json",
2837
},
2938
});
30-
};
39+
};
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import { NextResponse } from "next/server";
2-
import { stringify } from 'superjson';
2+
import { stringify } from "superjson";
33

4-
import { getFeedsCached } from "../../../../../server/pyth/get-feeds";
54
import { Cluster } from "../../../../../services/pyth";
5+
import { getFeeds } from "../../../../../services/pyth/get-feeds";
66

7-
export const GET = async (request: Request, { params }: { params: Promise<{ symbol: string }> }) => {
7+
export const GET = async (
8+
request: Request,
9+
{ params }: { params: Promise<{ symbol: string }> },
10+
) => {
811
const { symbol } = await params;
912
const { searchParams } = new URL(request.url);
10-
const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
13+
const cluster = Number.parseInt(
14+
searchParams.get("cluster") ?? Cluster.Pythnet.toString(),
15+
) as Cluster;
1116

1217
// check if cluster is valid
1318
if (cluster && !Object.values(Cluster).includes(cluster)) {
1419
return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
1520
}
16-
17-
const feeds = await getFeedsCached(cluster);
21+
22+
const feeds = await getFeeds(cluster);
1823
const feed = feeds.find((feed) => feed.symbol === symbol);
19-
24+
2025
return new Response(stringify(feed), {
2126
headers: {
22-
'Content-Type': 'application/json',
23-
}
27+
"Content-Type": "application/json",
28+
},
2429
});
25-
};
30+
};
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import { NextResponse } from "next/server";
2-
import { stringify } from 'superjson';
3-
import type { z } from 'zod';
2+
import { stringify } from "superjson";
43

5-
import { getFeedsCached } from "../../../../server/pyth/get-feeds";
6-
import { Cluster, priceFeedsSchema } from "../../../../services/pyth";
4+
import { Cluster } from "../../../../services/pyth";
5+
import { getFeeds } from "../../../../services/pyth/get-feeds";
76

87
export const GET = async (request: Request) => {
98
// get cluster from query params
109
const { searchParams } = new URL(request.url);
11-
const excludePriceComponents = searchParams.get("excludePriceComponents") === "true";
12-
const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
10+
const excludePriceComponents =
11+
searchParams.get("excludePriceComponents") === "true";
12+
const cluster = Number.parseInt(
13+
searchParams.get("cluster") ?? Cluster.Pythnet.toString(),
14+
) as Cluster;
1315

1416
// check if cluster is valid
1517
if (cluster && !Object.values(Cluster).includes(cluster)) {
1618
return NextResponse.json({ error: "Invalid cluster" }, { status: 400 });
1719
}
1820

19-
let feeds = await getFeedsCached(cluster) as Omit<z.infer<typeof priceFeedsSchema>[number], 'price'>[];
20-
21-
if(excludePriceComponents) {
22-
feeds = feeds.map((feed) => ({
23-
...feed,
24-
price: undefined,
25-
}));
26-
}
27-
28-
return new Response(stringify(feeds), {
21+
const feeds = await getFeeds(cluster);
22+
const filteredFeeds = excludePriceComponents
23+
? feeds.map((feed) => {
24+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
25+
const { price, ...rest } = feed;
26+
return rest;
27+
})
28+
: feeds;
29+
30+
return new Response(stringify(filteredFeeds), {
2931
headers: {
30-
'Content-Type': 'application/json',
32+
"Content-Type": "application/json",
3133
},
3234
});
33-
};
35+
};
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { NextResponse } from "next/server";
22

3-
import { getPublishersForClusterCached } from "../../../../../server/pyth/get-publishers-for-cluster";
43
import { Cluster } from "../../../../../services/pyth";
4+
import { getPublishersForCluster } from "../../../../../services/pyth/get-publishers-for-cluster";
55

6-
export const GET = async (request: Request, { params }: { params: Promise<{ symbol: string }> }) => {
6+
export const GET = async (
7+
request: Request,
8+
{ params }: { params: Promise<{ symbol: string }> },
9+
) => {
710
const { symbol } = await params;
811
// get cluster from query params
912
const { searchParams } = new URL(request.url);
10-
const cluster = Number.parseInt(searchParams.get("cluster") ?? Cluster.Pythnet.toString()) as Cluster;
13+
const cluster = Number.parseInt(
14+
searchParams.get("cluster") ?? Cluster.Pythnet.toString(),
15+
) as Cluster;
1116

1217
// check if cluster is valid
1318
if (cluster && !Object.values(Cluster).includes(cluster)) {
@@ -18,7 +23,7 @@ export const GET = async (request: Request, { params }: { params: Promise<{ symb
1823
return NextResponse.json({ error: "Symbol is required" }, { status: 400 });
1924
}
2025

21-
const map = await getPublishersForClusterCached(cluster);
26+
const map = await getPublishersForCluster(cluster);
2227

2328
return NextResponse.json(map[symbol] ?? []);
24-
};
29+
};

apps/insights/src/app/component-score-history/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export const GET = async (req: NextRequest) => {
1616
);
1717
if (parsed.success) {
1818
const { cluster, publisherKey, symbol, from, to } = parsed.data;
19-
const data = await getFeedScoreHistory(
19+
const data = await getFeedScoreHistory({
2020
cluster,
2121
publisherKey,
2222
symbol,
2323
from,
2424
to,
25-
);
25+
});
2626
return Response.json(data);
2727
} else {
2828
return new Response(fromError(parsed.error).toString(), {

apps/insights/src/app/historical-prices/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ export async function GET(req: NextRequest) {
66
const symbol = req.nextUrl.searchParams.get("symbol");
77
const until = req.nextUrl.searchParams.get("until");
88
if (symbol && until) {
9-
const res = await getHistoricalPrices(decodeURIComponent(symbol), until);
9+
const res = await getHistoricalPrices({
10+
symbol: decodeURIComponent(symbol),
11+
until,
12+
});
1013
return Response.json(res);
1114
} else {
1215
return new Response("Must provide `symbol` and `until`", { status: 400 });

apps/insights/src/app/price-feeds/[slug]/layout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { Metadata } from "next";
22
import { notFound } from "next/navigation";
33
import type { ReactNode } from "react";
44

5-
import { getFeedsCached } from "../../../server/pyth/get-feeds";
65
import { Cluster } from "../../../services/pyth";
6+
import { getFeeds } from "../../../services/pyth/get-feeds";
77

88
export { PriceFeedLayout as default } from "../../../components/PriceFeed/layout";
99

@@ -20,7 +20,7 @@ export const generateMetadata = async ({
2020
}: Props): Promise<Metadata> => {
2121
const [{ slug }, feeds] = await Promise.all([
2222
params,
23-
getFeedsCached(Cluster.Pythnet),
23+
getFeeds(Cluster.Pythnet),
2424
]);
2525
const symbol = decodeURIComponent(slug);
2626
const feed = feeds.find((item) => item.symbol === symbol);

apps/insights/src/components/Overview/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import PriceFeedsLight from "./price-feeds-light.svg";
99
import PublishersDark from "./publishers-dark.svg";
1010
import PublishersLight from "./publishers-light.svg";
1111
import { TabList } from "./tab-list";
12-
import { getFeedsCached } from "../../server/pyth/get-feeds";
1312
import { Cluster } from "../../services/pyth";
13+
import { getFeeds } from "../../services/pyth/get-feeds";
1414
import {
1515
totalVolumeTraded,
1616
activeChains,
@@ -24,7 +24,7 @@ import { FormattedDate } from "../FormattedDate";
2424
import { FormattedNumber } from "../FormattedNumber";
2525

2626
export const Overview = async () => {
27-
const priceFeeds = await getFeedsCached(Cluster.Pythnet);
27+
const priceFeeds = await getFeeds(Cluster.Pythnet);
2828
const today = new Date();
2929

3030
const feedCounts = [

apps/insights/src/components/PriceFeed/get-feed.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
import { notFound } from "next/navigation";
2-
import { parse } from "superjson";
3-
import { z } from "zod";
42

5-
import { PUBLIC_URL, VERCEL_AUTOMATION_BYPASS_SECRET } from '../../config/server';
6-
import { getFeedForSymbolCached } from '../../server/pyth';
7-
import { Cluster, priceFeedsSchema } from "../../services/pyth";
8-
import { DEFAULT_CACHE_TTL } from '../../utils/cache';
3+
import { getFeedForSymbolRequest, getFeedsRequest } from "../../server/pyth";
4+
import { Cluster } from "../../services/pyth";
95

106
export const getFeed = async (params: Promise<{ slug: string }>) => {
11-
const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${Cluster.Pythnet.toString()}&excludePriceComponents=true`, {
12-
next: {
13-
revalidate: DEFAULT_CACHE_TTL,
14-
},
15-
headers: {
16-
'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET,
17-
},
18-
});
19-
const dataJson = await data.text();
20-
const feeds: z.infer<typeof priceFeedsSchema> = parse(dataJson);
7+
const feeds = await getFeedsRequest(Cluster.Pythnet);
218

229
const { slug } = await params;
2310
const symbol = decodeURIComponent(slug);
24-
const feed = await getFeedForSymbolCached({symbol, cluster: Cluster.Pythnet});
11+
const feed = await getFeedForSymbolRequest({
12+
symbol,
13+
cluster: Cluster.Pythnet,
14+
});
2515

2616
return {
2717
feeds,

0 commit comments

Comments
 (0)