> For the complete documentation index, see [llms.txt](https://docs.fathom.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fathom.fi/fxd-stablecoin/bookkeeping-mechanism/collateral-accounting.md).

# Collateral Accounting

### Where is a position's collateral recorded?

Each position's collateral amount and debt are recorded in the `BookKeeper` contract. In the `BookKeeper` contract, [positions](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L28) are the double mapping that keeps track of each position's `lockedCollateral` and `debtShare`.&#x20;

<pre class="language-solidity" data-overflow="wrap" data-full-width="false"><code class="lang-solidity"><strong>mapping(bytes32 => mapping(address => Position)) public positions;
</strong></code></pre>

[positions](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L28) mapping has the value as `Position` struct, and the struct's first member, `lockedCollateral` keeps track of the collateral.

{% code overflow="wrap" fullWidth="false" %}

```solidity
struct Position {
    uint256 lockedCollateral; // Locked collateral inside this position
    uint256 debtShare;
}
```

{% endcode %}

### How is a position's collateral recorded?

#### [position.lockedCollateral](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L24)

`position.lockedCollateral` represents the amount of collateral locked in a specific position. It serves as the primary reference to determine the amount of collateral a particular position holds. The value of `position.lockedCollateral` increases when a position is opened and decreases when it is closed. During the position opening flow, the value of [CollateralToken](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L29) variable [increase temporarily](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/adapters/CollateralTokenAdapter/CollateralTokenAdapter.sol#L196C9-L196C9), and later gets transferred to `position.lockedCollateral`*.*

{% code overflow="wrap" %}

```solidity
mapping(bytes32 => mapping(address => uint256)) public collateralToken;
```

{% endcode %}

Similarly, the position `value.lockedCollateral` is temporarily transferred to the `CollateralToken` variable during the position closing flow. Then, when the collateral gets withdrawn from `CollateralTokenAdapter`, the temporarily increased number on [CollateralToken](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L29) decreases as much as [withdrawn](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/adapters/CollateralTokenAdapter/CollateralTokenAdapter.sol#L214).

Three functions are directly involved in `lockedCollateral` accounting.

[fn adjustPosition](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L291) in `BookKeeper`.

[fn moveCollateral](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/BookKeeper.sol#L246) in `BookKeeper`.

[fn withdraw](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/adapters/CollateralTokenAdapter/CollateralTokenAdapter.sol#L210) in `CollateralTokenAdapter`.

[fn deposit](https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/stablecoin-core/adapters/CollateralTokenAdapter/CollateralTokenAdapter.sol#L187) in `CollateralTokenAdapter`.


---

# 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.fathom.fi/fxd-stablecoin/bookkeeping-mechanism/collateral-accounting.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.
