Skip to content
Verified contractExactMIT

Pectra

Address0x17c11fddadac2b341f2455afe988fec4c3ba26e3
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
No
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,452,607
Creation transaction
0x87755b8f5b…f9597da962

src/Pectra.sol

/**
 * ██╗     ██╗   ██╗ ██████╗  █████╗ ███╗   ██╗ ██████╗ ██████╗ ███████╗███████╗
 * ██║     ██║   ██║██╔════╝ ██╔══██╗████╗  ██║██╔═══██╗██╔══██╗██╔════╝██╔════╝
 * ██║     ██║   ██║██║  ███╗███████║██╔██╗ ██║██║   ██║██║  ██║█████╗  ███████╗
 * ██║     ██║   ██║██║   ██║██╔══██║██║╚██╗██║██║   ██║██║  ██║██╔══╝  ╚════██║
 * ███████╗╚██████╔╝╚██████╔╝██║  ██║██║ ╚████║╚██████╔╝██████╔╝███████╗███████║
 * ╚══════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝
 */
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";

contract Pectra is IERC165, IERC721Receiver, IERC1155Receiver {
    address public constant consolidationTarget = 0x0000BBdDc7CE488642fb579F8B00f3a590007251;
    address public constant exitTarget = 0x00000961Ef480Eb55e80D19ad83579A64c007002;

    // Constants for validator-related parameters
    /// @dev The expected length of a validator public key in bytes
    uint256 public constant VALIDATOR_PUBKEY_LENGTH = 48;
    /// @dev The expected length of an amount value in bytes for EL exit
    uint256 public constant AMOUNT_LENGTH = 8;
    /// @dev Minimum number of validators required for batch operations
    uint256 public constant MIN_VALIDATORS = 1;
    /// @dev Maximum number of source validators allowed in consolidation,
    uint256 public constant MAX_SOURCE_VALIDATORS = 63;
    /// @dev Maximum number of validators allowed in switch and EL exit operations
    uint256 public constant MAX_VALIDATORS = 200;
    /// @dev Minimum fee required per validator
    uint256 public constant MIN_FEE = 1 wei;
    /// @dev Maximum withdrawal amount as a uint64 (representing 2048 ether in gwei)
    uint64 public constant MAX_WITHDRAWAL_AMOUNT = 2048000000000; // 2048 ETH in Gwei

    // Interface IDs
    bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
    bytes4 private constant _INTERFACE_ID_ERC721_RECEIVER = 0x150b7a02;
    bytes4 private constant _INTERFACE_ID_ERC1155_RECEIVER = 0x4e2312e0;

    // Failure reason codes as enum
    enum FailureReason {
        INVALID_PUBKEY_LENGTH,
        OPERATION_FAILED,
        INVALID_AMOUNT_LENGTH,
        INVALID_AMOUNT_VALUE,
        FULL_EXIT_NOT_CONFIRMED,
        AMOUNT_EXCEEDS_MAXIMUM,
        FULL_EXIT_WITH_AMOUNT
    }

    event ConsolidationFailed(FailureReason reasonCode, bytes sourcePubkey, bytes targetPubkey);
    event SwitchFailed(FailureReason reasonCode, bytes pubkey);
    event ExecutionLayerExitFailed(FailureReason reasonCode, bytes pubkey, uint64 amount);

    error Unauthorized();
    error InvalidTargetPubkeyLength(bytes invalidTargetPubkey);
    error MinimumValidatorRequired();
    error TooManySourceValidators();
    error TooManyValidators();
    error InsufficientFeePerValidator();

    receive() external payable {}
    fallback() external payable {}

    /**
     * @dev Implementation of the {IERC165} interface.
     *
     * Returns true if this contract implements the interface defined by
     * `interfaceId`.
     *
     * This function call must use less than 30,000 gas.
     */
    function supportsInterface(bytes4 interfaceId) public pure override returns (bool) {
        return interfaceId == _INTERFACE_ID_ERC165 || interfaceId == _INTERFACE_ID_ERC721_RECEIVER
            || interfaceId == _INTERFACE_ID_ERC1155_RECEIVER;
    }

    function onERC721Received(address, address, uint256, bytes calldata) external pure override returns (bytes4) {
        return this.onERC721Received.selector;
    }

    function onERC1155Received(address, address, uint256, uint256, bytes calldata)
        external
        pure
        override
        returns (bytes4)
    {
        return this.onERC1155Received.selector;
    }

    function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
        external
        pure
        override
        returns (bytes4)
    {
        return this.onERC1155BatchReceived.selector;
    }

    modifier onlySelf() {
        require(msg.sender == address(this), Unauthorized());
        _;
    }

    function getConsolidationFee() public view returns (uint256 fee) {
        (bool readOK, bytes memory feeData) = consolidationTarget.staticcall("");
        if (!readOK) return MIN_FEE;
        fee = uint256(bytes32(feeData));
    }

    function getExitFee() public view returns (uint256 fee) {
        (bool readOK, bytes memory feeData) = exitTarget.staticcall("");
        if (!readOK) return MIN_FEE;
        fee = uint256(bytes32(feeData));
    }

    function batchConsolidation(bytes[] calldata sourcePubkeys, bytes calldata targetPubkey)
        external
        payable
        onlySelf
    {
        uint256 batchSize = sourcePubkeys.length;
        require(batchSize >= MIN_VALIDATORS, MinimumValidatorRequired());
        require(batchSize <= MAX_SOURCE_VALIDATORS, TooManySourceValidators());
        if (targetPubkey.length != VALIDATOR_PUBKEY_LENGTH) {
            revert InvalidTargetPubkeyLength(targetPubkey);
        }

        uint256 consolidationFee = getConsolidationFee();
        require(msg.value >= batchSize * consolidationFee, InsufficientFeePerValidator());

        for (uint256 i = 0; i < batchSize; ++i) {
            if (sourcePubkeys[i].length != VALIDATOR_PUBKEY_LENGTH) {
                emit ConsolidationFailed(FailureReason.INVALID_PUBKEY_LENGTH, sourcePubkeys[i], targetPubkey);
                continue;
            }

            bytes memory concatenated = abi.encodePacked(sourcePubkeys[i], targetPubkey);
            (bool success,) = consolidationTarget.call{value: consolidationFee}(concatenated);
            if (!success) {
                emit ConsolidationFailed(FailureReason.OPERATION_FAILED, sourcePubkeys[i], targetPubkey);
                continue;
            }
        }
    }

    function batchSwitch(bytes[] calldata pubkeys) external payable onlySelf {
        uint256 batchSize = pubkeys.length;
        require(batchSize >= MIN_VALIDATORS, MinimumValidatorRequired());
        require(batchSize <= MAX_VALIDATORS, TooManyValidators());

        uint256 switchFee = getConsolidationFee();
        require(msg.value >= batchSize * switchFee, InsufficientFeePerValidator());

        for (uint256 i = 0; i < batchSize; ++i) {
            if (pubkeys[i].length != VALIDATOR_PUBKEY_LENGTH) {
                emit SwitchFailed(FailureReason.INVALID_PUBKEY_LENGTH, pubkeys[i]);
                continue;
            }

            bytes memory concatenated = abi.encodePacked(pubkeys[i], pubkeys[i]);
            (bool success,) = consolidationTarget.call{value: switchFee}(concatenated);
            if (!success) {
                emit SwitchFailed(FailureReason.OPERATION_FAILED, pubkeys[i]);
                continue;
            }
        }
    }

    // Define the ExitData struct
    struct ExitData {
        bytes pubkey; // 48-byte validator public key
        uint64 amount; // Amount in gwei (or zero for full exit)
        bool isFullExit; // Safety flag requiring explicit confirmation for full exits
    }

    function batchELExit(ExitData[] calldata data) external payable onlySelf {
        uint256 batchSize = data.length;
        require(batchSize >= MIN_VALIDATORS, MinimumValidatorRequired());
        require(batchSize <= MAX_VALIDATORS, TooManyValidators());

        uint256 exitFee = getExitFee();
        require(msg.value >= batchSize * exitFee, InsufficientFeePerValidator());

        for (uint256 i = 0; i < batchSize; ++i) {
            if (data[i].pubkey.length != VALIDATOR_PUBKEY_LENGTH) {
                emit ExecutionLayerExitFailed(FailureReason.INVALID_PUBKEY_LENGTH, data[i].pubkey, data[i].amount);
                continue;
            }

            bool isZeroAmount = data[i].amount == 0;

            if (data[i].isFullExit && !isZeroAmount) {
                emit ExecutionLayerExitFailed(FailureReason.FULL_EXIT_WITH_AMOUNT, data[i].pubkey, data[i].amount);
                continue;
            }

            if (isZeroAmount && !data[i].isFullExit) {
                emit ExecutionLayerExitFailed(FailureReason.FULL_EXIT_NOT_CONFIRMED, data[i].pubkey, data[i].amount);
                continue;
            }

            if (data[i].amount > MAX_WITHDRAWAL_AMOUNT) {
                emit ExecutionLayerExitFailed(FailureReason.AMOUNT_EXCEEDS_MAXIMUM, data[i].pubkey, data[i].amount);
                continue;
            }

            bytes memory concatenated = abi.encodePacked(data[i].pubkey, data[i].amount);
            (bool success,) = exitTarget.call{value: exitFee}(concatenated);
            if (!success) {
                emit ExecutionLayerExitFailed(FailureReason.OPERATION_FAILED, data[i].pubkey, data[i].amount);
                continue;
            }
        }
    }
}

lib/openzeppelin-contracts/contracts/token/ERC1155/IERC1155Receiver.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC1155/IERC1155Receiver.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../../utils/introspection/IERC165.sol";

/**
 * @dev Interface that must be implemented by smart contracts in order to receive
 * ERC-1155 token transfers.
 */
interface IERC1155Receiver is IERC165 {
    /**
     * @dev Handles the receipt of a single ERC-1155 token type. This function is
     * called at the end of a `safeTransferFrom` after the balance has been updated.
     *
     * NOTE: To accept the transfer, this must return
     * `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
     * (i.e. 0xf23a6e61, or its own function selector).
     *
     * @param operator The address which initiated the transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param id The ID of the token being transferred
     * @param value The amount of tokens being transferred
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
     */
    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) external returns (bytes4);

    /**
     * @dev Handles the receipt of a multiple ERC-1155 token types. This function
     * is called at the end of a `safeBatchTransferFrom` after the balances have
     * been updated.
     *
     * NOTE: To accept the transfer(s), this must return
     * `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
     * (i.e. 0xbc197c81, or its own function selector).
     *
     * @param operator The address which initiated the batch transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param ids An array containing ids of each token being transferred (order and length must match values array)
     * @param values An array containing amounts of each token being transferred (order and length must match ids array)
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
     */
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}

lib/openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.20;

/**
 * @title ERC-721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC-721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be
     * reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[ERC].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.