Skip to content
Verified contractPartialUNLICENSED

BatchSeizer

Address0xf5062933dc61fd3bc4e0a271bb73458f06512d93
Balance

0 ETH

 

Transactions

48

 

Compiler

solc 0.8.27

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.27+commit.40a35a09
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
UNLICENSED
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,429,599
Creation transaction
0x0fd5c5d825…3d78c2eb19

94 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/BatchSeizer.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;

import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {WarehouseVault} from "./WarehouseVault.sol";
import {Roles} from "./libraries/Roles.sol";

/// @notice Batches only the bound Vault's existing, ordinary warehouse seizures.
/// @dev Both this helper and its caller require the Vault's current LIQUIDATOR_ROLE.
contract BatchSeizer is ReentrancyGuard {
    struct Candidate {
        address borrower;
        uint256 facilityId;
        uint256 expectedNonce;
    }

    uint256 public constant MAX_BATCH_SIZE = 20;
    uint256 public constant SEIZURE_GAS = 1_500_000;
    // Covers the nonce read, call setup, bounded return handling and function exit.
    uint256 private constant CALL_HEADROOM = 50_000;
    uint256 private constant RESULT_GAS = 10_000;
    WarehouseVault public immutable vault;

    error InvalidVault();
    error Unauthorized(address account);
    error InvalidBatchSize();
    error DuplicateCandidate(uint256 first, uint256 second);
    error StaleNonce();
    error InsufficientGas();
    /// @dev Includes gas-cap exhaustion and reverts with fewer than four bytes of data.
    error UnspecifiedFailure();
    error InvalidReturnData();

    event SeizureResult(
        uint256 indexed index,
        address indexed borrower,
        uint256 indexed facilityId,
        uint256 expectedNonce,
        bool success,
        bytes32 seizureId,
        bytes4 reason
    );

    constructor(WarehouseVault vault_) {
        if (address(vault_).code.length == 0) revert InvalidVault();
        vault = vault_;
    }

    /// @param candidates Each expectedNonce is the nonce BEFORE the proposed seizure.
    /// @return successes Successful items, including zero when every item fails or is stale.
    /// @return processed Visited prefix length, including stale/failed items, excluding the gas-short tail.
    /// @dev Every item revert is isolated, including unexpected errors and gas-cap exhaustion.
    ///      Reserve every remaining result before each call. Gas-short tails are explicitly unattempted;
    ///      prior successes persist. Simulate/send with the same explicit gas limit, not estimateGas:
    ///      a low-gas simulation can return successfully with processed == 0.
    function seize(Candidate[] calldata candidates)
        external
        nonReentrant
        returns (uint256 successes, uint256 processed)
    {
        if (!vault.hasRole(Roles.LIQUIDATOR_ROLE, msg.sender)) {
            revert Unauthorized(msg.sender);
        }
        if (!vault.hasRole(Roles.LIQUIDATOR_ROLE, address(this))) {
            revert Unauthorized(address(this));
        }
        uint256 length = candidates.length;
        if (length == 0 || length > MAX_BATCH_SIZE) revert InvalidBatchSize();
        for (uint256 i; i < length; ++i) {
            for (uint256 j; j < i; ++j) {
                if (
                    candidates[i].borrower == candidates[j].borrower
                        && candidates[i].facilityId == candidates[j].facilityId
                ) {
                    revert DuplicateCandidate(j, i);
                }
            }
        }
        // This refusal happens before any seizure, so an unusably small budget cannot lose progress.
        if (gasleft() < length * RESULT_GAS + CALL_HEADROOM) revert InsufficientGas();
        for (uint256 i; i < length; ++i) {
            // EIP-150 must not reduce the fixed allowance. Even a call consuming all of it leaves
            // enough gas to report the entire unattempted suffix and return without rolling back.
            uint256 requiredGas =
                SEIZURE_GAS + (SEIZURE_GAS + 62) / 63 + CALL_HEADROOM + (length - i) * RESULT_GAS;
            if (gasleft() < requiredGas) {
                for (uint256 j = i; j < length; ++j) {
                    Candidate calldata unattempted = candidates[j];
                    emit SeizureResult(
                        j,
                        unattempted.borrower,
                        unattempted.facilityId,
                        unattempted.expectedNonce,
                        false,
                        bytes32(0),
                        InsufficientGas.selector
                    );
                }
                return (successes, processed);
            }
            Candidate calldata candidate = candidates[i];
            bool success;
            bytes32 seizureId;
            bytes4 reason;
            if (
                vault.seizureNonce(candidate.borrower, candidate.facilityId)
                    != candidate.expectedNonce
            ) {
                reason = StaleNonce.selector;
            } else {
                (success, seizureId, reason) = _seize(candidate);
                if (success) ++successes;
            }
            emit SeizureResult(
                i,
                candidate.borrower,
                candidate.facilityId,
                candidate.expectedNonce,
                success,
                seizureId,
                reason
            );
            ++processed;
        }
    }

    function _seize(Candidate calldata candidate)
        private
        returns (bool success, bytes32 seizureId, bytes4 reason)
    {
        bytes memory data = abi.encodeCall(
            vault.seizeToWarehouse, (candidate.borrower, candidate.facilityId)
        );
        address target = address(vault);
        uint256 allowance = SEIZURE_GAS;
        uint256 size;
        bytes32 result;
        assembly ("memory-safe") {
            mstore(0, 0)
            success := call(allowance, target, 0, add(data, 32), mload(data), 0, 32)
            size := returndatasize()
            result := mload(0)
        }
        // Copy at most one word, even if a downstream contract produces enormous revert data.
        if (success) {
            if (size != 32) revert InvalidReturnData();
            seizureId = result;
        } else {
            reason = size >= 4 ? bytes4(result) : UnspecifiedFailure.selector;
            if (reason == bytes4(0)) reason = UnspecifiedFailure.selector;
        }
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "lib/openzeppelin-community-contracts/contracts/:@openzeppelin/contracts/=lib/openzeppelin-community-contracts/lib/@openzeppelin-contracts/contracts/",
    "lib/openzeppelin-community-contracts/contracts/:@openzeppelin/contracts/=lib/openzeppelin-community-contracts/lib/@openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "forge-std/=lib/forge-std/src/",
    "@openzeppelin/community-contracts/=lib/openzeppelin-community-contracts/contracts/",
    "@openzeppelin7540/contracts/=lib/openzeppelin-community-contracts/lib/@openzeppelin-contracts/contracts/",
    "composable-cow/=lib/composable-cow/src/",
    "lib/composable-cow/:@openzeppelin/=lib/composable-cow/lib/@openzeppelin/",
    "lib/composable-cow/:@openzeppelin/=lib/composable-cow/lib/@openzeppelin/",
    "cowprotocol/=lib/composable-cow/lib/cowprotocol/src/",
    "safe/=lib/composable-cow/lib/safe/contracts/",
    "@axelar-network/axelar-gmp-sdk-solidity/=lib/openzeppelin-community-contracts/lib/axelar-gmp-sdk-solidity/",
    "@openzeppelin-contracts/=lib/openzeppelin-community-contracts/lib/@openzeppelin-contracts/",
    "@openzeppelin/=lib/composable-cow/lib/@openzeppelin/",
    "@zk-email/contracts/=lib/openzeppelin-community-contracts/lib/zk-email-verify/packages/contracts/",
    "@zk-email/email-tx-builder/=lib/openzeppelin-community-contracts/lib/email-tx-builder/packages/contracts/",
    "ds-test/=lib/composable-cow/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "murky/=lib/composable-cow/lib/murky/src/",
    "openzeppelin-community-contracts/=lib/openzeppelin-community-contracts/contracts/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/=lib/composable-cow/lib/@openzeppelin/contracts/",
    "wormhole-solidity-sdk/=lib/openzeppelin-community-contracts/lib/wormhole-solidity-sdk/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.