mentorship2022 icon indicating copy to clipboard operation
mentorship2022 copied to clipboard

Collateralized Vault

Open alcueca opened this issue 2 years ago • 0 comments

Exercise name:        Collateralized Vault
Assignment #:         5
Track:                Smart contracts
Testing framework:    Foundry
Deployment framework: Foundry (forge create)
Node provider:        Alchemy
Target network:       Ethereum-Rinkeby

Assignment summary

In this assignment you will code a very simple contract that acts as a collateralized debt engine. This contract will allow users to deposit an asset they own, which we will call collateral, to borrow a different asset that the contract owns, which we will call underlying. The exchange rate will be determined by an oracle, and if the value of the collateral drops in underlying terms, the user will be liquidated.

For this assignment we will use Wrapped Ether as the collateral, and DAI as the underlying. Pull the contract code for both from Etherscan, and add a mint() function that allows you to obtain as much as you need for testing.

Users can deposit WETH in the Collateralized Vault, this transfers WETH from the user to the Vault, with the Vault recording how much WETH the user has deposited.

Users can borrow DAI against their WETH, as long as the value DAI they borrow in WETH terms is less than the value of their WETH collateral. When borrowing, the Collateral Vault sends DAI to the users. The contract owner supplies DAI to the contract on construction.

The Vault knows the ETH/DAI exchange rate from a Chainlink Oracle.

Users can repay their debt with DAI.

Users can withdraw their WETH collateral. If they have a debt, they can't withdraw WETH in a way that makes the value of their collateral drop below the value of their debt.

To withdraw their Ether, the users must repay the DAI they borrowed.

If the ETH/DAI price changes in a way that the value of the debt of an user is more than the value of their collateral, the contract owner can erase the user records from the contract, cancelling the user debt, and at the same time stopping that user from withdrawing their collateral.

To test in your local network, code a contract that conforms to the Chainlink interface (import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";), but for which you can set up prices manually. Then deploy to Rinkeby using a real Chainlink oracle.

Additional Details I wrote a piece about dealing with oracles, with the lessons learned from Yield v1 and v2. It might come useful.

Peripheral Goals

  1. Most tokens have 18 decimals, but not all. Code another Collateralized Vault where the underlying is USDC.

  2. Using mock WETH and DAI contracts gets you a long way, but you can't always do that. Remove the mint() function and use the foundry cheatcodes to directly edit the registry that stores your balance of each.

alcueca avatar Apr 26 '22 10:04 alcueca