Use Case

Check that the sum of all positions is the same as the total supply reported by the protocol. This is useful to make sure that there isn’t a way to manipulate the protocol by minting or burning positions without it being accounted for in the total supply.

Explanation

The sum of all positions is the total amount of assets that have been borrowed or supplied in a lending protocol.

Note

This assertion assumes a cheatcode that allows iteration over all non-zero entries in a mapping.

Code Example

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import {Assertion} from "../lib/credible-std/Assertion.sol";

// We use Morpho as an example, but this could be any lending protocol
interface IMorpho {
    function totalSupply() external view returns (uint256);
}

// Assert that the sum of all positions is the same as the total supply reported by the protocol
contract PositionSumAssertion is Assertion {
    IMorpho morpho = IMorpho(address(0xbeef));

    function fnSelectors() external pure override returns (Trigger[] memory) {
        Trigger[] memory triggers = new Trigger[](1); // Define the number of triggers
        triggers[0] = Trigger(TriggerType.STORAGE, this.assertionPositionsSum.selector); // Define the trigger
        return triggers;
    }

    // Compare the sum of all positions to the total supply reported by the protocol
    // return true indicates a valid state
    // return false indicates an invalid state
    function assertionPositionsSum() external returns (bool) {
        ph.forkPostState();
        uint256[] memory assets = ph.iterator().assets; // TODO: Assuming there is a cheatcode that allows to iterate
        uint256 allSupplyPositionsSum = 0;
        for (uint256 i = 0; i < assets.length; i++) {
            allSupplyPositionsSum += assets[i];
        }
        return allSupplyPositionsSum == morpho.totalSupply();
    }
}