> ## Documentation Index
> Fetch the complete documentation index at: https://docs.phylax.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# Sum of all positions

> Assert that the sum of all positions is the same as the total supply reported by the protocol

## When to Use This Pattern

Verifies that the sum of all individual positions exactly matches the total supply reported by the protocol, preventing supply manipulation, double counting, and accounting discrepancies. Critical for lending protocols, liquidity pools, staking mechanisms, synthetic asset protocols, and fractionalized NFT protocols where accurate position accounting is fundamental to security.

Any discrepancy between individual positions and total supply could lead to incorrect calculations, withdrawal failures, or protocol insolvency.

## What This Pattern Checks

Since direct iteration over all positions isn't currently supported, this assertion uses a workaround that tracks position changes through function calls:

1. Capture the pre-state total supply before the transaction
2. Monitor all function calls that modify positions (deposit, withdraw)
3. Calculate the sum of changes to these positions
4. Verify that the new total supply equals the pre-state total supply plus the sum of position changes

Uses these cheatcodes:

* `_preTx()` / `_postTx()` with Reshiram snapshot reads: Capture total supply before and after transaction
* `ph.context()` and `ph.callinputAt()`: Inspect the triggered position-modifying call
* `registerFnCallTrigger()`: Trigger on position-modifying functions

Check [Modified Key Discovery](/credible/use-case-mappings/unsupported-use-cases/modified-keys) for details on what direct position iteration would look like.

For more information about cheatcodes, see the [Cheatcodes Documentation](/credible/cheatcodes-reference).

## Assertion Pattern

```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {Assertion} from "credible-std/Assertion.sol";
import {AssertionSpec} from "credible-std/SpecRecorder.sol";
import {PhEvm} from "credible-std/PhEvm.sol";

contract PositionSumAssertion is Assertion {
    bytes32 internal constant TOTAL_SUPPLY_SLOT = bytes32(uint256(0));

    constructor() {
        registerAssertionSpec(AssertionSpec.Reshiram);
    }

    function triggers() external view override {
        registerFnCallTrigger(this.assertionPositionsSum.selector, ILending.deposit.selector);
    }

    /// @notice Checks that a deposit call increases stored total supply by the deposited amount.
    function assertionPositionsSum() external view {
        address adopter = ph.getAssertionAdopter();
        PhEvm.TriggerContext memory ctx = ph.context();
        PhEvm.ForkId memory preCall = PhEvm.ForkId({forkType: 2, callIndex: ctx.callStart});
        PhEvm.ForkId memory postCall = PhEvm.ForkId({forkType: 3, callIndex: ctx.callEnd});

        uint256 preTotalSupply = uint256(ph.loadStateAt(adopter, TOTAL_SUPPLY_SLOT, preCall));
        uint256 postTotalSupply = uint256(ph.loadStateAt(adopter, TOTAL_SUPPLY_SLOT, postCall));
        (, uint256 amount) = abi.decode(_stripSelector(ph.callinputAt(ctx.callStart)), (address, uint256));

        require(postTotalSupply == preTotalSupply + amount, "Positions sum does not match total supply");
    }

    function _stripSelector(bytes memory data) internal pure returns (bytes memory) {
        bytes memory stripped = new bytes(data.length - 4);
        for (uint256 i = 4; i < data.length; i++) {
            stripped[i - 4] = data[i];
        }
        return stripped;
    }
}

interface ILending {
    function deposit(address user, uint256 amount) external;
}
```

<Note>Full examples and mock protocol code are available in [credible-std](https://github.com/phylaxsystems/credible-std/tree/master/examples/assertions-book).</Note>

## Related Patterns

* [Lending Health Factor](/assertions-book/assertions/ass07-lending-health-factor): Ensures user balances are consistent across different views
* [Liquidation Health Factor](/assertions-book/assertions/ass16-liquidation-health-factor): Verifies that the total supply doesn't exceed protocol limits
