# Credit Facade

> Markdown export of the Gearbox Protocol documentation page for agents and retrieval systems.

Canonical page: https://docs.gearbox.finance/developers/gm-ref-cf
Source file: content/developers/gm-ref-cf.mdx
Section router: https://docs.gearbox.finance/developers/llms.txt
Section full export: https://docs.gearbox.finance/developers/llms-full.txt

The **CreditFacadeV3** is the primary user-facing interface for Credit Account operations. It implements atomic multicall batching, enforces debt limits, manages bot permissions, and ensures all operations complete with a healthy account state.

## Core Data Structures

### MultiCall

```solidity
struct MultiCall {
    address target;   // Facade itself or whitelisted adapter
    bytes callData;   // Function selector + encoded arguments
}
```

### DebtLimits

```solidity
struct DebtLimits {
    uint128 minDebt;  // Minimum principal (except 0)
    uint128 maxDebt;  // Maximum principal
}
```

### Bot Permissions

Bot permissions are stored as a `uint192` bitmask:

| Permission | Value | Operation |
| --- | --- | --- |
| `ADD_COLLATERAL_PERMISSION` | `1 << 0` | Add funds to account |
| `INCREASE_DEBT_PERMISSION` | `1 << 1` | Borrow more from pool |
| `DECREASE_DEBT_PERMISSION` | `1 << 2` | Repay debt |
| `WITHDRAW_COLLATERAL_PERMISSION` | `1 << 5` | Withdraw assets |
| `UPDATE_QUOTA_PERMISSION` | `1 << 6` | Change token quotas |
| `EXTERNAL_CALLS_PERMISSION` | `1 << 16` | Execute adapter calls |

---

## Write Methods

### openCreditAccount

Creates a new Credit Account with initial operations executed atomically.

```solidity
function openCreditAccount(
    MultiCall[] calldata calls,
    address onBehalfOf
) external returns (address creditAccount);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `calls` | `MultiCall[]` | Initial operations (add collateral, increase debt, etc.) |
| `onBehalfOf` | `address` | Owner of the new Credit Account |

**Returns:** Address of the created Credit Account.

**Access:** Anyone. A full collateral check is performed after all calls execute.

---

### closeCreditAccount

Closes a Credit Account, repaying all debt and returning remaining assets.

```solidity
function closeCreditAccount(
    address creditAccount,
    MultiCall[] calldata calls
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to close |
| `calls` | `MultiCall[]` | Operations to execute before closing (e.g., swap to underlying) |

**Access:** Account owner only (`creditAccountOwnerOnly`).

---

### multicall

Executes a batch of operations on an existing Credit Account.

```solidity
function multicall(
    address creditAccount,
    MultiCall[] calldata calls
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Target account |
| `calls` | `MultiCall[]` | Operations to execute |

**Access:** Account owner only. Full collateral check after execution.

---

### botMulticall

Executes a batch of operations initiated by an authorized bot.

```solidity
function botMulticall(
    address creditAccount,
    MultiCall[] calldata calls
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Target account |
| `calls` | `MultiCall[]` | Operations to execute |

**Access:** Authorized bot only. Each operation is checked against the bot's permission bitmask.

---

### liquidateCreditAccount

Liquidates an unhealthy Credit Account (HF < 1.0).

```solidity
function liquidateCreditAccount(
    address creditAccount,
    address to,
    MultiCall[] calldata calls
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to liquidate |
| `to` | `address` | Recipient of remaining funds |
| `calls` | `MultiCall[]` | Operations to execute during liquidation |

**Access:** Anyone (account must be liquidatable).

---

### setBotPermissions

Grants or revokes bot permissions for a Credit Account.

```solidity
function setBotPermissions(
    address creditAccount,
    address bot,
    uint192 permissions
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Target account |
| `bot` | `address` | Bot address to authorize |
| `permissions` | `uint192` | Permission bitmask (0 to revoke) |

**Access:** Account owner only.

---

## Multicall-Only Methods

These methods can only be called as part of a `MultiCall` array (encoded in `callData` with the Facade as `target`).

### addCollateral

```solidity
function addCollateral(address token, uint256 amount) external;
```

Transfers tokens from the caller to the Credit Account.

### increaseDebt

```solidity
function increaseDebt(uint256 amount) external;
```

Borrows additional funds from the pool. Subject to debt limits and per-block caps.

### decreaseDebt

```solidity
function decreaseDebt(uint256 amount) external;
```

Repays debt to the pool.

### withdrawCollateral

```solidity
function withdrawCollateral(address token, uint256 amount, address to) external;
```

Withdraws collateral from the Credit Account to the specified address.

### updateQuota

```solidity
function updateQuota(address token, int96 quotaChange, uint96 minQuota) external;
```

Adjusts the quota for a specific token on the Credit Account.

### storeExpectedBalances / compareBalances

```solidity
function storeExpectedBalances(BalanceWithMask[] calldata balances) external;
function compareBalances() external;
```

Slippage protection pair: store expected balances before swaps, then verify after.

---

## View Methods

### debtLimits

Returns the min/max debt bounds for this Credit Facade.

```solidity
function debtLimits() external view returns (uint128 minDebt, uint128 maxDebt);
```

---

### maxDebtPerBlockMultiplier

Returns the per-block borrowing multiplier. Value of 0 means borrowing is disabled.

```solidity
function maxDebtPerBlockMultiplier() external view returns (uint8);
```

---

### creditManager

Returns the associated Credit Manager address.

```solidity
function creditManager() external view returns (address);
```

---

### canLiquidate

Checks whether a Credit Account is liquidatable.

```solidity
function canLiquidate(address creditAccount) external view returns (bool);
```

---

### botPermissions

Returns the permission bitmask for a bot on a specific Credit Account.

```solidity
function botPermissions(address creditAccount, address bot) external view returns (uint192);
```

---

### expirable / expirationDate

Returns whether this Facade has an expiration date and what it is.

```solidity
function expirable() external view returns (bool);
function expirationDate() external view returns (uint40);
```

---

## Events

### StartMultiCall

```solidity
event StartMultiCall(address indexed creditAccount, address indexed caller);
```

Emitted at the start of a multicall execution.

### FinishMultiCall

```solidity
event FinishMultiCall();
```

Emitted when a multicall completes successfully.

### OpenCreditAccount

```solidity
event OpenCreditAccount(
    address indexed creditAccount,
    address indexed onBehalfOf,
    address indexed caller
);
```

Emitted when a Credit Account is opened through the Facade.

### CloseCreditAccount

```solidity
event CloseCreditAccount(address indexed creditAccount, address indexed caller);
```

Emitted when a Credit Account is closed.

### LiquidateCreditAccount

```solidity
event LiquidateCreditAccount(
    address indexed creditAccount,
    address indexed liquidator,
    address indexed to,
    uint256 remainingFunds
);
```

Emitted when a Credit Account is liquidated.

### SetBotPermissions

```solidity
event SetBotPermissions(
    address indexed creditAccount,
    address indexed bot,
    uint192 permissions
);
```

Emitted when bot permissions are updated.

---

## Related Pages

- [Credit Manager](https://docs.gearbox.finance/developers/gm-ref-cm) -- Core accounting engine
- [Credit Configurator](https://docs.gearbox.finance/developers/gm-ref-cc) -- Administrative configuration
- [Pool (PoolV3)](https://docs.gearbox.finance/developers/gm-ref-pool) -- Liquidity pool
- [Smart Contracts Overview](https://docs.gearbox.finance/developers/gm-contracts) -- Full contract architecture
