# Pool.sol

### Pool

Pools are OpenZeppelin Clones of an immutable pool implementation. The pool contract resembles an OTC term sheet with a single borrower and one or more lenders. The borrower takes a loan from the lenders on a specific predefined loan-to-value ratio against collateral.&#x20;

#### borrower

```solidity
address borrower
```

Address of the borrower.

#### whitelistedLender

```solidity
address whitelistedLender
```

Address of the whitelisted lender.

*Zero address if the pool is open to everyone.*

#### lentAsset

```solidity
address lentAsset
```

The ERC20 token that lenders deposit and the borrower borrows.

#### collateralAssets

```solidity
address[] collateralAssets
```

The ERC20 tokens that can be used as collateral.

#### startsAt

```solidity
uint32 startsAt
```

The timestamp at which the pool was deployed. Lenders can deposit `lentAsset` until the pool becomes active.

*Set to block.timestamp during pool initialization.*

#### activeAt

```solidity
uint32 activeAt
```

The timestamp after which deposits close and borrowers can borrow the deposits. If the minimum is not reached, borrowers cannot borrow and lenders can withdraw their deposits without accruing yield.

#### maturesAt

```solidity
uint32 maturesAt
```

The timestamp at which the Pool matures. Lenders can withdraw their deposits after maturity.

#### coupon

```solidity
uint96 coupon
```

The yield for the pool during the term. The term is the duration at which the pool is active. Borrower must pay this

*Denominated in WAD. A value of 1e17 means the coupon is 10%. The APR can be calculated by multiplying this value by the number of seconds in a year and dividing it by `maturesAt-activeAt`.*

#### ltv

```solidity
uint96 ltv
```

The loan-to-value ratio of the pool. When borrowing the borrower must supply collateral such that the value of the loan = ltv \* value of the collateral.

*Denonimated in WAD. A value of 2e18 means the LTV is 200%. The collateralization ratio is the inverse of the LTV. If the LTV is 200% then the CR is 50%.*

#### originationFee

```solidity
uint96 originationFee
```

The origination fee charged to the protocol treasury. When borrowing, the origination fee is deducted from the borrowed amount and transferred to the protocol treasury.

*Denominated in WAD. A value of 3e15 means the origination fee is 0.3%. The Treasury address is kept in the `Router` contract.*

#### leftoversWithdrawn

```solidity
bool leftoversWithdrawn
```

A boolean that stores whether the borrower has withdrawn the redundant coupon for the pool. Borrowers are required to supply the coupon for the maximum capacity of the pool at pool creation. If the maximum capacity is not reached and the pool is active or mature, the coupon for the unfilled capacity can be withdrawn by the borrower.

#### minSupply

```solidity
uint256 minSupply
```

The minimum amount of `lentAsset` that lenders must deposit to activate borrowing functionality.

*If at least `minSupply` of `lentAsset` is deposited, borrowers can borrow all or a part of deposits. Borrowers must repay their loan before maturity or the pool will default.*

#### maxSupply

```solidity
uint256 maxSupply
```

The maximum amount of `lentAsset` that lenders can deposit. Borrowers must pay the coupon upfront based on this amount, so in practice this cannot be unbounded. This measure also keeps away pool spam.

#### supply

```solidity
uint256 supply
```

The pool supply reached. This is used for a checkpoint to calculate the amount of extra coupon to withdraw.

*When lenders deposit, the amount is added to `supply`. However when lenders redeem, the amount is NOT subtracted from `supply`. Therefore this variable can be interpreted as the supply reached. This is done so that the borrower can withdraw the extra coupon at any time.*

#### borrowed

```solidity
uint256 borrowed
```

The amount of `lentAsset` borrowed by the borrower. Borrowers must pay the coupon upfront based on this amount, so in practice this cannot be unbounded. This is also a measure to keep away pool spam.

#### notionals

```solidity
mapping(address => uint256) notionals
```

Stores a lender's notional (total deposited amount), used to calculate rewards.

#### collateralReserves

```solidity
mapping(address => uint256) collateralReserves
```

Collateral amounts supplied by the borrower.

#### router

```solidity
address router
```

Address of the Router contract.

#### onlyRouter

```solidity
modifier onlyRouter()
```

Verify that the caller is the router.

*Throws an error otherwise.*

#### constructor

```solidity
constructor(address _router) public
```

#### initialize

```solidity
function initialize(address _borrower, address _lentAsset, address[] _collateralAssets, uint96 _coupon, uint96 _ltv, uint96 _originationFee, uint32 _activeAt, uint32 _maturesAt, uint256 _minSupply, uint256 _maxSupply, address _whitelistedLender) external
```

Initialize the pool variables.

*Called by the Router when the clone is created.*

**Parameters**

| Name                | Type       | Description                                           |
| ------------------- | ---------- | ----------------------------------------------------- |
| \_borrower          | address    | The address of the borrower.                          |
| \_lentAsset         | address    | The address of the lent asset.                        |
| \_collateralAssets  | address\[] | The addresses of the collateral assets.               |
| \_coupon            | uint96     | The yield generated by lenders in WAD.                |
| \_ltv               | uint96     | The loan-to-value ratio for the borrower.             |
| \_originationFee    | uint96     | The fee charged by the protocol given by the .        |
| \_activeAt          | uint32     | When deposits end and borrowing is allowed.           |
| \_maturesAt         | uint32     | When the pool is over.                                |
| \_minSupply         | uint256    | Minimum supply of the lent asset to enable borrowing. |
| \_maxSupply         | uint256    | Maximum allowed supply of the lent asset.             |
| \_whitelistedLender | address    |                                                       |

#### deposit

```solidity
function deposit(address _src, uint256 _amt) external
```

Performs accounting when an asset is deposited. Called when the lender deposits.

*Can be called only by the Router.*

**Parameters**

| Name  | Type    | Description                       |
| ----- | ------- | --------------------------------- |
| \_src | address | The lender address.               |
| \_amt | uint256 | The amount of `_asset` deposited. |

#### supplyCollateral

```solidity
function supplyCollateral(address _asset, uint256 _amt) external
```

Performs accounting when the borrower supplies collateral.

*Can be called only by the Router.*

**Parameters**

| Name    | Type    | Description                         |
| ------- | ------- | ----------------------------------- |
| \_asset | address | The address of the asset deposited. |
| \_amt   | uint256 | The amount of `_asset` deposited.   |

#### borrow

```solidity
function borrow(address _lentAsset, uint256 _amt) external
```

Performs accounting and transfers on borrow. Called when the borrower borrows.

*Can be called only by the Router.*

**Parameters**

| Name        | Type    | Description                                         |
| ----------- | ------- | --------------------------------------------------- |
| \_lentAsset | address | The address of `lentAsset` supplied for efficiency. |
| \_amt       | uint256 | The amount of `lentAsset` to borrow.                |

#### repay

```solidity
function repay(uint256 _amt) external
```

Performs accounting and transfers back collateral on repay. Called when the borrower repays their loan.

*Can be called only by the Router.*

**Parameters**

| Name  | Type    | Description                         |
| ----- | ------- | ----------------------------------- |
| \_amt | uint256 | The amount of `lentAsset` to repay. |

#### redeem

```solidity
function redeem(address _src) external
```

Performs accounting and returns deposit to the lender. Called when the lender redeems their deposit. Partial redeems are not allowed.

*Can be called only by the Router.*

**Parameters**

| Name  | Type    | Description                |
| ----- | ------- | -------------------------- |
| \_src | address | The address of the lender. |

#### \_default

```solidity
function _default(address _src) external
```

Performs accounting and returns some lent asset and collateral to the lender. Called when the lender redeems their deposit and the borrower has not repaid all debt before maturity.

*Can be called only by the Router.*

**Parameters**

| Name  | Type    | Description                |
| ----- | ------- | -------------------------- |
| \_src | address | The address of the lender. |

#### withdrawLeftovers

```solidity
function withdrawLeftovers(uint256 _amt) external
```

Performs accounting when the borrower withdraws redundant rewards.

*Can be called only by the Router.*

**Parameters**

| Name  | Type    | Description                        |
| ----- | ------- | ---------------------------------- |
| \_amt | uint256 | The amount of rewards to withdraw. |

#### \_transfer

```solidity
function _transfer(address _asset, address _dst, uint256 _amt) private
```

Transfers an ERC20 token from the pool.

*Uses the OpenZeppelin's SafeERC20 library.*

**Parameters**

| Name    | Type    | Description                           |
| ------- | ------- | ------------------------------------- |
| \_asset | address | The address of the token to transfer. |
| \_dst   | address | The recipient of the transfer.        |
| \_amt   | uint256 | The amount to transfer.               |

#### getCollateralAssets

```solidity
function getCollateralAssets() external view returns (address[])
```

Retrieves the array of collateral assets.

*Used for off-chain data retrieval.*

**Return Values**

| Name | Type       | Description                           |
| ---- | ---------- | ------------------------------------- |
| \[0] | address\[] | `collateralAssets` as a memory array. |


---

# Agent Instructions: 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://docs.lombard.fi/developers/contract-reference/pool.sol.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.
