yieldprotocol / variable-rate-audit-obheda12

0 stars 0 forks source link

Variable Rate Lending/Borrowing

With this update we are bringing variable rate borrowing & lending to the Yield Protocol. This is a major update to the protocol and we are excited to bring this to the community.

Unlike fixed rate borrowing there is no intermediary token involved while borrowing. When user borrows an asset they directly get the asset in their choosen address. However, for lending there is an intermediate token called the VYToken. The VYToken represents the amount lent based on the interest rate at time of lending. The VYToken can be burnt at any time to get the lent asset back.

Here are the main components of the system:

  1. VRLadle - The main contract that handles the lending/borrowing of the variable rate loan.
  2. VRCauldron - The contract that handles the accounting.
  3. VRWitch - The contract that handles the liquidation.
  4. VYToken - The contract that handles the tokenization of the loan.
  5. VariableInterestRateOracle - The contract that determines the interest rate of the base for borrowing or lending.
  6. Join(not in scope) - The contract that holds the collateral & lent assets.

Contracts

VRLadle (177 SLOC)

Ladle is the contract which does the lending/borrowing of the variable rate loan. It is the main orchestrating contract of the system. Here are the broad operations the contract performs:

VRCauldron (286 SLOC)

Cauldron is the contract which handles the accounting of the variable rate loan. It is the contract that keeps track of the balances of the lenders and borrowers. The contract enforces checks to ensure that the loans are not under-collateralized.

VRWitch (46 SLOC)

It is the liquidation engine built on top of the existing battle tested Witch for the fixed rate lending. It has been modified to work in absence of fyToken. Also, the way the debtFromBase & debtToBase is calculated has been modified to work with the variable rate. You can refer to this for more details on the Witch.

VYToken (151 SLOC)

VYToken is a mechanism that allows user to lend their assets as we. The VYToken is a ERC20 token that represents the amount of asset lent. The VYToken can be burnt at any time to get the lent asset back.

VariableInterestRateOracle (149 SLOC)

The oracle that determines the interest rate for borrowing or lending of the base. The oracle works on the interest rate model that is used by Aave. The parameters we use are:

The Utilization Rate is calculated by dividing the total debt by the total collateral. The Variable Borrow Rate is calculated by using the Utilization Rate and the parameters mentioned above.


Terminology

Working

How does borrowing work?

To borrow a base you need to first deposit the collateral in the protocol. The collateral could be either deposited directly to the relevant Join by the user or the user could give permission to either the Join or Ladle to move the token on their behalf. The protocol will check if the collateral is sufficient to cover the debt. The check happens taking into account the collateralization ratio and the spot rate of the collateral in terms of the base. If the collateral is sufficient then the protocol will transfer the base to the user's determined address. The user could repay the debt at any time by depositing the base back into the protocol. The protocol will determine the amount owed based on the interest rate at the time of repayment.

Under the hood

The borrowing is executed through a batch call made to the VRLadle contract. The batch consists of the following operations:

  1. VRLadle.build - This operation creates a new vault for the user. Please note this is optional as the user could already have a vault.
  2. VRLadle.transfer/VRLadle.forwardPermit/VRLadle.forwardDaiPermit - This operation transfers the collateral from the user to the protocol. Please note that this is optional operation as the user could have already deposited the collateral in the protocol or they could have approved the join to transfer the collateral on their behalf in a separate transaction
  3. VRLadle.pour - This operation posts the collateral and transfers the borrowed base to the user supplied address.

How does lending work?

To lend an asset you deposit the asset into the protocol and mint the VYTokens. The deposit could happen the same way as described in borrowing. The amount of VYTokens minted is determined by the interest rate at the time of lending. The interest is received from the rateOracle. The VYTokens can be burnt at any time to get the lent back.

Under the hood

The lending is executed through a batch call made to the VRLadle contract. The batch consists of the following operations:

  1. VRLadle.transfer/VRLadle.forwardPermit/VRLadle.forwardDaiPermit - This operation transfers the collateral from the user to the protocol. Please note that this is optional operation as the user could have already deposited the collateral in the protocol or they could have approved the join to transfer the collateral on their behalf in a separate transaction
  2. VRLadle.moduleCall - This operation calls the VYToken contract to mint the VYTokens for the user.

Orchestration

The protocol modifications are handled through a three step process. The first step is to propose a proposal. The second step is to vote on the proposal. The third step is to execute the proposal. The three steps are managed through the TimeLock contract.

The VRLadle contract can be configured by the TimeLock contract. And the rest of the protocol can be configured by the VRLadle contract.

Here are the permissions allocated to different contracts:

Timelock will have the following permissions over VRLadle:

VRLadle has the following permissions over VRCauldron:

VRLadle will have following permissions over Joins:


Audit Scope

Here are the contracts that are in scope for the audit:

Type File Logic Contracts Interfaces Lines nLines nSLOC Comment Lines Complex. Score
📝 src/variable/VRCauldron.sol 1 **** 459 383 286 61 193
📝 src/variable/VRLadle.sol 1 **** 352 284 177 65 250
📝 src/variable/VRLadleStorage.sol 1 **** 34 34 27 6 21
📝 src/variable/VRWitch.sol 1 **** 102 82 46 23 25
📝 src/variable/VYToken.sol 1 **** 245 240 151 62 151
🔍 src/variable/interfaces/IVRCauldron.sol **** 1 109 9 6 22 37
🔍 src/variable/interfaces/IVRWitch.sol **** 1 140 46 33 78 19
📝 src/oracles/VariableInterestRateOracle.sol 1 **** 228 193 149 20 61
📝🔍 Totals 6 2 1669 1271 875 337 757

Building & Testing

This project uses foundry to build and test the contracts.


Note