# Pool Implementation

#### 🔹 Constructor

* **constructor(IPoolAddressesProvider provider)**\
  Initializes the pool with the address provider that manages system-wide addresses.

***

#### 🔹 Core State-Changing Functions

* **supply(asset, amount, onBehalfOf, referralCode)**\
  Deposit an asset into the protocol to earn yield. `onBehalfOf` allows crediting another user.
* **supplyWithPermit(asset, amount, onBehalfOf, referralCode, deadline, v, r, s)**\
  Same as `supply`, but uses EIP-2612 permit signatures to approve in a gas-efficient way.
* **withdraw(asset, amount, to)** → `uint256`\
  Withdraw supplied assets from the protocol and send them to `to`.
* **borrow(asset, amount, interestRateMode, referralCode, onBehalfOf)**\
  Borrow funds from the pool, choosing **stable** or **variable** interest rate.
* **repay(asset, amount, interestRateMode, onBehalfOf)** → `uint256`\
  Repay borrowed funds.
* **repayWithATokens(asset, amount, interestRateMode)** → `uint256`\
  Repay debt using `aTokens` instead of underlying asset.
* **repayWithPermit(asset, amount, interestRateMode, onBehalfOf, deadline, v, r, s)** → `uint256`\
  Repay using ERC-20 permit authorization.
* **swapBorrowRateMode(asset, interestRateMode)**\
  Switch between **stable** and **variable** borrowing modes.
* **rebalanceStableBorrowRate(asset, user)**\
  Adjust a user’s stable borrow rate to the current optimal one.
* **setUserUseReserveAsCollateral(asset, useAsCollateral)**\
  Enable or disable an asset as collateral.
* **liquidationCall(collateralAsset, debtAsset, user, debtToCover, receiveAToken)**\
  Liquidate a user’s debt if their health factor is too low. Liquidator repays debt and receives collateral.

***

#### 🔹 Flash Loans

* **flashLoan(receiver, assets\[], amounts\[], interestRateModes\[], onBehalfOf, params, referralCode)**\
  Multi-asset flash loan.
* **flashLoanSimple(receiver, asset, amount, params, referralCode)**\
  Simpler single-asset flash loan.

***

#### 🔹 Treasury / Protocol Functions

* **mintUnbacked(asset, amount, onBehalfOf, referralCode)**\
  Mint `aTokens` without providing liquidity (used in bridges).
* **backUnbacked(asset, amount, fee)** → `uint256`\
  Provide the underlying liquidity to back unbacked `aTokens`.
* **mintToTreasury(assets\[])**\
  Mint reserves accrued to the protocol treasury.
* **rescueTokens(token, to, amount)**\
  Recover ERC-20 tokens mistakenly sent to the pool.

***

#### 🔹 Configuration / Admin Functions

* **initReserve(asset, aToken, stableDebtToken, variableDebtToken, interestRateStrategy)**\
  Initialize a new reserve (asset market).
* **dropReserve(asset)**\
  Remove a reserve from the pool.
* **setConfiguration(asset, config)**\
  Update reserve risk parameters.
* **setReserveInterestRateStrategyAddress(asset, strategy)**\
  Change the interest rate strategy for a reserve.
* **configureEModeCategory(id, category)**\
  Create/update an eMode category (optimized LTVs for correlated assets).
* **setUserEMode(categoryId)**\
  Set a user’s efficiency mode.
* **updateBridgeProtocolFee(protocolFee)**\
  Adjust fee for bridge usage.
* **updateFlashloanPremiums(totalPremium, premiumToProtocol)**\
  Adjust flash loan fee parameters.
* **resetIsolationModeTotalDebt(asset)**\
  Reset debt tracking for isolation-mode assets.
* **finalizeTransfer(asset, from, to, amount, balanceFromBefore, balanceToBefore)**\
  Internal bookkeeping when `aTokens` are transferred.

***

#### 🔹 View / Read Functions

* **ADDRESSES\_PROVIDER() → address**\
  Returns the address provider.
* **BRIDGE\_PROTOCOL\_FEE() → uint256**\
  Fee applied to bridge mints.
* **FLASHLOAN\_PREMIUM\_TOTAL() → uint128**\
  Total flash loan fee (bps).
* **FLASHLOAN\_PREMIUM\_TO\_PROTOCOL() → uint128**\
  Portion of fee directed to protocol treasury.
* **MAX\_NUMBER\_RESERVES() → uint16**\
  Maximum number of reserves allowed.
* **MAX\_STABLE\_RATE\_BORROW\_SIZE\_PERCENT() → uint256**\
  Limit on stable-rate borrowing as % of liquidity.
* **POOL\_REVISION() → uint256**\
  Contract revision number.
* **getConfiguration(asset)**\
  Returns the encoded reserve configuration.
* **getReserveData(asset)**\
  Returns detailed reserve state (indexes, rates, addresses, treasury accruals, etc.).
* **getReserveNormalizedIncome(asset) → uint256**\
  Normalized liquidity index.
* **getReserveNormalizedVariableDebt(asset) → uint256**\
  Normalized variable debt index.
* **getReservesList() → address\[]**\
  Returns the list of all active reserves.
* **getReserveAddressById(id) → address**\
  Get reserve address by internal ID.
* **getEModeCategoryData(id)**\
  Returns parameters of an eMode category (LTV, liquidation thresholds, etc.).
* **getUserAccountData(user)**\
  Returns total collateral, total debt, available borrows, liquidation threshold, LTV, and health factor.
* **getUserConfiguration(user)**\
  Returns bitmaps of user’s asset usage (collateral/borrow).
* **getUserEMode(user) → uint256**\
  Returns the efficiency mode category a user is in.

***

#### 🔹 Events

* **Supply, Withdraw, Borrow, Repay, LiquidationCall, FlashLoan, MintUnbacked, BackUnbacked, ReserveDataUpdated, ReserveUsedAsCollateralEnabled/Disabled, UserEModeSet**\
  All these track on-chain state changes for transparency and analytics.

```
[
  {
    "type": "constructor",
    "inputs": [
      { "name": "provider", "internalType": "contract IPoolAddressesProvider", "type": "address" }
    ],
    "stateMutability": "nonpayable"
  },
  {
    "type": "event",
    "name": "BackUnbacked",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "backer", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "fee", "internalType": "uint256", "type": "uint256" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "Borrow",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "onBehalfOf", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "interestRateMode", "internalType": "enum DataTypes.InterestRateMode", "type": "uint8" },
      { "indexed": false, "name": "borrowRate", "internalType": "uint256", "type": "uint256" },
      { "indexed": true, "name": "referralCode", "internalType": "uint16", "type": "uint16" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "FlashLoan",
    "inputs": [
      { "indexed": true, "name": "target", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "initiator", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "asset", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "interestRateMode", "internalType": "enum DataTypes.InterestRateMode", "type": "uint8" },
      { "indexed": false, "name": "premium", "internalType": "uint256", "type": "uint256" },
      { "indexed": true, "name": "referralCode", "internalType": "uint16", "type": "uint16" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "IsolationModeTotalDebtUpdated",
    "inputs": [
      { "indexed": true, "name": "asset", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "totalDebt", "internalType": "uint256", "type": "uint256" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "LiquidationCall",
    "inputs": [
      { "indexed": true, "name": "collateralAsset", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "debtAsset", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "debtToCover", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "liquidatedCollateralAmount", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "liquidator", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "receiveAToken", "internalType": "bool", "type": "bool" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "MintUnbacked",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "onBehalfOf", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": true, "name": "referralCode", "internalType": "uint16", "type": "uint16" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "MintedToTreasury",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amountMinted", "internalType": "uint256", "type": "uint256" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "RebalanceStableBorrowRate",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "Repay",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "repayer", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "useATokens", "internalType": "bool", "type": "bool" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "ReserveDataUpdated",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "liquidityRate", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "stableBorrowRate", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "variableBorrowRate", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "liquidityIndex", "internalType": "uint256", "type": "uint256" },
      { "indexed": false, "name": "variableBorrowIndex", "internalType": "uint256", "type": "uint256" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "ReserveUsedAsCollateralDisabled",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "ReserveUsedAsCollateralEnabled",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "Supply",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "onBehalfOf", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" },
      { "indexed": true, "name": "referralCode", "internalType": "uint16", "type": "uint16" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "SwapBorrowRateMode",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "interestRateMode", "internalType": "enum DataTypes.InterestRateMode", "type": "uint8" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "UserEModeSet",
    "inputs": [
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "categoryId", "internalType": "uint8", "type": "uint8" }
    ],
    "anonymous": false
  },
  {
    "type": "event",
    "name": "Withdraw",
    "inputs": [
      { "indexed": true, "name": "reserve", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "user", "internalType": "address", "type": "address" },
      { "indexed": true, "name": "to", "internalType": "address", "type": "address" },
      { "indexed": false, "name": "amount", "internalType": "uint256", "type": "uint256" }
    ],
    "anonymous": false
  }
]

```


---

# 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://colend.gitbook.io/home/abi/pool-implementation.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.
