Skip to content

Commit

Permalink
must-be-carbon/feat/2377 add supply endpoint for KLIMA & BCT (#2382)
Browse files Browse the repository at this point in the history
* feat(app): add API endpoint for supply

* chore(github): fix typo in PR template

* feat(app): add support for supply by token

* feat(app): shorten api paths

* refactor(app): use getTokenDecimals

* refactor(app): make type a required param
  • Loading branch information
must-be-carbon authored Jul 30, 2024
1 parent 10ef099 commit ecdfb69
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Also related to <issue-number>

## How to Test
<!--
Pleas eprovide a shrot description of how a reviewer can confirm the changes
Please provide a short description of how a reviewer can confirm the changes
-->

1. _step1_
Expand Down
99 changes: 99 additions & 0 deletions app/pages/api/supply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
getContract,
getStaticProvider,
getTokenDecimals,
} from "@klimadao/lib/utils";
import { formatUnits } from "ethers/lib/utils";
import { DEFAULT_NETWORK } from "lib/constants";
import type { NextApiRequest, NextApiResponse } from "next";

export const config = {
api: {},
// Specifies the maximum allowed duration for this function to execute (in seconds)
maxDuration: 5,
};

type APIResponse =
| number
| {
totalSupply: number;
circulatingSupply: number;
}
| {
error: string;
};

const DEFAULT_TOKENS = ["klima", "bct"];

/**
* @description fetch total supply for a token
* @param req
* @param res
* @example GET /api/supply?type=total&token=klima Returns number
* @example GET /api/supply?type=circulating&token=klima Return number
*/
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<APIResponse>
) {
try {
switch (req.method) {
case "GET":
const { type, token } = req.query;

if (!token || !type) {
return res
.status(400)
.send({ error: "Token or type is missing from params" });
}

if (typeof token !== "string") {
return res.status(400).send({ error: "Token should be a string" });
}

const targetToken = DEFAULT_TOKENS.find((t) => t === token);

if (!targetToken) {
return res.status(400).send({ error: "Invalid token" });
}

const provider = getStaticProvider({
chain: "polygon",
});

const contract = getContract({
// TODO: Improve typing by exporting ContractName type from lib
contractName: targetToken as "klima" | "bct",
network: DEFAULT_NETWORK,
provider,
});

const totalSupply = await contract.totalSupply();

if (!totalSupply) {
return res.status(500).send({ error: "Failed to get total supply" });
}

const totalSupplyNum = Number(
formatUnits(totalSupply, getTokenDecimals(targetToken))
);

switch (type) {
case "total":
case "circulating":
return res.status(200).send(totalSupplyNum);
default:
return res.status(400).send({ error: "Invalid type" });
}

default:
res.setHeader("Allow", ["GET"]);
return res
.status(405)
.send({ error: `Method ${req.method} Not Allowed` });
}
} catch (error) {
console.error(error);
return res.status(500).send({ error: "Internal Server Error" });
}
}

0 comments on commit ecdfb69

Please sign in to comment.