|
1 | 1 | import { PythStakingClient } from "@pythnetwork/staking-sdk";
|
2 | 2 | import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
|
3 | 3 | import { clusterApiUrl, Connection } from "@solana/web3.js";
|
| 4 | +import { toNumber } from "dnum"; |
4 | 5 | import type { NextRequest } from "next/server";
|
5 | 6 | import { z } from "zod";
|
6 | 7 |
|
7 | 8 | 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"; |
11 | 10 |
|
12 | 11 | 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 |
| - |
23 | 12 | 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 }, |
33 | 17 | );
|
| 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)); |
34 | 24 | }
|
35 |
| - const q = query.data; |
| 25 | +} |
36 | 26 |
|
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(); |
42 | 33 | } 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; |
47 | 36 | }
|
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