> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/yield-mentorship-2022/projects/5-collateralized-vault.md).

# #5 Collateralized Vault

* Problem Statement: <https://github.com/yieldprotocol/mentorship2022/issues/5>
* Github: <https://github.com/calnix/Collateralized-Vault>

## Objective

Contract allows users to deposit an asset they own (collateral), to **borrow a different asset** that the Vault owns (underlying).&#x20;

* Exchange rate determined by oracle;&#x20;
* if value of collateral drops in underlying terms, the user will be liquidated.
* #### For example, user can deposit WETH as collateral, and borrow DAI against it.

### Sequence

1. Users deposit WETH into Vault
2. Users ***borrow*** DAI against their WETH collateral
3. Loan is considered healthy as long as the <mark style="color:red;">Vault does not lose money.</mark>
   1. <mark style="color:blue;">**This happens when**</mark><mark style="color:red;">**:**</mark>
      1. the weth collateral held by the Vault is worth less than DAI lent
      2. Vault cannot sell weth at market price to collect back DAI lent
   2. **This occurs when price appreciates:**
      1. Initial DAI/WETH price: 0.00038637284
         * *<mark style="color:red;">**price appreciation**</mark>*
         * 1 DAI : 0.38637284 WETH
      2. 1 DAI gets your more WETH
      3. But that means to get the same amount of DAI, Vaul <mark style="color:red;">**needs more WETH.**</mark>
4. **Liquidation occurs when the WETH collateral, calculated in weth terms.**
   1. if value of collateral drops in underlying terms, the user will be liquidated.

![](/files/zpughAfeV3JDqO4SGjxJ)

### Tokens

* Collateral: WETH&#x20;
* Underlying: DAI

### Contracts

1. Pull the contract code for both from Etherscan
2. Ensure there is a mint() function that allows you to obtain as much as you need for testing.

## Workflow

1. Users deposit WETH into Vault,
   * (Vault records WETH deposited by each user)
2. Users *borrow* DAI against their WETH collateral
   * maximum amt of DAI borrowable
     * DAI\_value\_in\_WETH < WETH Collateral
   * Vault transfers DAI to the users.
   * Vault owner finances DAI to the Vault on construction.
3. Exchange rate: Chainlink Oracle \[<https://docs.chain.link/docs/ethereum-addresses>]
4. Users repay debt in DAI
5. Withdrawal
   * To withdraw WETH, the users must repay the DAI they borrowed.
6. Liquidation
   * If DAI/ETH price appreciates such tt **debt\_value** *>* **collateral\_value**, Vault will erase user records from the contract&#x20;
   * -> cancelling the user debt, and at the same time stopping that user from withdrawing their collateral.

####


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/yield-mentorship-2022/projects/5-collateralized-vault.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
