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. This is useful for all dApps that track the total amount of assets as well as the balances of users, which is most.

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/src/Assertion.sol";

// !!! This assertion is not working currently, as the pre-compile needed is not yet implemented or fully specified

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

    function assets(uint256) 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 public morpho = IMorpho(address(0xbeef));

    function fnSelectors() external pure override returns (bytes4[] memory assertions) {
        assertions = new bytes4[](1);
        assertions[0] = this.assertionPositionsSum.selector;
    }

    // Compare the sum of all positions to the total supply reported by the protocol
    function assertionPositionsSum() external {
        ph.forkPostState();
        uint256[] memory assets = ph.iterate(morpho.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];
        }
        require(allSupplyPositionsSum == morpho.totalSupply(), "Positions sum does not match total supply");
    }
}