> 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/onchain-accounting/balance-adapters/sparklend.md).

# SparkLend

Reports an account's per-reserve lending positions in SparkLend. For each reserve it emits a supply/collateral leg (from the aToken balance) and a borrow leg (from variable debt). It enumerates reserves trustlessly from the SparkLend Protocol Data Provider, so it is a single fixed-scope contract.

* **Type:** Plain adapter
* **protocolSubId:** `keccak256("spark-lend")`
* **Source:** [`SparkLendBalanceAdapter.sol`](https://github.com/karpatkey/onchain-accounting/blob/main/src/balances/BalanceAdapters/SparkLendBalanceAdapter.sol)

***

## Positions returned

Up to two legs per reserve that is both a SparkLend reserve and registered on NAVCalculator:

| Leg        | PositionKind | isDebt  | Description                                                          |
| ---------- | ------------ | ------- | -------------------------------------------------------------------- |
| Supply     | `Supplied`   | `false` | aToken balance when `usageAsCollateralEnabled == false`              |
| Collateral | `Collateral` | `false` | aToken balance when `usageAsCollateralEnabled == true`               |
| Borrow     | `Borrowed`   | `true`  | Variable + legacy stable debt, summed — one Borrowed leg per reserve |

SparkLend is an Aave V3 fork: new stable borrows are deprecated, but **legacy stable debt can still persist**, so the adapter sums `variableDebt + stableDebt` into the borrow leg. Dropping the stable portion would understate debt and overstate NAV. Reserves with zero supply and zero debt are skipped.

For the same reason, if the account borrows a reserve whose asset is **unregistered** on NAVCalculator, the adapter reports **nothing at all** rather than emitting collateral without its offsetting debt. The borrowed set comes from the user-configuration bitmap, falling back to a full reserve scan when it is unavailable. See [Never report collateral without its debt](/funds/infrastructure/onchain-accounting/balance-adapters.md#how-an-adapter-is-built).

***

## Balance calculation

<figure><img src="/files/eNp2pXhJvJpxYjFG33JA" alt="Flowchart of the SparkLend balance adapter: read calls derive position legs into PositionBalance entries."><figcaption><p>SparkLend adapter — how the underlying balances reported to the NAV Calculator are derived.</p></figcaption></figure>

Per reserve, the adapter reads `getUserReserveData(asset, account)` from the Protocol Data Provider:

```
supply = currentATokenBalance
debt   = currentVariableDebt + currentStableDebt   // legacy stable folded in
asCollateral = usageAsCollateralEnabled
```

The supply leg uses `PositionKind.Collateral` if `asCollateral` is true, else `PositionKind.Supplied`. The borrow leg is emitted with `isDebt = true`.

{% hint style="info" %}
The auxiliary `getAdapterBalanceForAsset` / `getAdapterBalances` views return a single net `supply − debt` scalar per asset. The main NAV path, `getAdapterPositions`, keeps the legs separate.
{% endhint %}

***

## Identity

* **positionId:** `abi.encode(address pool)` — `pool` is the SparkLend Pool resolved at construction; the reserve each leg refers to is carried in `balanceAsset`, not the `positionId`
* **positionKind:** `Supplied` · `Collateral` · `Borrowed`

***

## Constructor

```solidity
constructor(address dataProvider_, address navCalculator_)
```

| Parameter        | Description                                                                                                 |
| ---------------- | ----------------------------------------------------------------------------------------------------------- |
| `dataProvider_`  | SparkLend Protocol Data Provider (must be a contract); source of the reserve list and per-user reserve data |
| `navCalculator_` | NAVCalculator address (must be a contract); source of the asset registry                                    |

The `POOL` address used in `positionId` is derived at construction from `dataProvider_.ADDRESSES_PROVIDER().getPool()`.

***

## Registration

Registered as a plain adapter with `addBalanceAdapters([adapter])` (MANAGER-gated); removed with `removeBalanceAdapters`.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kpk.io/funds/infrastructure/onchain-accounting/balance-adapters/sparklend.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
