Skip to content
Verified contractPartialMIT

WAD18SettlementEngine

Address0xe2ce0ac5a9bbe70db4afc804c98b72f0c4d0249a
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.20

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
No
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,359,918
Creation transaction
0x1300f93f17…67c108833c

WAD18SettlementEngine.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title WAD18SettlementEngine
 * @author Michael Aaron Russell
 * @notice Invariant-grounded, zero-drift double-entry settlement engine.
 * @dev Enforces 10^18 WAD scaling, strict leverage thresholds (45% warning / 48% freeze),
 *      and deterministic cross-chain replay integrity.
 */
contract WAD18SettlementEngine {
    // --- CONSTANTS & WAD SUBSTRATE ---
    uint256 public constant WAD = 1e18;
    
    // Policy Leverage Thresholds (Cross-multiplied WAD scale)
    uint256 public constant THRESHOLD_RESTRICTED = 450000000000000000; // 0.45 * 10^18 (45%)
    uint256 public constant THRESHOLD_FREEZE     = 480000000000000000; // 0.48 * 10^18 (48%)

    // --- ENUMS & STRUCTS ---
    enum SystemState { NORMAL, RESTRICTED, FROZEN }

    struct SubLedger {
        uint256 debits;
        uint256 credits;
    }

    // --- STATE VARIABLES ---
    SystemState public currentState;
    uint256 public totalDebt;
    uint256 public enterpriseValue;
    bytes32 public pinnedStateHash;

    mapping(address => SubLedger) public subLedgers;

    // --- EVENTS ---
    event TransactionExecuted(address indexed account, uint256 debits, uint256 credits, bytes32 newStateHash);
    event PolicyStateChanged(SystemState previousState, SystemState newState);
    event EngineFrozen(uint256 currentLeverageWad);
    event EngineUnfrozen(bytes32 auditProof);

    // --- MODIFIERS ---
    modifier whenNotFrozen() {
        require(currentState != SystemState.FROZEN, "WAD18: Engine is in sticky FROZEN state");
        _;
    }

    /**
     * @notice Constructor accepts clean base numbers (e.g., 2800 for $2.8B, 1000 for $1B) 
     *         and automatically scales them by WAD (1e18) to prevent Remix input errors.
     */
    constructor(uint256 initialEVBase, uint256 initialDebtBase) {
        require(initialEVBase > 0, "WAD18: EV must be positive");
        enterpriseValue = initialEVBase * WAD;
        totalDebt = initialDebtBase * WAD;
        currentState = SystemState.NORMAL;
        _updateStateHash(address(0), 0, 0); // Genesis hash
    }

    // --- MATH PRIMITIVES (Zero-Drift WAD18) ---
    function wadMul(uint256 a, uint256 b) public pure returns (uint256) {
        return (a * b + (WAD / 2)) / WAD;
    }

    function wadDiv(uint256 a, uint256 b) public pure returns (uint256) {
        require(b > 0, "WAD18: Division by zero");
        return (a * WAD + (b / 2)) / b;
    }

    // --- CORE SETTLEMENT ENGINE ---
    function settleBlock(
        address account,
        uint256 debitAmount,
        uint256 creditAmount,
        uint256 newDebtBalance
    ) external whenNotFrozen returns (bool) {
        require(debitAmount == creditAmount, "WAD18: Double-entry imbalance (Dr != Cr)");

        // 1. Pre-calculate projected leverage BEFORE modifying state
        uint256 projectedLeverage = wadDiv(newDebtBalance, enterpriseValue);

        // 2. Check if the projected leverage triggers a freeze.
        // If it does, we freeze the engine and REVERT the transaction so the 
        // bad debt is not written to the ledger.
        if (projectedLeverage > THRESHOLD_FREEZE) {
            currentState = SystemState.FROZEN;
            emit EngineFrozen(projectedLeverage);
            emit PolicyStateChanged(SystemState.NORMAL, SystemState.FROZEN);
            revert("WAD18: Transaction rejected, triggered FROZEN state");
        }

        // 3. Check RESTRICTED mode (deleveraging only)
        if (currentState == SystemState.RESTRICTED) {
            require(newDebtBalance < totalDebt, "WAD18: RESTRICTED mode permits deleveraging only");
        }

        // 4. Commit the ledger state
        subLedgers[account].debits += debitAmount;
        subLedgers[account].credits += creditAmount;
        totalDebt = newDebtBalance;

        // 5. Adjust policy state if in normal/restricted bands
        if (projectedLeverage > THRESHOLD_RESTRICTED) {
            if (currentState != SystemState.RESTRICTED) {
                currentState = SystemState.RESTRICTED;
                emit PolicyStateChanged(SystemState.NORMAL, SystemState.RESTRICTED);
            }
        } else {
            if (currentState != SystemState.NORMAL) {
                currentState = SystemState.NORMAL;
                emit PolicyStateChanged(SystemState.RESTRICTED, SystemState.NORMAL);
            }
        }

        bytes32 newHash = _updateStateHash(account, debitAmount, creditAmount);
        emit TransactionExecuted(account, debitAmount, creditAmount, newHash);

        return true;
    }

    function auditedUnfreeze(bytes32 auditProof, uint256 auditedDebt) external {
        require(currentState == SystemState.FROZEN, "WAD18: Engine not frozen");
        
        uint256 verifiedLeverage = wadDiv(auditedDebt, enterpriseValue);
        require(verifiedLeverage <= THRESHOLD_RESTRICTED, "WAD18: Leverage remains above restriction threshold");

        totalDebt = auditedDebt;
        currentState = SystemState.NORMAL;
        
        // Note: Unfreeze records address(0) as the actor, as it's a state reset
        bytes32 newHash = _updateStateHash(address(0), auditedDebt, 0);
        emit EngineUnfrozen(auditProof);
        emit PolicyStateChanged(SystemState.FROZEN, SystemState.NORMAL);
    }

    // --- INTERNAL AUDIT & STATE REPLAY ---
    
    /**
     * @dev The hash is a pure function of the state inputs. 
     * block.chainid has been removed to ensure 100% bit-for-bit cross-chain replay.
     * abi.encode is used instead of encodePacked to prevent hash collisions.
     */
    function _updateStateHash(
        address account, 
        uint256 debitAmount, 
        uint256 creditAmount
    ) internal returns (bytes32) {
        
        pinnedStateHash = keccak256(
            abi.encode(
                totalDebt,
                enterpriseValue,
                currentState,
                account,
                debitAmount,
                creditAmount
            )
        );
        return pinnedStateHash;
    }
}
Compiler settingsShow
{
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.