Skip to content
Verified contractExactBUSL-1.1

StakingContract

Address0x4526886fa76808485d620bb82fda11472e2bbb5d
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.13

 

Same code

3 more contracts

 

Details

Compiler
solc 0.8.13+commit.abaa5c0e
Language
solidity
Optimization
Yes, 200 runs
License
BUSL-1.1
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
17,237,503
Creation transaction
0x9efb70a99d…3a5aa97038
Main copy (most transactions)
0x4956639a…7b67a4f1

src/contracts/StakingContract.sol

//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.10;

import "./libs/UintLib.sol";
import "./libs/BytesLib.sol";
import "./interfaces/IFeeRecipient.sol";
import "./interfaces/IDepositContract.sol";
import "./libs/StakingContractStorageLib.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";

/// @title Ethereum Staking Contract
/// @author Kiln
/// @notice You can use this contract to store validator keys and have users fund them and trigger deposits.
contract StakingContract {
    using StakingContractStorageLib for bytes32;

    uint256 internal constant EXECUTION_LAYER_SALT_PREFIX = 0;
    uint256 internal constant CONSENSUS_LAYER_SALT_PREFIX = 1;
    uint256 public constant SIGNATURE_LENGTH = 96;
    uint256 public constant PUBLIC_KEY_LENGTH = 48;
    uint256 public constant DEPOSIT_SIZE = 32 ether;
    uint256 internal constant BASIS_POINTS = 10_000;

    error Forbidden();
    error InvalidFee();
    error Deactivated();
    error NoOperators();
    error InvalidCall();
    error Unauthorized();
    error DepositFailure();
    error InvalidArgument();
    error UnsortedIndexes();
    error InvalidPublicKeys();
    error InvalidSignatures();
    error AlreadyInitialized();
    error InvalidDepositValue();
    error NotEnoughValidators();
    error InvalidValidatorCount();
    error DuplicateValidatorKey(bytes);
    error FundedValidatorDeletionAttempt();
    error OperatorLimitTooHigh(uint256 limit, uint256 keyCount);
    error MaximumOperatorCountAlreadyReached();

    struct ValidatorAllocationCache {
        bool used;
        uint8 operatorIndex;
        uint32 funded;
        uint32 toDeposit;
        uint32 available;
    }

    event Deposit(address indexed caller, address indexed withdrawer, bytes publicKey, bytes signature);
    event ValidatorKeysAdded(uint256 indexed operatorIndex, bytes publicKeys, bytes signatures);
    event ValidatorKeyRemoved(uint256 indexed operatorIndex, bytes publicKey);
    event ChangedWithdrawer(bytes publicKey, address newWithdrawer);
    event ChangedOperatorLimit(uint256 operatorIndex, uint256 limit);
    event ChangedTreasury(address newTreasury);
    event ChangedGlobalFee(uint256 newGlobalFee);
    event ChangedOperatorFee(uint256 newOperatorFee);
    event ChangedAdmin(address newAdmin);
    event NewOperator(address operatorAddress, address feeRecipientAddress, uint256 index);
    event ChangedOperatorAddresses(uint256 operatorIndex, address operatorAddress, address feeRecipientAddress);
    event DeactivatedOperator(uint256 _operatorIndex);
    event ActivatedOperator(uint256 _operatorIndex);
    event SetWithdrawerCustomizationStatus(bool _status);

    /// @notice Ensures an initialisation call has been called only once per _version value
    /// @param _version The current initialisation value
    modifier init(uint256 _version) {
        if (_version != StakingContractStorageLib.getVersion() + 1) {
            revert AlreadyInitialized();
        }

        StakingContractStorageLib.setVersion(_version);
        _;
    }

    /// @notice Ensures that the caller is the admin
    modifier onlyAdmin() {
        if (msg.sender != StakingContractStorageLib.getAdmin()) {
            revert Unauthorized();
        }

        _;
    }

    /// @notice Ensures that the caller is the admin or the operator
    modifier onlyActiveOperatorOrAdmin(uint256 _operatorIndex) {
        if (msg.sender == StakingContractStorageLib.getAdmin()) {
            _;
        } else {
            _onlyActiveOperator(_operatorIndex);
            _;
        }
    }

    /// @notice Ensures that the caller is the admin
    modifier onlyActiveOperator(uint256 _operatorIndex) {
        _onlyActiveOperator(_operatorIndex);
        _;
    }

    /// @notice Ensures that the caller is the operator fee recipient
    modifier onlyOperatorFeeRecipient(uint256 _operatorIndex) {
        StakingContractStorageLib.OperatorInfo storage operatorInfo = StakingContractStorageLib.getOperators().value[
            _operatorIndex
        ];

        if (operatorInfo.deactivated) {
            revert Deactivated();
        }

        if (msg.sender != operatorInfo.feeRecipient) {
            revert Unauthorized();
        }

        _;
    }

    /// @notice Explicit deposit method using msg.sender
    /// @dev A multiple of 32 ETH should be sent
    function deposit() external payable {
        _deposit(msg.sender);
    }

    /// @notice Implicit deposit method
    /// @dev A multiple of 32 ETH should be sent
    /// @dev The withdrawer is set to the message sender address
    receive() external payable {
        _deposit(msg.sender);
    }

    /// @notice Fallback detection
    /// @dev Fails on any call that fallbacks
    fallback() external payable {
        revert InvalidCall();
    }

    function initialize_1(
        address _admin,
        address _treasury,
        address _depositContract,
        address _elDispatcher,
        address _clDispatcher,
        address _feeRecipientImplementation,
        uint256 _globalFee,
        uint256 _operatorFee
    ) external init(1) {
        StakingContractStorageLib.setAdmin(_admin);
        StakingContractStorageLib.setTreasury(_treasury);

        if (_globalFee > BASIS_POINTS) {
            revert InvalidFee();
        }
        StakingContractStorageLib.setGlobalFee(_globalFee);
        if (_operatorFee > BASIS_POINTS) {
            revert InvalidFee();
        }
        StakingContractStorageLib.setOperatorFee(_operatorFee);

        StakingContractStorageLib.setELDispatcher(_elDispatcher);
        StakingContractStorageLib.setCLDispatcher(_clDispatcher);
        StakingContractStorageLib.setDepositContract(_depositContract);
        StakingContractStorageLib.setFeeRecipientImplementation(_feeRecipientImplementation);
    }

    /// @notice Changes the behavior of the withdrawer customization logic
    /// @param _enabled True to allow users to customize the withdrawer
    function setWithdrawerCustomizationEnabled(bool _enabled) external onlyAdmin {
        StakingContractStorageLib.setWithdrawerCustomizationEnabled(_enabled);
        emit SetWithdrawerCustomizationStatus(_enabled);
    }

    /// @notice Retrieve system admin
    function getAdmin() external view returns (address) {
        return StakingContractStorageLib.getAdmin();
    }

    /// @notice Set new treasury
    /// @dev Only callable by admin
    /// @param _newTreasury New Treasury address
    function setTreasury(address _newTreasury) external onlyAdmin {
        emit ChangedTreasury(_newTreasury);
        StakingContractStorageLib.setTreasury(_newTreasury);
    }

    /// @notice Retrieve system treasury
    function getTreasury() external view returns (address) {
        return StakingContractStorageLib.getTreasury();
    }

    /// @notice Retrieve the global fee
    function getGlobalFee() external view returns (uint256) {
        return StakingContractStorageLib.getGlobalFee();
    }

    /// @notice Retrieve the operator fee
    function getOperatorFee() external view returns (uint256) {
        return StakingContractStorageLib.getOperatorFee();
    }

    /// @notice Compute the Execution Layer Fee recipient address for a given validator public key
    /// @param _publicKey Validator to get the recipient
    function getELFeeRecipient(bytes calldata _publicKey) external view returns (address) {
        return _getDeterministicReceiver(_publicKey, EXECUTION_LAYER_SALT_PREFIX);
    }

    /// @notice Compute the Consensus Layer Fee recipient address for a given validator public key
    /// @param _publicKey Validator to get the recipient
    function getCLFeeRecipient(bytes calldata _publicKey) external view returns (address) {
        return _getDeterministicReceiver(_publicKey, CONSENSUS_LAYER_SALT_PREFIX);
    }

    /// @notice Retrieve the Execution & Consensus Layer Fee operator recipient for a given public key
    function getOperatorFeeRecipient(bytes32 pubKeyRoot) external view returns (address) {
        return
            StakingContractStorageLib
                .getOperators()
                .value[StakingContractStorageLib.getOperatorIndexPerValidator().value[pubKeyRoot].operatorIndex]
                .feeRecipient;
    }

    /// @notice Retrieve withdrawer of public key
    /// @param _publicKey Public Key to check
    function getWithdrawer(bytes calldata _publicKey) external view returns (address) {
        return _getWithdrawer(_getPubKeyRoot(_publicKey));
    }

    /// @notice Retrieve withdrawer of public key root
    /// @param _publicKeyRoot Hash of the public key
    function getWithdrawerFromPublicKeyRoot(bytes32 _publicKeyRoot) external view returns (address) {
        return _getWithdrawer(_publicKeyRoot);
    }

    /// @notice Retrieve operator details
    /// @param _operatorIndex Operator index
    function getOperator(uint256 _operatorIndex)
        external
        view
        returns (
            address operatorAddress,
            address feeRecipientAddress,
            uint256 limit,
            uint256 keys,
            uint256 funded,
            uint256 available,
            bool deactivated
        )
    {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        if (_operatorIndex < operators.value.length) {
            StakingContractStorageLib.ValidatorsFundingInfo memory _operatorInfo = StakingContractStorageLib
                .getValidatorsFundingInfo(_operatorIndex);
            StakingContractStorageLib.OperatorInfo storage _operator = operators.value[_operatorIndex];

            (operatorAddress, feeRecipientAddress, limit, keys, deactivated) = (
                _operator.operator,
                _operator.feeRecipient,
                _operator.limit,
                _operator.publicKeys.length,
                _operator.deactivated
            );
            (funded, available) = (_operatorInfo.funded, _operatorInfo.availableKeys);
        }
    }

    /// @notice Get details about a validator
    /// @param _operatorIndex Index of the operator running the validator
    /// @param _validatorIndex Index of the validator
    function getValidator(uint256 _operatorIndex, uint256 _validatorIndex)
        external
        view
        returns (
            bytes memory publicKey,
            bytes memory signature,
            address withdrawer,
            bool funded
        )
    {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        publicKey = operators.value[_operatorIndex].publicKeys[_validatorIndex];
        signature = operators.value[_operatorIndex].signatures[_validatorIndex];
        withdrawer = _getWithdrawer(_getPubKeyRoot(publicKey));
        funded = _validatorIndex < StakingContractStorageLib.getValidatorsFundingInfo(_operatorIndex).funded;
    }

    /// @notice Get the total available keys that are ready to be used for deposits
    function getAvailableValidatorCount() external view returns (uint256) {
        return StakingContractStorageLib.getTotalAvailableValidators();
    }

    /// @notice Set new admin
    /// @dev Only callable by admin
    /// @param _newAdmin New Administrator address
    function transferOwnership(address _newAdmin) external onlyAdmin {
        StakingContractStorageLib.setPendingAdmin(_newAdmin);
    }

    /// @notice New admin must accept its role by calling this method
    /// @dev Only callable by new admin
    function acceptOwnership() external {
        address newAdmin = StakingContractStorageLib.getPendingAdmin();

        if (msg.sender != newAdmin) {
            revert Unauthorized();
        }
        StakingContractStorageLib.setAdmin(newAdmin);
        emit ChangedAdmin(newAdmin);
    }

    /// @notice Get the new admin's address previously set for an ownership transfer
    function getPendingAdmin() external view returns (address) {
        return StakingContractStorageLib.getPendingAdmin();
    }

    /// @notice Add new operator
    /// @dev Only callable by admin
    /// @param _operatorAddress Operator address allowed to add / remove validators
    /// @param _feeRecipientAddress Operator address used to manage rewards
    function addOperator(address _operatorAddress, address _feeRecipientAddress) external onlyAdmin returns (uint256) {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        StakingContractStorageLib.OperatorInfo memory newOperator;

        if (operators.value.length == 251) {
            revert MaximumOperatorCountAlreadyReached();
        }
        newOperator.operator = _operatorAddress;
        newOperator.feeRecipient = _feeRecipientAddress;
        operators.value.push(newOperator);
        uint256 operatorIndex = operators.value.length - 1;
        emit NewOperator(_operatorAddress, _feeRecipientAddress, operatorIndex);
        return operatorIndex;
    }

    /// @notice Set new operator addresses (operations and reward management)
    /// @dev Only callable by fee recipient address manager
    /// @param _operatorIndex Index of the operator to update
    /// @param _operatorAddress New operator address for operations management
    /// @param _feeRecipientAddress New operator address for reward management
    function setOperatorAddresses(
        uint256 _operatorIndex,
        address _operatorAddress,
        address _feeRecipientAddress
    ) external onlyOperatorFeeRecipient(_operatorIndex) {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();

        operators.value[_operatorIndex].operator = _operatorAddress;
        operators.value[_operatorIndex].feeRecipient = _feeRecipientAddress;
        emit ChangedOperatorAddresses(_operatorIndex, _operatorAddress, _feeRecipientAddress);
    }

    /// @notice Set withdrawer for public key
    /// @dev Only callable by current public key withdrawer
    /// @param _publicKey Public key to change withdrawer
    /// @param _newWithdrawer New withdrawer address
    function setWithdrawer(bytes calldata _publicKey, address _newWithdrawer) external {
        if (!StakingContractStorageLib.getWithdrawerCustomizationEnabled()) {
            revert Forbidden();
        }
        bytes32 pubkeyRoot = sha256(BytesLib.pad64(_publicKey));
        StakingContractStorageLib.WithdrawersSlot storage withdrawers = StakingContractStorageLib.getWithdrawers();

        if (withdrawers.value[pubkeyRoot] != msg.sender) {
            revert Unauthorized();
        }

        emit ChangedWithdrawer(_publicKey, _newWithdrawer);

        withdrawers.value[pubkeyRoot] = _newWithdrawer;
    }

    /// @notice Set operator staking limits
    /// @dev Only callable by admin
    /// @dev Limit should not exceed the validator key count of the operator
    /// @dev Keys should be registered before limit is increased
    /// @dev Allows all keys to be verified by the system admin before limit is increased
    /// @param _operatorIndex Operator Index
    /// @param _limit New staking limit
    function setOperatorLimit(uint256 _operatorIndex, uint256 _limit) external onlyAdmin {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        if (operators.value[_operatorIndex].deactivated) {
            revert Deactivated();
        }
        uint256 publicKeyCount = operators.value[_operatorIndex].publicKeys.length;
        if (publicKeyCount < _limit) {
            revert OperatorLimitTooHigh(_limit, publicKeyCount);
        }
        operators.value[_operatorIndex].limit = _limit;
        _updateAvailableValidatorCount(_operatorIndex);
        emit ChangedOperatorLimit(_operatorIndex, _limit);
    }

    /// @notice Deactivates an operator and changes the fee recipient address and the staking limit
    /// @param _operatorIndex Operator Index
    /// @param _temporaryFeeRecipient Temporary address to receive funds decided by the system admin
    function deactivateOperator(uint256 _operatorIndex, address _temporaryFeeRecipient) external onlyAdmin {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        operators.value[_operatorIndex].limit = 0;
        emit ChangedOperatorLimit(_operatorIndex, 0);
        operators.value[_operatorIndex].deactivated = true;
        emit DeactivatedOperator(_operatorIndex);
        operators.value[_operatorIndex].feeRecipient = _temporaryFeeRecipient;
        emit ChangedOperatorAddresses(_operatorIndex, operators.value[_operatorIndex].operator, _temporaryFeeRecipient);
        _updateAvailableValidatorCount(_operatorIndex);
    }

    /// @notice Activates an operator, without changing its 0 staking limit
    /// @param _operatorIndex Operator Index
    /// @param _newFeeRecipient Sets the fee recipient address
    function activateOperator(uint256 _operatorIndex, address _newFeeRecipient) external onlyAdmin {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        operators.value[_operatorIndex].deactivated = false;
        emit ActivatedOperator(_operatorIndex);
        operators.value[_operatorIndex].feeRecipient = _newFeeRecipient;
        emit ChangedOperatorAddresses(_operatorIndex, operators.value[_operatorIndex].operator, _newFeeRecipient);
    }

    /// @notice Change the Operator fee
    /// @param _operatorFee Fee in Basis Point
    function setOperatorFee(uint256 _operatorFee) external onlyAdmin {
        if (_operatorFee > BASIS_POINTS) {
            revert InvalidFee();
        }
        StakingContractStorageLib.setOperatorFee(_operatorFee);
        emit ChangedOperatorFee(_operatorFee);
    }

    /// @notice Change the Global fee
    /// @param _globalFee Fee in Basis Point
    function setGlobalFee(uint256 _globalFee) external onlyAdmin {
        if (_globalFee > BASIS_POINTS) {
            revert InvalidFee();
        }
        StakingContractStorageLib.setGlobalFee(_globalFee);
        emit ChangedGlobalFee(_globalFee);
    }

    /// @notice Add new validator public keys and signatures
    /// @dev Only callable by operator
    /// @param _operatorIndex Operator Index
    /// @param _keyCount Number of keys added
    /// @param _publicKeys Concatenated _keyCount public keys
    /// @param _signatures Concatenated _keyCount signatures
    function addValidators(
        uint256 _operatorIndex,
        uint256 _keyCount,
        bytes calldata _publicKeys,
        bytes calldata _signatures
    ) external onlyActiveOperator(_operatorIndex) {
        if (_keyCount == 0) {
            revert InvalidArgument();
        }

        if (_publicKeys.length % PUBLIC_KEY_LENGTH != 0 || _publicKeys.length / PUBLIC_KEY_LENGTH != _keyCount) {
            revert InvalidPublicKeys();
        }

        if (_signatures.length % SIGNATURE_LENGTH != 0 || _signatures.length / SIGNATURE_LENGTH != _keyCount) {
            revert InvalidSignatures();
        }

        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        StakingContractStorageLib.OperatorIndexPerValidatorSlot
            storage operatorIndexPerValidator = StakingContractStorageLib.getOperatorIndexPerValidator();

        for (uint256 i; i < _keyCount; ) {
            bytes memory publicKey = BytesLib.slice(_publicKeys, i * PUBLIC_KEY_LENGTH, PUBLIC_KEY_LENGTH);
            bytes memory signature = BytesLib.slice(_signatures, i * SIGNATURE_LENGTH, SIGNATURE_LENGTH);

            operators.value[_operatorIndex].publicKeys.push(publicKey);
            operators.value[_operatorIndex].signatures.push(signature);

            bytes32 pubKeyRoot = _getPubKeyRoot(publicKey);

            if (operatorIndexPerValidator.value[pubKeyRoot].enabled) {
                revert DuplicateValidatorKey(publicKey);
            }

            operatorIndexPerValidator.value[pubKeyRoot] = StakingContractStorageLib.OperatorIndex({
                enabled: true,
                operatorIndex: uint32(_operatorIndex)
            });

            unchecked {
                ++i;
            }
        }

        emit ValidatorKeysAdded(_operatorIndex, _publicKeys, _signatures);

        _updateAvailableValidatorCount(_operatorIndex);
    }

    /// @notice Remove unfunded validators
    /// @dev Only callable by operator
    /// @dev Indexes should be provided in decreasing order
    /// @dev The limit will be set to the lowest removed operator index to ensure all changes above the
    ///      lowest removed validator key are verified by the system administrator
    /// @param _operatorIndex Operator Index
    /// @param _indexes List of indexes to delete, in decreasing order
    function removeValidators(uint256 _operatorIndex, uint256[] calldata _indexes)
        external
        onlyActiveOperatorOrAdmin(_operatorIndex)
    {
        if (_indexes.length == 0) {
            revert InvalidArgument();
        }

        StakingContractStorageLib.ValidatorsFundingInfo memory operatorInfo = StakingContractStorageLib
            .getValidatorsFundingInfo(_operatorIndex);
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();

        if (_indexes[_indexes.length - 1] < operatorInfo.funded) {
            revert FundedValidatorDeletionAttempt();
        }
        for (uint256 i; i < _indexes.length; ) {
            if (i > 0 && _indexes[i] >= _indexes[i - 1]) {
                revert UnsortedIndexes();
            }

            emit ValidatorKeyRemoved(_operatorIndex, operators.value[_operatorIndex].publicKeys[_indexes[i]]);
            if (_indexes[i] == operators.value[_operatorIndex].publicKeys.length - 1) {
                operators.value[_operatorIndex].publicKeys.pop();
                operators.value[_operatorIndex].signatures.pop();
            } else {
                operators.value[_operatorIndex].publicKeys[_indexes[i]] = operators.value[_operatorIndex].publicKeys[
                    operators.value[_operatorIndex].publicKeys.length - 1
                ];
                operators.value[_operatorIndex].publicKeys.pop();
                operators.value[_operatorIndex].signatures[_indexes[i]] = operators.value[_operatorIndex].signatures[
                    operators.value[_operatorIndex].signatures.length - 1
                ];
                operators.value[_operatorIndex].signatures.pop();
            }

            unchecked {
                ++i;
            }
        }

        if (_indexes[_indexes.length - 1] < operators.value[_operatorIndex].limit) {
            operators.value[_operatorIndex].limit = _indexes[_indexes.length - 1];
            emit ChangedOperatorLimit(_operatorIndex, _indexes[_indexes.length - 1]);
        }

        _updateAvailableValidatorCount(_operatorIndex);
    }

    /// @notice Withdraw the Execution Layer Fee for a given validator public key
    /// @dev Funds are sent to the withdrawer account
    /// @dev This method is public on purpose
    /// @param _publicKey Validator to withdraw Execution Layer Fees from
    function withdrawELFee(bytes calldata _publicKey) external {
        _deployAndWithdraw(_publicKey, EXECUTION_LAYER_SALT_PREFIX, StakingContractStorageLib.getELDispatcher());
    }

    /// @notice Withdraw the Consensus Layer Fee for a given validator public key
    /// @dev Funds are sent to the withdrawer account
    /// @dev This method is public on purpose
    /// @param _publicKey Validator to withdraw Consensus Layer Fees from
    function withdrawCLFee(bytes calldata _publicKey) external {
        _deployAndWithdraw(_publicKey, CONSENSUS_LAYER_SALT_PREFIX, StakingContractStorageLib.getCLDispatcher());
    }

    /// @notice Withdraw both Consensus and Execution Layer Fee for a given validator public key
    /// @dev Reverts if any is null
    /// @param _publicKey Validator to withdraw Execution and Consensus Layer Fees from
    function withdraw(bytes calldata _publicKey) external {
        _deployAndWithdraw(_publicKey, EXECUTION_LAYER_SALT_PREFIX, StakingContractStorageLib.getELDispatcher());
        _deployAndWithdraw(_publicKey, CONSENSUS_LAYER_SALT_PREFIX, StakingContractStorageLib.getCLDispatcher());
    }

    /// ██ ███    ██ ████████ ███████ ██████  ███    ██  █████  ██
    /// ██ ████   ██    ██    ██      ██   ██ ████   ██ ██   ██ ██
    /// ██ ██ ██  ██    ██    █████   ██████  ██ ██  ██ ███████ ██
    /// ██ ██  ██ ██    ██    ██      ██   ██ ██  ██ ██ ██   ██ ██
    /// ██ ██   ████    ██    ███████ ██   ██ ██   ████ ██   ██ ███████

    function _onlyActiveOperator(uint256 _operatorIndex) internal view {
        StakingContractStorageLib.OperatorInfo storage operatorInfo = StakingContractStorageLib.getOperators().value[
            _operatorIndex
        ];

        if (operatorInfo.deactivated) {
            revert Deactivated();
        }

        if (msg.sender != operatorInfo.operator) {
            revert Unauthorized();
        }
    }

    function _getPubKeyRoot(bytes memory _publicKey) internal pure returns (bytes32) {
        return sha256(BytesLib.pad64(_publicKey));
    }

    function _getWithdrawer(bytes32 _publicKeyRoot) internal view returns (address) {
        return StakingContractStorageLib.getWithdrawers().value[_publicKeyRoot];
    }

    function _updateAvailableValidatorCount(uint256 _operatorIndex) internal {
        StakingContractStorageLib.ValidatorsFundingInfo memory validatorFundingInfo = StakingContractStorageLib
            .getValidatorsFundingInfo(_operatorIndex);
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();

        uint32 oldAvailableCount = validatorFundingInfo.availableKeys;
        uint32 newAvailableCount = 0;
        uint256 cap = _min(operators.value[_operatorIndex].limit, operators.value[_operatorIndex].publicKeys.length);

        if (cap <= validatorFundingInfo.funded) {
            StakingContractStorageLib.setValidatorsFundingInfo(_operatorIndex, 0, validatorFundingInfo.funded);
        } else {
            newAvailableCount = uint32(cap - validatorFundingInfo.funded);
            StakingContractStorageLib.setValidatorsFundingInfo(
                _operatorIndex,
                newAvailableCount,
                validatorFundingInfo.funded
            );
        }

        if (oldAvailableCount != newAvailableCount) {
            StakingContractStorageLib.setTotalAvailableValidators(
                (StakingContractStorageLib.getTotalAvailableValidators() - oldAvailableCount) + newAvailableCount
            );
        }
    }

    function _addressToWithdrawalCredentials(address _recipient) internal pure returns (bytes32) {
        return
            bytes32(uint256(uint160(_recipient)) + 0x0100000000000000000000000000000000000000000000000000000000000000);
    }

    function _depositValidatorsOfOperator(
        uint256 _operatorIndex,
        uint256 _validatorCount,
        address _withdrawer
    ) internal {
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        StakingContractStorageLib.OperatorInfo storage operator = operators.value[_operatorIndex];
        StakingContractStorageLib.ValidatorsFundingInfo memory vfi = StakingContractStorageLib.getValidatorsFundingInfo(
            _operatorIndex
        );

        for (uint256 i = vfi.funded; i < vfi.funded + _validatorCount; ) {
            bytes memory publicKey = operator.publicKeys[i];
            bytes memory signature = operator.signatures[i];
            address consensusLayerRecipient = _getDeterministicReceiver(publicKey, CONSENSUS_LAYER_SALT_PREFIX);
            bytes32 withdrawalCredentials = _addressToWithdrawalCredentials(consensusLayerRecipient);
            _depositValidator(publicKey, signature, withdrawalCredentials);
            bytes32 pubkeyRoot = _getPubKeyRoot(publicKey);
            StakingContractStorageLib.getWithdrawers().value[pubkeyRoot] = _withdrawer;
            emit Deposit(msg.sender, _withdrawer, publicKey, signature);
            unchecked {
                ++i;
            }
        }

        StakingContractStorageLib.setValidatorsFundingInfo(
            _operatorIndex,
            uint32(vfi.availableKeys - _validatorCount),
            uint32(vfi.funded + _validatorCount)
        );
    }

    /// @notice Internal utility to deposit a public key, its signature and 32 ETH to the consensus layer
    /// @param _publicKey The Public Key to deposit
    /// @param _signature The Signature to deposit
    /// @param _withdrawalCredentials The Withdrawal Credentials to deposit
    function _depositValidator(
        bytes memory _publicKey,
        bytes memory _signature,
        bytes32 _withdrawalCredentials
    ) internal {
        bytes32 pubkeyRoot = _getPubKeyRoot(_publicKey);
        bytes32 signatureRoot = sha256(
            abi.encodePacked(
                sha256(BytesLib.slice(_signature, 0, 64)),
                sha256(BytesLib.pad64(BytesLib.slice(_signature, 64, SIGNATURE_LENGTH - 64)))
            )
        );

        uint256 depositAmount = DEPOSIT_SIZE / 1000000000 wei;
        assert(depositAmount * 1000000000 wei == DEPOSIT_SIZE);

        bytes32 depositDataRoot = sha256(
            abi.encodePacked(
                sha256(abi.encodePacked(pubkeyRoot, _withdrawalCredentials)),
                sha256(abi.encodePacked(Uint256Lib.toLittleEndian64(depositAmount), signatureRoot))
            )
        );

        uint256 targetBalance = address(this).balance - DEPOSIT_SIZE;

        IDepositContract(StakingContractStorageLib.getDepositContract()).deposit{value: DEPOSIT_SIZE}(
            _publicKey,
            abi.encodePacked(_withdrawalCredentials),
            _signature,
            depositDataRoot
        );

        if (address(this).balance != targetBalance) {
            revert DepositFailure();
        }
    }

    function _depositOnOneOperator(
        address _withdrawer,
        uint256 _depositCount,
        uint256 _totalAvailableValidators
    ) internal {
        _depositValidatorsOfOperator(0, _depositCount, _withdrawer);
        StakingContractStorageLib.setTotalAvailableValidators(_totalAvailableValidators - _depositCount);
    }

    function _depositOnTwoOperators(
        address _withdrawer,
        uint256 _depositCount,
        uint256 _totalAvailableValidators
    ) internal {
        StakingContractStorageLib.ValidatorsFundingInfo memory oneOsi = StakingContractStorageLib
            .getValidatorsFundingInfo(0);
        StakingContractStorageLib.ValidatorsFundingInfo memory twoOsi = StakingContractStorageLib
            .getValidatorsFundingInfo(1);

        uint256 oneDepositCount;
        uint256 twoDepositCount;

        // using this tactic to prevent deposits of 1 validator to always go to operator 2
        if (block.number % 2 == 0) {
            oneDepositCount = _depositCount / 2;
            twoDepositCount = _depositCount - oneDepositCount;
        } else {
            twoDepositCount = _depositCount / 2;
            oneDepositCount = _depositCount - twoDepositCount;
        }

        if (oneDepositCount > oneOsi.availableKeys) {
            twoDepositCount = _depositCount - oneOsi.availableKeys;
            oneDepositCount = oneOsi.availableKeys;
        } else if (twoDepositCount > twoOsi.availableKeys) {
            oneDepositCount = _depositCount - twoOsi.availableKeys;
            twoDepositCount = twoOsi.availableKeys;
        }

        if (oneDepositCount > 0) {
            _depositValidatorsOfOperator(0, oneDepositCount, _withdrawer);
        }
        if (twoDepositCount > 0) {
            _depositValidatorsOfOperator(1, twoDepositCount, _withdrawer);
        }
        StakingContractStorageLib.setTotalAvailableValidators(
            _totalAvailableValidators - (oneDepositCount + twoDepositCount)
        );
    }

    function _getBaseSkip(
        bytes32 blockHash,
        uint256 index,
        uint8 prime
    ) internal pure returns (uint8 base, uint8 skip) {
        base = uint8(blockHash[(index * 2) % 32]) % prime;
        skip = (uint8(blockHash[((index * 2) + 1) % 32]) % (prime - 1)) + 1;
    }

    function _getOperatorFundedCount(uint8 operatorIndex, ValidatorAllocationCache[] memory vd)
        internal
        view
        returns (uint32)
    {
        if (operatorIndex >= vd.length) {
            return 0;
        }
        if (vd[operatorIndex].used == false) {
            StakingContractStorageLib.ValidatorsFundingInfo memory osi = StakingContractStorageLib
                .getValidatorsFundingInfo(operatorIndex);
            vd[operatorIndex].used = true;
            vd[operatorIndex].funded = osi.funded;
            vd[operatorIndex].available = osi.availableKeys;
        }
        return vd[operatorIndex].funded + vd[operatorIndex].toDeposit;
    }

    function _getOperatorAvailableCount(uint8 operatorIndex, ValidatorAllocationCache[] memory vd)
        internal
        view
        returns (uint32)
    {
        if (operatorIndex >= vd.length) {
            return 0;
        }
        if (vd[operatorIndex].used == false) {
            StakingContractStorageLib.ValidatorsFundingInfo memory osi = StakingContractStorageLib
                .getValidatorsFundingInfo(operatorIndex);
            vd[operatorIndex].used = true;
            vd[operatorIndex].funded = osi.funded;
            vd[operatorIndex].available = osi.availableKeys;
        }
        return vd[operatorIndex].available - vd[operatorIndex].toDeposit;
    }

    function _assignTemporaryDeposit(uint8 operatorIndex, ValidatorAllocationCache[] memory vd) internal pure {
        vd[operatorIndex].toDeposit += 1;
    }

    function _getBestOperator(
        uint8 alphaIndex,
        uint8 betaIndex,
        bytes32 blockHash,
        ValidatorAllocationCache[] memory vd
    ) internal view returns (uint8) {
        uint256 alphaFundedCount = _getOperatorFundedCount(alphaIndex, vd);
        uint256 betaFundedCount = _getOperatorFundedCount(betaIndex, vd);
        if (alphaFundedCount < betaFundedCount) {
            return alphaIndex;
        } else if (alphaFundedCount > betaFundedCount) {
            return betaIndex;
        } else {
            bool coinToss = (uint8(blockHash[(alphaIndex + betaIndex) % 32]) % 2) == 1;
            if (coinToss == false) {
                return betaIndex;
            } else {
                return alphaIndex;
            }
        }
    }

    function _getElligibleOperators(
        uint8 base,
        uint8 skip,
        uint8 prime,
        ValidatorAllocationCache[] memory vd
    ) internal view returns (uint8, uint8) {
        int16 alphaIndex = -1;
        int16 betaIndex = -1;
        uint8 index = base;
        while (alphaIndex == -1 || betaIndex == -1) {
            if (_getOperatorAvailableCount(index, vd) > 0) {
                if (alphaIndex == -1) {
                    alphaIndex = int8(index);
                } else {
                    betaIndex = int8(index);
                }
            }
            index = uint8((uint256(index) + skip) % prime);
            if (index == base && betaIndex == -1) {
                betaIndex = alphaIndex;
            }
        }
        return (uint8(int8(alphaIndex)), uint8(int8(betaIndex)));
    }

    function _depositOnThreeOrMoreOperators(
        address _withdrawer,
        uint256 _depositCount,
        uint256 _totalAvailableValidators,
        StakingContractStorageLib.OperatorsSlot storage _operators
    ) internal {
        uint256 operatorCount = _operators.value.length;
        uint8 optimusPrime = _getClosestPrimeAbove(uint8(operatorCount));
        bytes32 blockHash = blockhash(block.number - 1); // weak random number as it's not a security issue

        ValidatorAllocationCache[] memory vd = new ValidatorAllocationCache[](operatorCount);

        for (uint256 index; index < _depositCount; ) {
            // Retrieve base index and skip value based on block hash and current loop index
            (uint8 base, uint8 skip) = _getBaseSkip(blockHash, index, optimusPrime);
            // Retrieve two operator indexes pointing to two (or the same) operator(s) that have at least one available
            // validator key to be used for a deposit. This method takes into account possible pending deposits from
            // previous loop rounds.
            (uint8 alphaIndex, uint8 betaIndex) = _getElligibleOperators(base, skip, optimusPrime, vd);

            if (alphaIndex == betaIndex) {
                // Assign the deposit to the only operator having available keys
                _assignTemporaryDeposit(alphaIndex, vd);
            } else {
                // Assign the deposit to the operator having the lowest amount of funded keys
                _assignTemporaryDeposit(_getBestOperator(alphaIndex, betaIndex, blockHash, vd), vd);
            }

            unchecked {
                ++index;
            }
        }

        // Loop through the cached operator values and deposit any pending deposits
        for (uint256 index; index < vd.length; ) {
            if (vd[index].toDeposit > 0) {
                _depositValidatorsOfOperator(index, vd[index].toDeposit, _withdrawer);
            }
            unchecked {
                ++index;
            }
        }

        StakingContractStorageLib.setTotalAvailableValidators(_totalAvailableValidators - _depositCount);
    }

    function _deposit(address _withdrawer) internal {
        if (msg.value == 0 || msg.value % DEPOSIT_SIZE != 0) {
            revert InvalidDepositValue();
        }
        uint256 totalAvailableValidators = StakingContractStorageLib.getTotalAvailableValidators();
        uint256 depositCount = msg.value / DEPOSIT_SIZE;
        if (depositCount > totalAvailableValidators) {
            revert NotEnoughValidators();
        }
        StakingContractStorageLib.OperatorsSlot storage operators = StakingContractStorageLib.getOperators();
        if (operators.value.length == 0) {
            revert NoOperators();
        } else if (operators.value.length == 1) {
            _depositOnOneOperator(_withdrawer, depositCount, totalAvailableValidators);
        } else if (operators.value.length == 2) {
            _depositOnTwoOperators(_withdrawer, depositCount, totalAvailableValidators);
        } else {
            _depositOnThreeOrMoreOperators(_withdrawer, depositCount, totalAvailableValidators, operators);
        }
    }

    function _primes() internal pure returns (uint8[54] memory primes) {
        primes = [
            2,
            3,
            5,
            7,
            11,
            13,
            17,
            19,
            23,
            29,
            31,
            37,
            41,
            43,
            47,
            53,
            59,
            61,
            67,
            71,
            73,
            79,
            83,
            89,
            97,
            101,
            103,
            107,
            109,
            113,
            127,
            131,
            137,
            139,
            149,
            151,
            157,
            163,
            167,
            173,
            179,
            181,
            191,
            193,
            197,
            199,
            211,
            223,
            227,
            229,
            233,
            239,
            241,
            251
        ];
    }

    function _getClosestPrimeAbove(uint8 _count) internal pure returns (uint8) {
        uint8[54] memory primes = _primes();
        for (uint256 i; i < primes.length; ) {
            if (primes[i] >= _count) {
                return primes[i];
            }
            unchecked {
                ++i;
            }
        }
        revert InvalidValidatorCount();
    }

    function _min(uint256 _a, uint256 _b) internal pure returns (uint256) {
        if (_a < _b) {
            return _a;
        }
        return _b;
    }

    /// @notice Internal utility to compute the receiver deterministic address
    /// @param _publicKey Public Key assigned to the receiver
    /// @param _prefix Prefix used to generate multiple receivers per public key
    function _getDeterministicReceiver(bytes memory _publicKey, uint256 _prefix) internal view returns (address) {
        bytes32 publicKeyRoot = _getPubKeyRoot(_publicKey);
        bytes32 salt = sha256(abi.encodePacked(_prefix, publicKeyRoot));
        address implementation = StakingContractStorageLib.getFeeRecipientImplementation();
        return Clones.predictDeterministicAddress(implementation, salt);
    }

    /// @notice Internal utility to deploy and withdraw the fees from a receiver
    /// @param _publicKey Public Key assigned to the receiver
    /// @param _prefix Prefix used to generate multiple receivers per public key
    /// @param _dispatcher Address of the dispatcher contract
    function _deployAndWithdraw(
        bytes calldata _publicKey,
        uint256 _prefix,
        address _dispatcher
    ) internal {
        bytes32 publicKeyRoot = _getPubKeyRoot(_publicKey);
        bytes32 feeRecipientSalt = sha256(abi.encodePacked(_prefix, publicKeyRoot));
        address implementation = StakingContractStorageLib.getFeeRecipientImplementation();
        address feeRecipientAddress = Clones.predictDeterministicAddress(implementation, feeRecipientSalt);
        if (feeRecipientAddress.code.length == 0) {
            Clones.cloneDeterministic(implementation, feeRecipientSalt);
            IFeeRecipient(feeRecipientAddress).init(_dispatcher, publicKeyRoot);
        }
        IFeeRecipient(feeRecipientAddress).withdraw();
    }
}

@openzeppelin/contracts/proxy/Clones.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (proxy/Clones.sol)

pragma solidity ^0.8.0;

/**
 * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
 * deploying minimal proxy contracts, also known as "clones".
 *
 * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
 * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
 *
 * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
 * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
 * deterministic method.
 *
 * _Available since v3.4._
 */
library Clones {
    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create opcode, which should never revert.
     */
    function clone(address implementation) internal returns (address instance) {
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
            instance := create(0, ptr, 0x37)
        }
        require(instance != address(0), "ERC1167: create failed");
    }

    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create2 opcode and a `salt` to deterministically deploy
     * the clone. Using the same `implementation` and `salt` multiple time will revert, since
     * the clones cannot be deployed twice at the same address.
     */
    function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
            instance := create2(0, ptr, 0x37, salt)
        }
        require(instance != address(0), "ERC1167: create2 failed");
    }

    /**
     * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
     */
    function predictDeterministicAddress(
        address implementation,
        bytes32 salt,
        address deployer
    ) internal pure returns (address predicted) {
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
            mstore(add(ptr, 0x38), shl(0x60, deployer))
            mstore(add(ptr, 0x4c), salt)
            mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
            predicted := keccak256(add(ptr, 0x37), 0x55)
        }
    }

    /**
     * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
     */
    function predictDeterministicAddress(address implementation, bytes32 salt)
        internal
        view
        returns (address predicted)
    {
        return predictDeterministicAddress(implementation, salt, address(this));
    }
}

src/contracts/interfaces/IDepositContract.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

interface IDepositContract {
    function deposit(
        bytes calldata pubkey,
        bytes calldata withdrawalCredentials,
        bytes calldata signature,
        bytes32 depositDataRoot
    ) external payable;
}

src/contracts/interfaces/IFeeRecipient.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

interface IFeeRecipient {
    function init(address _dispatcher, bytes32 _publicKeyRoot) external;

    function withdraw() external;
}

src/contracts/libs/BytesLib.sol

//SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

library BytesLib {
    function pad64(bytes memory _b) internal pure returns (bytes memory) {
        assert(_b.length >= 32 && _b.length <= 64);
        if (64 == _b.length) {
            return _b;
        }

        bytes memory zero32 = new bytes(32);
        assembly {
            mstore(add(zero32, 0x20), 0)
        }

        if (32 == _b.length) {
            return BytesLib.concat(_b, zero32);
        } else {
            return BytesLib.concat(_b, BytesLib.slice(zero32, 0, uint256(64) - _b.length));
        }
    }

    function concat(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bytes memory) {
        bytes memory tempBytes;

        assembly {
            // Get a location of some free memory and store it in tempBytes as
            // Solidity does for memory variables.
            tempBytes := mload(0x40)

            // Store the length of the first bytes array at the beginning of
            // the memory for tempBytes.
            let length := mload(_preBytes)
            mstore(tempBytes, length)

            // Maintain a memory counter for the current write location in the
            // temp bytes array by adding the 32 bytes for the array length to
            // the starting location.
            let mc := add(tempBytes, 0x20)
            // Stop copying when the memory counter reaches the length of the
            // first bytes array.
            let end := add(mc, length)

            for {
                // Initialize a copy counter to the start of the _preBytes data,
                // 32 bytes into its memory.
                let cc := add(_preBytes, 0x20)
            } lt(mc, end) {
                // Increase both counters by 32 bytes each iteration.
                mc := add(mc, 0x20)
                cc := add(cc, 0x20)
            } {
                // Write the _preBytes data into the tempBytes memory 32 bytes
                // at a time.
                mstore(mc, mload(cc))
            }

            // Add the length of _postBytes to the current length of tempBytes
            // and store it as the new length in the first 32 bytes of the
            // tempBytes memory.
            length := mload(_postBytes)
            mstore(tempBytes, add(length, mload(tempBytes)))

            // Move the memory counter back from a multiple of 0x20 to the
            // actual end of the _preBytes data.
            mc := end
            // Stop copying when the memory counter reaches the new combined
            // length of the arrays.
            end := add(mc, length)

            for {
                let cc := add(_postBytes, 0x20)
            } lt(mc, end) {
                mc := add(mc, 0x20)
                cc := add(cc, 0x20)
            } {
                mstore(mc, mload(cc))
            }

            // Update the free-memory pointer by padding our last write location
            // to 32 bytes: add 31 bytes to the end of tempBytes to move to the
            // next 32 byte block, then round down to the nearest multiple of
            // 32. If the sum of the length of the two arrays is zero then add
            // one before rounding down to leave a blank 32 bytes (the length block with 0).
            mstore(
                0x40,
                and(
                    add(add(end, iszero(add(length, mload(_preBytes)))), 31),
                    not(31) // Round down to the nearest 32 bytes.
                )
            )
        }

        return tempBytes;
    }

    function slice(
        bytes memory _bytes,
        uint256 _start,
        uint256 _length
    ) internal pure returns (bytes memory) {
        require(_length + 31 >= _length, "slice_overflow");
        require(_bytes.length >= _start + _length, "slice_outOfBounds");

        bytes memory tempBytes;

        assembly {
            switch iszero(_length)
            case 0 {
                // Get a location of some free memory and store it in tempBytes as
                // Solidity does for memory variables.
                tempBytes := mload(0x40)

                // The first word of the slice result is potentially a partial
                // word read from the original array. To read it, we calculate
                // the length of that partial word and start copying that many
                // bytes into the array. The first word we copy will start with
                // data we don't care about, but the last `lengthmod` bytes will
                // land at the beginning of the contents of the new array. When
                // we're done copying, we overwrite the full first word with
                // the actual length of the slice.
                let lengthmod := and(_length, 31)

                // The multiplication in the next line is necessary
                // because when slicing multiples of 32 bytes (lengthmod == 0)
                // the following copy loop was copying the origin's length
                // and then ending prematurely not copying everything it should.
                let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
                let end := add(mc, _length)

                for {
                    // The multiplication in the next line has the same exact purpose
                    // as the one above.
                    let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
                } lt(mc, end) {
                    mc := add(mc, 0x20)
                    cc := add(cc, 0x20)
                } {
                    mstore(mc, mload(cc))
                }

                mstore(tempBytes, _length)

                //update free-memory pointer
                //allocating the array padded to 32 bytes like the compiler does now
                mstore(0x40, and(add(mc, 31), not(31)))
            }
            //if we want a zero-length slice let's just return a zero-length array
            default {
                tempBytes := mload(0x40)
                //zero out the 32 bytes slice we are about to return
                //we need to do it because Solidity does not garbage collect
                mstore(tempBytes, 0)

                mstore(0x40, add(tempBytes, 0x20))
            }
        }

        return tempBytes;
    }
}

src/contracts/libs/StakingContractStorageLib.sol

//SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

library StakingContractStorageLib {
    function getUint256(bytes32 position) internal view returns (uint256 data) {
        assembly {
            data := sload(position)
        }
    }

    function setUint256(bytes32 position, uint256 data) internal {
        assembly {
            sstore(position, data)
        }
    }

    function getAddress(bytes32 position) internal view returns (address data) {
        assembly {
            data := sload(position)
        }
    }

    function setAddress(bytes32 position, address data) internal {
        assembly {
            sstore(position, data)
        }
    }

    function getBool(bytes32 position) internal view returns (bool data) {
        assembly {
            data := sload(position)
        }
    }

    function setBool(bytes32 position, bool data) internal {
        assembly {
            sstore(position, data)
        }
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant VERSION_SLOT = keccak256("StakingContract.version");

    function getVersion() internal view returns (uint256) {
        return getUint256(VERSION_SLOT);
    }

    function setVersion(uint256 _newVersion) internal {
        setUint256(VERSION_SLOT, _newVersion);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant ADMIN_SLOT = keccak256("StakingContract.admin");
    bytes32 internal constant PENDING_ADMIN_SLOT = keccak256("StakingContract.pendingAdmin");

    function getAdmin() internal view returns (address) {
        return getAddress(ADMIN_SLOT);
    }

    function setAdmin(address _newAdmin) internal {
        setAddress(ADMIN_SLOT, _newAdmin);
    }

    function getPendingAdmin() internal view returns (address) {
        return getAddress(PENDING_ADMIN_SLOT);
    }

    function setPendingAdmin(address _newPendingAdmin) internal {
        setAddress(PENDING_ADMIN_SLOT, _newPendingAdmin);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant TREASURY_SLOT = keccak256("StakingContract.treasury");

    function getTreasury() internal view returns (address) {
        return getAddress(TREASURY_SLOT);
    }

    function setTreasury(address _newTreasury) internal {
        setAddress(TREASURY_SLOT, _newTreasury);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant DEPOSIT_CONTRACT_SLOT = keccak256("StakingContract.depositContract");

    function getDepositContract() internal view returns (address) {
        return getAddress(DEPOSIT_CONTRACT_SLOT);
    }

    function setDepositContract(address _newDepositContract) internal {
        setAddress(DEPOSIT_CONTRACT_SLOT, _newDepositContract);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant OPERATORS_SLOT = keccak256("StakingContract.operators");

    struct OperatorInfo {
        address operator;
        address feeRecipient;
        uint256 limit;
        bytes[] publicKeys;
        bytes[] signatures;
        bool deactivated;
    }

    struct OperatorsSlot {
        OperatorInfo[] value;
    }

    function getOperators() internal pure returns (OperatorsSlot storage p) {
        bytes32 slot = OPERATORS_SLOT;
        assembly {
            p.slot := slot
        }
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant VALIDATORS_FUNDING_INFO_SLOT = keccak256("StakingContract.validatorsFundingInfo");

    struct ValidatorsFundingInfo {
        uint32 availableKeys;
        uint32 funded;
    }

    struct UintToUintMappingSlot {
        mapping(uint256 => uint256) value;
    }

    function getValidatorsFundingInfo(uint256 _index) internal view returns (ValidatorsFundingInfo memory vfi) {
        UintToUintMappingSlot storage p;
        bytes32 slot = VALIDATORS_FUNDING_INFO_SLOT;

        assembly {
            p.slot := slot
        }

        uint256 slotIndex = _index >> 2;
        uint256 innerIndex = (_index & 3) << 6;
        uint256 value = p.value[slotIndex] >> innerIndex;
        vfi.availableKeys = uint32(value);
        vfi.funded = uint32(value >> 32);
    }

    function setValidatorsFundingInfo(
        uint256 _index,
        uint32 _availableKeys,
        uint32 _funded
    ) internal {
        UintToUintMappingSlot storage p;
        bytes32 slot = VALIDATORS_FUNDING_INFO_SLOT;

        assembly {
            p.slot := slot
        }

        uint256 slotIndex = _index >> 2;
        uint256 innerIndex = (_index & 3) << 6;
        p.value[slotIndex] =
            (p.value[slotIndex] & (~(uint256(0xFFFFFFFFFFFFFFFF) << innerIndex))) |
            ((uint256(_availableKeys) | (uint256(_funded) << 32)) << innerIndex);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant TOTAL_AVAILABLE_VALIDATORS_SLOT = keccak256("StakingContract.totalAvailableValidators");

    function getTotalAvailableValidators() internal view returns (uint256) {
        return getUint256(TOTAL_AVAILABLE_VALIDATORS_SLOT);
    }

    function setTotalAvailableValidators(uint256 _newTotal) internal {
        setUint256(TOTAL_AVAILABLE_VALIDATORS_SLOT, _newTotal);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant WITHDRAWERS_SLOT = keccak256("StakingContract.withdrawers");

    struct WithdrawersSlot {
        mapping(bytes32 => address) value;
    }

    function getWithdrawers() internal pure returns (WithdrawersSlot storage p) {
        bytes32 slot = WITHDRAWERS_SLOT;
        assembly {
            p.slot := slot
        }
    }

    /* ========================================
    ===========================================
    =========================================*/

    struct OperatorIndex {
        bool enabled;
        uint32 operatorIndex;
    }

    struct OperatorIndexPerValidatorSlot {
        mapping(bytes32 => OperatorIndex) value;
    }

    bytes32 internal constant OPERATOR_INDEX_PER_VALIDATOR_SLOT =
        keccak256("StakingContract.operatorIndexPerValidator");

    function getOperatorIndexPerValidator() internal pure returns (OperatorIndexPerValidatorSlot storage p) {
        bytes32 slot = OPERATOR_INDEX_PER_VALIDATOR_SLOT;
        assembly {
            p.slot := slot
        }
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant GLOBAL_FEE_SLOT = keccak256("StakingContract.globalFee");

    function getGlobalFee() internal view returns (uint256) {
        return getUint256(GLOBAL_FEE_SLOT);
    }

    function setGlobalFee(uint256 _newTreasuryFee) internal {
        setUint256(GLOBAL_FEE_SLOT, _newTreasuryFee);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant OPERATOR_FEE_SLOT = keccak256("StakingContract.operatorFee");

    function getOperatorFee() internal view returns (uint256) {
        return getUint256(OPERATOR_FEE_SLOT);
    }

    function setOperatorFee(uint256 _newOperatorFee) internal {
        setUint256(OPERATOR_FEE_SLOT, _newOperatorFee);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant EL_DISPATCHER_SLOT = keccak256("StakingContract.executionLayerDispatcher");

    function getELDispatcher() internal view returns (address) {
        return getAddress(EL_DISPATCHER_SLOT);
    }

    function setELDispatcher(address _newElDispatcher) internal {
        setAddress(EL_DISPATCHER_SLOT, _newElDispatcher);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant CL_DISPATCHER_SLOT = keccak256("StakingContract.consensusLayerDispatcher");

    function getCLDispatcher() internal view returns (address) {
        return getAddress(CL_DISPATCHER_SLOT);
    }

    function setCLDispatcher(address _newClDispatcher) internal {
        setAddress(CL_DISPATCHER_SLOT, _newClDispatcher);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant FEE_RECIPIENT_IMPLEMENTATION_SLOT =
        keccak256("StakingContract.feeRecipientImplementation");

    function getFeeRecipientImplementation() internal view returns (address) {
        return getAddress(FEE_RECIPIENT_IMPLEMENTATION_SLOT);
    }

    function setFeeRecipientImplementation(address _newFeeRecipientImplementation) internal {
        setAddress(FEE_RECIPIENT_IMPLEMENTATION_SLOT, _newFeeRecipientImplementation);
    }

    /* ========================================
    ===========================================
    =========================================*/

    bytes32 internal constant WITHDRAWER_CUSTOMIZATION_ENABLED_SLOT =
        keccak256("StakingContract.withdrawerCustomizationEnabled");

    function getWithdrawerCustomizationEnabled() internal view returns (bool) {
        return getBool(WITHDRAWER_CUSTOMIZATION_ENABLED_SLOT);
    }

    function setWithdrawerCustomizationEnabled(bool _enabled) internal {
        setBool(WITHDRAWER_CUSTOMIZATION_ENABLED_SLOT, _enabled);
    }
}

src/contracts/libs/UintLib.sol

//SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

library Uint256Lib {
    function toLittleEndian64(uint256 _value) internal pure returns (uint256 result) {
        result = 0;
        uint256 temp_value = _value;
        for (uint256 i = 0; i < 8; ++i) {
            result = (result << 8) | (temp_value & 0xFF);
            temp_value >>= 8;
        }

        assert(0 == temp_value); // fully converted
        result <<= (24 * 8);
    }
}
Compiler settingsShow
{
  "metadata": {
    "useLiteralContent": true
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.