Skip to content

Commit 853f018

Browse files
authored
Merge pull request #2291 from pyth-network/cprussin/update-endponits-for-cmc
fix(staking): match cmc endpoint format to expected format
2 parents 0ac24d8 + 3bcc183 commit 853f018

File tree

1 file changed

+35
-33
lines changed
  • apps/staking/src/app/api/v1/cmc/supply

1 file changed

+35
-33
lines changed
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
import { PythStakingClient } from "@pythnetwork/staking-sdk";
22
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
33
import { clusterApiUrl, Connection } from "@solana/web3.js";
4+
import { toNumber } from "dnum";
45
import type { NextRequest } from "next/server";
56
import { z } from "zod";
67

78
import { MAINNET_API_RPC } from "../../../../../config/server";
8-
import { tokensToString } from "../../../../../tokens";
9-
10-
const querySchema = z.enum(["totalSupply", "circulatingSupply"]);
9+
import { DECIMALS } from "../../../../../tokens";
1110

1211
export async function GET(req: NextRequest) {
13-
const isMainnet = req.nextUrl.searchParams.get("devnet") !== "true";
14-
const asDecimal = req.nextUrl.searchParams.get("as_decimal") === "true";
15-
const stakingClient = new PythStakingClient({
16-
connection: new Connection(
17-
isMainnet && MAINNET_API_RPC !== undefined
18-
? MAINNET_API_RPC
19-
: clusterApiUrl(WalletAdapterNetwork.Devnet),
20-
),
21-
});
22-
2312
const query = querySchema.safeParse(req.nextUrl.searchParams.get("q"));
24-
if (!query.success) {
25-
return Response.json(
26-
{
27-
error:
28-
"The 'q' query parameter must be one of 'totalSupply' or 'circulatingSupply'.",
29-
},
30-
{
31-
status: 400,
32-
},
13+
if (query.error) {
14+
return new Response(
15+
"The 'q' query parameter must be one of 'totalSupply' or 'circulatingSupply'.",
16+
{ status: 400 },
3317
);
18+
} else {
19+
const isMainnet = req.nextUrl.searchParams.get("devnet") !== "true";
20+
const asDecimal = req.nextUrl.searchParams.get("as_decimal") === "true";
21+
const circulating = query.data === "circulatingSupply";
22+
const supply = await getSupply(isMainnet, circulating);
23+
return Response.json(formatAmount(asDecimal, supply));
3424
}
35-
const q = query.data;
25+
}
3626

37-
if (q === "circulatingSupply") {
38-
const circulatingSupply = await stakingClient.getCirculatingSupply();
39-
return Response.json(
40-
asDecimal ? tokensToString(circulatingSupply) : Number(circulatingSupply),
41-
);
27+
const querySchema = z.enum(["totalSupply", "circulatingSupply"]);
28+
29+
const getSupply = async (isMainnet: boolean, circulating: boolean) => {
30+
const client = isMainnet ? mainnetClient : devnetClient;
31+
if (circulating) {
32+
return client.getCirculatingSupply();
4233
} else {
43-
const pythMint = await stakingClient.getPythTokenMint();
44-
return Response.json(
45-
asDecimal ? tokensToString(pythMint.supply) : Number(pythMint.supply),
46-
);
34+
const { supply } = await client.getPythTokenMint();
35+
return supply;
4736
}
48-
}
37+
};
38+
39+
const mainnetClient = new PythStakingClient({
40+
connection: new Connection(
41+
MAINNET_API_RPC ?? clusterApiUrl(WalletAdapterNetwork.Mainnet),
42+
),
43+
});
44+
45+
const devnetClient = new PythStakingClient({
46+
connection: new Connection(clusterApiUrl(WalletAdapterNetwork.Devnet)),
47+
});
48+
49+
const formatAmount = (asDecimal: boolean, amount: bigint) =>
50+
asDecimal ? toNumber([amount, DECIMALS]) : Number(amount);

0 commit comments

Comments
 (0)