# 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`.
