Developer Docs

Getting Started

These docs are a comprehensive guide to the Rifi protocol, based on the Rifi Whitepaper (May 2021).

Networks

The Rifi Protocol is currently deployed on BSC Mainnet:

ContractABIAddress

rUSDC

rToken

JSON

0x916e87d16B...cf3B5C11f5

0x916e87d16B2F3E097B9A6375DC7393cf3B5C11f5

rUSDT

rToken

JSON

0x383598668C...f18D311063

0x383598668C025Be0798E90E7c5485Ff18D311063

rBUSD

rToken

JSON

0x6db6A55E57...988666553A

0x6db6A55E57AC8c90477bBF00ce874B988666553A

rBTC

rToken

JSON

0x53aBF990bF...cF8bdF11eB

0x53aBF990bF7A37FaA783A75FDD75bbcF8bdF11eB

rDAI

rToken

JSON

0x9B9006cb01...b37d8bC078

0x9B9006cb01B1F664Ac25137D3a3a20b37d8bC078

RIFI

JSON

0xe17fbdf671...f4245a3ffe

0xe17fbdf671f3cce0f354cacbd27e03f4245a3ffe

Cointroller

JSON

0x4f3e801Bd5...21d31F64e4

0x4f3e801Bd57dC3D641E72f2774280b21d31F64e4

Price Feed

JSON

0xD55f01B4B5...70A1a9DBa5

0xD55f01B4B51B7F48912cD8Ca3CDD8070A1a9DBa5

Protocol Math

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

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.

rToken and Underlying Decimals

Prices and exchange rates are scaled by the decimals unique to each asset; rTokens are BEP-20 tokens with 8 decimals, while their underlying tokens vary, and have a public member named decimals.

rTokenrToken DecimalsUnderlyingUnderlying Decimals

rUSDC

8

USDC

18

rUSDT

8

USDT

18

rBUSD

8

BUSD

18

rBTC

8

BTC

18

rDAI

8

DAI

18

Interpreting Exchange Rates

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

oneRTokenInUnderlying =exchangeRateCurrent / (1 * 10 ^ (18 +underlyingDecimals -rTokenDecimals))

Here is an example of finding the value of 1 rUSDT in USDT with Web3.js JavaScript.

constrTokenDecimals = 8; // all rTokens have 8 decimal placesconst underlying = new web3.eth.Contract(erc20Abi, usdtAddress);const rToken = new web3.eth.Contract(rTokenAbi, rUsdtAddress);constunderlyingDecimals = awaitunderlying.methods.decimals().call();constexchangeRateCurrent = awaitrToken.methods.exchangeRateCurrent().call();const mantissa = 18 + parseInt(underlyingDecimals) - rTokenDecimals;constoneRTokenInUnderlying =exchangeRateCurrent / Math.pow(10, mantissa);console.log('1 rUSDT can be redeemed for', oneRTokenInUnderlying, 'USDT');

To find the number of underlying tokens that can be redeemed for rTokens, multiply the number of rTokens by the above value oneRTokenInUnderlying.

underlyingTokens = rTokenAmount *oneRTokenInUnderlying

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 interest rates are changed depends 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 address interacts with the market’s rToken contract, calling one of these functions: mint, redeem, borrow, or repay. Successful execution of one of these functions triggers theaccrueInterest 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 rToken contract). Interest rifis only during blocks in which the rToken contract has one of the aforementioned methods invoked.

Here is an example of supply interest accrual:

Alice supplies 1 USDT to the Rifi protocol. At the time of supply, the supplyRatePerBlock is 37893605 Wei, or 0.00000012 USDT per block. No one interacts with the rToken contract for 3 blocks. On the subsequent 4th block, Bob borrows some USDT. Alice’s underlying balance is now 1.00000050 USDT (which is 37893605 Wei times 4 blocks, plus the original 1 USDT). Alice’s underlying USDT balance in subsequent blocks will have interest accrued based on the new value of 1.00000050 USDT instead of the initial 1 USDT. 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:

Rate = rToken.supplyRatePerBlock(); // IntegerRate = 37893566USDT Mantissa = 1 * 10 ^ 18 (USDT has 18 decimal places)Blocks Per Day = 28800 (3 seconds per block)Days Per Year = 365APY = ((((Rate / USDT 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:

constusdtMantissa = 1e18;constblocksPerDay = 28800;  // 13.15 seconds per blockconstdaysPerYear = 365;const rToken = new web3.usdt.Contract(rUsdtAbi, rUsdtAddress);constsupplyRatePerBlock = awaitrToken.methods.supplyRatePerBlock().call();constborrowRatePerBlock = awaitrToken.methods.borrowRatePerBlock().call();const supplyApy = (((Math.pow((supplyRatePerBlock / usdtMantissa * blocksPerDay) + 1, daysPerYear))) - 1) * 100;const borrowApy = (((Math.pow((borrowRatePerBlock / usdtMantissa * blocksPerDay) + 1, daysPerYear))) - 1) * 100;console.log(Supply APY for USDT ${supplyApy} %);console.log(Borrow APY for USDT ${borrowApy} %);

Gas Costs

FunctionTypical Gas Cost

Mint

< 150K

Redeem, Transfer

< 250K if borrowing, otherwise < 90K

Borrow

< 300K

Repay Borrow

< 90K

Liquidate Borrow

< 400K

Last updated