> For the complete documentation index, see [llms.txt](https://docs.kpk.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kpk.io/funds/infrastructure/architecture/onchain-accounting/meta-balance-adapters/gearbox-credit-accounts.md). Agents can also search and read this documentation through its [MCP server](https://docs.kpk.io/~gitbook/mcp).

# Gearbox Credit Accounts

One immutable meta-adapter that reports the borrow side of an account's Gearbox V3 Credit Accounts, the collateral basket netted against the debt.

One immutable meta-adapter that reports the **borrow side** of Gearbox V3, each Credit Account's collateral basket netted against its debt. The supply side (passive liquidity in the ERC-4626 farming pools) is the separate [Gearbox Markets](https://docs.kpk.io/funds/infrastructure/architecture/onchain-accounting/meta-balance-adapters/gearbox.md) adapter. Which Credit Accounts it queries is a governed instance set on the NAV Calculator; the adapter holds no per-account state.

* **Type:** Meta-adapter
* **protocolSubId:** `keccak256("gearbox-v3-credit-accounts")` (brand `gearbox`, ecosystem `gearbox-v3`)
* **Instance key:** the **Credit Account address**, widened to `bytes32`
* **Source:** `GearboxV3CreditAccountsMetaBalanceAdapter.sol`

{% hint style="warning" %}
**This page documents the design;** [**Deployment addresses**](https://docs.kpk.io/funds/infrastructure/architecture/onchain-accounting/deployment-addresses.md) **is the authority on whether it is live.** If this adapter has no entry there, it is not deployed and has no instance set.
{% endhint %}

## Why key on the Credit Account

Gearbox has no `borrower → creditAccount` view, only `creditAccountInfo(ca).borrower` and `creditAccounts()`. Each read therefore confirms `creditAccountInfo(ca).borrower == account`; a closed or liquidated account reads `address(0)` and contributes nothing.

## Positions returned

Per Credit Account the account owns, one debt leg plus one leg per enabled collateral token:

| Leg        | PositionKind | isDebt  | isLocked | Description                                                                                                             |
| ---------- | ------------ | ------- | -------- | ----------------------------------------------------------------------------------------------------------------------- |
| Borrowed   | `Borrowed`   | `true`  | `false`  | Total owed in the pool's underlying, `debt + accruedInterest + accruedFees` from `calcDebtAndCollateral(ca, DEBT_ONLY)` |
| Collateral | `Collateral` | `false` | `false`  | One leg per enabled token the account holds (`balanceOf(creditAccount)`)                                                |

Collateral tokens that are unregistered or unpriced in NAVCalculator (or excluded by `assetFilter`) and zero balances are skipped.

{% hint style="info" %}
**Never over-reports.** The adapter values the debt **first**. If the debt cannot be read or reported (underlying unreadable or unregistered on NAVCalculator), it drops the **whole** Credit Account rather than emit collateral with no offsetting debt. Dropping an unregistered collateral token while counting the full debt only under-reports. See [Never report collateral without its debt](https://docs.kpk.io/funds/infrastructure/architecture/onchain-accounting/balance-adapters.md#how-an-adapter-is-built).
{% endhint %}

## Balance calculation

<figure><picture><source srcset="https://1699303810-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUsQM4bzcmOPSgkWddkeN%2Fuploads%2FzkN6U2noZFC241jnQdF6%2Fmeta-gearbox-credit-accounts_D.png?alt=media&amp;token=236d8421-234f-49fe-ac5c-44b2365fa3bb" media="(prefers-color-scheme: dark)"><img src="https://1699303810-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUsQM4bzcmOPSgkWddkeN%2Fuploads%2F8WeSpP1DRsxulI7lUvOL%2Fmeta-gearbox-credit-accounts.png?alt=media&amp;token=c8733b10-d5b6-410f-9895-cdd2df236f8b" alt="Flowchart of the Gearbox Credit Accounts balance adapter: each cached Credit Account is checked for ownership and a readable debt, then valued into a Borrowed leg plus per-token Collateral legs."></picture><figcaption><p>Gearbox Credit Accounts adapter: how the underlying balances reported to the NAV Calculator are derived.</p></figcaption></figure>

For each Credit Account:

```
cm = ca.creditManager()                              // ICreditAccountV3
(enabledMask, borrower) = cm.creditAccountInfo(ca)
require borrower == account                          // else: empty

(debtOk, debt) = cm.calcDebtAndCollateral(ca, DEBT_ONLY)
if !debtOk: return []                                // whole-account drop (no over-report)

for each enabled token (set bit of enabledMask):
    bal = token.balanceOf(ca)                        // → Collateral leg (if registered & > 0)
if debt > 0:
    → Borrowed leg in cm.underlying()
```

## Identity

* **positionId:** `abi.encode(address creditAccount)`; the Borrowed leg and every Collateral leg share it and are told apart by `positionKind` and `balanceAsset.asset`.
* **positionInstanceId:** `bytes32(0)`; the Credit Account address is a stable anchor, so identity lives in `positionId`.
* **positionKind:** `Borrowed` · `Collateral`
* **labels:** the position has no `labels` field. `positionLabels(positionId)` returns `["Credit Account", "<credit-manager name()>"]`, falling back to the pool's underlying asset symbol (e.g. `"USDC"`) when the credit manager's `name()` is missing or empty. Surfaced only by the verbose reads; the full breadcrumb is `[protocolName, ...labels]`.

## Constructor

```solidity
constructor(address navCalculator_)
```

| Parameter        | Description                                             |
| ---------------- | ------------------------------------------------------- |
| `navCalculator_` | NAV Calculator contract (asset registry + instance set) |

## Registration

Register once and seed Credit Accounts with `addMetaBalanceAdapter(adapter, instances)`, then adjust with `addMetaInstances(adapter, instances)` / `removeMetaInstances(adapter, instances)` (MANAGER-gated). Each instance coordinate is a **Credit Account address widened to `bytes32`** (decoded via `_toAddress`). A malformed coordinate is isolated per-instance by the fan-out.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.kpk.io/funds/infrastructure/architecture/onchain-accounting/meta-balance-adapters/gearbox-credit-accounts.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
