How to write a reserve plugin part 2

Contructor params of a collateral contract

  • fallbackPrice
  • chainlinkFeed
  • ERC20+rewardERC20
  • maxTradeVolume
  • oracleTimeout
  • targetName
  • delayUnitDefault

What’s fallbackPrice?

fallback price is when we try to get the real price but revert then we provide a fallback price.
fallback price is return when function price(bool allowFallback) is called and the param past in is true.
In the price() function we’ll call strictPrice() function first once it revert we’ll check if the value of allowFallback is true. If false return value is (isFallback-bool,fallbackPrice)
the unit of fallback price if {UoA}

What’s chainlinkFeed ?

Firstly ,it’s been created by chainlink.The interface allows your contract run functions on that deployed aggregator contract. For example ,you can use chainlink data feed to connect your smart contract to asset pricing data like the ETH/USD feed.
the unit of feed is {UoA/tok}

What’s ERC20 ?

this is import from openzeppein/contracts/token/ERC20/extensions/IERC20Metadata.sql
IERC20Metadata is inherit IERC20
some function like transfer,totalSuoply,balanceOf,approve,transferFrom all we need to create an ERC20 token.

What’s max trade volume?

the unit of max trade price if {UoA}

What’s oracle timeout?

the number of seconds until a oracle value becomes invalid.Otherwise we have to change the collateral status by calling markStatus().

What’s target name?

the canonical name of this collateral’s target unit.

What’s delayUnitDefault?

the number of seconds an oracle can mulfunction.
this value is used when we call markStatus() function.
when collateral status is “IFFY” . smart contract compare blocktime+delayUnitDfault and _whenDefault and then return the min(Math.min) one?

Reserve protocol introduce

All you need to know for writing a reserve protocol collateral plugins quick start

core plugin depends on two plugin types:

  • Assets / Collateral (contracts/plugins/assets)
  • Trading (contract/plugins/trading) not discussed here

Collateral is erc20 token + below:

  • if ERC20 back ?
  • refresh()
  • status() SOUND/IFFY/DISABLED
  • rate exchange

Monetary units

  • Unit of Account
  • Target unit
    RToken maintaining stability or appreciation against its target unit
  • Reference unit

e.g. of 3 different tokens

  • Collateral : cUSDC
  • Refence : USDC
  • Target : USD

Units definition

  • {UoA} unit of account recommand USD here
  • {tok} whole token
  • {ref} whole reference token
  • {target} whole target unit
  • {ref/tok} refPerTok()
  • {target/ref} targetPerRef()
  • {UoA/target} pricePerTarget()

Basket definition

  • Prime basket
    this is set by governance , on changes through successful governance proposals. it consists of an array of triples
    <collateral token,target unit,tartget amount>
    e.g. : <cUSDC,USD,0.1>
  • Reference basket
    <collateral token,reference unit,reference amount>
    e.g. : <aDAI,DAI,0.33>
  • Collateral bascket
    <collateral token,token amount>
    e.g.: <cUSDC,0.29>

IAsset interface

alt "interface"

Accounting units

before we create a collateral plugin we have to choose 3 accounting units

Collateral unit

its just erc20 token

Reference unit

to be ask , what’s a unit that this collateral token will always be worth the same value or more of .

Target unit

target unit has to do with a concept called the target basket. what’s prime basket ?
A example of linear combination of target units:

  • 1 USD
  • 0.5 USD + 0.55 EURO
  • 0.5 USD + 0.35 EURO + 0.00001BTC

Unit of account

we can assume UoA == USD , because for now USD price are king.

Important properties for collateral plugins

Collateral plugins should be permisionless and should be able to used by any number if RTokens
Token balance can’t be rebasing

refresh() should never be revert

refresh is been called before any transactions , it return exchange rates.
if occur an important error , refresh should change the state to DISABLED

strictPrice() price(bool) status()

the IFFY status should be temporary

Collateral must default if refPerTok() falls

Defaulted Collateral must stay defaulted

if status() ever returns disabled , then it must always return disabled in the nearly future

Token rewards should be claimable

  • rewardERC20()
  • getClaimCalldata()

Smaller Constraints

The value of the following methods should never change:

  • isColleteral()
  • targetName()
  • erc20()
  • rewardERC20()
  • erc20Deciamls()

Function-by-function walkthrough

  • strictPrice() {UoA/tok}
  • price(bool) {UoA/tok}
    if revert depends on the bool value true or false
  • refPerTok() {ref/tok}
  • targetPerRef() {target/ref}
  • pricePerTarget(){UoA/tartget}