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.
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);
constfees24h=BigNumber.min(token0Fees, token1Fees);constafterProtocolFee=newBigNumber(1).minus(protocol.protocolFee); constswapAPR=fees24h.times(365).times(afterProtocolFee).div(reserve).times(100);