Skip to content

Commit

Permalink
Merge pull request #50 from pancakeswap/fix-supply-api
Browse files Browse the repository at this point in the history
fix supply api
  • Loading branch information
chef-huan authored Jul 18, 2024
2 parents de8c97b + 00b4535 commit 2164b7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
35 changes: 17 additions & 18 deletions api/supply.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
import { NowRequest, NowResponse } from "@vercel/node";
import {
getBurnedSupply,
getDeadSupply,
getLockedCake,
getTotalSupply,
getTotalMint,
planetFinanceBurnedTokensWei,
maxSupply,
getVeCakeLocked,
} from "../utils/supply";
import formatNumber from "../utils/formatNumber";
import BigNumber from "bignumber.js";

export default async (req: NowRequest, res: NowResponse): Promise<void> => {
let totalSupply = await getTotalSupply();
totalSupply = totalSupply.div(1e18);
let totalMint = await getTotalMint();
totalMint = totalMint.div(1e18);

let burnedSupply = await getBurnedSupply();
burnedSupply = burnedSupply.div(1e18);
let deadSupply = await getDeadSupply();
deadSupply = deadSupply.div(1e18);

let lockedCakePool = await getLockedCake();
lockedCakePool = lockedCakePool.div(1e18);

let lockedVeCake = await getVeCakeLocked();
lockedVeCake = lockedVeCake.div(1e18);

let lockedCake = lockedCakePool.plus(lockedVeCake);
let totalLockedCake = lockedCakePool.plus(lockedVeCake);

const planetFinanceBurnedTokens = planetFinanceBurnedTokensWei.div(1e18);

const totalBurnedTokens = burnedSupply.plus(planetFinanceBurnedTokens);
const totalBurnedTokens = deadSupply.plus(planetFinanceBurnedTokens);

const burnedAndLockedTokens = totalBurnedTokens.plus(lockedCake);
const burnedAndLockedTokens = totalBurnedTokens.plus(totalLockedCake);

const unburntCake = totalSupply.minus(totalBurnedTokens);
const totalSupply = totalMint.minus(totalBurnedTokens);

const circulatingSupply = totalSupply.minus(burnedAndLockedTokens);
const circulatingSupply = totalMint.minus(burnedAndLockedTokens);

if (req.query?.q === "totalSupply") {
res.json(unburntCake.toNumber());
res.json(totalSupply.toNumber());
} else if (req.query?.q === "circulatingSupply") {
res.json(circulatingSupply.toNumber());
} else if (req.query?.verbose) {
res.json({
totalMinted: formatNumber(totalSupply.toNumber()),
totalSupply: formatNumber(unburntCake.toNumber()),
burnedSupply: formatNumber(burnedSupply.toNumber()),
totalMinted: formatNumber(totalMint.toNumber()),
totalSupply: formatNumber(totalSupply.toNumber()),
burnedSupply: formatNumber(totalBurnedTokens.toNumber()),
circulatingSupply: formatNumber(circulatingSupply.toNumber()),
lockedCake: formatNumber(lockedCake.toNumber()),
lockedCake: formatNumber(totalLockedCake.toNumber()),
maxSupply: formatNumber(maxSupply),
});
} else {
res.json({
totalSupply: unburntCake.toNumber(),
totalSupply: totalSupply.toNumber(),
burnedSupply: totalBurnedTokens.toNumber(),
circulatingSupply: circulatingSupply.toNumber(),
});
Expand Down
15 changes: 15 additions & 0 deletions utils/abis/veCake.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"inputs": [],
"name": "supply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
14 changes: 8 additions & 6 deletions utils/supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { getContract } from "./web3";
import { CAKE, DEAD, LOCKED_CAKE_POOL, VECAKE } from "./constants";
import bep20 from "./abis/bep20.json";
import lockedCakePool from "./abis/lockedCakePool.json";
import veCake from "./abis/veCake.json";

const contract = getContract(bep20, CAKE);
const cakeContract = getContract(bep20, CAKE);
const veCakeContract = getContract(veCake, VECAKE);
const lockedCakePoolContract = getContract(lockedCakePool, LOCKED_CAKE_POOL);

export const getTotalSupply = async (): Promise<BigNumber> => {
const supply = await contract.methods.totalSupply().call();
export const getTotalMint = async (): Promise<BigNumber> => {
const supply = await cakeContract.methods.totalSupply().call();

return new BigNumber(supply);
};

export const getBurnedSupply = async (): Promise<BigNumber> => {
const balance = await contract.methods.balanceOf(DEAD).call();
export const getDeadSupply = async (): Promise<BigNumber> => {
const balance = await cakeContract.methods.balanceOf(DEAD).call();

return new BigNumber(balance);
};
Expand All @@ -25,7 +27,7 @@ export const getLockedCake = async (): Promise<BigNumber> => {
};

export const getVeCakeLocked = async (): Promise<BigNumber> => {
const balance = await contract.methods.balanceOf(VECAKE).call();
const balance = await veCakeContract.methods.supply().call();
return new BigNumber(balance);
};

Expand Down

0 comments on commit 2164b7c

Please sign in to comment.