# Credit Manager

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

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

The **CreditManagerV3** is the core accounting engine of the Gearbox Protocol. It manages Credit Account lifecycle, tracks debt and collateral, calculates health factors, and enforces risk parameters. All Credit Account state is stored and managed through this contract.

## Core Data Structures

### CreditAccountInfo

Every Credit Account's state is tracked in a `CreditAccountInfo` struct:

| Field | Type | Description |
| --- | --- | --- |
| `debt` | `uint256` | Principal amount borrowed from the pool |
| `cumulativeIndexLastUpdate` | `uint256` | Pool's interest index at last debt update |
| `cumulativeQuotaInterest` | `uint128` | Accrued quota interest not yet added to debt |
| `quotaFees` | `uint128` | Quota fees owed |
| `enabledTokensMask` | `uint256` | Bitmask of currently enabled collateral tokens |
| `flags` | `uint16` | Account state flags (e.g., `BOT_PERMISSIONS_SET_FLAG`) |
| `lastDebtUpdate` | `uint64` | Timestamp of last debt change (flash-loan protection) |
| `borrower` | `address` | Current account owner |

### Collateral Calculation Modes

| Mode | Use Case |
| --- | --- |
| `DEBT_ONLY` | Calculate debt + interest without collateral |
| `DEBT_COLLATERAL` | Full TWV + HF calculation |
| `FULL_COLLATERAL_CHECK_LAZY` | Optimized: stops early when HF exceeds threshold |

---

## Write Methods

### openCreditAccount

Opens a new Credit Account for a borrower.

```solidity
function openCreditAccount(address onBehalfOf) external returns (address creditAccount);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `onBehalfOf` | `address` | Address that will own the Credit Account |

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

**Access:** CreditFacade only.

---

### closeCreditAccount

Closes a Credit Account and settles all debt.

```solidity
function closeCreditAccount(address creditAccount) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Address of the Credit Account to close |

**Access:** CreditFacade only. Repays pool debt and returns remaining assets to the borrower.

---

### liquidateCreditAccount

Liquidates an unhealthy Credit Account.

```solidity
function liquidateCreditAccount(
    address creditAccount,
    uint256 collateralDebtData,
    address to
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to liquidate |
| `collateralDebtData` | `uint256` | Pre-computed collateral and debt data |
| `to` | `address` | Address to receive remaining assets |

**Access:** CreditFacade only. Settles debt with pool, reports profit/loss, and distributes remaining collateral.

---

### manageDebt

Increases or decreases the debt of a Credit Account.

```solidity
function manageDebt(
    address creditAccount,
    uint256 amount,
    uint256 enabledTokensMask,
    ManageDebtAction action
) external returns (uint256 newDebt, uint256 tokensToEnable, uint256 tokensToDisable);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Target Credit Account |
| `amount` | `uint256` | Amount to increase or decrease |
| `enabledTokensMask` | `uint256` | Current enabled tokens mask |
| `action` | `ManageDebtAction` | `INCREASE_DEBT` or `DECREASE_DEBT` |

**Returns:** New debt amount and token mask changes.

**Access:** CreditFacade only. Flash-loan protection prevents multiple debt changes in the same block.

---

### addCollateral

Adds collateral tokens to a Credit Account.

```solidity
function addCollateral(
    address payer,
    address creditAccount,
    address token,
    uint256 amount
) external returns (uint256 tokensToEnable);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `payer` | `address` | Address providing the tokens |
| `creditAccount` | `address` | Receiving Credit Account |
| `token` | `address` | Token to add |
| `amount` | `uint256` | Amount to transfer |

**Returns:** Token mask to enable.

**Access:** CreditFacade only.

---

### withdrawCollateral

Withdraws collateral tokens from a Credit Account.

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

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Source Credit Account |
| `token` | `address` | Token to withdraw |
| `amount` | `uint256` | Amount to withdraw |
| `to` | `address` | Recipient address |

**Returns:** Token mask to disable (if balance reaches zero).

**Access:** CreditFacade only.

---

### setActiveCreditAccount

Sets the active Credit Account for adapter execution during multicalls.

```solidity
function setActiveCreditAccount(address creditAccount) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to set as active |

**Access:** CreditFacade only.

---

### execute

Executes an external call from a Credit Account through a whitelisted adapter.

```solidity
function execute(bytes calldata data) external returns (bytes memory);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `data` | `bytes` | Encoded function call to execute |

**Returns:** Return data from the adapter call.

**Access:** Credit Account (via adapter) only.

---

### fullCollateralCheck

Performs a full collateral check on a Credit Account after operations.

```solidity
function fullCollateralCheck(
    address creditAccount,
    uint256 enabledTokensMask,
    uint256[] calldata collateralHints,
    uint16 minHealthFactor,
    bool useSafePrices
) external;
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to check |
| `enabledTokensMask` | `uint256` | Current enabled tokens mask |
| `collateralHints` | `uint256[]` | Hints for optimized collateral iteration |
| `minHealthFactor` | `uint16` | Minimum acceptable HF (typically 10000) |
| `useSafePrices` | `bool` | Whether to use safe (conservative) prices |

**Access:** CreditFacade only. Reverts if health factor is below minimum.

---

## View Methods

### calcDebtAndCollateral

Calculates debt, interest, and collateral values for a Credit Account.

```solidity
function calcDebtAndCollateral(
    address creditAccount,
    CollateralCalcTask task
) external view returns (CollateralDebtData memory);
```

| Parameter | Type | Description |
| --- | --- | --- |
| `creditAccount` | `address` | Account to calculate for |
| `task` | `CollateralCalcTask` | Calculation mode (see Collateral Calculation Modes) |

**Returns:** `CollateralDebtData` struct with `debt`, `accruedInterest`, `accruedFees`, `totalDebtUSD`, `totalValue`, `twvUSD`, `enabledTokensMask`.

---

### creditAccountInfo

Returns the full state struct for a Credit Account.

```solidity
function creditAccountInfo(address creditAccount)
    external view returns (CreditAccountInfo memory);
```

---

### enabledTokensMaskOf

Returns the bitmask of enabled collateral tokens for an account.

```solidity
function enabledTokensMaskOf(address creditAccount) external view returns (uint256);
```

---

### flagsOf

Returns the flags for a Credit Account.

```solidity
function flagsOf(address creditAccount) external view returns (uint16);
```

---

### getBorrowerOrRevert

Returns the borrower address of a Credit Account, reverting if not found.

```solidity
function getBorrowerOrRevert(address creditAccount) external view returns (address);
```

---

### getTokenMaskOrRevert

Returns the bitmask for a given token address, reverting if not registered.

```solidity
function getTokenMaskOrRevert(address token) external view returns (uint256);
```

---

### getTokenByMask

Returns the token address for a given bitmask.

```solidity
function getTokenByMask(uint256 tokenMask) external view returns (address);
```

---

### collateralTokenByMask

Returns collateral token data including liquidation threshold parameters.

```solidity
function collateralTokenByMask(uint256 tokenMask)
    external view returns (address token, uint16 ltInitial, uint16 ltFinal, uint40 timestampRampStart, uint24 rampDuration);
```

---

### forbiddenTokenMask

Returns the bitmask of forbidden tokens.

```solidity
function forbiddenTokenMask() external view returns (uint256);
```

---

### contractToAdapter / adapterToContract

Returns the adapter for a target contract, or vice versa.

```solidity
function contractToAdapter(address targetContract) external view returns (address);
function adapterToContract(address adapter) external view returns (address);
```

---

### adapters

Returns all registered adapter addresses.

```solidity
function adapters() external view returns (address[] memory);
```

---

### creditFacade / creditConfigurator / pool / underlying

Returns addresses of associated contracts.

```solidity
function creditFacade() external view returns (address);
function creditConfigurator() external view returns (address);
function pool() external view returns (address);
function underlying() external view returns (address);
```

---

## Events

### OpenCreditAccount

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

Emitted when a new Credit Account is opened.

### CloseCreditAccount

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

Emitted when a Credit Account is closed.

### LiquidateCreditAccount

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

Emitted when a Credit Account is liquidated.

### ManageDebt

```solidity
event ManageDebt(address indexed creditAccount, uint256 amount, ManageDebtAction action);
```

Emitted when debt is increased or decreased.

### AddCollateral

```solidity
event AddCollateral(address indexed creditAccount, address indexed token, uint256 amount);
```

Emitted when collateral is added to an account.

### WithdrawCollateral

```solidity
event WithdrawCollateral(address indexed creditAccount, address indexed token, uint256 amount, address to);
```

Emitted when collateral is withdrawn from an account.

---

## Related Pages

- [Credit Facade](https://docs.gearbox.finance/developers/gm-ref-cf) -- User-facing interface for Credit Account operations
- [Credit Configurator](https://docs.gearbox.finance/developers/gm-ref-cc) -- Administrative parameter management
- [Pool (PoolV3)](https://docs.gearbox.finance/developers/gm-ref-pool) -- Liquidity pool that Credit Manager borrows from
- [Smart Contracts Overview](https://docs.gearbox.finance/developers/gm-contracts) -- Full contract architecture
