# L2 Pool

On Layer 2 networks, the main cost of transactions comes from calldata. To reduce this cost, Tydro uses a specialised contract on L2 that compresses the calldata of Pool methods.

The L2Pool contract provides the L2-optimised user-facing methods of the protocol. It accepts byte-encoded input arguments and exposes the same liquidity management functions available in the standard Pool contract. Users can interact with L2Pool through Solidity or Web3 libraries. By using compact calldata representations, L2Pool helps lower transaction costs on rollups.

Not all methods are redefined in L2Pool.sol. Functions such as `flashLoan` and `setUserEMode` remain identical to their implementation in the base Pool contract. For those functions, refer to the [Pool](https://docs.tydro.com/developers/smart-contracts/pool) documentation.

Because L2 networks use a limited set of supported assets, each asset is assigned an individual 16-bit asset ID, which replaces the standard 160-bit asset address in encoded arguments.

The source code is available on [<mark style="color:blue;">GitHub</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/protocol/pool/L2Pool.sol).

Tydro also provides an [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract with view methods to encode transaction parameters for the compressed L2Pool functions. This ensures developers can efficiently generate the calldata required for low-cost interactions.

### Methods

#### supply

```
function supply(bytes32 args) external override
```

Calldata efficient wrapper of the supply function on behalf of the caller. Supplies asset into the protocol, minting the same amount of corresponding aTokens, and transferring them to msg.sender.

You can use data returned from encodeSupplyParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

| Name | Type    | Description                                                                                                                                                                                                                                                                                                                                                  |
| ---- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| args | bytes32 | <p>Arguments for the supply function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144-159: uint16 referralCode - used for 3rd party integrations</p> |

#### supplyWithPermit

```
function supplyWithPermit(bytes32 args, bytes32 r, bytes32 s) external override
```

Calldata efficient wrapper of the supplyWithPermit function on behalf of the caller. Supply with transfer approval of supplied asset via permit function. This method removes the need for separate approval transaction before supplying asset to the pool.

You can use data returned from encodeSupplyWithPermitParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the supply function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144-159: uint16 referralCode - used for 3rd party integrations<br>bit 160-191: uint32 shortenedDeadline - shortened deadline from the original uint256<br>bit 192-199: uint8 permitV - the V parameter of ERC712 permit signature</td></tr><tr><td valign="top">r</td><td valign="top">bytes32</td><td>The R parameter of ERC712 permit signature</td></tr><tr><td valign="top">s</td><td valign="top">bytes32</td><td>The S parameter of ERC712 permit signature</td></tr></tbody></table>

#### withdraw

```
function withdraw(bytes32 args) external override returns (uint256)
```

Calldata efficient wrapper of the withdraw function, withdrawing to the caller. Withdraws amount of the underlying asset, i.e. redeems the underlying token and burns the aTokens.

If the user has any existing debt backed by the underlying token, the maximum amount available to withdraw is the amount that will not leave the user with a health factor < 1 after the withdrawal.

You can use data returned from encodeWithdrawParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

| Name | Type    | Description                                                                                                                                                                                                                                                                              |
| ---- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| args | bytes32 | <p>Arguments for the withdraw function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max</p> |

**Return Value:**

| Name   | Type    | Description                                                                                                                                                                                                                                                             |
| ------ | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| amount | uint256 | The final amount of the underlying asset withdrawn, denominated in the base unit of the asset (e.g., wei for ETH, smallest unit for ERC-20 tokens). This is the amount actually transferred to the caller, accounting for constraints like liquidity and health factor. |

#### borrow

```
function borrow(bytes32 args) external override
```

Calldata efficient wrapper of the borrow function, borrowing on behalf of the caller. Borrows amount of asset with interestRateMode, sending the amount to msg.sender, with the debt being incurred by onBehalfOf.

You can use data returned from encodeBorrowParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the borrow function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144 - 151: uint8 shortenedInterestRateMode<br>bit 152 - 167: uint16 referralCode - used for 3rd party integrations</td></tr></tbody></table>

#### repay

```
function repay(bytes32 args) external override returns (uint256)
```

Calldata efficient wrapper of the repay function, repaying on behalf of the caller. Repays debt of an asset for the given interestRateMode.

You can use data returned from encodeRepayParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the repay function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144 - 151: uint8 shortenedInterestRateMode</td></tr></tbody></table>

**Return Values:**

| Type    | Description             |
| ------- | ----------------------- |
| uint256 | The final amount repaid |

#### repayWithPermit

```
function repayWithPermit(bytes32 args, bytes32 r, bytes32 s) external override returns (uint256)
```

Calldata efficient wrapper of the repayWithPermit function, repaying on behalf of the caller. Repay with transfer approval of borrowed asset via permit function. This method removes the need for separate approval transaction before repaying asset to the pool.

You can use data returned from encodeRepayWithPermitParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.​

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the repayWithPermit function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144 - 151: uint8 shortenedInterestRateMode<br>bit 152-183: uint32 shortenedDeadline - shortened deadline from original uint256<br>bit 184-191: uint8 permitV - the V parameter of ERC712 permit signature</td></tr><tr><td valign="top">r</td><td valign="top">bytes32</td><td>The R parameter of ERC712 permit signature</td></tr><tr><td valign="top">s</td><td valign="top">bytes32</td><td>The S parameter of ERC712 permit signature</td></tr></tbody></table>

**Return Values:**

| Type    | Description             |
| ------- | ----------------------- |
| uint256 | The final amount repaid |

#### repayWithATokens

```
function repayWithATokens(bytes32 args) external override returns (uint256)
```

Calldata efficient wrapper of the repayWithATokens function. Allows user to repay with aTokens of the underlying debt asset without any approvals, for example, Pay DAI debt using aDAI tokens.

You can use data data returned from encodeRepayWithATokensParams() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the repayWithATokens function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16-143: uint128 shortenedAmount - cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 144 - 151: uint8 shortenedInterestRateMode</td></tr></tbody></table>

**Return Values:**

| Type    | Description             |
| ------- | ----------------------- |
| uint256 | The final amount repaid |

#### setUserUseReserveAsCollateral

```
function setUserUseReserveAsCollateral(bytes32 args) external override
```

Calldata efficient wrapper of the setUserUseReserveAsCollateral function. Sets the asset of msg.sender to be used as collateral or not.

You can use data returned from encodeSetUserUseReserveAsCollateral() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.​

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args</td><td valign="top">bytes32</td><td>Arguments for the setUserUseReserveAsCollateral function packed in one bytes32<br>bit 0-15: uint16 assetId - the index of the asset in the reservesList<br>bit 16: 0 => enable useAsCollateral, 1 => disable useAsCollateral</td></tr></tbody></table>

#### liquidationCall

```
function liquidationCall(bytes32 args1, bytes32 args2) external override
```

Calldata efficient wrapper of the liquidationCall function. Liquidate positions with a health factor below 1.

You can use data returned from encodeLiquidationCall() method in [<mark style="color:blue;">L2Encoder</mark>](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract to pass to this method.​

**Input Parameters:**

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td>Description</td></tr><tr><td valign="top">args1</td><td valign="top">bytes32</td><td>Part of the arguments for the liquidationCall function packed in one bytes32<br>bit 0-15: uint16 collateralAssetId - the index of the collateral asset in the reservesList<br>bit 16-31: uint16 debtAssetId - the index of the debt asset in the reservesList<br>bit 32-191: address of the user being liquidated</td></tr><tr><td valign="top">args2</td><td valign="top">bytes32</td><td>Part of the arguments for the liquidationCall function packed in one bytes32<br>bit 0-127: uint128 shortenedDebtToCover is cast to 256 bits at decode time, if type(uint128).max the value will be expanded to type(uint256).max<br>bit 128: receiveAToken - 0 => receive aToken, 1 => receive underlying asset</td></tr></tbody></table>
