> For the complete documentation index, see [llms.txt](https://docs.teller.org/teller-diamond/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.teller.org/teller-diamond/facets-aka-mini-contracts/market.md).

# Market

### `market`

The market directories deals with interfaces, implementations, libraries and facets that implement the creation of loans, collateral escrows and the repayment of loans.

#### `collateral`

* The `collateral` directory contains the interface and implementation of the collateral escrow
* Functions include
  * `init()` initializing the collateral escrow
  * `deposit()` depositing collateral tokens
  * `withdraw()` withdrawing collateral tokens
  * `loanSupply()` returning the loan supply of collateral
  * `totalSupply()` returning the total supply of the collateral
* The above functions are all included in `ICollateralEscrow`

#### `libraries`

The libraries directory contains files that implement logic for the Facets to use. This logic relates to functionalities to interface with collateral escrows, loans and the processing of loans.

* `LibCollateral` contains functions that interact with the `ICollateralEscrow`
* `LibConsensus` contains one function to process loan terms `processLoanTerms()` and abstracted functions like `_getConsensus()`, `_getChainId()` and `validateLoanRequest()` to help with processing the loan terms
  * To satisfy the Teller consensus of validating a loan, it is required that the ratio of responses to signers is greater than the percentage of required submissions
  * Each response after iteration is subjected to multiple checks to see if it's got the correct signer, asset address, correct signature and unexpired response time
  * When the response is processed correctly, then interest rate, collateral ratio and max loan amount is added to the loan terms. The values are then averaged out with a tolerance value supported by the consensus
* `LibLoans` contains functions that deal with pulling information from respective loans
  * You can get how much loan is owed, percentage of interest for a loan, ratio of interest and the amount needed in collateral

#### `facets`

All the facets are available in the root directory. They are the `CollateralFacet`, `CreateLoanFacet`, `LoanDataFacet`, `RepayFacet` and the `SignersFacet`.

* `CollateralFacet` interfaces directly with the `LibCollateral` to deposit, add, get and withdraw collaterals. `LibLoans` is also used to help with some functions
* `CreateLoanFacet` connects to multiple libraries to help:
  * `createLoanWithTerms()` create a loan
  * `takeOutLoan()` take out a loan
  * `takeOutLoanWithNFTs()` take out a loan with an NFT
  * It also contains a `CreateLoanLib` that:
    * `initLoan()` initializes a loan
    * `createEscrow()` creates a loan escrow
    * `verifyCreateLoan()` verify a newly created loan
* `LoanDataFacet` contains functions that deals with pulling out data from both loans and loan escrows. Data includes :
  * `getLoanTerms()` loan terms
  * `getBorrowerLoans()` borrower loans
  * `getDebtOwed()` debt owed
  * `getInterestOwed()` interest owed
  * `getCollateralNeededInfo()` collateral needed
* `RepayFacet` contains functions that deal with both the repaying and the liquidation of loans given that a user wants to pay a little bit or a loan is undercollateralized (or expired), respectively. Functions include:
  * `escrowRepay()` repaying the loan with the respective escrow
  * `repayLoan()` repaying the loan from a regular address
  * `liquidateLoan()` liquidate a loan and payout the liquidator
  * There's also a library `RepayLib` that stores other functions like
    * `claimEscrowTokens()` which claims escrow tokens to the liquidator after a loan fails
    * `getLiquidationReward()` gets collateral value and reward amount for the liquidator
    * `payOutLiquidator()` checks if loan has an escrow and then claims any tokens before paying out the loan collateral
* `SignersFacet` simply adds signers and checks if a signer is part of a list of signers in an asset
