📕
DeFiPIE
  • Getting Started
  • pTokens
  • Controller
  • Registry
  • PTokenFactory
  • UniswapPriceOracle
  • API
Powered by GitBook
On this page
  • Introduction
  • Networks
  • Protocol Math
  • pToken and Underlying Decimals
  • Interpreting Exchange Rates
  • Calculating Accrued Interest
  • Calculating the APY Using Rate Per Block

Was this helpful?

Getting Started

Introduction to the DeFiPIE lending protocol

NextpTokens

Last updated 3 years ago

Was this helpful?

Introduction

The DeFiPIE protocol is a series of interest rate pools running on a variety of blockchains. When users and applications deposit their assets to the DeFIPIE Protocol, they begin earning a variable interest rate instantly. Interest accrues every block (for Ethereum ~13 seconds, for Binance Smart Chain ~3 seconds), and users can withdraw their principal plus interest anytime.

On the other hand, users can borrow tokens from the pool by paying an interest rate to all pool lenders

When users deposit assets, they receive pTokens from DeFiPIE in exchange. pTokens are that can be redeemed for their underlying assets at any time. As interest accrues to the assets deposited, pTokens are redeemable at an exchange rate (relative to the underlying asset) that constantly increases over time, based on the rate of interest earned by the underlying asset.

Networks

The DeFiPIE Protocol is currently deployed on the following networks:

Contract

Address

Controller

Registry

Factory

UniswapPriceOracle

BaseInterestRateModel

claimCalc

Maximillion

PIE

Contract

Address

Controller

Registry

Factory

UniswapPriceOracle

BaseInterestRateModel

claimCalc

Maximillion

PIE

Contract

Address

Controller

Registry

Factory

UniswapPriceOracle

BaseInterestRateModel

claimCalc

Maximillion

PIE

Contract

Address

Controller

Registry

Factory

UniswapPriceOracle

BaseInterestRateModel

claimCalc

Maximillion

PIE

Protocol Math

Most numbers are represented as a mantissa, an unsigned integer scaled by 1 * 10 ^ 18, in order to perform basic math at a high level of precision.

pToken and Underlying Decimals

Prices and exchange rates are scaled by the decimals unique to each asset; pTokens are ERC-20 tokens with 8 decimals In most cases (there are several pTokens exceptions in Ethereum network with 18 decimals), while their underlying tokens vary, and have a public member named decimals.

pTokens exceptions in Ethereum network:

pToken

decimals

pPIE

18

pETH

18

pDAI

18

Interpreting Exchange Rates

The pToken Exchange Rate is scaled by the difference in decimals between the pToken and the underlying asset.

onePTokenInUnderlying = exchangeRateCurrent / (1 * 10 ^ (18 + underlyingDecimals - pTokenDecimals))

Here is an example of finding the value of 1 pPIE in PIE with Web3.js JavaScript.

const underlying = new web3.eth.Contract(erc20Abi, pieAddress);
const pToken = new web3.eth.Contract(pTokenAbi, pPieAddress);
const underlyingDecimals = await underlying.methods.decimals().call();
const pTokenDecimals = await pToken.methods.decimals().call();
const exchangeRateCurrent = await pToken.methods.exchangeRateCurrent().call();
const mantissa = 18 + parseInt(underlyingDecimals) - parseInt(pTokenDecimals);
const onePTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
console.log('1 pPIE can be redeemed for', onePTokenInUnderlying, 'PIE');

There is no underlying contract for ETH and BNB, so to do this with pETH and pBNB, 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.

underlyingTokens = pTokenAmount * onePTokenInUnderlying

Calculating Accrued Interest

Interest rates for each pool update on any block in which the ratio of borrowed assets to deposited assets in the pool has changed. The amount interest rates are changed depends on the interest rate model smart contract implemented for the pool, and the amount of change in the ratio of borrowed assets to deposited assets in the pool.

Interest accrues to all lenders and borrowers in a pool when any Ethereum address interacts with the pool’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 lender and borrower in the pool. 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 deposits 1 ETH to the DeFiPIE protocol. At the time of deposit, 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 lending or borrowing in each pool can be calculated using the value of supplyRatePerBlock (for deposit APY) or borrowRatePerBlock (for borrow APY) in this formula:

Rate = pToken.supplyRatePerBlock(); // Integer
Rate = 37893566
ETH Mantissa = 1 * 10 ^ 18 (ETH has 18 decimal places)
Blocks Per Day = 4 * 60 * 24 (based on 4 blocks occurring every minute)
Days Per Year = 365

APY = ((((Rate / ETH Mantissa * Blocks Per Day + 1) ^ Days Per Year)) - 1) * 100

Here is an example of calculating the deposit and borrow APY with Web3.js JavaScript:

const ethMantissa = 1e18;
const blocksPerDay = 4 * 60 * 24;
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 depositApy = (((Math.pow((supplyRatePerBlock / ethMantissa * blocksPerDay) + 1, daysPerYear))) - 1) * 100;
const borrowApy = (((Math.pow((borrowRatePerBlock / ethMantissa * blocksPerDay) + 1, daysPerYear))) - 1) * 100;
console.log(`Deposit APY for ETH ${depositApy} %`);
console.log(`Borrow APY for ETH ${borrowApy} %`);

The DeFiPIE protocol contracts use a system of exponential math, , in order to represent fractional quantities with sufficient precision.

ERC20 tokens
Exponential.sol
0x36de5Bbc618A04c9B471208Ef52eE2b1F536E92d
0x1135270BbB0627e769a7a2e24f2b2C7F14B3d83A
0x54f04010FcfCfc95eC8Fd19Fe4cF57ADfBbb6136
0x21790F2a8C6117CD93E65c30F55C6BB99Cb6c16D
0xd47d39A66bb4912D127fBfC1b90884fCB3546137
0x466CF3EA420237cD1669d78efd9519caAFe5a73a
0x1913B2A20ae5CC0A177af1faaeCCDb267423c3B6
0x607C794cDa77efB21F8848B7910ecf27451Ae842
0xD204bE259F703503EF2eA03eB401Ce6E07254d96
0xC5B3cfcC8aD60565997cAEed1Cce6CC915A08B90
0x12FfF44587bA55992A9eDe625D4033385F708584
0x155aE77115DE61AE5755aBD2C12CFD020Eb9d37E
0x2CaD4031C013Fa66E1F2F15baA135f1B2E45208e
0x6871F928713814ba2C6EB2789C3CfF24F817606E
0x4A6cbFb03Ada1F38186F494ad475536b6Ef73679
0xb36Afc9F38d8ac6F991bb9939d3EE8D45A7A1285
0x8ca3BD13BbF3468D48369DF508b9B614D9aa1802
0xF2F88D8e974D07A2E27943310780B108f01a18b2
0xd6eAC7cf547002d289dC85954Ec0ABE217A9b80D
0x2eC8BF5BeA6736EF549dd89c80BA3CFdc6d7BdfE
0xDD2F62B666684226Fbe7a648E411B51b3D3c78f8
0x1Ea24b8cFcf26C9d7b134f3fBDCA0A920655b17C
0x9CeC0108803FB54dD83e58eE61bebB26F171b9f8
0xC4B35d3A24E3e8941c5d87fD21D0725642F50308
0x660E1093AA3D7CB0f6A8dE172F85d0d7b010ef26
0xddf147af5Bb55B1bE9bf6c37BFA5FEAf08Ac6Abd
0xcD686fFCFFf135AC7d0021E5a0bcAAD25f3ae4bf
0xB76B79A327EC69a1F91e9Ba60fB2EA72979f6732
0xc09481E51Ca917EfF5b2Ff164e65560Ca10Bf0b8
0x6ef8E32ea4623aC0439Bace0E3564BBE23f13CcF
0x80978b220789e0d6336673aC9D826e114FCdAb78
0xd50da88069c69BF093e8dca1532Cc81711D9e0F4