> For the complete documentation index, see [llms.txt](https://docs.lombard.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lombard.fi/developers/contract-reference/router.sol.md).

# Router.sol

### Router

The router is the entry point for interacting with the LombardFi system. All of the transfers from user to pool are executed by the router. *Much of the verification logic is done in the router to compress the size of the Pool contract.*

#### poolFactory

```solidity
contract IPoolFactory poolFactory
```

Address of the PoolFactory.

#### oracleManager

```solidity
contract IOracleManager oracleManager
```

Address of the OracleManager.

#### treasury

```solidity
address treasury
```

Address of the protocol treasury.

*Receives the origination fee at pool creation.*

#### nonZero

```solidity
modifier nonZero(uint256 amt)
```

Verify that an integer is greater than 0.

*Throws an error if the uint256 is equal to 0*

**Parameters**

| Name | Type    | Description           |
| ---- | ------- | --------------------- |
| amt  | uint256 | The integer to check. |

#### nonZeroAddress

```solidity
modifier nonZeroAddress(address _address)
```

Verify that an address is not the zero address.

*Throws an error if the address is the zero address.*

**Parameters**

| Name      | Type    | Description           |
| --------- | ------- | --------------------- |
| \_address | address | The address to check. |

#### setFactory

```solidity
function setFactory(contract IPoolFactory _poolFactory) external
```

Set the PoolFactory implementation address.&#x20;

Callable only by the owner.

*Throws an error if the supplied address is the zero address.*

**Parameters**

| Name          | Type                  | Description             |
| ------------- | --------------------- | ----------------------- |
| \_poolFactory | contract IPoolFactory | The new implementation. |

#### setOracleManager

```solidity
function setOracleManager(contract IOracleManager _oracleManager) external
```

Set the OracleManager implementation address.&#x20;

Callable only by the owner.

*Throws an error if the address is the zero address.*

**Parameters**

| Name            | Type                    | Description             |
| --------------- | ----------------------- | ----------------------- |
| \_oracleManager | contract IOracleManager | The new implementation. |

#### setTreasury

```solidity
function setTreasury(address _treasury) external
```

Set the treasury address. Callable only by the owner.

*Throws an error if the address is the zero address.*

**Parameters**

| Name       | Type    | Description        |
| ---------- | ------- | ------------------ |
| \_treasury | address | The new recipient. |

#### pause

```solidity
function pause() external
```

Pause the contract. Callable only by the owner.

#### unpause

```solidity
function unpause() external
```

Unpause the contract. Callable only by the owner.

*The contract must be paused to unpause it.*

#### deposit

```solidity
function deposit(uint256 _pid, uint256 _amt) external
```

Deposit into a pool.

*The contract must not be paused. `_amt` must be nonzero.*&#x20;

*A pool with the `_pid` must exist.*&#x20;

*Caller must not be the pool's borrower.*&#x20;

*Caller must be the whitelisted lender if the pool has one.*&#x20;

*Caller must have approved this contract to spend `_amt` of the pool's lent asset.*&#x20;

*The pool must not be active or mature.*&#x20;

*The pool must have sufficient open capacity for `_amt`.*

**Parameters**

| Name  | Type    | Description                                     |
| ----- | ------- | ----------------------------------------------- |
| \_pid | uint256 | The id of the pool to deposit in.               |
| \_amt | uint256 | The amount of the pool's lent asset to deposit. |

#### borrow

```solidity
function borrow(uint256 _pid, address[] _collateralAssets, uint256[] _amts) external
```

Borrow the available amount of lent asset from a pool. Transfers the pool's lent asset from pool to borrower. Transfers collateral from borrower to pool.

*The contract must not be paused.*&#x20;

*A pool with the `_pid` must exist.*&#x20;

*Caller must be the pool's borrower.*&#x20;

*Lengths of `_collateralAssets` and `amts` must match.*&#x20;

*`_collateralAssets` must be the pool's collateral assets or a subset.*&#x20;

*The pool must be active.*&#x20;

*The pool's minimum deposit must have been reached.*&#x20;

*The loan amount and value of the supplied collateral must satisfy the loan-to-value ratio.*

**Parameters**

| Name               | Type       | Description                                  |
| ------------------ | ---------- | -------------------------------------------- |
| \_pid              | uint256    | The id of the pool to borrow from.           |
| \_collateralAssets | address\[] | The assets to deposit as collateral          |
| \_amts             | uint256\[] | The amounts corresponding to the collaterals |

#### repay

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

Repay a part of the loan. Transfers the pool's lent asset from borrower to pool. Transfers collateral from pool to borrower.

*A pool with the `_pid` must exist. `_amt` must be nonzero.*&#x20;

*The contract must not be paused.*&#x20;

*Pool must be active if the router is under normal operation.*&#x20;

*Caller must be the pool's borrower.*&#x20;

*The pool must be active.*

**Parameters**

| Name  | Type    | Description                                   |
| ----- | ------- | --------------------------------------------- |
| \_pid | uint256 | The id of the pool to repay in.               |
| \_amt | uint256 | The amount of the pool's lent asset to repay. |

#### redeem

```solidity
function redeem(uint256 _pid) external
```

Redeem notional and yield from a mature pool or redeem notional from an unsuccessful pool. Transfers the pool's lent asset from pool to caller. Transfers collateral from caller to pool.

*The contract must not be paused.*&#x20;

*A pool with the `_pid` must exist.*&#x20;

*Caller must not be the pool's borrower.*&#x20;

*The pool must be mature or active with less deposits than the minimum.*&#x20;

*Caller must have made a deposit.*&#x20;

*Caller can redeem only once per pool.*

**Parameters**

| Name  | Type    | Description                        |
| ----- | ------- | ---------------------------------- |
| \_pid | uint256 | The id of the pool to redeem from. |

#### withdrawLeftovers

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

Withdraw redundant yield from a pool. Transfers a part of the upfront for the unrealised size back to the borrower.

*The contract must not be paused.*&#x20;

*A pool with the `_pid` must exist.*&#x20;

*Caller must be the pool's borrower.*&#x20;

*Borrower can withdraw only once.*&#x20;

*The pool must be active.*

**Parameters**

| Name  | Type    | Description                                    |
| ----- | ------- | ---------------------------------------------- |
| \_pid | uint256 | The id of the pool to withdraw leftovers from. |

#### getBorrowingPower

```solidity
function getBorrowingPower(address[] _collateralAssets, uint256[] _amts) public view returns (uint256 _borrowingPower)
```

Utility function that returns the value of collateral. Prices are fetched from the OracleManager.

*Also used for off-chain data retrieval.*

**Parameters**

| Name               | Type       | Description                                         |
| ------------------ | ---------- | --------------------------------------------------- |
| \_collateralAssets | address\[] | Array of collateral assets.                         |
| \_amts             | uint256\[] | The amounts corresponding to the collateral assets. |

**Return Values**

| Name             | Type    | Description                        |
| ---------------- | ------- | ---------------------------------- |
| \_borrowingPower | uint256 | The total value of the collateral. |

#### \_assetsAreValidPoolCollateral

```solidity
function _assetsAreValidPoolCollateral(contract IPool _pool, address[] _assets) private view returns (bool)
```

Utility function that checks whether an array of addresses match pool collateral. They must be a subset.

**Parameters**

| Name     | Type           | Description                                                   |
| -------- | -------------- | ------------------------------------------------------------- |
| \_pool   | contract IPool | The pool to check the assets against.                         |
| \_assets | address\[]     | The array of ERC20 token addresses to check against the pool. |

**Return Values**

| Name | Type | Description                                                   |
| ---- | ---- | ------------------------------------------------------------- |
| \[0] | bool | Whether the given assets are valid subset of pool collateral. |

#### \_verifyCallerIsNotBorrower

```solidity
function _verifyCallerIsNotBorrower(contract IPool _pool) private view
```

Private function that verifies that the caller is not the pool's borrower.

**Parameters**

| Name   | Type           | Description        |
| ------ | -------------- | ------------------ |
| \_pool | contract IPool | The pool contract. |

#### \_getPool

```solidity
function _getPool(uint256 _pid) private view returns (contract IPool _pool)
```

Private function that gets a pool address from a pool id.

*Throws an error if a pool with the `_pid` does not exist.*

**Parameters**

| Name  | Type    | Description                               |
| ----- | ------- | ----------------------------------------- |
| \_pid | uint256 | The id of the pool to get the address of. |

**Return Values**

| Name   | Type           | Description        |
| ------ | -------------- | ------------------ |
| \_pool | contract IPool | The pool contract. |
