Getting Started
const pTokenDecimals = 8; // all pTokens have 8 decimal places
const underlying = new web3.eth.Contract(erc20Abi, batAddress);
const pToken = new web3.eth.Contract(pTokenAbi, cBatAddress);
const underlyingDecimals = await underlying.methods.decimals().call();
const exchangeRateCurrent = await pToken.methods.exchangeRateCurrent().call();
const mantissa = 18 + parseInt(underlyingDecimals) - pTokenDecimals;
const onepTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
console.log('1 cBAT can be redeemed for', onepTokenInUnderlying, 'BAT');
There is no underlying contract for ETH, so to do this with pETH, set underlyingDecimals
to 18.
To find the number of underlying tokens that can be redeemed for pTokens, multiply the number of pTokens by the above value onepTokenInUnderlying
.
Copy
underlyingTokens = pTokenAmount * onepTokenInUnderlying
Calculating Accrued Interest
Interest rates for each market update on any block in which the ratio of borrowed assets to supplied assets in the market has changed. The amount of interest rate changes are dependent on the interest rate model smart contract implemented for the market, and the amount of change in the ratio of borrowed assets to supplied assets in the market.
Interest accrues to all suppliers and borrowers in a market when any Ethereum address interacts with the market’s pToken contract, calling one of these functions: mint, redeem, borrow, or repay. Successful execution of one of these functions triggers the accrueInterest
method, which causes interest to be added to the underlying balance of every supplier and borrower in the market. Interest accrues for the current block, as well as each prior block in which the accrueInterest
method was not triggered (no user interacted with the pToken contract). Interest compounds only during blocks in which the pToken contract has one of the aforementioned methods invoked.
Here is an example of supply interest accrual:
Alice supplies 1 ETH to the Hyper AI Protocol. At the time of supply, the supplyRatePerBlock
is 37893605 Wei, or 0.000000000037893605 ETH per block. No one interacts with the PEther contract for 3 Ethereum blocks. On the subsequent 4th block, Bob borrows some ETH. Alice’s underlying balance is now 1.000000000151574420 ETH (which is 37893605 Wei times 4 blocks, plus the original 1 ETH). Alice’s underlying ETH balance in subsequent blocks will have interest accrued based on the new value of 1.000000000151574420 ETH instead of the initial 1 ETH. Note that the supplyRatePerBlock
value may change at any time.
Calculating the APY Using Rate Per Block
The Annual Percentage Yield (APY) for supplying or borrowing in each market can be calculated using the value of supplyRatePerBlock
(for supply APY) or borrowRatePerBlock
(for borrow APY) in this formula:
Copy
Rate = pToken.supplyRatePerBlock(); // Integer
Rate = 37893566
ETH Mantissa = 1 * 10 ^ 18 (ETH has 18 decimal places)
Blocks Per Day = 7200 (12 seconds per block)
Days Per Year = 365
APY = ((((Rate / ETH Mantissa * Blocks Per Day + 1) ^ Days Per Year)) - 1) * 100
Here is an example of calculating the supply and borrow APY with Web3.js JavaScript:
Copy
const ethMantissa = 1e18;
const blocksPerDay = 7200; // 12 seconds per block
const daysPerYear = 365;
const pToken = new web3.eth.Contract(pEthAbi, pEthAddress);
const supplyRatePerBlock = await pToken.methods.supplyRatePerBlock().call();
const borrowRatePerBlock = await pToken.methods.borrowRatePerBlock().call();
const supplyApy =
(Math.pow(
(supplyRatePerBlock / ethMantissa) * blocksPerDay + 1,
daysPerYear
) -
1) *
100;
const borrowApy =
(Math.pow(
(borrowRatePerBlock / ethMantissa) * blocksPerDay + 1,
daysPerYear
) -
1) *
100;
console.log(`Supply APY for ETH ${supplyApy} %`);
console.log(`Borrow APY for ETH ${borrowApy} %`);
Last updated