# Aave V3 L2 Pool Contract

The main transaction cost on L2 comes from calldata. To minimize this cost, Aave V3 uses a different contract on L2 networks that allows calldata of Pool methods to be compressed.

`L2Pool` is the contract for the L2 optimized user facing methods of the protocol that takes byte encoded input arguments. It exposes the liquidity management methods that can be invoked using either Solidity or Web3 libraries. The `L2Pool` contract is a calldata optimized extension of the Pool contract allowing users to pass compact calldata representation to reduce transaction costs on L2 rollups.

> [!NOTE]
> Pool methods not exposed in `L2Pool.sol`, such as flashLoan and setUserEMode,
> work the same as on other networks. See [Pool](./pool).

Encoded arguments use the 16-bit asset id (the reserve index in `getReservesList()`) instead of the 160-bit asset address.

The source code is available on [GitHub](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/protocol/pool/L2Pool.sol).

There is an additional [L2Encoder](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol) helper contract with view methods to encode transaction params for compressed methods.

## Methods

### supply

```solidity
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`.

> [!NOTE]
> Build the input with `encodeSupplyParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                              |
| :--- | :-------- | :--------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the supply function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits    | Type      | Field           | Description                                                                                               |
| :------ | :-------- | :-------------- | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId         | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount | Cast to 256 bits at decode time                                                                           |
| 144-159 | `uint16`  | referralCode    | Used for 3rd party integrations                                                                           |

### supplyWithPermit

```solidity
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.

> [!NOTE]
> Build the input with `encodeSupplyWithPermitParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                              |
| :--- | :-------- | :--------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the supply function packed in one bytes32. See the `args` bit layout below |
| **r** | `bytes32` | The R parameter of EIP-712 permit signature                                              |
| **s** | `bytes32` | The S parameter of EIP-712 permit signature                                              |

The `args` bit layout:

| Bits    | Type      | Field             | Description                                                                                               |
| :------ | :-------- | :---------------- | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId           | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount   | Cast to 256 bits at decode time                                                                           |
| 144-159 | `uint16`  | referralCode      | Used for 3rd party integrations                                                                           |
| 160-191 | `uint32`  | shortenedDeadline | Shortened deadline from the original `uint256`                                                            |
| 192-199 | `uint8`   | permitV           | The V parameter of EIP-712 permit signature                                                               |

### withdraw

```solidity
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.

> [!NOTE]
> Build the input with `encodeWithdrawParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                                |
| :--- | :-------- | :----------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the withdraw function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits   | Type      | Field           | Description                                                                                               |
| :----- | :-------- | :-------------- | :-------------------------------------------------------------------------------------------------------- |
| 0-15   | `uint16`  | assetId         | The index of the asset in the reservesList                                                                |
| 16-143 | `uint128` | shortenedAmount | Cast to 256 bits at decode time, if `type(uint128).max` the value will be expanded to `type(uint256).max` |

**Return Values:**

| 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

```solidity
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`, and `msg.sender` incurs the debt.

> [!NOTE]
> Build the input with `encodeBorrowParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                              |
| :--- | :-------- | :--------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the borrow function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits    | Type      | Field                     | Description                                                                                               |
| :------ | :-------- | :------------------------ | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId                   | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount           | Cast to 256 bits at decode time                                                                           |
| 144-151 | `uint8`   | shortenedInterestRateMode | Must be 2 (variable)                                                                                      |
| 152-167 | `uint16`  | referralCode              | Used for 3rd party integrations                                                                           |

### repay

```solidity
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`.

> [!NOTE]
> Build the input with `encodeRepayParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                             |
| :--- | :-------- | :-------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the repay function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits    | Type      | Field                     | Description                                                                                               |
| :------ | :-------- | :------------------------ | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId                   | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount           | Cast to 256 bits at decode time, if `type(uint128).max` the value will be expanded to `type(uint256).max` |
| 144-151 | `uint8`   | shortenedInterestRateMode | Must be 2 (variable)                                                                                      |

**Return Values:**

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

### repayWithPermit

```solidity
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.

> [!NOTE]
> Build the input with `encodeRepayWithPermitParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                                       |
| :--- | :-------- | :------------------------------------------------------------------------------------------------ |
| **args** | `bytes32` | Arguments for the repayWithPermit function packed in one bytes32. See the `args` bit layout below |
| **r** | `bytes32` | The R parameter of EIP-712 permit signature                                                       |
| **s** | `bytes32` | The S parameter of EIP-712 permit signature                                                       |

The `args` bit layout:

| Bits    | Type      | Field                     | Description                                                                                               |
| :------ | :-------- | :------------------------ | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId                   | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount           | Cast to 256 bits at decode time, if `type(uint128).max` the value will be expanded to `type(uint256).max` |
| 144-151 | `uint8`   | shortenedInterestRateMode | Must be 2 (variable)                                                                                      |
| 152-183 | `uint32`  | shortenedDeadline         | Shortened deadline from original `uint256`                                                                |
| 184-191 | `uint8`   | permitV                   | The V parameter of EIP-712 permit signature                                                               |

**Return Values:**

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

### repayWithATokens

```solidity
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.

> [!NOTE]
> Build the input with `encodeRepayWithATokensParams()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                                        |
| :--- | :-------- | :------------------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the repayWithATokens function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits    | Type      | Field                     | Description                                                                                               |
| :------ | :-------- | :------------------------ | :-------------------------------------------------------------------------------------------------------- |
| 0-15    | `uint16`  | assetId                   | The index of the asset in the reservesList                                                                |
| 16-143  | `uint128` | shortenedAmount           | Cast to 256 bits at decode time, if `type(uint128).max` the value will be expanded to `type(uint256).max` |
| 144-151 | `uint8`   | shortenedInterestRateMode | Must be 2 (variable)                                                                                      |

**Return Values:**

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

### setUserUseReserveAsCollateral

```solidity
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.

> [!NOTE]
> Build the input with `encodeSetUserUseReserveAsCollateral()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name | Type      | Description                                                                                                     |
| :--- | :-------- | :-------------------------------------------------------------------------------------------------------------- |
| **args** | `bytes32` | Arguments for the setUserUseReserveAsCollateral function packed in one bytes32. See the `args` bit layout below |

The `args` bit layout:

| Bits | Type     | Field           | Description                                            |
| :--- | :------- | :-------------- | :----------------------------------------------------- |
| 0-15 | `uint16` | assetId         | The index of the asset in the reservesList             |
| 16   | `bool`   | useAsCollateral | 1 to enable the reserve as collateral, 0 to disable it |

### liquidationCall

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

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

> [!NOTE]
> Build the input with `encodeLiquidationCall()` in the
> [`L2Encoder`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/helpers/L2Encoder.sol)
> helper contract.

**Input Parameters:**

| Name  | Type      | Description                                                                                                    |
| :---- | :-------- | :------------------------------------------------------------------------------------------------------------- |
| **args1** | `bytes32` | Part of the arguments for the liquidationCall function packed in one bytes32. See the `args1` bit layout below |
| **args2** | `bytes32` | Part of the arguments for the liquidationCall function packed in one bytes32. See the `args2` bit layout below |

The `args1` bit layout:

| Bits   | Type      | Field             | Description                                           |
| :----- | :-------- | :---------------- | :---------------------------------------------------- |
| 0-15   | `uint16`  | collateralAssetId | The index of the collateral asset in the reservesList |
| 16-31  | `uint16`  | debtAssetId       | The index of the debt asset in the reservesList       |
| 32-191 | `address` | user              | The address of the user being liquidated              |

The `args2` bit layout:

| Bits  | Type      | Field                | Description                                                                                                |
| :---- | :-------- | :------------------- | :--------------------------------------------------------------------------------------------------------- |
| 0-127 | `uint128` | shortenedDebtToCover | Cast to 256 bits at decode time , if `type(uint128).max` the value will be expanded to `type(uint256).max` |
| 128   | `bool`    | receiveAToken        | 1 to receive aTokens, 0 to receive the underlying asset                                                    |
