2

So i'm trying to build most of the stuff from scratch to get a understanding of the inner workings of farms.

I am testing out the calculations by using a existing farm. SaltSwap

window.chef is = to web3.eth.Contract(MasterChef, "0xB4405445fFAcF2B86BC2bD7D1C874AC739265658");

    var totalAllocPoints = await window.chef.methods.totalAllocPoint().call(); // Get totalAlloc from Contract
    var tokensPerBlock = await window.chef.methods.saltPerBlock().call(); // Get TokensPerBlock from Contract
    var poolInfo = await window.chef.methods.poolInfo(pool).call();

    var poolAlloc = ((tokensPerBlock * poolInfo['allocPoint']) / totalAllocPoints);

    var lpTokenContract = new window.w3.eth.Contract(IERC20.abi, poolInfo["lpToken"]);
    var chefLPBalance = await lpTokenContract.methods.balanceOf("0xB4405445fFAcF2B86BC2bD7D1C874AC739265658").call();

    console.log("PoolID: " + pool);
    console.log("LP Address: " + poolInfo["lpToken"]);

    console.log("Total Allocation Points: " + totalAllocPoints);
    console.log("Total Tokens Per Block: " + window.w3.utils.fromWei(tokensPerBlock.toString(), 'ether'));

    console.log("Pool Allocation Points: " + poolInfo['allocPoint']);
    console.log("Pool Allocation Tokens Per Block: " + window.w3.utils.fromWei(poolAlloc.toString(), 'ether'));
    console.log("Pool Allocation Tokens Per Year: " + (window.w3.utils.fromWei(poolAlloc.toString(), 'ether') * 28800 * 365));

    console.log("Master Chef LP Balance: " + window.w3.utils.fromWei(chefLPBalance.toString(), 'ether'));

How would i calculate the APR for the pools?

1 Answer 1

6
+50

this is the code snippet from the frontend of Pancakeswap, and if I not wrong MasterChef calculates in the same way.

/**
 * Get the APR value in %
 * @param stakingTokenPrice Token price in the same quote currency
 * @param rewardTokenPrice Token price in the same quote currency
 * @param totalStaked Total amount of stakingToken in the pool
 * @param tokenPerBlock Amount of new cake allocated to the pool for each new block
 * @returns Null if the APR is NaN or infinite.
 */
export const getPoolApr = (
  stakingTokenPrice: number,
  rewardTokenPrice: number,
  totalStaked: number,
  tokenPerBlock: number,
): 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()
}

the calculations are self-explanatory and parameters are commented on what they stand for.

To sum up,

APR Pool(in %) = (total_value_of_reward_token/total_value_of_staked_token)*100

where,

total_value_of_reward_token = no_of_reward_tokens * price_of_one_reward_token
no_of_reward_tokens = tokens_per_block * total_no_of_blocks_in_a_year

total_value_of_staked_token = no_of_staked_token * price_of_one__staked_token

however, these calculations do not account for fluctuations in prices of reward and staked tokens. As the pool progresses, the total no. of staked tokens increases and hence the denominator, so APY will also fluctuate.

3
  • Thank you for this. Where to find or how to calculate BLOCKS_PER_YEAR?
    – Jak
    Jul 29, 2021 at 12:45
  • @hack3r_0m How can I find the total Staked token in the pool?
    – crypto S.
    Aug 19, 2021 at 13:48
  • 1
    @cryptoS. you can use balanceOf(stakingContractAddress) on the erc20 contract address. this will give you how much erc20 staking contract holds
    – hack3r-0m
    Aug 20, 2021 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.