APR: getAssetData to fetch liquidity mining incentives for a/s/vToken.
APY: getReserveDatato fetch deposit and borrow rates of asset.
[, liquidityIndex, variableBorrowIndex,currentLiquidityRate, currentVariableBorrowRate,currentStableBorrowRate,,aTokenAddress, stableDebtTokenAddress,variableDebtTokenAddress,, ] =LendingPool.getReserveData(asset.address) // asset is the ERC20 supplied or borrowed, eg. DAI, WETH//For stable borrows, the rate is specific to a user and can be queried through the ProtocolDataProvider contractuserStableBorrowRate =ProtocolDataProvider.getUserReserveData(asset.address, userAddress)[,aEmissionPerSecond,] =IncentivesController.getAssetData(aTokenAddress)[,vEmissionPerSecond,] =IncentivesController.getAssetData(variableDebtTokenAddress)[,sEmissionPerSecond,] =IncentivesController.getAssetData(stableDebtTokenAddress)// If the current UNIX timestamp > distributionEnd then the incentives are no longer activedistributionEnd =IncentivesController.DISTRIBUTION_END();
Compute Data
Calculate rates as👇🏻with js, python or whatever you like 😉
RAY=10**27// 10 to the power 27SECONDS_PER_YEAR=31536000// Deposit and Borrow calculations// APY and APR are returned here as decimals, multiply by 100 to get the percentsdepositAPR = liquidityRate/RAYvariableBorrowAPR = variableBorrowRate/RAYstableBorrowAPR = variableBorrowRate/RAYdepositAPY = ((1+ (depositAPR /SECONDS_PER_YEAR)) ^SECONDS_PER_YEAR) -1variableBorrowAPY = ((1+ (variableBorrowAPR /SECONDS_PER_YEAR)) ^SECONDS_PER_YEAR) -1stableBorrowAPY = ((1+ (stableBorrowAPR /SECONDS_PER_YEAR)) ^SECONDS_PER_YEAR) -1// Incentives calculationaEmissionPerYear = aEmissionPerSecond *SECONDS_PER_YEARvEmissionPerYear = vEmissionPerSecond *SECONDS_PER_YEARWEI_DECIMALS=10**18// All emissions are in wei units, 18 decimal places// UNDERLYING_TOKEN_DECIMALS will be the decimals of token underlying the aToken or debtToken// For Example, UNDERLYING_TOKEN_DECIMALS for aUSDC will be 10**6 because USDC has 6 decimalsincentiveDepositAPRPercent =100* (aEmissionPerYear *REWARD_PRICE_ETH*WEI_DECIMALS)/ (totalATokenSupply *TOKEN_PRICE_ETH*UNDERLYING_TOKEN_DECIMALS)incentiveBorrowAPRPercent =100* (vEmissionPerYear *REWARD_PRICE_ETH*WEI_DECIMALS)/ (totalCurrentVariableDebt *TOKEN_PRICE_ETH*UNDERLYING_TOKEN_DECIMALS)
Conversions
Both of these conversions take the input and ouput in decimal form. Multiply the output by 100 to get the percentage.
APR -> APY
To convert the APR to APY compounded per second the formula is:
APY -> APR
To convert APY compounded per second to APR the formula is: