POL Price: $0.127206 (-3.52%)
 

Overview

POL Balance

Polygon PoS Chain LogoPolygon PoS Chain LogoPolygon PoS Chain Logo0 POL

POL Value

$0.00

Token Holdings

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To
Increase Bid708098332025-04-27 8:01:01272 days ago1745740861IN
0xcC0CfC5C...573EADEb5
0 POL0.0010418130.00000022
Increase Bid708098062025-04-27 8:00:03272 days ago1745740803IN
0xcC0CfC5C...573EADEb5
0 POL0.0010418130.00000027
Increase Bid708098052025-04-27 8:00:01272 days ago1745740801IN
0xcC0CfC5C...573EADEb5
0 POL0.0010418130.00000028
Increase Bid708098052025-04-27 8:00:01272 days ago1745740801IN
0xcC0CfC5C...573EADEb5
0 POL0.0010418130.00000028
Increase Bid708098042025-04-27 7:59:59272 days ago1745740799IN
0xcC0CfC5C...573EADEb5
0 POL0.0044507430.00000029
Increase Bid708098032025-04-27 7:59:57272 days ago1745740797IN
0xcC0CfC5C...573EADEb5
0 POL0.0025064130.00000028
Increase Bid708098032025-04-27 7:59:57272 days ago1745740797IN
0xcC0CfC5C...573EADEb5
0 POL0.074173500
Increase Bid708098022025-04-27 7:59:55272 days ago1745740795IN
0xcC0CfC5C...573EADEb5
0 POL0.0044503830.00000028
Increase Bid708098022025-04-27 7:59:55272 days ago1745740795IN
0xcC0CfC5C...573EADEb5
0 POL0.0025064130.00000028
Increase Bid708098022025-04-27 7:59:55272 days ago1745740795IN
0xcC0CfC5C...573EADEb5
0 POL0.0025060530.00000028
Increase Bid708098022025-04-27 7:59:55272 days ago1745740795IN
0xcC0CfC5C...573EADEb5
0 POL0.0025381530.37999996
Increase Bid708098012025-04-27 7:59:53272 days ago1745740793IN
0xcC0CfC5C...573EADEb5
0 POL0.0044507430.00000029
Increase Bid708098012025-04-27 7:59:53272 days ago1745740793IN
0xcC0CfC5C...573EADEb5
0 POL0.0027566533.00000029
Increase Bid708097982025-04-27 7:59:47272 days ago1745740787IN
0xcC0CfC5C...573EADEb5
0 POL0.0044507430.0000003
Increase Bid708097982025-04-27 7:59:47272 days ago1745740787IN
0xcC0CfC5C...573EADEb5
0 POL0.0025064130.0000003
Increase Bid708097972025-04-27 7:59:45272 days ago1745740785IN
0xcC0CfC5C...573EADEb5
0 POL0.0026114731.2620003
Increase Bid708097962025-04-27 7:59:43272 days ago1745740783IN
0xcC0CfC5C...573EADEb5
0 POL0.0025064130.0000003
Increase Bid708097962025-04-27 7:59:43272 days ago1745740783IN
0xcC0CfC5C...573EADEb5
0 POL0.0029241435.0000003
Increase Bid708097962025-04-27 7:59:43272 days ago1745740783IN
0xcC0CfC5C...573EADEb5
0 POL0.0037615345.02300026
Increase Bid708097952025-04-27 7:59:41272 days ago1745740781IN
0xcC0CfC5C...573EADEb5
0 POL0.0044503830.00000029
Increase Bid708097942025-04-27 7:59:39272 days ago1745740779IN
0xcC0CfC5C...573EADEb5
0 POL0.0025060530
Increase Bid708097942025-04-27 7:59:39272 days ago1745740779IN
0xcC0CfC5C...573EADEb5
0 POL0.0044503830.00000029
Increase Bid708097942025-04-27 7:59:39272 days ago1745740779IN
0xcC0CfC5C...573EADEb5
0 POL0.0029237235.00000029
Increase Bid708097932025-04-27 7:59:37272 days ago1745740777IN
0xcC0CfC5C...573EADEb5
0 POL0.0044503830.00000028
Increase Bid708097932025-04-27 7:59:37272 days ago1745740777IN
0xcC0CfC5C...573EADEb5
0 POL0.0044500230.00000028
View all transactions

Parent Transaction Hash Block From To
View All Internal Transactions
Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
MagicAlchemyMarathon

Compiler Version
v0.8.28+commit.7893614a

Optimization Enabled:
Yes with 200 runs

Other Settings:
cancun EvmVersion
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Packing} from "openzeppelin-contracts/contracts/utils/Packing.sol";
import {EnumerableMap} from "openzeppelin-contracts/contracts/utils/structs/EnumerableMap.sol";
import {Math} from "openzeppelin-contracts/contracts/utils/math/Math.sol";
import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol";
import {ReentrancyGuardTransient} from "openzeppelin-contracts/contracts/utils/ReentrancyGuardTransient.sol";

import {IMagicAlchemyMarathon} from "./interfaces/IMagicAlchemyMarathon.sol";
import {ITetherToken} from "./interfaces/ITetherToken.sol";

import {MagicAlchemyRarity as Rarity} from "./utils/MagicAlchemyRarity.sol";

/// @title MagicAlchemyMarathon
/// @notice A contract that manages a bidding marathon with rounds, bid increments, and rarity-based rewards.
/// @dev Inherits from Ownable, ReentrancyGuardTransient and implements IMagicAlchemyMarathon.
contract MagicAlchemyMarathon is Ownable, ReentrancyGuardTransient, IMagicAlchemyMarathon {
    string public constant ENVIRONMENT = "prod";

    using Math for uint256;
    using EnumerableMap for EnumerableMap.AddressToBytes32Map;

    /// @notice Structure holding a player's address, their bid and a nonce -- unique sequential number of the bid.
    struct PlayerWithBid {
        address player;
        uint256 bid;
        uint256 nonce;
    }

    /// @notice Structure holding round schedule information.
    /// @param round The round number.
    /// @param start The starting timestamp of the round.
    /// @param end The ending timestamp of the round.
    struct RoundSchedule {
        uint256 round;
        uint256 start;
        uint256 end;
    }

    /* -------------------------------------------------------------------------- */
    /*                                   EVENTS                                   */
    /* -------------------------------------------------------------------------- */

    /// @notice Emitted when a player increases their bid.
    /// @param player The address of the player.
    /// @param round The current round.
    /// @param bidNonce The nonce associated with the bid.
    /// @param increment The bid increment amount.
    /// @param totalBid The total bid amount after increment.
    event BidIncreased(
        address indexed player, uint256 indexed round, uint256 indexed bidNonce, uint256 increment, uint256 totalBid
    );

    /// @notice Emitted when the maximum round is changed.
    /// @param oldMaxRound The previous maximum round.
    /// @param newMaxRound The new maximum round.
    event MaxRoundChanged(uint256 indexed oldMaxRound, uint256 indexed newMaxRound);

    /// @notice Emitted when the maximum round is locked.
    /// @param maxRound The maximum round value that has been locked.
    event MaxRoundLocked(uint256 indexed maxRound);

    /// @notice Emitted when the minimum bid increment is changed.
    /// @param oldMinBidIncrement The previous minimum bid increment.
    /// @param newMinBidIncrement The new minimum bid increment.
    event MinBidIncrementChanged(uint256 indexed oldMinBidIncrement, uint256 indexed newMinBidIncrement);

    /// @notice Emitted when the minimum bid increment is locked.
    /// @param minBidIncrement The minimum bid increment value that is now locked.
    event MinBidIncrementLocked(uint256 indexed minBidIncrement);

    /// @notice Emitted when the current round schedule is rescheduled.
    /// @param round The round number being rescheduled.
    /// @param start The new start timestamp.
    /// @param end The new end timestamp.
    event CurrentRoundRescheduled(uint256 indexed round, uint256 indexed start, uint256 indexed end);

    /// @notice Emitted when funds are withdrawn.
    /// @param to The recipient address.
    /// @param amount The amount withdrawn.
    event Withdrawn(address indexed to, uint256 indexed amount);

    /* -------------------------------------------------------------------------- */
    /*                                  ERRORS                                    */
    /* -------------------------------------------------------------------------- */

    /// @notice Error when the quota numerator exceeds the denominator.
    /// @param quotaNumerator_ The quota numerator provided.
    error QuotaNumeratorExceedsDenominator(uint256 quotaNumerator_);

    /// @notice Error when the provided round time range is invalid: start >= end.
    error BadRoundTimeRange();

    /// @notice Error when the total bid is below the required minimum.
    /// @param minBid The minimum total bid required.
    error TotalBidTooLow(uint256 minBid);

    /// @notice Error when the bid increment is too low.
    /// @param minBid The minimum bid increment required.
    error BidIncrementTooLow(uint256 minBid);

    /// @notice Error when attempting to interact with the marathon after it has ended.
    error MarathonOver();

    /// @notice Error when trying to change maxRound after it has been locked.
    /// @param maxRound The current maxRound value.
    error MaxRoundIsLocked(uint256 maxRound);

    /// @notice Error when trying to change minBidIncrement after it has been locked.
    /// @param maxRound The current minBidIncrement value.
    error MinBidIncrementIsLocked(uint256 maxRound);

    /// @notice Error when the new maxRound is less than the current round.
    /// @param currentRound The current round number.
    error MaxRoundLessThanCurrent(uint256 currentRound);

    /// @notice Error when the minimum bid is zero.
    error MinBidIsZero();

    /// @notice Error when the round duration is zero.
    error RoundDurationIsZero();

    /// @notice Error when trying to operate on a round that has not finished.
    /// @param endAt The ending timestamp of the round.
    error RoundNotFinished(uint256 endAt);

    /// @notice Error when the round has not yet started.
    error RoundNotStarted();

    /// @notice Error when the provided round number is out of bounds.
    error RoundOutOfBounds();

    /// @notice Error when scheduling a round in the past.
    /// @param currentTimestamp The current block timestamp.
    error ScheduleInThePast(uint256 currentTimestamp);

    /// @notice Error when an unknown rarity is provided.
    error UnknownRarity();

    /* -------------------------------------------------------------------------- */
    /*                                CONSTANTS                                   */
    /* -------------------------------------------------------------------------- */

    /// @notice The denominator for rarity quota calculation.
    uint256 public constant ROUND_QUOTA_DENOMINATOR = 10000;

    /* -------------------------------------------------------------------------- */
    /*                             STATE VARIABLES                                */
    /* -------------------------------------------------------------------------- */

    ITetherToken private _tether;
    RoundSchedule private _referenceSchedule;
    bool private _maxRoundIsLocked;
    bool private _minBidIncrementIsLocked;
    uint256 private _lastBidNonce;
    uint256 private _minBidIncrement;
    uint256 private _minTotalBid;
    uint256 private _maxRound;
    uint256 private _roundDuration;
    uint256 private _roundLegendaryQuota;
    uint256 private _roundEpicQuota;
    uint256 private _roundEpicQuotaNumerator;
    uint256 private _roundRareQuota;
    uint256 private _roundRareQuotaNumerator;

    /// @notice Mapping of round numbers to packed bid data (bid and nonce) for players.
    mapping(uint256 round => EnumerableMap.AddressToBytes32Map) private _packedBids;

    /* -------------------------------------------------------------------------- */
    /*                                 CONSTRUCTOR                                */
    /* -------------------------------------------------------------------------- */

    /// @notice Initializes the contract with initial configuration parameters.
    /// @param minTotalBid_ The minimum total bid required.
    /// @param minBidIncrement_ The minimum increment for each bid increase.
    /// @param maxRound_ The maximum round number.
    /// @param firstRoundStart_ The start timestamp for the first round.
    /// @param firstRoundEnd_ The end timestamp for the first round.
    /// @param roundDuration_ The duration for each subsequent round.
    /// @param roundLegendaryQuota_ The absolute quota for legendary rarity.
    /// @param roundEpicQuota_ The absolute quota for epic rarity.
    /// @param roundEpicQuotaNumerator_ The numerator of the fraction representing epic rarity relative quota from total flasks.
    /// @param roundRareQuota_ The absolute quota for rare rarity.
    /// @param roundRareQuotaNumerator_ The numerator of the fraction representing rare rarity relative quota from total flasks.
    /// @param tether_ The address of the Tether token contract.
    /// @param initialOwner_ The initial owner of the contract.
    constructor(
        uint256 minTotalBid_,
        uint256 minBidIncrement_,
        uint256 maxRound_,
        uint256 firstRoundStart_,
        uint256 firstRoundEnd_,
        uint256 roundDuration_,
        uint256 roundLegendaryQuota_,
        uint256 roundEpicQuota_,
        uint256 roundEpicQuotaNumerator_,
        uint256 roundRareQuota_,
        uint256 roundRareQuotaNumerator_,
        ITetherToken tether_,
        address initialOwner_
    ) Ownable(initialOwner_) {
        _lastBidNonce = 0;
        _maxRoundIsLocked = false;
        _minBidIncrementIsLocked = false;

        _setMinBidIncrement(minBidIncrement_);

        if (minTotalBid_ == 0) {
            revert MinBidIsZero();
        }
        _minTotalBid = minTotalBid_;

        _setReferenceSchedule(1, firstRoundStart_, firstRoundEnd_);

        // Set the maximum round to the highest possible value to ensure all checks pass within _setMaxRound.
        _maxRound = type(uint256).max;

        _setMaxRound(maxRound_);
        _setRoundDuration(roundDuration_);

        _roundLegendaryQuota = roundLegendaryQuota_;
        _roundEpicQuota = roundEpicQuota_;
        _roundRareQuota = roundRareQuota_;

        if (roundEpicQuotaNumerator_ > ROUND_QUOTA_DENOMINATOR) {
            revert QuotaNumeratorExceedsDenominator(roundEpicQuotaNumerator_);
        }
        _roundEpicQuotaNumerator = roundEpicQuotaNumerator_;

        if (roundRareQuotaNumerator_ > ROUND_QUOTA_DENOMINATOR) {
            revert QuotaNumeratorExceedsDenominator(roundRareQuotaNumerator_);
        }
        _roundRareQuotaNumerator = roundRareQuotaNumerator_;

        _tether = tether_;
    }

    /* -------------------------------------------------------------------------- */
    /*                              VIEW FUNCTIONS                                */
    /* -------------------------------------------------------------------------- */

    /// @notice Returns the tether token instance used in the contract.
    /// @return The Tether Token instance.
    function tether() public view returns (ITetherToken) {
        return _tether;
    }

    /// @notice Returns the minimum total bid required for participation.
    /// @return The minimum total bid value.
    function minTotalBid() public view returns (uint256) {
        return _minTotalBid;
    }

    /// @notice Returns the minimum bid increment required.
    /// @return The minimum bid increment value.
    function minBidIncrement() public view returns (uint256) {
        return _minBidIncrement;
    }

    /// @notice Indicates whether the maximum round has been locked.
    /// @return True if locked, false otherwise.
    function maxRoundIsLocked() public view returns (bool) {
        return _maxRoundIsLocked;
    }

    /// @notice Returns the maximum round number allowed.
    /// @return The maximum round value.
    function maxRound() public view returns (uint256) {
        return _maxRound;
    }

    /// @notice Returns the current round number.
    /// @dev Reverts if there is no active round: either the marathon or the round has not started, or the marathon has ended.
    /// @return The current round number.
    function currentRound() public view returns (uint256) {
        if (block.timestamp < _referenceSchedule.start) {
            revert RoundNotStarted();
        }

        uint256 round_ = _currentRound();
        if (round_ > _maxRound) {
            revert MarathonOver();
        }

        return round_;
    }

    /// @notice Returns the schedule for the current or upcoming round.
    /// @dev Reverts if the marathon has ended.
    /// @return The RoundSchedule struct for the current/upcoming round.
    function currentOrUpcomingRoundSchedule() public view returns (RoundSchedule memory) {
        RoundSchedule memory roundSchedule_ = _currentOrUpcomingRoundSchedule();
        if (roundSchedule_.round > _maxRound) {
            revert MarathonOver();
        }
        return roundSchedule_;
    }

    /// @notice Calculates the final count for flasks based on rarity for a finished round.
    /// @dev Reverts if the round has not finished.
    /// @param round_ The round number.
    /// @param rarity_ The rarity level.
    /// @return The final count corresponding to the given round and rarity.
    function flaskFinalCountFor(uint256 round_, Rarity rarity_)
        external
        view
        onlyFinishedRound(round_)
        returns (uint256)
    {
        uint256 bidCount_ = _packedBids[round_].length();
        uint256 remainingCount_ = bidCount_;

        // Calculates legendary flask count
        uint256 rarityCount_ = _roundLegendaryQuota.min(bidCount_);

        if (rarity_ == Rarity.Legendary) {
            return rarityCount_;
        }

        remainingCount_ -= rarityCount_;

        // Calculates epic flask count
        rarityCount_ = _roundEpicQuota.max(bidCount_ * _roundEpicQuotaNumerator / ROUND_QUOTA_DENOMINATOR);
        if (rarityCount_ > remainingCount_) {
            rarityCount_ = remainingCount_;
        }

        if (rarity_ == Rarity.Epic) {
            return rarityCount_;
        }

        remainingCount_ -= rarityCount_;

        // Calculates rare flask count
        rarityCount_ = _roundRareQuota.max(bidCount_ * _roundRareQuotaNumerator / ROUND_QUOTA_DENOMINATOR);
        if (rarityCount_ > remainingCount_) {
            rarityCount_ = remainingCount_;
        }

        if (rarity_ == Rarity.Rare) {
            return rarityCount_;
        }

        if (rarity_ == Rarity.Common) {
            return remainingCount_ - rarityCount_;
        }

        revert UnknownRarity();
    }

    /// @notice Returns the number of bids placed in a given round, in other words, the number of players participating in the round.
    /// @param round_ The round number.
    /// @return The bid count.
    function bidCount(uint256 round_) public view returns (uint256) {
        return _packedBids[round_].length();
    }

    /// @notice Retrieves a player's bid details by index for a given round.
    /// @dev Requirements: index must be strictly less than the round's bid count.
    /// @param round_ The round number.
    /// @param index_ The index of the bid.
    /// @return A PlayerWithBid struct containing the player's address, bid, and nonce.
    function playerWithBidByIndex(uint256 round_, uint256 index_) public view returns (PlayerWithBid memory) {
        (address player_, bytes32 packedBid_) = _packedBids[round_].at(index_);
        (uint256 bid_, uint256 nonce_) = _unpackBidAndNonce(packedBid_);

        return PlayerWithBid(player_, bid_, nonce_);
    }

    /// @notice Retrieves a list of player bids starting from a given index.
    /// @dev Requirements: index must be strictly less than the round's bid count.
    /// @param round_ The round number.
    /// @param index_ The starting index.
    /// @param count_ The number of bids to retrieve.
    /// @return An array of PlayerWithBid structs.
    function playersWithBidFromIndex(uint256 round_, uint256 index_, uint256 count_)
        public
        view
        returns (PlayerWithBid[] memory)
    {
        PlayerWithBid[] memory playersWithBids_ = new PlayerWithBid[](count_);
        for (uint256 i = 0; i < count_; i++) {
            (address player_, bytes32 packedBid_) = _packedBids[round_].at(index_ + i);
            (uint256 bid_, uint256 nonce_) = _unpackBidAndNonce(packedBid_);
            playersWithBids_[i] = PlayerWithBid(player_, bid_, nonce_);
        }
        return playersWithBids_;
    }

    /// @notice Returns the bid and nonce for a specific player in a given round.
    /// @param player_ The player's address.
    /// @param round_ The round number.
    /// @return A tuple containing the bid amount and the bid nonce.
    function bidOf(address player_, uint256 round_) public view returns (uint256, uint256) {
        (bool exists_, bytes32 packedBid_) = _packedBids[round_].tryGet(player_);
        return exists_ ? _unpackBidAndNonce(packedBid_) : (0, 0);
    }

    /// @notice Checks if the contract supports a given interface. Only IMagicAlchemyMarathon is supported.
    /// @param interfaceId The interface identifier.
    /// @return True if the interface is supported, false otherwise.
    function supportsInterface(bytes4 interfaceId) public pure returns (bool) {
        return interfaceId == type(IMagicAlchemyMarathon).interfaceId;
    }

    /* -------------------------------------------------------------------------- */
    /*                           STATE CHANGING FUNCTIONS                         */
    /* -------------------------------------------------------------------------- */

    /// @notice Increases the bid of the caller by a specified increment.
    /// @dev Requires the bid increment to be at least the minimum bid increment and the first bid of the round to be above the minimum total bid.
    /// @param bidIncrement_ The amount to increase the bid by.
    /// @return A tuple containing the new total bid and the bid nonce.
    function increaseBid(uint256 bidIncrement_) public nonReentrant returns (uint256, uint256) {
        if (bidIncrement_ < _minBidIncrement) {
            revert BidIncrementTooLow(_minBidIncrement);
        }

        uint256 round_ = currentRound();
        address player_ = _msgSender();

        EnumerableMap.AddressToBytes32Map storage packedBids_ = _packedBids[round_];

        (bool exists_, bytes32 packedBid_) = packedBids_.tryGet(player_);
        uint256 totalBid_ = exists_ ? _unpackBid(packedBid_) + bidIncrement_ : bidIncrement_;

        if (totalBid_ < _minTotalBid) {
            revert TotalBidTooLow(_minTotalBid);
        }

        _tether.transferFrom(player_, address(this), bidIncrement_);

        _lastBidNonce++;
        packedBids_.set(player_, _packBidAndNonce(totalBid_, _lastBidNonce));

        emit BidIncreased(player_, round_, _lastBidNonce, bidIncrement_, totalBid_);

        return (totalBid_, _lastBidNonce);
    }

    /* -------------------------------------------------------------------------- */
    /*                         OWNER-RESTRICTED FUNCTIONS                         */
    /* -------------------------------------------------------------------------- */

    /// @notice Updates the minimum bid increment.
    /// @dev Only callable by the owner. Will revert if the minimum bid increment is locked.
    /// @param minBidIncrement_ The new minimum bid increment.
    function setMinBidIncrement(uint256 minBidIncrement_) public onlyOwner {
        _setMinBidIncrement(minBidIncrement_);
    }

    /// @notice Locks the minimum bid increment, preventing further changes.
    /// @dev Only callable by the owner.
    function lockMinBidIncrement() public onlyOwner {
        _minBidIncrementIsLocked = true;

        emit MinBidIncrementLocked(_minBidIncrement);
    }

    /// @notice Sets a new maximum round value.
    /// @dev Only callable by the owner and will revert if the new max round is less than the current round or if locked.
    /// @param maxRound_ The new maximum round value.
    function setMaxRound(uint256 maxRound_) public onlyOwner {
        _setMaxRound(maxRound_);
    }

    /// @notice Locks the maximum round, preventing further changes.
    /// @dev Only callable by the owner.
    function lockMaxRound() public onlyOwner {
        _maxRoundIsLocked = true;

        emit MaxRoundLocked(_maxRound);
    }

    /// @notice Reschedules the current or upcoming round.
    /// @dev Only callable by the owner. The new schedule must be in the future and the start time must be before the end time.
    /// @param start_ The new start timestamp.
    /// @param end_ The new end timestamp.
    function rescheduleCurrentOrUpcomingRound(uint256 start_, uint256 end_) public onlyOwner {
        if (block.timestamp > start_) {
            revert ScheduleInThePast(block.timestamp);
        }

        uint256 round_ = _currentRound();
        if (round_ > _maxRound) {
            revert MarathonOver();
        }

        _setReferenceSchedule(round_, start_, end_);
    }

    /// @notice Withdraws a specified amount of tether tokens to a given address.
    /// @dev Only callable by the owner.
    /// @param amount The amount to withdraw.
    /// @param to The recipient address.
    function withdraw(uint256 amount, address to) public nonReentrant onlyOwner {
        _tether.transfer(to, amount);

        emit Withdrawn(to, amount);
    }

    /* -------------------------------------------------------------------------- */
    /*                              INTERNAL HELPERS                              */
    /* -------------------------------------------------------------------------- */

    /// @notice Packs a bid and its nonce into a single bytes32 value.
    /// @param bid_ The bid amount.
    /// @param nonce_ The bid nonce.
    /// @return The packed bid and nonce.
    function _packBidAndNonce(uint256 bid_, uint256 nonce_) private pure returns (bytes32) {
        return Packing.pack_24_8(bytes24(uint192(bid_)), bytes8(uint64(nonce_)));
    }

    /// @notice Unpacks the bid amount from a packed bid.
    /// @param _packedBid The packed bid.
    /// @return The bid amount.
    function _unpackBid(bytes32 _packedBid) private pure returns (uint256) {
        return uint192(Packing.extract_32_24(_packedBid, 0));
    }

    /// @notice Unpacks both the bid and nonce from a packed bid.
    /// @param packedBid_ The packed bid.
    /// @return A tuple containing the bid amount and the bid nonce.
    function _unpackBidAndNonce(bytes32 packedBid_) private pure returns (uint256, uint256) {
        uint256 bid_ = _unpackBid(packedBid_);
        uint256 nonce_ = uint64(Packing.extract_32_8(packedBid_, 24));
        return (bid_, nonce_);
    }

    /// @notice Sets a new minimum bid increment.
    /// @dev Reverts if the minimum bid increment is locked or if the new value is zero.
    /// @param newMinBidIncrement_ The new minimum bid increment.
    function _setMinBidIncrement(uint256 newMinBidIncrement_) private {
        if (_minBidIncrementIsLocked) {
            revert MinBidIncrementIsLocked(_minBidIncrement);
        }

        if (newMinBidIncrement_ == 0) {
            revert MinBidIsZero();
        }

        uint256 oldMinBidIncrement_ = _minBidIncrement;
        _minBidIncrement = newMinBidIncrement_;

        emit MinBidIncrementChanged(oldMinBidIncrement_, newMinBidIncrement_);
    }

    /// @notice Sets a new maximum round value.
    /// @dev Reverts if the maximum round is locked, if the current round exceeds the new value, or if the new value is less than the current round.
    /// @param newMaxRound_ The new maximum round.
    function _setMaxRound(uint256 newMaxRound_) private {
        if (_maxRoundIsLocked) {
            revert MaxRoundIsLocked(_maxRound);
        }

        uint256 round_ = _currentRound();

        if (round_ > _maxRound) {
            revert MarathonOver();
        }

        if (newMaxRound_ < round_) {
            revert MaxRoundLessThanCurrent(round_);
        }

        uint256 oldMaxRound_ = _maxRound;
        _maxRound = newMaxRound_;

        emit MaxRoundChanged(oldMaxRound_, newMaxRound_);
    }

    /// @notice Returns the current round number based on the reference schedule.
    /// @return The current round number.
    function _currentRound() private view returns (uint256) {
        return block.timestamp < _referenceSchedule.end ? _referenceSchedule.round : _currentRoundAfterReference();
    }

    /// @notice Returns the schedule for the active or upcoming round.
    /// @return A RoundSchedule struct for the active or upcoming round.
    function _currentOrUpcomingRoundSchedule() private view returns (RoundSchedule memory) {
        if (block.timestamp < _referenceSchedule.end) {
            return _referenceSchedule;
        } else {
            uint256 round_ = _currentRoundAfterReference();
            uint256 start_ = block.timestamp - (block.timestamp - _referenceSchedule.end) % _roundDuration;
            uint256 end_ = start_ + _roundDuration;

            return RoundSchedule(round_, start_, end_);
        }
    }

    /// @notice Computes the round number after the end of reference schedule.
    /// @return The computed round number.
    function _currentRoundAfterReference() private view returns (uint256) {
        return _referenceSchedule.round + (block.timestamp - _referenceSchedule.end) / _roundDuration + 1;
    }

    /// @notice Sets the duration for each round.
    /// @dev Reverts if the provided duration is zero.
    /// @param roundDuration_ The duration of a round in seconds.
    function _setRoundDuration(uint256 roundDuration_) private {
        if (roundDuration_ == 0) {
            revert RoundDurationIsZero();
        }
        _roundDuration = roundDuration_;
    }

    /// @notice Sets the reference schedule for rounds.
    /// @dev Reverts if the start time is not before the end time.
    /// @param round_ The round number for the schedule.
    /// @param start_ The start timestamp.
    /// @param end_ The end timestamp.
    function _setReferenceSchedule(uint256 round_, uint256 start_, uint256 end_) private {
        if (start_ >= end_) {
            revert BadRoundTimeRange();
        }

        _referenceSchedule = RoundSchedule(round_, start_, end_);

        emit CurrentRoundRescheduled(round_, start_, end_);
    }

    /* -------------------------------------------------------------------------- */
    /*                                  MODIFIERS                                 */
    /* -------------------------------------------------------------------------- */

    /// @notice Modifier to ensure that the round is finished before executing a function.
    /// @param round_ The round number to check.
    modifier onlyFinishedRound(uint256 round_) {
        if (round_ == 0 || round_ > _maxRound) {
            revert RoundOutOfBounds();
        }

        RoundSchedule memory currentSchedule_ = _currentOrUpcomingRoundSchedule();
        if (round_ > currentSchedule_.round) {
            revert RoundNotStarted();
        }

        if (round_ == currentSchedule_.round) {
            if (block.timestamp <= currentSchedule_.start) {
                revert RoundNotStarted();
            }

            if (block.timestamp <= currentSchedule_.end) {
                revert RoundNotFinished(currentSchedule_.end);
            }
        }

        _;
    }
}

File 2 of 16 : Packing.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (utils/Packing.sol)
// This file was procedurally generated from scripts/generate/templates/Packing.js.

pragma solidity ^0.8.20;

/**
 * @dev Helper library packing and unpacking multiple values into bytesXX.
 *
 * Example usage:
 *
 * ```solidity
 * library MyPacker {
 *     type MyType is bytes32;
 *
 *     function _pack(address account, bytes4 selector, uint64 period) external pure returns (MyType) {
 *         bytes12 subpack = Packing.pack_4_8(selector, bytes8(period));
 *         bytes32 pack = Packing.pack_20_12(bytes20(account), subpack);
 *         return MyType.wrap(pack);
 *     }
 *
 *     function _unpack(MyType self) external pure returns (address, bytes4, uint64) {
 *         bytes32 pack = MyType.unwrap(self);
 *         return (
 *             address(Packing.extract_32_20(pack, 0)),
 *             Packing.extract_32_4(pack, 20),
 *             uint64(Packing.extract_32_8(pack, 24))
 *         );
 *     }
 * }
 * ```
 *
 * _Available since v5.1._
 */
// solhint-disable func-name-mixedcase
library Packing {
    error OutOfRangeAccess();

    function pack_1_1(bytes1 left, bytes1 right) internal pure returns (bytes2 result) {
        assembly ("memory-safe") {
            left := and(left, shl(248, not(0)))
            right := and(right, shl(248, not(0)))
            result := or(left, shr(8, right))
        }
    }

    function pack_2_2(bytes2 left, bytes2 right) internal pure returns (bytes4 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_4(bytes2 left, bytes4 right) internal pure returns (bytes6 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_6(bytes2 left, bytes6 right) internal pure returns (bytes8 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_8(bytes2 left, bytes8 right) internal pure returns (bytes10 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_10(bytes2 left, bytes10 right) internal pure returns (bytes12 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(176, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_20(bytes2 left, bytes20 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(96, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_2_22(bytes2 left, bytes22 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(240, not(0)))
            right := and(right, shl(80, not(0)))
            result := or(left, shr(16, right))
        }
    }

    function pack_4_2(bytes4 left, bytes2 right) internal pure returns (bytes6 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_4(bytes4 left, bytes4 right) internal pure returns (bytes8 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_6(bytes4 left, bytes6 right) internal pure returns (bytes10 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_8(bytes4 left, bytes8 right) internal pure returns (bytes12 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_12(bytes4 left, bytes12 right) internal pure returns (bytes16 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_16(bytes4 left, bytes16 right) internal pure returns (bytes20 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(128, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_20(bytes4 left, bytes20 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(96, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_24(bytes4 left, bytes24 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(64, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_4_28(bytes4 left, bytes28 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(224, not(0)))
            right := and(right, shl(32, not(0)))
            result := or(left, shr(32, right))
        }
    }

    function pack_6_2(bytes6 left, bytes2 right) internal pure returns (bytes8 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_6_4(bytes6 left, bytes4 right) internal pure returns (bytes10 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_6_6(bytes6 left, bytes6 right) internal pure returns (bytes12 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_6_10(bytes6 left, bytes10 right) internal pure returns (bytes16 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(176, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_6_16(bytes6 left, bytes16 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(128, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_6_22(bytes6 left, bytes22 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(208, not(0)))
            right := and(right, shl(80, not(0)))
            result := or(left, shr(48, right))
        }
    }

    function pack_8_2(bytes8 left, bytes2 right) internal pure returns (bytes10 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_4(bytes8 left, bytes4 right) internal pure returns (bytes12 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_8(bytes8 left, bytes8 right) internal pure returns (bytes16 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_12(bytes8 left, bytes12 right) internal pure returns (bytes20 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_16(bytes8 left, bytes16 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(128, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_20(bytes8 left, bytes20 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(96, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_8_24(bytes8 left, bytes24 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(192, not(0)))
            right := and(right, shl(64, not(0)))
            result := or(left, shr(64, right))
        }
    }

    function pack_10_2(bytes10 left, bytes2 right) internal pure returns (bytes12 result) {
        assembly ("memory-safe") {
            left := and(left, shl(176, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(80, right))
        }
    }

    function pack_10_6(bytes10 left, bytes6 right) internal pure returns (bytes16 result) {
        assembly ("memory-safe") {
            left := and(left, shl(176, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(80, right))
        }
    }

    function pack_10_10(bytes10 left, bytes10 right) internal pure returns (bytes20 result) {
        assembly ("memory-safe") {
            left := and(left, shl(176, not(0)))
            right := and(right, shl(176, not(0)))
            result := or(left, shr(80, right))
        }
    }

    function pack_10_12(bytes10 left, bytes12 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(176, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(80, right))
        }
    }

    function pack_10_22(bytes10 left, bytes22 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(176, not(0)))
            right := and(right, shl(80, not(0)))
            result := or(left, shr(80, right))
        }
    }

    function pack_12_4(bytes12 left, bytes4 right) internal pure returns (bytes16 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_12_8(bytes12 left, bytes8 right) internal pure returns (bytes20 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_12_10(bytes12 left, bytes10 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(176, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_12_12(bytes12 left, bytes12 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_12_16(bytes12 left, bytes16 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(128, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_12_20(bytes12 left, bytes20 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(160, not(0)))
            right := and(right, shl(96, not(0)))
            result := or(left, shr(96, right))
        }
    }

    function pack_16_4(bytes16 left, bytes4 right) internal pure returns (bytes20 result) {
        assembly ("memory-safe") {
            left := and(left, shl(128, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(128, right))
        }
    }

    function pack_16_6(bytes16 left, bytes6 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(128, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(128, right))
        }
    }

    function pack_16_8(bytes16 left, bytes8 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(128, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(128, right))
        }
    }

    function pack_16_12(bytes16 left, bytes12 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(128, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(128, right))
        }
    }

    function pack_16_16(bytes16 left, bytes16 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(128, not(0)))
            right := and(right, shl(128, not(0)))
            result := or(left, shr(128, right))
        }
    }

    function pack_20_2(bytes20 left, bytes2 right) internal pure returns (bytes22 result) {
        assembly ("memory-safe") {
            left := and(left, shl(96, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(160, right))
        }
    }

    function pack_20_4(bytes20 left, bytes4 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(96, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(160, right))
        }
    }

    function pack_20_8(bytes20 left, bytes8 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(96, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(160, right))
        }
    }

    function pack_20_12(bytes20 left, bytes12 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(96, not(0)))
            right := and(right, shl(160, not(0)))
            result := or(left, shr(160, right))
        }
    }

    function pack_22_2(bytes22 left, bytes2 right) internal pure returns (bytes24 result) {
        assembly ("memory-safe") {
            left := and(left, shl(80, not(0)))
            right := and(right, shl(240, not(0)))
            result := or(left, shr(176, right))
        }
    }

    function pack_22_6(bytes22 left, bytes6 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(80, not(0)))
            right := and(right, shl(208, not(0)))
            result := or(left, shr(176, right))
        }
    }

    function pack_22_10(bytes22 left, bytes10 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(80, not(0)))
            right := and(right, shl(176, not(0)))
            result := or(left, shr(176, right))
        }
    }

    function pack_24_4(bytes24 left, bytes4 right) internal pure returns (bytes28 result) {
        assembly ("memory-safe") {
            left := and(left, shl(64, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(192, right))
        }
    }

    function pack_24_8(bytes24 left, bytes8 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(64, not(0)))
            right := and(right, shl(192, not(0)))
            result := or(left, shr(192, right))
        }
    }

    function pack_28_4(bytes28 left, bytes4 right) internal pure returns (bytes32 result) {
        assembly ("memory-safe") {
            left := and(left, shl(32, not(0)))
            right := and(right, shl(224, not(0)))
            result := or(left, shr(224, right))
        }
    }

    function extract_2_1(bytes2 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 1) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_2_1(bytes2 self, bytes1 value, uint8 offset) internal pure returns (bytes2 result) {
        bytes1 oldValue = extract_2_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_4_1(bytes4 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 3) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_4_1(bytes4 self, bytes1 value, uint8 offset) internal pure returns (bytes4 result) {
        bytes1 oldValue = extract_4_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_4_2(bytes4 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_4_2(bytes4 self, bytes2 value, uint8 offset) internal pure returns (bytes4 result) {
        bytes2 oldValue = extract_4_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_6_1(bytes6 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 5) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_6_1(bytes6 self, bytes1 value, uint8 offset) internal pure returns (bytes6 result) {
        bytes1 oldValue = extract_6_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_6_2(bytes6 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_6_2(bytes6 self, bytes2 value, uint8 offset) internal pure returns (bytes6 result) {
        bytes2 oldValue = extract_6_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_6_4(bytes6 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_6_4(bytes6 self, bytes4 value, uint8 offset) internal pure returns (bytes6 result) {
        bytes4 oldValue = extract_6_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_8_1(bytes8 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 7) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_8_1(bytes8 self, bytes1 value, uint8 offset) internal pure returns (bytes8 result) {
        bytes1 oldValue = extract_8_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_8_2(bytes8 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_8_2(bytes8 self, bytes2 value, uint8 offset) internal pure returns (bytes8 result) {
        bytes2 oldValue = extract_8_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_8_4(bytes8 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_8_4(bytes8 self, bytes4 value, uint8 offset) internal pure returns (bytes8 result) {
        bytes4 oldValue = extract_8_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_8_6(bytes8 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_8_6(bytes8 self, bytes6 value, uint8 offset) internal pure returns (bytes8 result) {
        bytes6 oldValue = extract_8_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_10_1(bytes10 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 9) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_10_1(bytes10 self, bytes1 value, uint8 offset) internal pure returns (bytes10 result) {
        bytes1 oldValue = extract_10_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_10_2(bytes10 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_10_2(bytes10 self, bytes2 value, uint8 offset) internal pure returns (bytes10 result) {
        bytes2 oldValue = extract_10_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_10_4(bytes10 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_10_4(bytes10 self, bytes4 value, uint8 offset) internal pure returns (bytes10 result) {
        bytes4 oldValue = extract_10_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_10_6(bytes10 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_10_6(bytes10 self, bytes6 value, uint8 offset) internal pure returns (bytes10 result) {
        bytes6 oldValue = extract_10_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_10_8(bytes10 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_10_8(bytes10 self, bytes8 value, uint8 offset) internal pure returns (bytes10 result) {
        bytes8 oldValue = extract_10_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_1(bytes12 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 11) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_12_1(bytes12 self, bytes1 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes1 oldValue = extract_12_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_2(bytes12 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 10) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_12_2(bytes12 self, bytes2 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes2 oldValue = extract_12_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_4(bytes12 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_12_4(bytes12 self, bytes4 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes4 oldValue = extract_12_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_6(bytes12 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_12_6(bytes12 self, bytes6 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes6 oldValue = extract_12_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_8(bytes12 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_12_8(bytes12 self, bytes8 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes8 oldValue = extract_12_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_12_10(bytes12 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_12_10(bytes12 self, bytes10 value, uint8 offset) internal pure returns (bytes12 result) {
        bytes10 oldValue = extract_12_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_1(bytes16 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 15) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_16_1(bytes16 self, bytes1 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes1 oldValue = extract_16_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_2(bytes16 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 14) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_16_2(bytes16 self, bytes2 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes2 oldValue = extract_16_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_4(bytes16 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_16_4(bytes16 self, bytes4 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes4 oldValue = extract_16_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_6(bytes16 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 10) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_16_6(bytes16 self, bytes6 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes6 oldValue = extract_16_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_8(bytes16 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_16_8(bytes16 self, bytes8 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes8 oldValue = extract_16_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_10(bytes16 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_16_10(bytes16 self, bytes10 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes10 oldValue = extract_16_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_16_12(bytes16 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_16_12(bytes16 self, bytes12 value, uint8 offset) internal pure returns (bytes16 result) {
        bytes12 oldValue = extract_16_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_1(bytes20 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 19) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_20_1(bytes20 self, bytes1 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes1 oldValue = extract_20_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_2(bytes20 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 18) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_20_2(bytes20 self, bytes2 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes2 oldValue = extract_20_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_4(bytes20 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 16) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_20_4(bytes20 self, bytes4 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes4 oldValue = extract_20_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_6(bytes20 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 14) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_20_6(bytes20 self, bytes6 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes6 oldValue = extract_20_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_8(bytes20 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_20_8(bytes20 self, bytes8 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes8 oldValue = extract_20_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_10(bytes20 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 10) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_20_10(bytes20 self, bytes10 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes10 oldValue = extract_20_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_12(bytes20 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_20_12(bytes20 self, bytes12 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes12 oldValue = extract_20_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_20_16(bytes20 self, uint8 offset) internal pure returns (bytes16 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(128, not(0)))
        }
    }

    function replace_20_16(bytes20 self, bytes16 value, uint8 offset) internal pure returns (bytes20 result) {
        bytes16 oldValue = extract_20_16(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(128, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_1(bytes22 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 21) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_22_1(bytes22 self, bytes1 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes1 oldValue = extract_22_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_2(bytes22 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 20) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_22_2(bytes22 self, bytes2 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes2 oldValue = extract_22_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_4(bytes22 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 18) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_22_4(bytes22 self, bytes4 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes4 oldValue = extract_22_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_6(bytes22 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 16) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_22_6(bytes22 self, bytes6 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes6 oldValue = extract_22_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_8(bytes22 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 14) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_22_8(bytes22 self, bytes8 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes8 oldValue = extract_22_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_10(bytes22 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_22_10(bytes22 self, bytes10 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes10 oldValue = extract_22_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_12(bytes22 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 10) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_22_12(bytes22 self, bytes12 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes12 oldValue = extract_22_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_16(bytes22 self, uint8 offset) internal pure returns (bytes16 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(128, not(0)))
        }
    }

    function replace_22_16(bytes22 self, bytes16 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes16 oldValue = extract_22_16(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(128, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_22_20(bytes22 self, uint8 offset) internal pure returns (bytes20 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(96, not(0)))
        }
    }

    function replace_22_20(bytes22 self, bytes20 value, uint8 offset) internal pure returns (bytes22 result) {
        bytes20 oldValue = extract_22_20(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(96, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_1(bytes24 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 23) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_24_1(bytes24 self, bytes1 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes1 oldValue = extract_24_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_2(bytes24 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 22) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_24_2(bytes24 self, bytes2 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes2 oldValue = extract_24_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_4(bytes24 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 20) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_24_4(bytes24 self, bytes4 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes4 oldValue = extract_24_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_6(bytes24 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 18) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_24_6(bytes24 self, bytes6 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes6 oldValue = extract_24_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_8(bytes24 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 16) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_24_8(bytes24 self, bytes8 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes8 oldValue = extract_24_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_10(bytes24 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 14) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_24_10(bytes24 self, bytes10 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes10 oldValue = extract_24_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_12(bytes24 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_24_12(bytes24 self, bytes12 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes12 oldValue = extract_24_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_16(bytes24 self, uint8 offset) internal pure returns (bytes16 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(128, not(0)))
        }
    }

    function replace_24_16(bytes24 self, bytes16 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes16 oldValue = extract_24_16(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(128, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_20(bytes24 self, uint8 offset) internal pure returns (bytes20 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(96, not(0)))
        }
    }

    function replace_24_20(bytes24 self, bytes20 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes20 oldValue = extract_24_20(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(96, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_24_22(bytes24 self, uint8 offset) internal pure returns (bytes22 result) {
        if (offset > 2) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(80, not(0)))
        }
    }

    function replace_24_22(bytes24 self, bytes22 value, uint8 offset) internal pure returns (bytes24 result) {
        bytes22 oldValue = extract_24_22(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(80, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_1(bytes28 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 27) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_28_1(bytes28 self, bytes1 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes1 oldValue = extract_28_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_2(bytes28 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 26) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_28_2(bytes28 self, bytes2 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes2 oldValue = extract_28_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_4(bytes28 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 24) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_28_4(bytes28 self, bytes4 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes4 oldValue = extract_28_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_6(bytes28 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 22) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_28_6(bytes28 self, bytes6 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes6 oldValue = extract_28_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_8(bytes28 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 20) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_28_8(bytes28 self, bytes8 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes8 oldValue = extract_28_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_10(bytes28 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 18) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_28_10(bytes28 self, bytes10 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes10 oldValue = extract_28_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_12(bytes28 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 16) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_28_12(bytes28 self, bytes12 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes12 oldValue = extract_28_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_16(bytes28 self, uint8 offset) internal pure returns (bytes16 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(128, not(0)))
        }
    }

    function replace_28_16(bytes28 self, bytes16 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes16 oldValue = extract_28_16(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(128, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_20(bytes28 self, uint8 offset) internal pure returns (bytes20 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(96, not(0)))
        }
    }

    function replace_28_20(bytes28 self, bytes20 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes20 oldValue = extract_28_20(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(96, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_22(bytes28 self, uint8 offset) internal pure returns (bytes22 result) {
        if (offset > 6) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(80, not(0)))
        }
    }

    function replace_28_22(bytes28 self, bytes22 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes22 oldValue = extract_28_22(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(80, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_28_24(bytes28 self, uint8 offset) internal pure returns (bytes24 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(64, not(0)))
        }
    }

    function replace_28_24(bytes28 self, bytes24 value, uint8 offset) internal pure returns (bytes28 result) {
        bytes24 oldValue = extract_28_24(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(64, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_1(bytes32 self, uint8 offset) internal pure returns (bytes1 result) {
        if (offset > 31) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(248, not(0)))
        }
    }

    function replace_32_1(bytes32 self, bytes1 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes1 oldValue = extract_32_1(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(248, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_2(bytes32 self, uint8 offset) internal pure returns (bytes2 result) {
        if (offset > 30) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(240, not(0)))
        }
    }

    function replace_32_2(bytes32 self, bytes2 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes2 oldValue = extract_32_2(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(240, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_4(bytes32 self, uint8 offset) internal pure returns (bytes4 result) {
        if (offset > 28) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(224, not(0)))
        }
    }

    function replace_32_4(bytes32 self, bytes4 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes4 oldValue = extract_32_4(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(224, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_6(bytes32 self, uint8 offset) internal pure returns (bytes6 result) {
        if (offset > 26) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(208, not(0)))
        }
    }

    function replace_32_6(bytes32 self, bytes6 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes6 oldValue = extract_32_6(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(208, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_8(bytes32 self, uint8 offset) internal pure returns (bytes8 result) {
        if (offset > 24) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(192, not(0)))
        }
    }

    function replace_32_8(bytes32 self, bytes8 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes8 oldValue = extract_32_8(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(192, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_10(bytes32 self, uint8 offset) internal pure returns (bytes10 result) {
        if (offset > 22) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(176, not(0)))
        }
    }

    function replace_32_10(bytes32 self, bytes10 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes10 oldValue = extract_32_10(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(176, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_12(bytes32 self, uint8 offset) internal pure returns (bytes12 result) {
        if (offset > 20) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(160, not(0)))
        }
    }

    function replace_32_12(bytes32 self, bytes12 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes12 oldValue = extract_32_12(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(160, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_16(bytes32 self, uint8 offset) internal pure returns (bytes16 result) {
        if (offset > 16) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(128, not(0)))
        }
    }

    function replace_32_16(bytes32 self, bytes16 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes16 oldValue = extract_32_16(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(128, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_20(bytes32 self, uint8 offset) internal pure returns (bytes20 result) {
        if (offset > 12) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(96, not(0)))
        }
    }

    function replace_32_20(bytes32 self, bytes20 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes20 oldValue = extract_32_20(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(96, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_22(bytes32 self, uint8 offset) internal pure returns (bytes22 result) {
        if (offset > 10) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(80, not(0)))
        }
    }

    function replace_32_22(bytes32 self, bytes22 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes22 oldValue = extract_32_22(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(80, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_24(bytes32 self, uint8 offset) internal pure returns (bytes24 result) {
        if (offset > 8) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(64, not(0)))
        }
    }

    function replace_32_24(bytes32 self, bytes24 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes24 oldValue = extract_32_24(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(64, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }

    function extract_32_28(bytes32 self, uint8 offset) internal pure returns (bytes28 result) {
        if (offset > 4) revert OutOfRangeAccess();
        assembly ("memory-safe") {
            result := and(shl(mul(8, offset), self), shl(32, not(0)))
        }
    }

    function replace_32_28(bytes32 self, bytes28 value, uint8 offset) internal pure returns (bytes32 result) {
        bytes28 oldValue = extract_32_28(self, offset);
        assembly ("memory-safe") {
            value := and(value, shl(32, not(0)))
            result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
        }
    }
}

File 3 of 16 : EnumerableMap.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/structs/EnumerableMap.sol)
// This file was procedurally generated from scripts/generate/templates/EnumerableMap.js.

pragma solidity ^0.8.20;

import {EnumerableSet} from "./EnumerableSet.sol";

/**
 * @dev Library for managing an enumerable variant of Solidity's
 * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
 * type.
 *
 * Maps have the following properties:
 *
 * - Entries are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Entries are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```solidity
 * contract Example {
 *     // Add the library methods
 *     using EnumerableMap for EnumerableMap.UintToAddressMap;
 *
 *     // Declare a set state variable
 *     EnumerableMap.UintToAddressMap private myMap;
 * }
 * ```
 *
 * The following map types are supported:
 *
 * - `uint256 -> address` (`UintToAddressMap`) since v3.0.0
 * - `address -> uint256` (`AddressToUintMap`) since v4.6.0
 * - `bytes32 -> bytes32` (`Bytes32ToBytes32Map`) since v4.6.0
 * - `uint256 -> uint256` (`UintToUintMap`) since v4.7.0
 * - `bytes32 -> uint256` (`Bytes32ToUintMap`) since v4.7.0
 * - `uint256 -> bytes32` (`UintToBytes32Map`) since v5.1.0
 * - `address -> address` (`AddressToAddressMap`) since v5.1.0
 * - `address -> bytes32` (`AddressToBytes32Map`) since v5.1.0
 * - `bytes32 -> address` (`Bytes32ToAddressMap`) since v5.1.0
 *
 * [WARNING]
 * ====
 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
 * unusable.
 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 * In order to clean an EnumerableMap, you can either remove all elements one by one or create a fresh instance using an
 * array of EnumerableMap.
 * ====
 */
library EnumerableMap {
    using EnumerableSet for EnumerableSet.Bytes32Set;

    // To implement this library for multiple types with as little code repetition as possible, we write it in
    // terms of a generic Map type with bytes32 keys and values. The Map implementation uses private functions,
    // and user-facing implementations such as `UintToAddressMap` are just wrappers around the underlying Map.
    // This means that we can only create new EnumerableMaps for types that fit in bytes32.

    /**
     * @dev Query for a nonexistent map key.
     */
    error EnumerableMapNonexistentKey(bytes32 key);

    struct Bytes32ToBytes32Map {
        // Storage of keys
        EnumerableSet.Bytes32Set _keys;
        mapping(bytes32 key => bytes32) _values;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(Bytes32ToBytes32Map storage map, bytes32 key, bytes32 value) internal returns (bool) {
        map._values[key] = value;
        return map._keys.add(key);
    }

    /**
     * @dev Removes a key-value pair from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
        delete map._values[key];
        return map._keys.remove(key);
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
        return map._keys.contains(key);
    }

    /**
     * @dev Returns the number of key-value pairs in the map. O(1).
     */
    function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
        return map._keys.length();
    }

    /**
     * @dev Returns the key-value pair stored at position `index` in the map. O(1).
     *
     * Note that there are no guarantees on the ordering of entries inside the
     * array, and it may change when more entries are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32 key, bytes32 value) {
        bytes32 atKey = map._keys.at(index);
        return (atKey, map._values[atKey]);
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool exists, bytes32 value) {
        bytes32 val = map._values[key];
        if (val == bytes32(0)) {
            return (contains(map, key), bytes32(0));
        } else {
            return (true, val);
        }
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
        bytes32 value = map._values[key];
        if (value == 0 && !contains(map, key)) {
            revert EnumerableMapNonexistentKey(key);
        }
        return value;
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(Bytes32ToBytes32Map storage map) internal view returns (bytes32[] memory) {
        return map._keys.values();
    }

    // UintToUintMap

    struct UintToUintMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(UintToUintMap storage map, uint256 key, uint256 value) internal returns (bool) {
        return set(map._inner, bytes32(key), bytes32(value));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
        return remove(map._inner, bytes32(key));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
        return contains(map._inner, bytes32(key));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(UintToUintMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintToUintMap storage map, uint256 index) internal view returns (uint256 key, uint256 value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (uint256(atKey), uint256(val));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool exists, uint256 value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
        return (success, uint256(val));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
        return uint256(get(map._inner, bytes32(key)));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(UintToUintMap storage map) internal view returns (uint256[] memory) {
        bytes32[] memory store = keys(map._inner);
        uint256[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // UintToAddressMap

    struct UintToAddressMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
        return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
        return remove(map._inner, bytes32(key));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
        return contains(map._inner, bytes32(key));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(UintToAddressMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256 key, address value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (uint256(atKey), address(uint160(uint256(val))));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool exists, address value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
        return (success, address(uint160(uint256(val))));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
        return address(uint160(uint256(get(map._inner, bytes32(key)))));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(UintToAddressMap storage map) internal view returns (uint256[] memory) {
        bytes32[] memory store = keys(map._inner);
        uint256[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // UintToBytes32Map

    struct UintToBytes32Map {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(UintToBytes32Map storage map, uint256 key, bytes32 value) internal returns (bool) {
        return set(map._inner, bytes32(key), value);
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(UintToBytes32Map storage map, uint256 key) internal returns (bool) {
        return remove(map._inner, bytes32(key));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(UintToBytes32Map storage map, uint256 key) internal view returns (bool) {
        return contains(map._inner, bytes32(key));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(UintToBytes32Map storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintToBytes32Map storage map, uint256 index) internal view returns (uint256 key, bytes32 value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (uint256(atKey), val);
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(UintToBytes32Map storage map, uint256 key) internal view returns (bool exists, bytes32 value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(key));
        return (success, val);
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(UintToBytes32Map storage map, uint256 key) internal view returns (bytes32) {
        return get(map._inner, bytes32(key));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(UintToBytes32Map storage map) internal view returns (uint256[] memory) {
        bytes32[] memory store = keys(map._inner);
        uint256[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // AddressToUintMap

    struct AddressToUintMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
        return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(AddressToUintMap storage map, address key) internal returns (bool) {
        return remove(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
        return contains(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(AddressToUintMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressToUintMap storage map, uint256 index) internal view returns (address key, uint256 value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (address(uint160(uint256(atKey))), uint256(val));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(AddressToUintMap storage map, address key) internal view returns (bool exists, uint256 value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
        return (success, uint256(val));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
        return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(AddressToUintMap storage map) internal view returns (address[] memory) {
        bytes32[] memory store = keys(map._inner);
        address[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // AddressToAddressMap

    struct AddressToAddressMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(AddressToAddressMap storage map, address key, address value) internal returns (bool) {
        return set(map._inner, bytes32(uint256(uint160(key))), bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(AddressToAddressMap storage map, address key) internal returns (bool) {
        return remove(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(AddressToAddressMap storage map, address key) internal view returns (bool) {
        return contains(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(AddressToAddressMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressToAddressMap storage map, uint256 index) internal view returns (address key, address value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (address(uint160(uint256(atKey))), address(uint160(uint256(val))));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(AddressToAddressMap storage map, address key) internal view returns (bool exists, address value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
        return (success, address(uint160(uint256(val))));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(AddressToAddressMap storage map, address key) internal view returns (address) {
        return address(uint160(uint256(get(map._inner, bytes32(uint256(uint160(key)))))));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(AddressToAddressMap storage map) internal view returns (address[] memory) {
        bytes32[] memory store = keys(map._inner);
        address[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // AddressToBytes32Map

    struct AddressToBytes32Map {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(AddressToBytes32Map storage map, address key, bytes32 value) internal returns (bool) {
        return set(map._inner, bytes32(uint256(uint160(key))), value);
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(AddressToBytes32Map storage map, address key) internal returns (bool) {
        return remove(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(AddressToBytes32Map storage map, address key) internal view returns (bool) {
        return contains(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(AddressToBytes32Map storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressToBytes32Map storage map, uint256 index) internal view returns (address key, bytes32 value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (address(uint160(uint256(atKey))), val);
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(AddressToBytes32Map storage map, address key) internal view returns (bool exists, bytes32 value) {
        (bool success, bytes32 val) = tryGet(map._inner, bytes32(uint256(uint160(key))));
        return (success, val);
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(AddressToBytes32Map storage map, address key) internal view returns (bytes32) {
        return get(map._inner, bytes32(uint256(uint160(key))));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(AddressToBytes32Map storage map) internal view returns (address[] memory) {
        bytes32[] memory store = keys(map._inner);
        address[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // Bytes32ToUintMap

    struct Bytes32ToUintMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(Bytes32ToUintMap storage map, bytes32 key, uint256 value) internal returns (bool) {
        return set(map._inner, key, bytes32(value));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
        return remove(map._inner, key);
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
        return contains(map._inner, key);
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32 key, uint256 value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (atKey, uint256(val));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool exists, uint256 value) {
        (bool success, bytes32 val) = tryGet(map._inner, key);
        return (success, uint256(val));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
        return uint256(get(map._inner, key));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(Bytes32ToUintMap storage map) internal view returns (bytes32[] memory) {
        bytes32[] memory store = keys(map._inner);
        bytes32[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // Bytes32ToAddressMap

    struct Bytes32ToAddressMap {
        Bytes32ToBytes32Map _inner;
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(Bytes32ToAddressMap storage map, bytes32 key, address value) internal returns (bool) {
        return set(map._inner, key, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a map. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(Bytes32ToAddressMap storage map, bytes32 key) internal returns (bool) {
        return remove(map._inner, key);
    }

    /**
     * @dev Returns true if the key is in the map. O(1).
     */
    function contains(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool) {
        return contains(map._inner, key);
    }

    /**
     * @dev Returns the number of elements in the map. O(1).
     */
    function length(Bytes32ToAddressMap storage map) internal view returns (uint256) {
        return length(map._inner);
    }

    /**
     * @dev Returns the element stored at position `index` in the map. O(1).
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32ToAddressMap storage map, uint256 index) internal view returns (bytes32 key, address value) {
        (bytes32 atKey, bytes32 val) = at(map._inner, index);
        return (atKey, address(uint160(uint256(val))));
    }

    /**
     * @dev Tries to returns the value associated with `key`. O(1).
     * Does not revert if `key` is not in the map.
     */
    function tryGet(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (bool exists, address value) {
        (bool success, bytes32 val) = tryGet(map._inner, key);
        return (success, address(uint160(uint256(val))));
    }

    /**
     * @dev Returns the value associated with `key`. O(1).
     *
     * Requirements:
     *
     * - `key` must be in the map.
     */
    function get(Bytes32ToAddressMap storage map, bytes32 key) internal view returns (address) {
        return address(uint160(uint256(get(map._inner, key))));
    }

    /**
     * @dev Return the an array containing all the keys
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the map grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function keys(Bytes32ToAddressMap storage map) internal view returns (bytes32[] memory) {
        bytes32[] memory store = keys(map._inner);
        bytes32[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }
}

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

pragma solidity ^0.8.20;

import {Panic} from "../Panic.sol";
import {SafeCast} from "./SafeCast.sol";

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    enum Rounding {
        Floor, // Toward negative infinity
        Ceil, // Toward positive infinity
        Trunc, // Toward zero
        Expand // Away from zero
    }

    /**
     * @dev Returns the addition of two unsigned integers, with an success flag (no overflow).
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow).
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow).
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
     *
     * IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
     * However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
     * one branch when needed, making this function more expensive.
     */
    function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
        unchecked {
            // branchless ternary works because:
            // b ^ (a ^ b) == a
            // b ^ 0 == b
            return b ^ ((a ^ b) * SafeCast.toUint(condition));
        }
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return ternary(a > b, a, b);
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return ternary(a < b, a, b);
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds towards infinity instead
     * of rounding towards zero.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        if (b == 0) {
            // Guarantee the same behavior as in a regular Solidity division.
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }

        // The following calculation ensures accurate ceiling division without overflow.
        // Since a is non-zero, (a - 1) / b will not overflow.
        // The largest possible result occurs when (a - 1) / b is type(uint256).max,
        // but the largest value we can obtain is type(uint256).max - 1, which happens
        // when a = type(uint256).max and b = 1.
        unchecked {
            return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
        }
    }

    /**
     * @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
     * denominator == 0.
     *
     * Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
     * Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use
            // the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2²⁵⁶ + prod0.
            uint256 prod0 = x * y; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return prod0 / denominator;
            }

            // Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0.
            if (denominator <= prod1) {
                Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
            }

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator.
            // Always >= 1. See https://cs.stackexchange.com/q/138556/92363.

            uint256 twos = denominator & (0 - denominator);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * twos;

            // Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such
            // that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv ≡ 1 mod 2⁴.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also
            // works in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2⁸
            inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶
            inverse *= 2 - denominator * inverse; // inverse mod 2³²
            inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴
            inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸
            inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2²⁵⁶. Since the preconditions guarantee that the outcome is
            // less than 2²⁵⁶, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /**
     * @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
    }

    /**
     * @dev Calculate the modular multiplicative inverse of a number in Z/nZ.
     *
     * If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0.
     * If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible.
     *
     * If the input value is not inversible, 0 is returned.
     *
     * NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the
     * inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}.
     */
    function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
        unchecked {
            if (n == 0) return 0;

            // The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version)
            // Used to compute integers x and y such that: ax + ny = gcd(a, n).
            // When the gcd is 1, then the inverse of a modulo n exists and it's x.
            // ax + ny = 1
            // ax = 1 + (-y)n
            // ax ≡ 1 (mod n) # x is the inverse of a modulo n

            // If the remainder is 0 the gcd is n right away.
            uint256 remainder = a % n;
            uint256 gcd = n;

            // Therefore the initial coefficients are:
            // ax + ny = gcd(a, n) = n
            // 0a + 1n = n
            int256 x = 0;
            int256 y = 1;

            while (remainder != 0) {
                uint256 quotient = gcd / remainder;

                (gcd, remainder) = (
                    // The old remainder is the next gcd to try.
                    remainder,
                    // Compute the next remainder.
                    // Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd
                    // where gcd is at most n (capped to type(uint256).max)
                    gcd - remainder * quotient
                );

                (x, y) = (
                    // Increment the coefficient of a.
                    y,
                    // Decrement the coefficient of n.
                    // Can overflow, but the result is casted to uint256 so that the
                    // next value of y is "wrapped around" to a value between 0 and n - 1.
                    x - y * int256(quotient)
                );
            }

            if (gcd != 1) return 0; // No inverse exists.
            return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative.
        }
    }

    /**
     * @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`.
     *
     * From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is
     * prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that
     * `a**(p-2)` is the modular multiplicative inverse of a in Fp.
     *
     * NOTE: this function does NOT check that `p` is a prime greater than `2`.
     */
    function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
        unchecked {
            return Math.modExp(a, p - 2, p);
        }
    }

    /**
     * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m)
     *
     * Requirements:
     * - modulus can't be zero
     * - underlying staticcall to precompile must succeed
     *
     * IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make
     * sure the chain you're using it on supports the precompiled contract for modular exponentiation
     * at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise,
     * the underlying function will succeed given the lack of a revert, but the result may be incorrectly
     * interpreted as 0.
     */
    function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
        (bool success, uint256 result) = tryModExp(b, e, m);
        if (!success) {
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }
        return result;
    }

    /**
     * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m).
     * It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying
     * to operate modulo 0 or if the underlying precompile reverted.
     *
     * IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain
     * you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in
     * https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack
     * of a revert, but the result may be incorrectly interpreted as 0.
     */
    function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
        if (m == 0) return (false, 0);
        assembly ("memory-safe") {
            let ptr := mload(0x40)
            // | Offset    | Content    | Content (Hex)                                                      |
            // |-----------|------------|--------------------------------------------------------------------|
            // | 0x00:0x1f | size of b  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x20:0x3f | size of e  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x40:0x5f | size of m  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x60:0x7f | value of b | 0x<.............................................................b> |
            // | 0x80:0x9f | value of e | 0x<.............................................................e> |
            // | 0xa0:0xbf | value of m | 0x<.............................................................m> |
            mstore(ptr, 0x20)
            mstore(add(ptr, 0x20), 0x20)
            mstore(add(ptr, 0x40), 0x20)
            mstore(add(ptr, 0x60), b)
            mstore(add(ptr, 0x80), e)
            mstore(add(ptr, 0xa0), m)

            // Given the result < m, it's guaranteed to fit in 32 bytes,
            // so we can use the memory scratch space located at offset 0.
            success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
            result := mload(0x00)
        }
    }

    /**
     * @dev Variant of {modExp} that supports inputs of arbitrary length.
     */
    function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
        (bool success, bytes memory result) = tryModExp(b, e, m);
        if (!success) {
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }
        return result;
    }

    /**
     * @dev Variant of {tryModExp} that supports inputs of arbitrary length.
     */
    function tryModExp(
        bytes memory b,
        bytes memory e,
        bytes memory m
    ) internal view returns (bool success, bytes memory result) {
        if (_zeroBytes(m)) return (false, new bytes(0));

        uint256 mLen = m.length;

        // Encode call args in result and move the free memory pointer
        result = abi.encodePacked(b.length, e.length, mLen, b, e, m);

        assembly ("memory-safe") {
            let dataPtr := add(result, 0x20)
            // Write result on top of args to avoid allocating extra memory.
            success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
            // Overwrite the length.
            // result.length > returndatasize() is guaranteed because returndatasize() == m.length
            mstore(result, mLen)
            // Set the memory pointer after the returned data.
            mstore(0x40, add(dataPtr, mLen))
        }
    }

    /**
     * @dev Returns whether the provided byte array is zero.
     */
    function _zeroBytes(bytes memory byteArray) private pure returns (bool) {
        for (uint256 i = 0; i < byteArray.length; ++i) {
            if (byteArray[i] != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
     * towards zero.
     *
     * This method is based on Newton's method for computing square roots; the algorithm is restricted to only
     * using integer operations.
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        unchecked {
            // Take care of easy edge cases when a == 0 or a == 1
            if (a <= 1) {
                return a;
            }

            // In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a
            // sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between
            // the current value as `ε_n = | x_n - sqrt(a) |`.
            //
            // For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root
            // of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is
            // bigger than any uint256.
            //
            // By noticing that
            // `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)`
            // we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar
            // to the msb function.
            uint256 aa = a;
            uint256 xn = 1;

            if (aa >= (1 << 128)) {
                aa >>= 128;
                xn <<= 64;
            }
            if (aa >= (1 << 64)) {
                aa >>= 64;
                xn <<= 32;
            }
            if (aa >= (1 << 32)) {
                aa >>= 32;
                xn <<= 16;
            }
            if (aa >= (1 << 16)) {
                aa >>= 16;
                xn <<= 8;
            }
            if (aa >= (1 << 8)) {
                aa >>= 8;
                xn <<= 4;
            }
            if (aa >= (1 << 4)) {
                aa >>= 4;
                xn <<= 2;
            }
            if (aa >= (1 << 2)) {
                xn <<= 1;
            }

            // We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1).
            //
            // We can refine our estimation by noticing that the middle of that interval minimizes the error.
            // If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2).
            // This is going to be our x_0 (and ε_0)
            xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2)

            // From here, Newton's method give us:
            // x_{n+1} = (x_n + a / x_n) / 2
            //
            // One should note that:
            // x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a
            //              = ((x_n² + a) / (2 * x_n))² - a
            //              = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a
            //              = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²)
            //              = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²)
            //              = (x_n² - a)² / (2 * x_n)²
            //              = ((x_n² - a) / (2 * x_n))²
            //              ≥ 0
            // Which proves that for all n ≥ 1, sqrt(a) ≤ x_n
            //
            // This gives us the proof of quadratic convergence of the sequence:
            // ε_{n+1} = | x_{n+1} - sqrt(a) |
            //         = | (x_n + a / x_n) / 2 - sqrt(a) |
            //         = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) |
            //         = | (x_n - sqrt(a))² / (2 * x_n) |
            //         = | ε_n² / (2 * x_n) |
            //         = ε_n² / | (2 * x_n) |
            //
            // For the first iteration, we have a special case where x_0 is known:
            // ε_1 = ε_0² / | (2 * x_0) |
            //     ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2)))
            //     ≤ 2**(2*e-4) / (3 * 2**(e-1))
            //     ≤ 2**(e-3) / 3
            //     ≤ 2**(e-3-log2(3))
            //     ≤ 2**(e-4.5)
            //
            // For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n:
            // ε_{n+1} = ε_n² / | (2 * x_n) |
            //         ≤ (2**(e-k))² / (2 * 2**(e-1))
            //         ≤ 2**(2*e-2*k) / 2**e
            //         ≤ 2**(e-2*k)
            xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5)  -- special case, see above
            xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9)    -- general case with k = 4.5
            xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18)   -- general case with k = 9
            xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36)   -- general case with k = 18
            xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72)   -- general case with k = 36
            xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144)  -- general case with k = 72

            // Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision
            // ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either
            // sqrt(a) or sqrt(a) + 1.
            return xn - SafeCast.toUint(xn > a / xn);
        }
    }

    /**
     * @dev Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
        }
    }

    /**
     * @dev Return the log in base 2 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        uint256 exp;
        unchecked {
            exp = 128 * SafeCast.toUint(value > (1 << 128) - 1);
            value >>= exp;
            result += exp;

            exp = 64 * SafeCast.toUint(value > (1 << 64) - 1);
            value >>= exp;
            result += exp;

            exp = 32 * SafeCast.toUint(value > (1 << 32) - 1);
            value >>= exp;
            result += exp;

            exp = 16 * SafeCast.toUint(value > (1 << 16) - 1);
            value >>= exp;
            result += exp;

            exp = 8 * SafeCast.toUint(value > (1 << 8) - 1);
            value >>= exp;
            result += exp;

            exp = 4 * SafeCast.toUint(value > (1 << 4) - 1);
            value >>= exp;
            result += exp;

            exp = 2 * SafeCast.toUint(value > (1 << 2) - 1);
            value >>= exp;
            result += exp;

            result += SafeCast.toUint(value > 1);
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
        }
    }

    /**
     * @dev Return the log in base 10 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
        }
    }

    /**
     * @dev Return the log in base 256 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        uint256 isGt;
        unchecked {
            isGt = SafeCast.toUint(value > (1 << 128) - 1);
            value >>= isGt * 128;
            result += isGt * 16;

            isGt = SafeCast.toUint(value > (1 << 64) - 1);
            value >>= isGt * 64;
            result += isGt * 8;

            isGt = SafeCast.toUint(value > (1 << 32) - 1);
            value >>= isGt * 32;
            result += isGt * 4;

            isGt = SafeCast.toUint(value > (1 << 16) - 1);
            value >>= isGt * 16;
            result += isGt * 2;

            result += SafeCast.toUint(value > (1 << 8) - 1);
        }
        return result;
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
        }
    }

    /**
     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
     */
    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
        return uint8(rounding) % 2 == 1;
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

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

pragma solidity ^0.8.24;

import {TransientSlot} from "./TransientSlot.sol";

/**
 * @dev Variant of {ReentrancyGuard} that uses transient storage.
 *
 * NOTE: This variant only works on networks where EIP-1153 is available.
 *
 * _Available since v5.1._
 */
abstract contract ReentrancyGuardTransient {
    using TransientSlot for *;

    // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
    bytes32 private constant REENTRANCY_GUARD_STORAGE =
        0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;

    /**
     * @dev Unauthorized reentrant call.
     */
    error ReentrancyGuardReentrantCall();

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be NOT_ENTERED
        if (_reentrancyGuardEntered()) {
            revert ReentrancyGuardReentrantCall();
        }

        // Any calls to nonReentrant after this point will fail
        REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
    }

    function _nonReentrantAfter() private {
        REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return REENTRANCY_GUARD_STORAGE.asBoolean().tload();
    }
}

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

import {IERC165} from "openzeppelin-contracts/contracts/interfaces/IERC165.sol";

import {MagicAlchemyRarity as Rarity} from "../utils/MagicAlchemyRarity.sol";

interface IMagicAlchemyMarathon is IERC165 {
    // revert if round isn't finished yet
    function flaskFinalCountFor(uint256 round, Rarity rarity) external view returns (uint256);
}

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

interface ITetherToken {
    function transferFrom(address from, address to, uint256 value) external;
    function transfer(address to, uint256 value) external;
}

File 9 of 16 : MagicAlchemyRarity.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/**
 * @dev Enumeration of rarity levels used throughout the Magic Alchemy contracts.
 * It defines the four distinct tiers: Common, Rare, Epic, and Legendary.
 */
enum MagicAlchemyRarity {
    Common,
    Rare,
    Epic,
    Legendary
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/structs/EnumerableSet.sol)
// This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.

pragma solidity ^0.8.20;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```solidity
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 *
 * [WARNING]
 * ====
 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
 * unusable.
 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
 * array of EnumerableSet.
 * ====
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;
        // Position is the index of the value in the `values` array plus 1.
        // Position 0 is used to mean a value is not in the set.
        mapping(bytes32 value => uint256) _positions;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._positions[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We cache the value's position to prevent multiple reads from the same storage slot
        uint256 position = set._positions[value];

        if (position != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 valueIndex = position - 1;
            uint256 lastIndex = set._values.length - 1;

            if (valueIndex != lastIndex) {
                bytes32 lastValue = set._values[lastIndex];

                // Move the lastValue to the index where the value to delete is
                set._values[valueIndex] = lastValue;
                // Update the tracked position of the lastValue (that was just moved)
                set._positions[lastValue] = position;
            }

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the tracked position for the deleted slot
            delete set._positions[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._positions[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function _values(Set storage set) private view returns (bytes32[] memory) {
        return set._values;
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
        bytes32[] memory store = _values(set._inner);
        bytes32[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(AddressSet storage set) internal view returns (address[] memory) {
        bytes32[] memory store = _values(set._inner);
        address[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }

    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(UintSet storage set) internal view returns (uint256[] memory) {
        bytes32[] memory store = _values(set._inner);
        uint256[] memory result;

        assembly ("memory-safe") {
            result := store
        }

        return result;
    }
}

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

pragma solidity ^0.8.20;

/**
 * @dev Helper library for emitting standardized panic codes.
 *
 * ```solidity
 * contract Example {
 *      using Panic for uint256;
 *
 *      // Use any of the declared internal constants
 *      function foo() { Panic.GENERIC.panic(); }
 *
 *      // Alternatively
 *      function foo() { Panic.panic(Panic.GENERIC); }
 * }
 * ```
 *
 * Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil].
 *
 * _Available since v5.1._
 */
// slither-disable-next-line unused-state
library Panic {
    /// @dev generic / unspecified error
    uint256 internal constant GENERIC = 0x00;
    /// @dev used by the assert() builtin
    uint256 internal constant ASSERT = 0x01;
    /// @dev arithmetic underflow or overflow
    uint256 internal constant UNDER_OVERFLOW = 0x11;
    /// @dev division or modulo by zero
    uint256 internal constant DIVISION_BY_ZERO = 0x12;
    /// @dev enum conversion error
    uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
    /// @dev invalid encoding in storage
    uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
    /// @dev empty array pop
    uint256 internal constant EMPTY_ARRAY_POP = 0x31;
    /// @dev array out of bounds access
    uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
    /// @dev resource error (too large allocation or too large array)
    uint256 internal constant RESOURCE_ERROR = 0x41;
    /// @dev calling invalid internal function
    uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;

    /// @dev Reverts with a panic code. Recommended to use with
    /// the internal constants with predefined codes.
    function panic(uint256 code) internal pure {
        assembly ("memory-safe") {
            mstore(0x00, 0x4e487b71)
            mstore(0x20, code)
            revert(0x1c, 0x24)
        }
    }
}

File 12 of 16 : SafeCast.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.

pragma solidity ^0.8.20;

/**
 * @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
 * checks.
 *
 * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
 * easily result in undesired exploitation or bugs, since developers usually
 * assume that overflows raise errors. `SafeCast` restores this intuition by
 * reverting the transaction when such an operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeCast {
    /**
     * @dev Value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);

    /**
     * @dev An int value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedIntToUint(int256 value);

    /**
     * @dev Value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);

    /**
     * @dev An uint value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedUintToInt(uint256 value);

    /**
     * @dev Returns the downcasted uint248 from uint256, reverting on
     * overflow (when the input is greater than largest uint248).
     *
     * Counterpart to Solidity's `uint248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toUint248(uint256 value) internal pure returns (uint248) {
        if (value > type(uint248).max) {
            revert SafeCastOverflowedUintDowncast(248, value);
        }
        return uint248(value);
    }

    /**
     * @dev Returns the downcasted uint240 from uint256, reverting on
     * overflow (when the input is greater than largest uint240).
     *
     * Counterpart to Solidity's `uint240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toUint240(uint256 value) internal pure returns (uint240) {
        if (value > type(uint240).max) {
            revert SafeCastOverflowedUintDowncast(240, value);
        }
        return uint240(value);
    }

    /**
     * @dev Returns the downcasted uint232 from uint256, reverting on
     * overflow (when the input is greater than largest uint232).
     *
     * Counterpart to Solidity's `uint232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toUint232(uint256 value) internal pure returns (uint232) {
        if (value > type(uint232).max) {
            revert SafeCastOverflowedUintDowncast(232, value);
        }
        return uint232(value);
    }

    /**
     * @dev Returns the downcasted uint224 from uint256, reverting on
     * overflow (when the input is greater than largest uint224).
     *
     * Counterpart to Solidity's `uint224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toUint224(uint256 value) internal pure returns (uint224) {
        if (value > type(uint224).max) {
            revert SafeCastOverflowedUintDowncast(224, value);
        }
        return uint224(value);
    }

    /**
     * @dev Returns the downcasted uint216 from uint256, reverting on
     * overflow (when the input is greater than largest uint216).
     *
     * Counterpart to Solidity's `uint216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toUint216(uint256 value) internal pure returns (uint216) {
        if (value > type(uint216).max) {
            revert SafeCastOverflowedUintDowncast(216, value);
        }
        return uint216(value);
    }

    /**
     * @dev Returns the downcasted uint208 from uint256, reverting on
     * overflow (when the input is greater than largest uint208).
     *
     * Counterpart to Solidity's `uint208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toUint208(uint256 value) internal pure returns (uint208) {
        if (value > type(uint208).max) {
            revert SafeCastOverflowedUintDowncast(208, value);
        }
        return uint208(value);
    }

    /**
     * @dev Returns the downcasted uint200 from uint256, reverting on
     * overflow (when the input is greater than largest uint200).
     *
     * Counterpart to Solidity's `uint200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toUint200(uint256 value) internal pure returns (uint200) {
        if (value > type(uint200).max) {
            revert SafeCastOverflowedUintDowncast(200, value);
        }
        return uint200(value);
    }

    /**
     * @dev Returns the downcasted uint192 from uint256, reverting on
     * overflow (when the input is greater than largest uint192).
     *
     * Counterpart to Solidity's `uint192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toUint192(uint256 value) internal pure returns (uint192) {
        if (value > type(uint192).max) {
            revert SafeCastOverflowedUintDowncast(192, value);
        }
        return uint192(value);
    }

    /**
     * @dev Returns the downcasted uint184 from uint256, reverting on
     * overflow (when the input is greater than largest uint184).
     *
     * Counterpart to Solidity's `uint184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toUint184(uint256 value) internal pure returns (uint184) {
        if (value > type(uint184).max) {
            revert SafeCastOverflowedUintDowncast(184, value);
        }
        return uint184(value);
    }

    /**
     * @dev Returns the downcasted uint176 from uint256, reverting on
     * overflow (when the input is greater than largest uint176).
     *
     * Counterpart to Solidity's `uint176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toUint176(uint256 value) internal pure returns (uint176) {
        if (value > type(uint176).max) {
            revert SafeCastOverflowedUintDowncast(176, value);
        }
        return uint176(value);
    }

    /**
     * @dev Returns the downcasted uint168 from uint256, reverting on
     * overflow (when the input is greater than largest uint168).
     *
     * Counterpart to Solidity's `uint168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toUint168(uint256 value) internal pure returns (uint168) {
        if (value > type(uint168).max) {
            revert SafeCastOverflowedUintDowncast(168, value);
        }
        return uint168(value);
    }

    /**
     * @dev Returns the downcasted uint160 from uint256, reverting on
     * overflow (when the input is greater than largest uint160).
     *
     * Counterpart to Solidity's `uint160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toUint160(uint256 value) internal pure returns (uint160) {
        if (value > type(uint160).max) {
            revert SafeCastOverflowedUintDowncast(160, value);
        }
        return uint160(value);
    }

    /**
     * @dev Returns the downcasted uint152 from uint256, reverting on
     * overflow (when the input is greater than largest uint152).
     *
     * Counterpart to Solidity's `uint152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toUint152(uint256 value) internal pure returns (uint152) {
        if (value > type(uint152).max) {
            revert SafeCastOverflowedUintDowncast(152, value);
        }
        return uint152(value);
    }

    /**
     * @dev Returns the downcasted uint144 from uint256, reverting on
     * overflow (when the input is greater than largest uint144).
     *
     * Counterpart to Solidity's `uint144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toUint144(uint256 value) internal pure returns (uint144) {
        if (value > type(uint144).max) {
            revert SafeCastOverflowedUintDowncast(144, value);
        }
        return uint144(value);
    }

    /**
     * @dev Returns the downcasted uint136 from uint256, reverting on
     * overflow (when the input is greater than largest uint136).
     *
     * Counterpart to Solidity's `uint136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toUint136(uint256 value) internal pure returns (uint136) {
        if (value > type(uint136).max) {
            revert SafeCastOverflowedUintDowncast(136, value);
        }
        return uint136(value);
    }

    /**
     * @dev Returns the downcasted uint128 from uint256, reverting on
     * overflow (when the input is greater than largest uint128).
     *
     * Counterpart to Solidity's `uint128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toUint128(uint256 value) internal pure returns (uint128) {
        if (value > type(uint128).max) {
            revert SafeCastOverflowedUintDowncast(128, value);
        }
        return uint128(value);
    }

    /**
     * @dev Returns the downcasted uint120 from uint256, reverting on
     * overflow (when the input is greater than largest uint120).
     *
     * Counterpart to Solidity's `uint120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toUint120(uint256 value) internal pure returns (uint120) {
        if (value > type(uint120).max) {
            revert SafeCastOverflowedUintDowncast(120, value);
        }
        return uint120(value);
    }

    /**
     * @dev Returns the downcasted uint112 from uint256, reverting on
     * overflow (when the input is greater than largest uint112).
     *
     * Counterpart to Solidity's `uint112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toUint112(uint256 value) internal pure returns (uint112) {
        if (value > type(uint112).max) {
            revert SafeCastOverflowedUintDowncast(112, value);
        }
        return uint112(value);
    }

    /**
     * @dev Returns the downcasted uint104 from uint256, reverting on
     * overflow (when the input is greater than largest uint104).
     *
     * Counterpart to Solidity's `uint104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toUint104(uint256 value) internal pure returns (uint104) {
        if (value > type(uint104).max) {
            revert SafeCastOverflowedUintDowncast(104, value);
        }
        return uint104(value);
    }

    /**
     * @dev Returns the downcasted uint96 from uint256, reverting on
     * overflow (when the input is greater than largest uint96).
     *
     * Counterpart to Solidity's `uint96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toUint96(uint256 value) internal pure returns (uint96) {
        if (value > type(uint96).max) {
            revert SafeCastOverflowedUintDowncast(96, value);
        }
        return uint96(value);
    }

    /**
     * @dev Returns the downcasted uint88 from uint256, reverting on
     * overflow (when the input is greater than largest uint88).
     *
     * Counterpart to Solidity's `uint88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toUint88(uint256 value) internal pure returns (uint88) {
        if (value > type(uint88).max) {
            revert SafeCastOverflowedUintDowncast(88, value);
        }
        return uint88(value);
    }

    /**
     * @dev Returns the downcasted uint80 from uint256, reverting on
     * overflow (when the input is greater than largest uint80).
     *
     * Counterpart to Solidity's `uint80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toUint80(uint256 value) internal pure returns (uint80) {
        if (value > type(uint80).max) {
            revert SafeCastOverflowedUintDowncast(80, value);
        }
        return uint80(value);
    }

    /**
     * @dev Returns the downcasted uint72 from uint256, reverting on
     * overflow (when the input is greater than largest uint72).
     *
     * Counterpart to Solidity's `uint72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toUint72(uint256 value) internal pure returns (uint72) {
        if (value > type(uint72).max) {
            revert SafeCastOverflowedUintDowncast(72, value);
        }
        return uint72(value);
    }

    /**
     * @dev Returns the downcasted uint64 from uint256, reverting on
     * overflow (when the input is greater than largest uint64).
     *
     * Counterpart to Solidity's `uint64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toUint64(uint256 value) internal pure returns (uint64) {
        if (value > type(uint64).max) {
            revert SafeCastOverflowedUintDowncast(64, value);
        }
        return uint64(value);
    }

    /**
     * @dev Returns the downcasted uint56 from uint256, reverting on
     * overflow (when the input is greater than largest uint56).
     *
     * Counterpart to Solidity's `uint56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toUint56(uint256 value) internal pure returns (uint56) {
        if (value > type(uint56).max) {
            revert SafeCastOverflowedUintDowncast(56, value);
        }
        return uint56(value);
    }

    /**
     * @dev Returns the downcasted uint48 from uint256, reverting on
     * overflow (when the input is greater than largest uint48).
     *
     * Counterpart to Solidity's `uint48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toUint48(uint256 value) internal pure returns (uint48) {
        if (value > type(uint48).max) {
            revert SafeCastOverflowedUintDowncast(48, value);
        }
        return uint48(value);
    }

    /**
     * @dev Returns the downcasted uint40 from uint256, reverting on
     * overflow (when the input is greater than largest uint40).
     *
     * Counterpart to Solidity's `uint40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toUint40(uint256 value) internal pure returns (uint40) {
        if (value > type(uint40).max) {
            revert SafeCastOverflowedUintDowncast(40, value);
        }
        return uint40(value);
    }

    /**
     * @dev Returns the downcasted uint32 from uint256, reverting on
     * overflow (when the input is greater than largest uint32).
     *
     * Counterpart to Solidity's `uint32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toUint32(uint256 value) internal pure returns (uint32) {
        if (value > type(uint32).max) {
            revert SafeCastOverflowedUintDowncast(32, value);
        }
        return uint32(value);
    }

    /**
     * @dev Returns the downcasted uint24 from uint256, reverting on
     * overflow (when the input is greater than largest uint24).
     *
     * Counterpart to Solidity's `uint24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toUint24(uint256 value) internal pure returns (uint24) {
        if (value > type(uint24).max) {
            revert SafeCastOverflowedUintDowncast(24, value);
        }
        return uint24(value);
    }

    /**
     * @dev Returns the downcasted uint16 from uint256, reverting on
     * overflow (when the input is greater than largest uint16).
     *
     * Counterpart to Solidity's `uint16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toUint16(uint256 value) internal pure returns (uint16) {
        if (value > type(uint16).max) {
            revert SafeCastOverflowedUintDowncast(16, value);
        }
        return uint16(value);
    }

    /**
     * @dev Returns the downcasted uint8 from uint256, reverting on
     * overflow (when the input is greater than largest uint8).
     *
     * Counterpart to Solidity's `uint8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toUint8(uint256 value) internal pure returns (uint8) {
        if (value > type(uint8).max) {
            revert SafeCastOverflowedUintDowncast(8, value);
        }
        return uint8(value);
    }

    /**
     * @dev Converts a signed int256 into an unsigned uint256.
     *
     * Requirements:
     *
     * - input must be greater than or equal to 0.
     */
    function toUint256(int256 value) internal pure returns (uint256) {
        if (value < 0) {
            revert SafeCastOverflowedIntToUint(value);
        }
        return uint256(value);
    }

    /**
     * @dev Returns the downcasted int248 from int256, reverting on
     * overflow (when the input is less than smallest int248 or
     * greater than largest int248).
     *
     * Counterpart to Solidity's `int248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toInt248(int256 value) internal pure returns (int248 downcasted) {
        downcasted = int248(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(248, value);
        }
    }

    /**
     * @dev Returns the downcasted int240 from int256, reverting on
     * overflow (when the input is less than smallest int240 or
     * greater than largest int240).
     *
     * Counterpart to Solidity's `int240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toInt240(int256 value) internal pure returns (int240 downcasted) {
        downcasted = int240(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(240, value);
        }
    }

    /**
     * @dev Returns the downcasted int232 from int256, reverting on
     * overflow (when the input is less than smallest int232 or
     * greater than largest int232).
     *
     * Counterpart to Solidity's `int232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toInt232(int256 value) internal pure returns (int232 downcasted) {
        downcasted = int232(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(232, value);
        }
    }

    /**
     * @dev Returns the downcasted int224 from int256, reverting on
     * overflow (when the input is less than smallest int224 or
     * greater than largest int224).
     *
     * Counterpart to Solidity's `int224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toInt224(int256 value) internal pure returns (int224 downcasted) {
        downcasted = int224(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(224, value);
        }
    }

    /**
     * @dev Returns the downcasted int216 from int256, reverting on
     * overflow (when the input is less than smallest int216 or
     * greater than largest int216).
     *
     * Counterpart to Solidity's `int216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toInt216(int256 value) internal pure returns (int216 downcasted) {
        downcasted = int216(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(216, value);
        }
    }

    /**
     * @dev Returns the downcasted int208 from int256, reverting on
     * overflow (when the input is less than smallest int208 or
     * greater than largest int208).
     *
     * Counterpart to Solidity's `int208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toInt208(int256 value) internal pure returns (int208 downcasted) {
        downcasted = int208(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(208, value);
        }
    }

    /**
     * @dev Returns the downcasted int200 from int256, reverting on
     * overflow (when the input is less than smallest int200 or
     * greater than largest int200).
     *
     * Counterpart to Solidity's `int200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toInt200(int256 value) internal pure returns (int200 downcasted) {
        downcasted = int200(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(200, value);
        }
    }

    /**
     * @dev Returns the downcasted int192 from int256, reverting on
     * overflow (when the input is less than smallest int192 or
     * greater than largest int192).
     *
     * Counterpart to Solidity's `int192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toInt192(int256 value) internal pure returns (int192 downcasted) {
        downcasted = int192(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(192, value);
        }
    }

    /**
     * @dev Returns the downcasted int184 from int256, reverting on
     * overflow (when the input is less than smallest int184 or
     * greater than largest int184).
     *
     * Counterpart to Solidity's `int184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toInt184(int256 value) internal pure returns (int184 downcasted) {
        downcasted = int184(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(184, value);
        }
    }

    /**
     * @dev Returns the downcasted int176 from int256, reverting on
     * overflow (when the input is less than smallest int176 or
     * greater than largest int176).
     *
     * Counterpart to Solidity's `int176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toInt176(int256 value) internal pure returns (int176 downcasted) {
        downcasted = int176(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(176, value);
        }
    }

    /**
     * @dev Returns the downcasted int168 from int256, reverting on
     * overflow (when the input is less than smallest int168 or
     * greater than largest int168).
     *
     * Counterpart to Solidity's `int168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toInt168(int256 value) internal pure returns (int168 downcasted) {
        downcasted = int168(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(168, value);
        }
    }

    /**
     * @dev Returns the downcasted int160 from int256, reverting on
     * overflow (when the input is less than smallest int160 or
     * greater than largest int160).
     *
     * Counterpart to Solidity's `int160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toInt160(int256 value) internal pure returns (int160 downcasted) {
        downcasted = int160(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(160, value);
        }
    }

    /**
     * @dev Returns the downcasted int152 from int256, reverting on
     * overflow (when the input is less than smallest int152 or
     * greater than largest int152).
     *
     * Counterpart to Solidity's `int152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toInt152(int256 value) internal pure returns (int152 downcasted) {
        downcasted = int152(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(152, value);
        }
    }

    /**
     * @dev Returns the downcasted int144 from int256, reverting on
     * overflow (when the input is less than smallest int144 or
     * greater than largest int144).
     *
     * Counterpart to Solidity's `int144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toInt144(int256 value) internal pure returns (int144 downcasted) {
        downcasted = int144(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(144, value);
        }
    }

    /**
     * @dev Returns the downcasted int136 from int256, reverting on
     * overflow (when the input is less than smallest int136 or
     * greater than largest int136).
     *
     * Counterpart to Solidity's `int136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toInt136(int256 value) internal pure returns (int136 downcasted) {
        downcasted = int136(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(136, value);
        }
    }

    /**
     * @dev Returns the downcasted int128 from int256, reverting on
     * overflow (when the input is less than smallest int128 or
     * greater than largest int128).
     *
     * Counterpart to Solidity's `int128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toInt128(int256 value) internal pure returns (int128 downcasted) {
        downcasted = int128(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(128, value);
        }
    }

    /**
     * @dev Returns the downcasted int120 from int256, reverting on
     * overflow (when the input is less than smallest int120 or
     * greater than largest int120).
     *
     * Counterpart to Solidity's `int120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toInt120(int256 value) internal pure returns (int120 downcasted) {
        downcasted = int120(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(120, value);
        }
    }

    /**
     * @dev Returns the downcasted int112 from int256, reverting on
     * overflow (when the input is less than smallest int112 or
     * greater than largest int112).
     *
     * Counterpart to Solidity's `int112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toInt112(int256 value) internal pure returns (int112 downcasted) {
        downcasted = int112(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(112, value);
        }
    }

    /**
     * @dev Returns the downcasted int104 from int256, reverting on
     * overflow (when the input is less than smallest int104 or
     * greater than largest int104).
     *
     * Counterpart to Solidity's `int104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toInt104(int256 value) internal pure returns (int104 downcasted) {
        downcasted = int104(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(104, value);
        }
    }

    /**
     * @dev Returns the downcasted int96 from int256, reverting on
     * overflow (when the input is less than smallest int96 or
     * greater than largest int96).
     *
     * Counterpart to Solidity's `int96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toInt96(int256 value) internal pure returns (int96 downcasted) {
        downcasted = int96(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(96, value);
        }
    }

    /**
     * @dev Returns the downcasted int88 from int256, reverting on
     * overflow (when the input is less than smallest int88 or
     * greater than largest int88).
     *
     * Counterpart to Solidity's `int88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toInt88(int256 value) internal pure returns (int88 downcasted) {
        downcasted = int88(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(88, value);
        }
    }

    /**
     * @dev Returns the downcasted int80 from int256, reverting on
     * overflow (when the input is less than smallest int80 or
     * greater than largest int80).
     *
     * Counterpart to Solidity's `int80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toInt80(int256 value) internal pure returns (int80 downcasted) {
        downcasted = int80(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(80, value);
        }
    }

    /**
     * @dev Returns the downcasted int72 from int256, reverting on
     * overflow (when the input is less than smallest int72 or
     * greater than largest int72).
     *
     * Counterpart to Solidity's `int72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toInt72(int256 value) internal pure returns (int72 downcasted) {
        downcasted = int72(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(72, value);
        }
    }

    /**
     * @dev Returns the downcasted int64 from int256, reverting on
     * overflow (when the input is less than smallest int64 or
     * greater than largest int64).
     *
     * Counterpart to Solidity's `int64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toInt64(int256 value) internal pure returns (int64 downcasted) {
        downcasted = int64(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(64, value);
        }
    }

    /**
     * @dev Returns the downcasted int56 from int256, reverting on
     * overflow (when the input is less than smallest int56 or
     * greater than largest int56).
     *
     * Counterpart to Solidity's `int56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toInt56(int256 value) internal pure returns (int56 downcasted) {
        downcasted = int56(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(56, value);
        }
    }

    /**
     * @dev Returns the downcasted int48 from int256, reverting on
     * overflow (when the input is less than smallest int48 or
     * greater than largest int48).
     *
     * Counterpart to Solidity's `int48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toInt48(int256 value) internal pure returns (int48 downcasted) {
        downcasted = int48(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(48, value);
        }
    }

    /**
     * @dev Returns the downcasted int40 from int256, reverting on
     * overflow (when the input is less than smallest int40 or
     * greater than largest int40).
     *
     * Counterpart to Solidity's `int40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toInt40(int256 value) internal pure returns (int40 downcasted) {
        downcasted = int40(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(40, value);
        }
    }

    /**
     * @dev Returns the downcasted int32 from int256, reverting on
     * overflow (when the input is less than smallest int32 or
     * greater than largest int32).
     *
     * Counterpart to Solidity's `int32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toInt32(int256 value) internal pure returns (int32 downcasted) {
        downcasted = int32(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(32, value);
        }
    }

    /**
     * @dev Returns the downcasted int24 from int256, reverting on
     * overflow (when the input is less than smallest int24 or
     * greater than largest int24).
     *
     * Counterpart to Solidity's `int24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toInt24(int256 value) internal pure returns (int24 downcasted) {
        downcasted = int24(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(24, value);
        }
    }

    /**
     * @dev Returns the downcasted int16 from int256, reverting on
     * overflow (when the input is less than smallest int16 or
     * greater than largest int16).
     *
     * Counterpart to Solidity's `int16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toInt16(int256 value) internal pure returns (int16 downcasted) {
        downcasted = int16(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(16, value);
        }
    }

    /**
     * @dev Returns the downcasted int8 from int256, reverting on
     * overflow (when the input is less than smallest int8 or
     * greater than largest int8).
     *
     * Counterpart to Solidity's `int8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toInt8(int256 value) internal pure returns (int8 downcasted) {
        downcasted = int8(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(8, value);
        }
    }

    /**
     * @dev Converts an unsigned uint256 into a signed int256.
     *
     * Requirements:
     *
     * - input must be less than or equal to maxInt256.
     */
    function toInt256(uint256 value) internal pure returns (int256) {
        // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
        if (value > uint256(type(int256).max)) {
            revert SafeCastOverflowedUintToInt(value);
        }
        return int256(value);
    }

    /**
     * @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
     */
    function toUint(bool b) internal pure returns (uint256 u) {
        assembly ("memory-safe") {
            u := iszero(iszero(b))
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/TransientSlot.sol)
// This file was procedurally generated from scripts/generate/templates/TransientSlot.js.

pragma solidity ^0.8.24;

/**
 * @dev Library for reading and writing value-types to specific transient storage slots.
 *
 * Transient slots are often used to store temporary values that are removed after the current transaction.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 *  * Example reading and writing values using transient storage:
 * ```solidity
 * contract Lock {
 *     using TransientSlot for *;
 *
 *     // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
 *     bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
 *
 *     modifier locked() {
 *         require(!_LOCK_SLOT.asBoolean().tload());
 *
 *         _LOCK_SLOT.asBoolean().tstore(true);
 *         _;
 *         _LOCK_SLOT.asBoolean().tstore(false);
 *     }
 * }
 * ```
 *
 * TIP: Consider using this library along with {SlotDerivation}.
 */
library TransientSlot {
    /**
     * @dev UDVT that represent a slot holding a address.
     */
    type AddressSlot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a AddressSlot.
     */
    function asAddress(bytes32 slot) internal pure returns (AddressSlot) {
        return AddressSlot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a bool.
     */
    type BooleanSlot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a BooleanSlot.
     */
    function asBoolean(bytes32 slot) internal pure returns (BooleanSlot) {
        return BooleanSlot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a bytes32.
     */
    type Bytes32Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Bytes32Slot.
     */
    function asBytes32(bytes32 slot) internal pure returns (Bytes32Slot) {
        return Bytes32Slot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a uint256.
     */
    type Uint256Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Uint256Slot.
     */
    function asUint256(bytes32 slot) internal pure returns (Uint256Slot) {
        return Uint256Slot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a int256.
     */
    type Int256Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Int256Slot.
     */
    function asInt256(bytes32 slot) internal pure returns (Int256Slot) {
        return Int256Slot.wrap(slot);
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(AddressSlot slot) internal view returns (address value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(AddressSlot slot, address value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(BooleanSlot slot) internal view returns (bool value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(BooleanSlot slot, bool value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Bytes32Slot slot) internal view returns (bytes32 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Bytes32Slot slot, bytes32 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Uint256Slot slot) internal view returns (uint256 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Uint256Slot slot, uint256 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Int256Slot slot) internal view returns (int256 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Int256Slot slot, int256 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }
}

File 15 of 16 : IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC165.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../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);
}

Settings
{
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "chainlink/=lib/chainlink/",
    "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/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "metadata": {
    "useLiteralContent": false,
    "bytecodeHash": "ipfs",
    "appendCBOR": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "cancun",
  "viaIR": true,
  "libraries": {}
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"uint256","name":"minTotalBid_","type":"uint256"},{"internalType":"uint256","name":"minBidIncrement_","type":"uint256"},{"internalType":"uint256","name":"maxRound_","type":"uint256"},{"internalType":"uint256","name":"firstRoundStart_","type":"uint256"},{"internalType":"uint256","name":"firstRoundEnd_","type":"uint256"},{"internalType":"uint256","name":"roundDuration_","type":"uint256"},{"internalType":"uint256","name":"roundLegendaryQuota_","type":"uint256"},{"internalType":"uint256","name":"roundEpicQuota_","type":"uint256"},{"internalType":"uint256","name":"roundEpicQuotaNumerator_","type":"uint256"},{"internalType":"uint256","name":"roundRareQuota_","type":"uint256"},{"internalType":"uint256","name":"roundRareQuotaNumerator_","type":"uint256"},{"internalType":"contract ITetherToken","name":"tether_","type":"address"},{"internalType":"address","name":"initialOwner_","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"BadRoundTimeRange","type":"error"},{"inputs":[{"internalType":"uint256","name":"minBid","type":"uint256"}],"name":"BidIncrementTooLow","type":"error"},{"inputs":[],"name":"MarathonOver","type":"error"},{"inputs":[{"internalType":"uint256","name":"maxRound","type":"uint256"}],"name":"MaxRoundIsLocked","type":"error"},{"inputs":[{"internalType":"uint256","name":"currentRound","type":"uint256"}],"name":"MaxRoundLessThanCurrent","type":"error"},{"inputs":[{"internalType":"uint256","name":"maxRound","type":"uint256"}],"name":"MinBidIncrementIsLocked","type":"error"},{"inputs":[],"name":"MinBidIsZero","type":"error"},{"inputs":[],"name":"OutOfRangeAccess","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"inputs":[{"internalType":"uint256","name":"quotaNumerator_","type":"uint256"}],"name":"QuotaNumeratorExceedsDenominator","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[],"name":"RoundDurationIsZero","type":"error"},{"inputs":[{"internalType":"uint256","name":"endAt","type":"uint256"}],"name":"RoundNotFinished","type":"error"},{"inputs":[],"name":"RoundNotStarted","type":"error"},{"inputs":[],"name":"RoundOutOfBounds","type":"error"},{"inputs":[{"internalType":"uint256","name":"currentTimestamp","type":"uint256"}],"name":"ScheduleInThePast","type":"error"},{"inputs":[{"internalType":"uint256","name":"minBid","type":"uint256"}],"name":"TotalBidTooLow","type":"error"},{"inputs":[],"name":"UnknownRarity","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"player","type":"address"},{"indexed":true,"internalType":"uint256","name":"round","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"bidNonce","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"increment","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalBid","type":"uint256"}],"name":"BidIncreased","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"round","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"start","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"end","type":"uint256"}],"name":"CurrentRoundRescheduled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"oldMaxRound","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"newMaxRound","type":"uint256"}],"name":"MaxRoundChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"maxRound","type":"uint256"}],"name":"MaxRoundLocked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"oldMinBidIncrement","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"newMinBidIncrement","type":"uint256"}],"name":"MinBidIncrementChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"minBidIncrement","type":"uint256"}],"name":"MinBidIncrementLocked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdrawn","type":"event"},{"inputs":[],"name":"ENVIRONMENT","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROUND_QUOTA_DENOMINATOR","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"round_","type":"uint256"}],"name":"bidCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"player_","type":"address"},{"internalType":"uint256","name":"round_","type":"uint256"}],"name":"bidOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"currentOrUpcomingRoundSchedule","outputs":[{"components":[{"internalType":"uint256","name":"round","type":"uint256"},{"internalType":"uint256","name":"start","type":"uint256"},{"internalType":"uint256","name":"end","type":"uint256"}],"internalType":"struct MagicAlchemyMarathon.RoundSchedule","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"currentRound","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"round_","type":"uint256"},{"internalType":"enum MagicAlchemyRarity","name":"rarity_","type":"uint8"}],"name":"flaskFinalCountFor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"bidIncrement_","type":"uint256"}],"name":"increaseBid","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"lockMaxRound","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"lockMinBidIncrement","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"maxRound","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxRoundIsLocked","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minBidIncrement","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minTotalBid","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"round_","type":"uint256"},{"internalType":"uint256","name":"index_","type":"uint256"}],"name":"playerWithBidByIndex","outputs":[{"components":[{"internalType":"address","name":"player","type":"address"},{"internalType":"uint256","name":"bid","type":"uint256"},{"internalType":"uint256","name":"nonce","type":"uint256"}],"internalType":"struct MagicAlchemyMarathon.PlayerWithBid","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"round_","type":"uint256"},{"internalType":"uint256","name":"index_","type":"uint256"},{"internalType":"uint256","name":"count_","type":"uint256"}],"name":"playersWithBidFromIndex","outputs":[{"components":[{"internalType":"address","name":"player","type":"address"},{"internalType":"uint256","name":"bid","type":"uint256"},{"internalType":"uint256","name":"nonce","type":"uint256"}],"internalType":"struct MagicAlchemyMarathon.PlayerWithBid[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"start_","type":"uint256"},{"internalType":"uint256","name":"end_","type":"uint256"}],"name":"rescheduleCurrentOrUpcomingRound","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"maxRound_","type":"uint256"}],"name":"setMaxRound","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"minBidIncrement_","type":"uint256"}],"name":"setMinBidIncrement","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"tether","outputs":[{"internalType":"contract ITetherToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]

60806040523461030557604051601f61140338819003918201601f19168301916001600160401b038311848410176102c0578084926101a094604052833981010312610305578051906020810151604082015192606083015190608084015160a085015160c08601519060e08701519261010088015195610120890151956101408a0151986101608b01519a60018060a01b038c16809c036103055761018001516001600160a01b038116908190036103055780156102f2575f80546001600160a01b03198116831782556001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09080a35f60065561ffff196005541660055580156102e357600754816007557f6f62dd1ca47593f14621a41617ecace941fe55092de3bb3bf6b22392970616b75f80a380156102e357600855818110156102d457604051606081016001600160401b038111828210176102c057839160409182526001815283602082015201526001600255806003558160045560017f89a2bc2a3b4c0d2f928ecf9033289a146b3eb6468185c58f13aa3e48f8de78005f80a46004548042105f1461026b575060015b808910610259575087600955604051975f197f2cd714ff7997e41a952a314e83fd55eb969e5489b17b32805c356de06166cb845f80a3801561024a57600a55600b55600c55600e55612710811161023857600d55612710811161023857600f55600180546001600160a01b0319169190911790556110f9908161030a8239f35b63477fdfef60e11b5f5260045260245ffd5b631fd703f760e31b5f5260045ffd5b6307b682b960e21b5f5260045260245ffd5b420342811161029857600a549081156102ac570480600101908160011161029857600201809111156101b8575b634e487b7160e01b5f52601160045260245ffd5b634e487b7160e01b5f52601260045260245ffd5b634e487b7160e01b5f52604160045260245ffd5b6310e2e5a360e21b5f5260045ffd5b6349ad3f4160e01b5f5260045ffd5b631e4fbdf760e01b5f525f60045260245ffd5b5f80fdfe60806040526004361015610011575f80fd5b5f5f3560e01c806270c53714610a7c578062f714ce146109a257806301ffc9a714610963578063335b115e1461094557806337fe5dc0146109275780633ddcb3a3146108d5578063428a96d2146108605780634e270358146107df57806355ee09d71461075d5780635efc071a146107345780636c29b66f146107115780636c63bf961461067d5780636d7acff814610622578063715018a6146105c85780638a19c8bc146105ad5780638da5cb5b1461058657806393c2f48214610568578063a3264e8e14610417578063b9a958b4146103ed578063ce0442a5146103d0578063e1d6785c14610321578063e9b38ae01461026c578063eda9a63f1461021c578063f2fde38b146101965763f9faf1b41461012b575f80fd5b346101935760403660031901126101935760409061016861014a610c2e565b602435835260106020528383206001600160a01b0390911690610fb6565b901561018c57604081901c915067ffffffffffffffff165b82519182526020820152f35b5080610180565b80fd5b5034610193576020366003190112610193576101b0610c2e565b6101b8610eac565b6001600160a01b031680156102085781546001600160a01b03198116821783556001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b631e4fbdf760e01b82526004829052602482fd5b5034610193578060031936011261019357610235610eac565b600160ff1960055416176005556009547fbca8afaa174e45e7689c0bc787fd11b0424ddc414fb9685fc33b5ea002e2e6718280a280f35b50346101935761027b36610c44565b90610284610eac565b80421161030e57610293610f9e565b60095481116102ff57828210156102f05782604080516102b281610c5a565b83815284602082015201528060025581600355826004557f89a2bc2a3b4c0d2f928ecf9033289a146b3eb6468185c58f13aa3e48f8de78008480a480f35b6310e2e5a360e21b8452600484fd5b638a6b78ef60e01b8452600484fd5b635e80e1c960e11b835242600452602483fd5b50346101935760403660031901126101935760043560243591600483101561019357811580156103c5575b6103b657610358610f01565b805183116103a7578051831461037d575b60206103758585610d86565b604051908152f35b60208101514211156103a757604001518042116103695763b1749e9760e01b825260045260249150fd5b6323a71b8760e21b8252600482fd5b63d39a184960e01b8152600490fd5b50600954821161034c565b503461019357806003193601126101935760206040516127108152f35b50346101935760203660031901126101935760406020916004358152601083522054604051908152f35b50346101935760603660031901126101935760043560443560243561043b82610d26565b926104496040519485610c8a565b828452601f1961045884610d26565b01855b818110610551575050845b8381106104da578486604051918291602083016020845282518091526020604085019301915b81811061049a575050500390f35b9193509160206060826104cc60019488516040809160018060a01b038151168452602081015160208501520151910152565b01940191019184939261048c565b60019082875260106020526105146104ff604089206104f98488610cac565b90610ed2565b9091604082901c9167ffffffffffffffff1690565b906040519261052284610c5a565b858060a01b031683526020830152604082015261053f8288610d3e565b5261054a8187610d3e565b5001610466565b60209061055c610ccd565b8282890101520161045b565b50346101935780600319360112610193576020600954604051908152f35b5034610193578060031936011261019357546040516001600160a01b039091168152602090f35b50346101935780600319360112610193576020610375610ceb565b50346101935780600319360112610193576105e1610eac565b80546001600160a01b03198116825581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b503461019357806003193601126101935761063b610ccd565b50610644610f01565b9081516009541061066e576060826040805191805183526020810151602084015201516040820152f35b638a6b78ef60e01b8152600490fd5b50346101935760203660031901126101935760043561069a610eac565b60ff600554166106fc576106ac610f9e565b600954908181116102ff578083106106ea5750816009557f2cd714ff7997e41a952a314e83fd55eb969e5489b17b32805c356de06166cb848380a380f35b6307b682b960e21b8452600452602483fd5b600954632306779360e11b8352600452602482fd5b5034610193578060031936011261019357602060ff600554166040519015158152f35b50346101935780600319360112610193576001546040516001600160a01b039091168152602090f35b50346101935760203660031901126101935760043561077a610eac565b60ff60055460081c166107ca5780156107bb57600754816007557f6f62dd1ca47593f14621a41617ecace941fe55092de3bb3bf6b22392970616b78380a380f35b6349ad3f4160e01b8252600482fd5b600754633943f9b160e01b8352600452602482fd5b50346101935780600319360112610193576040516040810181811067ffffffffffffffff82111761084c579060409182526004815260208101631c1c9bd960e21b81528251938492602084525180928160208601528585015e828201840152601f01601f19168101030190f35b634e487b7160e01b83526041600452602483fd5b5034610193576108926104ff606092604061087a36610c44565b9290610884610ccd565b508152601060205220610ed2565b90604051926108a084610c5a565b6001600160a01b0316808452602080850192835260409485019384528451918252915191810191909152905191810191909152f35b50346101935780600319360112610193576108ee610eac565b61010061ff001960055416176005556007547fef24f5921fc4da5ed2fe2c113b96c6cc11ececd7533b2247c888ba52610413588280a280f35b50346101935780600319360112610193576020600854604051908152f35b50346101935780600319360112610193576020600754604051908152f35b50346101935760203660031901126101935760043563ffffffff60e01b811680910361099e576040516338759e1760e21b9091148152602090f35b5080fd5b5034610193576040366003190112610193576004356024356001600160a01b03811690819003610a6d576109d4610e77565b6109dc610eac565b60015483906001600160a01b0316803b1561099e5781809160446040518094819363a9059cbb60e01b83528860048401528960248401525af18015610a7157610a58575b50807f7084f5476618d8e60b11ef0d7d3f06914655adb8793e28ff7f018d4c76d505d591a3805f5160206110a45f395f51905f525d80f35b81610a6291610c8a565b610a6d57825f610a20565b8280fd5b6040513d84823e3d90fd5b5034610bfe576020366003190112610bfe57600435610a99610e77565b600754808210610c1c5750610aac610ceb565b805f52601060205260405f209282610ac43386610fb6565b919015610c1457610ad79160401c610cac565b925b600854808510610c0257506001546001600160a01b0316803b15610bfe575f80916064604051809481936323b872dd60e01b83523360048401523060248401528760448401525af18015610bf357610bde575b506006545f198114610bca5760409567ffffffffffffffff80806001610b789501806006553388526002850160205216161667ffffffffffffffff1987891b161787852055339061103d565b5060065492839186519081528560208201527f2b4e4880c44f105569fae08aef7bff9e0d262af0a841b7be1b9bdf092c94863c873392a45f5160206110a45f395f51905f525d82519182526020820152f35b634e487b7160e01b83526011600452602483fd5b610beb9192505f90610c8a565b5f905f610b2c565b6040513d5f823e3d90fd5b5f80fd5b63d4764d3560e01b5f5260045260245ffd5b905092610ad9565b633ec3def560e11b5f5260045260245ffd5b600435906001600160a01b0382168203610bfe57565b6040906003190112610bfe576004359060243590565b6060810190811067ffffffffffffffff821117610c7657604052565b634e487b7160e01b5f52604160045260245ffd5b90601f8019910116810190811067ffffffffffffffff821117610c7657604052565b91908201809211610cb957565b634e487b7160e01b5f52601160045260245ffd5b60405190610cda82610c5a565b5f6040838281528260208201520152565b6003544210610d1757610cfc610f9e565b6009548111610d085790565b638a6b78ef60e01b5f5260045ffd5b6323a71b8760e21b5f5260045ffd5b67ffffffffffffffff8111610c765760051b60200190565b8051821015610d525760209160051b010190565b634e487b7160e01b5f52603260045260245ffd5b91908203918211610cb957565b81810292918115918404141715610cb957565b5f52601060205260405f205490600b548280821091180282186004821015610e635760038214610e5d57610dba9083610d66565b600c54612710610dcc600d5486610d73565b0490818082119118021890808211610e55575b60028314610e4e5790610df191610d66565b91612710610e05600e5492600f5490610d73565b0490818082119118021890828211610e46575b60018114610e405715610e345763e489255960e01b5f5260045ffd5b610e3d91610d66565b90565b50905090565b829150610e18565b5091505090565b905080610ddf565b91505090565b634e487b7160e01b5f52602160045260245ffd5b5f5160206110a45f395f51905f525c610e9d5760015f5160206110a45f395f51905f525d565b633ee5aeb560e01b5f5260045ffd5b5f546001600160a01b03163303610ebf57565b63118cdaa760e01b5f523360045260245ffd5b9190610ee060029184611028565b90549060031b1c92835f520160205260405f20549160018060a01b03169190565b610f09610ccd565b506004548042105f14610f3b5750604051610f2381610c5a565b60025481526003546020820152600454604082015290565b610f4d610f46610ff6565b9142610d66565b600a54908115610f8a57610f6682610f6d920642610d66565b9182610cac565b9060405192610f7b84610c5a565b83526020830152604082015290565b634e487b7160e01b5f52601260045260245ffd5b600454421015610fae5760025490565b610e3d610ff6565b9190805f526002830160205260405f20549283155f14610fee57610fe99293506001915f520160205260405f2054151590565b905f90565b505060019190565b60025461100560045442610d66565b90600a548015610f8a5761101a920490610cac565b60018101809111610cb95790565b8054821015610d52575f5260205f2001905f90565b5f82815260018201602052604090205461109d5780549068010000000000000000821015610c7657611076826001809401835582611028565b81549060031b9085821b915f19901b19161790558054925f520160205260405f2055600190565b50505f9056fe9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00a264697066735822122047bfc41ee37781956d8392733bfb28e527a96ac94bb3311f7805ce92c4d1d0a764736f6c634300081c0033000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000680b6b3000000000000000000000000000000000000000000000000000000000680ba3700000000000000000000000000000000000000000000000000000000000000e100000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000384000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000898000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f00000000000000000000000029c9613590b3adac1007c9f399a2cafa12ab8389

Deployed Bytecode

0x60806040526004361015610011575f80fd5b5f5f3560e01c806270c53714610a7c578062f714ce146109a257806301ffc9a714610963578063335b115e1461094557806337fe5dc0146109275780633ddcb3a3146108d5578063428a96d2146108605780634e270358146107df57806355ee09d71461075d5780635efc071a146107345780636c29b66f146107115780636c63bf961461067d5780636d7acff814610622578063715018a6146105c85780638a19c8bc146105ad5780638da5cb5b1461058657806393c2f48214610568578063a3264e8e14610417578063b9a958b4146103ed578063ce0442a5146103d0578063e1d6785c14610321578063e9b38ae01461026c578063eda9a63f1461021c578063f2fde38b146101965763f9faf1b41461012b575f80fd5b346101935760403660031901126101935760409061016861014a610c2e565b602435835260106020528383206001600160a01b0390911690610fb6565b901561018c57604081901c915067ffffffffffffffff165b82519182526020820152f35b5080610180565b80fd5b5034610193576020366003190112610193576101b0610c2e565b6101b8610eac565b6001600160a01b031680156102085781546001600160a01b03198116821783556001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b631e4fbdf760e01b82526004829052602482fd5b5034610193578060031936011261019357610235610eac565b600160ff1960055416176005556009547fbca8afaa174e45e7689c0bc787fd11b0424ddc414fb9685fc33b5ea002e2e6718280a280f35b50346101935761027b36610c44565b90610284610eac565b80421161030e57610293610f9e565b60095481116102ff57828210156102f05782604080516102b281610c5a565b83815284602082015201528060025581600355826004557f89a2bc2a3b4c0d2f928ecf9033289a146b3eb6468185c58f13aa3e48f8de78008480a480f35b6310e2e5a360e21b8452600484fd5b638a6b78ef60e01b8452600484fd5b635e80e1c960e11b835242600452602483fd5b50346101935760403660031901126101935760043560243591600483101561019357811580156103c5575b6103b657610358610f01565b805183116103a7578051831461037d575b60206103758585610d86565b604051908152f35b60208101514211156103a757604001518042116103695763b1749e9760e01b825260045260249150fd5b6323a71b8760e21b8252600482fd5b63d39a184960e01b8152600490fd5b50600954821161034c565b503461019357806003193601126101935760206040516127108152f35b50346101935760203660031901126101935760406020916004358152601083522054604051908152f35b50346101935760603660031901126101935760043560443560243561043b82610d26565b926104496040519485610c8a565b828452601f1961045884610d26565b01855b818110610551575050845b8381106104da578486604051918291602083016020845282518091526020604085019301915b81811061049a575050500390f35b9193509160206060826104cc60019488516040809160018060a01b038151168452602081015160208501520151910152565b01940191019184939261048c565b60019082875260106020526105146104ff604089206104f98488610cac565b90610ed2565b9091604082901c9167ffffffffffffffff1690565b906040519261052284610c5a565b858060a01b031683526020830152604082015261053f8288610d3e565b5261054a8187610d3e565b5001610466565b60209061055c610ccd565b8282890101520161045b565b50346101935780600319360112610193576020600954604051908152f35b5034610193578060031936011261019357546040516001600160a01b039091168152602090f35b50346101935780600319360112610193576020610375610ceb565b50346101935780600319360112610193576105e1610eac565b80546001600160a01b03198116825581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b503461019357806003193601126101935761063b610ccd565b50610644610f01565b9081516009541061066e576060826040805191805183526020810151602084015201516040820152f35b638a6b78ef60e01b8152600490fd5b50346101935760203660031901126101935760043561069a610eac565b60ff600554166106fc576106ac610f9e565b600954908181116102ff578083106106ea5750816009557f2cd714ff7997e41a952a314e83fd55eb969e5489b17b32805c356de06166cb848380a380f35b6307b682b960e21b8452600452602483fd5b600954632306779360e11b8352600452602482fd5b5034610193578060031936011261019357602060ff600554166040519015158152f35b50346101935780600319360112610193576001546040516001600160a01b039091168152602090f35b50346101935760203660031901126101935760043561077a610eac565b60ff60055460081c166107ca5780156107bb57600754816007557f6f62dd1ca47593f14621a41617ecace941fe55092de3bb3bf6b22392970616b78380a380f35b6349ad3f4160e01b8252600482fd5b600754633943f9b160e01b8352600452602482fd5b50346101935780600319360112610193576040516040810181811067ffffffffffffffff82111761084c579060409182526004815260208101631c1c9bd960e21b81528251938492602084525180928160208601528585015e828201840152601f01601f19168101030190f35b634e487b7160e01b83526041600452602483fd5b5034610193576108926104ff606092604061087a36610c44565b9290610884610ccd565b508152601060205220610ed2565b90604051926108a084610c5a565b6001600160a01b0316808452602080850192835260409485019384528451918252915191810191909152905191810191909152f35b50346101935780600319360112610193576108ee610eac565b61010061ff001960055416176005556007547fef24f5921fc4da5ed2fe2c113b96c6cc11ececd7533b2247c888ba52610413588280a280f35b50346101935780600319360112610193576020600854604051908152f35b50346101935780600319360112610193576020600754604051908152f35b50346101935760203660031901126101935760043563ffffffff60e01b811680910361099e576040516338759e1760e21b9091148152602090f35b5080fd5b5034610193576040366003190112610193576004356024356001600160a01b03811690819003610a6d576109d4610e77565b6109dc610eac565b60015483906001600160a01b0316803b1561099e5781809160446040518094819363a9059cbb60e01b83528860048401528960248401525af18015610a7157610a58575b50807f7084f5476618d8e60b11ef0d7d3f06914655adb8793e28ff7f018d4c76d505d591a3805f5160206110a45f395f51905f525d80f35b81610a6291610c8a565b610a6d57825f610a20565b8280fd5b6040513d84823e3d90fd5b5034610bfe576020366003190112610bfe57600435610a99610e77565b600754808210610c1c5750610aac610ceb565b805f52601060205260405f209282610ac43386610fb6565b919015610c1457610ad79160401c610cac565b925b600854808510610c0257506001546001600160a01b0316803b15610bfe575f80916064604051809481936323b872dd60e01b83523360048401523060248401528760448401525af18015610bf357610bde575b506006545f198114610bca5760409567ffffffffffffffff80806001610b789501806006553388526002850160205216161667ffffffffffffffff1987891b161787852055339061103d565b5060065492839186519081528560208201527f2b4e4880c44f105569fae08aef7bff9e0d262af0a841b7be1b9bdf092c94863c873392a45f5160206110a45f395f51905f525d82519182526020820152f35b634e487b7160e01b83526011600452602483fd5b610beb9192505f90610c8a565b5f905f610b2c565b6040513d5f823e3d90fd5b5f80fd5b63d4764d3560e01b5f5260045260245ffd5b905092610ad9565b633ec3def560e11b5f5260045260245ffd5b600435906001600160a01b0382168203610bfe57565b6040906003190112610bfe576004359060243590565b6060810190811067ffffffffffffffff821117610c7657604052565b634e487b7160e01b5f52604160045260245ffd5b90601f8019910116810190811067ffffffffffffffff821117610c7657604052565b91908201809211610cb957565b634e487b7160e01b5f52601160045260245ffd5b60405190610cda82610c5a565b5f6040838281528260208201520152565b6003544210610d1757610cfc610f9e565b6009548111610d085790565b638a6b78ef60e01b5f5260045ffd5b6323a71b8760e21b5f5260045ffd5b67ffffffffffffffff8111610c765760051b60200190565b8051821015610d525760209160051b010190565b634e487b7160e01b5f52603260045260245ffd5b91908203918211610cb957565b81810292918115918404141715610cb957565b5f52601060205260405f205490600b548280821091180282186004821015610e635760038214610e5d57610dba9083610d66565b600c54612710610dcc600d5486610d73565b0490818082119118021890808211610e55575b60028314610e4e5790610df191610d66565b91612710610e05600e5492600f5490610d73565b0490818082119118021890828211610e46575b60018114610e405715610e345763e489255960e01b5f5260045ffd5b610e3d91610d66565b90565b50905090565b829150610e18565b5091505090565b905080610ddf565b91505090565b634e487b7160e01b5f52602160045260245ffd5b5f5160206110a45f395f51905f525c610e9d5760015f5160206110a45f395f51905f525d565b633ee5aeb560e01b5f5260045ffd5b5f546001600160a01b03163303610ebf57565b63118cdaa760e01b5f523360045260245ffd5b9190610ee060029184611028565b90549060031b1c92835f520160205260405f20549160018060a01b03169190565b610f09610ccd565b506004548042105f14610f3b5750604051610f2381610c5a565b60025481526003546020820152600454604082015290565b610f4d610f46610ff6565b9142610d66565b600a54908115610f8a57610f6682610f6d920642610d66565b9182610cac565b9060405192610f7b84610c5a565b83526020830152604082015290565b634e487b7160e01b5f52601260045260245ffd5b600454421015610fae5760025490565b610e3d610ff6565b9190805f526002830160205260405f20549283155f14610fee57610fe99293506001915f520160205260405f2054151590565b905f90565b505060019190565b60025461100560045442610d66565b90600a548015610f8a5761101a920490610cac565b60018101809111610cb95790565b8054821015610d52575f5260205f2001905f90565b5f82815260018201602052604090205461109d5780549068010000000000000000821015610c7657611076826001809401835582611028565b81549060031b9085821b915f19901b19161790558054925f520160205260405f2055600190565b50505f9056fe9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00a264697066735822122047bfc41ee37781956d8392733bfb28e527a96ac94bb3311f7805ce92c4d1d0a764736f6c634300081c0033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000680b6b3000000000000000000000000000000000000000000000000000000000680ba3700000000000000000000000000000000000000000000000000000000000000e100000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000384000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000898000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f00000000000000000000000029c9613590b3adac1007c9f399a2cafa12ab8389

-----Decoded View---------------
Arg [0] : minTotalBid_ (uint256): 10000000
Arg [1] : minBidIncrement_ (uint256): 1000000
Arg [2] : maxRound_ (uint256): 42
Arg [3] : firstRoundStart_ (uint256): 1745578800
Arg [4] : firstRoundEnd_ (uint256): 1745593200
Arg [5] : roundDuration_ (uint256): 3600
Arg [6] : roundLegendaryQuota_ (uint256): 5
Arg [7] : roundEpicQuota_ (uint256): 15
Arg [8] : roundEpicQuotaNumerator_ (uint256): 900
Arg [9] : roundRareQuota_ (uint256): 30
Arg [10] : roundRareQuotaNumerator_ (uint256): 2200
Arg [11] : tether_ (address): 0xc2132D05D31c914a87C6611C10748AEb04B58e8F
Arg [12] : initialOwner_ (address): 0x29c9613590b3aDaC1007c9F399a2cafA12aB8389

-----Encoded View---------------
13 Constructor Arguments found :
Arg [0] : 0000000000000000000000000000000000000000000000000000000000989680
Arg [1] : 00000000000000000000000000000000000000000000000000000000000f4240
Arg [2] : 000000000000000000000000000000000000000000000000000000000000002a
Arg [3] : 00000000000000000000000000000000000000000000000000000000680b6b30
Arg [4] : 00000000000000000000000000000000000000000000000000000000680ba370
Arg [5] : 0000000000000000000000000000000000000000000000000000000000000e10
Arg [6] : 0000000000000000000000000000000000000000000000000000000000000005
Arg [7] : 000000000000000000000000000000000000000000000000000000000000000f
Arg [8] : 0000000000000000000000000000000000000000000000000000000000000384
Arg [9] : 000000000000000000000000000000000000000000000000000000000000001e
Arg [10] : 0000000000000000000000000000000000000000000000000000000000000898
Arg [11] : 000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f
Arg [12] : 00000000000000000000000029c9613590b3adac1007c9f399a2cafa12ab8389


Block Transaction Gas Used Reward
view all blocks produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.