Swap APR

For calculating the swap APR we are going to calculate the USD amount of fees in the previous 24hours and annualize it.

We first need to get the TVL in USD

Reserve TVL in USD

Same for the total reserve, we can get the amount of each reserve in the pool and then multiplying for each token USD price. The sum of those is the total reserve value in USD terms.

 const [reserve0, reserve1] = await getReserves(bunniKey);
 const reserve = reserve0.div(Math.pow(10, bunniToken.pool.token0.decimals)).times(token0PriceUSD)
      .plus(reserve1.div(Math.pow(10, bunniToken.pool.token1.decimals)).times(token1PriceUSD));

Now that we have the TVL we get the fees in the last 24 hours for each token in USD terms and pick the minimum, then we subtract any protocol fee and lastly we annualize the 24hours fee in USD.

Swap vAPR

const token0Fees = new BigNumber(bunniToken.token0Volume).minus(block.token0Volume).times(bunniToken.pool.fee).div(1e6).times(token0PriceUSD);
const token1Fees = new BigNumber(bunniToken.token1Volume).minus(block.token1Volume).times(bunniToken.pool.fee).div(1e6).times(token1PriceUSD);
const fees24h = BigNumber.min(token0Fees, token1Fees);
const afterProtocolFee = new BigNumber(1).minus(protocol.protocolFee); 
      
const swapAPR = fees24h.times(365).times(afterProtocolFee).div(reserve).times(100);

Last updated