feature: show pools apr
This commit is contained in:
22
app/javascript/src/utils/apr.ts
Normal file
22
app/javascript/src/utils/apr.ts
Normal 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();
|
||||
};
|
||||
21
app/javascript/src/utils/getPrice.ts
Normal file
21
app/javascript/src/utils/getPrice.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
30
app/javascript/src/utils/getTotalStaked.ts
Normal file
30
app/javascript/src/utils/getTotalStaked.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user