feature: show pools apr

This commit is contained in:
Claudio Ramos
2021-08-15 02:21:39 -03:00
parent c1129b9953
commit 9e96a3664b
17 changed files with 996 additions and 145 deletions

View File

@@ -0,0 +1,22 @@
import BigNumber from "bignumber.js";
import { BLOCKS_PER_YEAR } from "../constants";
export const getApr = (
stakingTokenPrice: number,
rewardTokenPrice: number,
totalStaked: number,
tokenPerBlock: number
) => {
const totalRewardPricePerYear = new BigNumber(rewardTokenPrice)
.times(tokenPerBlock)
.times(BLOCKS_PER_YEAR);
const totalStakingTokenInPool = new BigNumber(stakingTokenPrice).times(
totalStaked
);
const apr = totalRewardPricePerYear.div(totalStakingTokenInPool).times(100);
return apr.isNaN() || !apr.isFinite() ? null : apr.toNumber();
};

View File

@@ -0,0 +1,21 @@
import { ethers } from "ethers";
import { tokens } from "../constants/pancake/Tokens";
import type { Token } from "../constants/pancake/Tokens";
import type { BscContext } from "../contexts/BscProvider";
// 1 Wei = 1*10^18 Ether
const ONE_BUSD_IN_WEI = ethers.utils.parseUnits("1", 18);
export const getPriceInBusd = async (router: any, token: Token) => {
try {
const result = await router.getAmountsOut(ONE_BUSD_IN_WEI, [
token.address["56"],
tokens.busd.address["56"],
]);
return result[1].toString() / 1e18;
} catch {
return 0;
}
};

View File

@@ -0,0 +1,30 @@
import { ethers } from "ethers";
import BigNumber from "bignumber.js";
import erc20 from "../abi/erc20.json";
import { Token } from "../constants/pancake/Tokens";
import type { PoolConfig } from "../types";
export const getTotalStaked = async (
provider: ethers.providers.Provider,
pool: PoolConfig
) => {
if (pool.stakingToken.symbol === "BNB") {
// TODO: BNB
return 0;
}
const contract = new ethers.Contract(
pool.stakingToken.address["56"],
erc20,
provider
);
try {
const result = await contract.balanceOf(pool.contractAddress["56"]);
return new BigNumber(result.toJSON().hex).toNumber();
} catch {
return 0;
}
};