# LoanManager Contracts

The `LoanManager` contract holds all the main borrower-oriented actions of the Teller protocol.&#x20;

There are 2 types of loans. Loans with Ether as collateral and loans with an ERC20 token as collateral. The main difference is how the loans are created and collateral paid out.&#x20;

Below are the key methods that reside in the contract. For the complete source code, check out [LoanManager.sol on GitHub](https://github.com/teller-protocol/teller-protocol-v1/blob/develop/contracts/base/loans/LoanManager.sol).

## {1} createLoanWithTerms()

`function createLoanWithTerms(`\
&#x20; `TellerCommon.LoanRequest calldata request,`\
&#x20; `TellerCommon.LoanResponse[] calldata responses,` \
&#x20; `uint256 collateral`\
`)`

Creates an instance of a loan with terms on the protocol that has been accepted by a consensus of nodes. There are two special parameters in this method, that are structs specific to the Teller protocol.

`TellerCommon.LoanRequest` is a request struct/object that is sent to the Teller Nodes and subsequently on-chain, with all the required entries such as:

| Variable           | Type              | Description                                                                           |
| ------------------ | ----------------- | ------------------------------------------------------------------------------------- |
| `borrower`         | `address payable` | The wallet address of the borrower                                                    |
| `recipient`        | `address`         | The address where funds will be sent, only applicable in over collateralized loans    |
| `consensusAddress` | `address`         | The address of the Teller loan consensus contract to which the request should be sent |
| `requestNonce`     | `uint256`         | The nonce of the borrower wallet address required for authentication                  |
| `amount`           | `uint256`         | The number of tokens requested by the borrower for the loan                           |
| `duration`         | `uint256`         | The length of time in seconds that the loan has been requested for                    |
| `requestTime`      | `uint256`         | The timestamp at which the loan was requested                                         |

`TellerCommon.LoanResponse[]` is an array of signed response structs/objects that are returned from registered Teller nodes that need to be passed on-chain in order to register the loan terms on-chain.

| Variable           | Type        | Description                                                                                          |
| ------------------ | ----------- | ---------------------------------------------------------------------------------------------------- |
| `signer`           | `address`   | The wallet address of the signer validating the interest request of the lender                       |
| `consensusAddress` | `address`   | The address of the Teller loan consensus contract to which the request should be sent                |
| `responseTime`     | `uint256`   | The timestamp at which the response was sent                                                         |
| `interestRate`     | `uint256`   | The signed interest rate generated by the signer's Credit Risk Algorithm (CRA)                       |
| `collateralRatio`  | `uint256`   | The ratio of collateral to loan amount that is generated by the signer's Credit Risk Algorithm (CRA) |
| `maxLoanAmount`    | `uint256`   | The largest amount of tokens that can be taken out in the loan by the borrower                       |
| `signature`        | `Signature` | The signature generated by the signer in the format of the below Signature struct                    |

The last param `collateral` is the amount of collateral that needs to be deposited into the protocol in order to be able to take out the funds of the loan.

## {2} depositCollateral()&#x20;

`function(`\
&#x20; `address borrower,`\
&#x20; `uint256 loanID,`\
&#x20; `uint256 amount`\
`)`

In situations where a borrower's collateral drops in value (e.g. price drops 90% in value), the borrower would need to deposit more collateral to keep the loan `Active` and out of `liquidation.` This can be done with the `depositCollateral()` method called by the borrower or developer dApp.

| Param      | Type      | Description                              |
| ---------- | --------- | ---------------------------------------- |
| `borrower` | `address` | The address of the loan borrower         |
| `loanID`   | `uint256` | The ID of the loan the collateral is for |
| `amount`   | `uint256` | The amount to deposit as collateral      |

## {3} takeOutLoan()

`function(`\
&#x20; `uint256 loanID,`\
&#x20; `uint256 amountBorrow`\
`)`

Once a loan with terms has been created on-chain with `createLoanWithTerms()` and collateral deposited, a borrower can now take out the loan.

| Param          | Type      | Description                                 |
| -------------- | --------- | ------------------------------------------- |
| `loanID`       | `uint256` | The ID of the loan being taken out          |
| `amountBorrow` | `uint256` | The number of funds to take out in the loan |

## {4} repay()

`function(`\
&#x20; `uint256 amount,`\
&#x20; `uint256 loanID`\
`)`

Once the borrower has used the loan he will also need to pay back the loan. This can be done with the `repay()` method on the respective loan contract.

| Param    | Type      | Description                                   |
| -------- | --------- | --------------------------------------------- |
| `amount` | `uint256` | The number of funds to pay back into the loan |
| `loanID` | `uint256` | The ID of the loan the payment is for         |

One thing to note: if a loan is repaid in full with the `repay()` method, the borrower's collateral is automatically sent to their wallet, no additional calls are needed!

## {5} withdrawCollateral()

`function(`\
&#x20; `uint256 amount,`\
&#x20; `uint256 loanID`\
`)`

In situations where the value of the borrower's collateral has risen (hello there new ATH!), a borrower can withdraw a portion of their collateral and still keep the loan active by calling the `withdrawCollateral()` method on-chain.&#x20;

| Param    | Type      | Description                                                              |
| -------- | --------- | ------------------------------------------------------------------------ |
| `amount` | `uint256` | The amount of collateral token or ether the caller is hoping to withdraw |
| `loanID` | `uint256` | The ID of the loan the collateral is for                                 |
