Source Code
Overview
ETH Balance
0 ETH
More Info
ContractCreator
Multichain Info
N/A
View more zero value Internal Transactions in Advanced View mode
Advanced mode:
Loading...
Loading
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.
Contract Source Code Verified (Exact Match)
Contract Name:
OptInService
Compiler Version
v0.8.25+commit.b61c2a91
Optimization Enabled:
Yes with 200 runs
Other Settings:
paris EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.25; import {StaticDelegateCallable} from "../common/StaticDelegateCallable.sol"; import {IOptInService} from "../../interfaces/service/IOptInService.sol"; import {IRegistry} from "../../interfaces/common/IRegistry.sol"; import {Checkpoints} from "../libraries/Checkpoints.sol"; import {Time} from "@openzeppelin/contracts/utils/types/Time.sol"; contract OptInService is StaticDelegateCallable, IOptInService { using Checkpoints for Checkpoints.Trace208; /** * @inheritdoc IOptInService */ address public immutable WHO_REGISTRY; /** * @inheritdoc IOptInService */ address public immutable WHERE_REGISTRY; mapping(address who => mapping(address where => Checkpoints.Trace208 value)) internal _isOptedIn; constructor(address whoRegistry, address whereRegistry) { WHO_REGISTRY = whoRegistry; WHERE_REGISTRY = whereRegistry; } /** * @inheritdoc IOptInService */ function isOptedInAt( address who, address where, uint48 timestamp, bytes calldata hint ) external view returns (bool) { return _isOptedIn[who][where].upperLookupRecent(timestamp, hint) == 1; } /** * @inheritdoc IOptInService */ function isOptedIn(address who, address where) public view returns (bool) { return _isOptedIn[who][where].latest() == 1; } /** * @inheritdoc IOptInService */ function optIn( address where ) external { if (!IRegistry(WHO_REGISTRY).isEntity(msg.sender)) { revert NotWho(); } if (!IRegistry(WHERE_REGISTRY).isEntity(where)) { revert NotWhereEntity(); } if (isOptedIn(msg.sender, where)) { revert AlreadyOptedIn(); } _isOptedIn[msg.sender][where].push(Time.timestamp(), 1); emit OptIn(msg.sender, where); } /** * @inheritdoc IOptInService */ function optOut( address where ) external { (, uint48 latestTimestamp, uint208 latestValue) = _isOptedIn[msg.sender][where].latestCheckpoint(); if (latestValue == 0) { revert NotOptedIn(); } if (latestTimestamp == Time.timestamp()) { revert OptOutCooldown(); } _isOptedIn[msg.sender][where].push(Time.timestamp(), 0); emit OptOut(msg.sender, where); } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity 0.8.25; abstract contract StaticDelegateCallable { function staticDelegateCall(address target, bytes calldata data) external { (bool success, bytes memory returndata) = target.delegatecall(data); bytes memory revertData = abi.encode(success, returndata); assembly { revert(add(32, revertData), mload(revertData)) } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IOptInService { error AlreadyOptedIn(); error NotOptedIn(); error NotWhereEntity(); error NotWho(); error OptOutCooldown(); /** * @notice Emitted when a "who" opts into a "where" entity. * @param who address of the "who" * @param where address of the "where" entity */ event OptIn(address indexed who, address indexed where); /** * @notice Emitted when a "who" opts out from a "where" entity. * @param who address of the "who" * @param where address of the "where" entity */ event OptOut(address indexed who, address indexed where); /** * @notice Get the "who" registry's address. * @return address of the "who" registry */ function WHO_REGISTRY() external view returns (address); /** * @notice Get the address of the registry where to opt-in. * @return address of the "where" registry */ function WHERE_REGISTRY() external view returns (address); /** * @notice Get if a given "who" is opted-in to a particular "where" entity at a given timestamp using a hint. * @param who address of the "who" * @param where address of the "where" registry * @param timestamp time point to get if the "who" is opted-in at * @param hint hint for the checkpoint index * @return if the "who" is opted-in at the given timestamp */ function isOptedInAt( address who, address where, uint48 timestamp, bytes calldata hint ) external view returns (bool); /** * @notice Check if a given "who" is opted-in to a particular "where" entity. * @param who address of the "who" * @param where address of the "where" registry * @return if the "who" is opted-in */ function isOptedIn(address who, address where) external view returns (bool); /** * @notice Opt-in a calling "who" to a particular "where" entity. * @param where address of the "where" registry */ function optIn( address where ) external; /** * @notice Opt-out a calling "who" from a particular "where" entity. * @param where address of the "where" registry */ function optOut( address where ) external; }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IRegistry { error EntityNotExist(); /** * @notice Emitted when an entity is added. * @param entity address of the added entity */ event AddEntity(address indexed entity); /** * @notice Get if a given address is an entity. * @param account address to check * @return if the given address is an entity */ function isEntity( address account ) external view returns (bool); /** * @notice Get a total number of entities. * @return total number of entities added */ function totalEntities() external view returns (uint256); /** * @notice Get an entity given its index. * @param index index of the entity to get * @return address of the entity */ function entity( uint256 index ) external view returns (address); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import {Checkpoints as OZCheckpoints} from "@openzeppelin/contracts/utils/structs/Checkpoints.sol"; import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; /** * @dev This library defines the `Trace*` struct, for checkpointing values as they change at different points in * time, and later looking up past values by key. */ library Checkpoints { using OZCheckpoints for OZCheckpoints.Trace208; error SystemCheckpoint(); struct Trace208 { OZCheckpoints.Trace208 _trace; } struct Checkpoint208 { uint48 _key; uint208 _value; } struct Trace256 { OZCheckpoints.Trace208 _trace; uint256[] _values; } struct Checkpoint256 { uint48 _key; uint256 _value; } /** * @dev Pushes a (`key`, `value`) pair into a Trace208 so that it is stored as the checkpoint. * * Returns previous value and new value. */ function push(Trace208 storage self, uint48 key, uint208 value) internal returns (uint208, uint208) { return self._trace.push(key, value); } /** * @dev Returns the value in the last (most recent) checkpoint with a key lower or equal than the search key, or zero * if there is none. */ function upperLookupRecent(Trace208 storage self, uint48 key) internal view returns (uint208) { return self._trace.upperLookupRecent(key); } /** * @dev Returns the value in the last (most recent) checkpoint with a key lower or equal than the search key, or zero * if there is none. * * NOTE: This is a variant of {upperLookupRecent} that can be optimized by getting the hint * (index of the checkpoint with a key lower or equal than the search key). */ function upperLookupRecent(Trace208 storage self, uint48 key, bytes memory hint_) internal view returns (uint208) { if (hint_.length == 0) { return upperLookupRecent(self, key); } uint32 hint = abi.decode(hint_, (uint32)); Checkpoint208 memory checkpoint = at(self, hint); if (checkpoint._key == key) { return checkpoint._value; } if (checkpoint._key < key && (hint == length(self) - 1 || at(self, hint + 1)._key > key)) { return checkpoint._value; } return upperLookupRecent(self, key); } /** * @dev Returns whether there is a checkpoint with a key lower or equal than the search key in the structure (i.e. it is not empty), * and if so the key and value in the checkpoint, and its position in the trace. */ function upperLookupRecentCheckpoint( Trace208 storage self, uint48 key ) internal view returns (bool, uint48, uint208, uint32) { uint256 len = self._trace._checkpoints.length; uint256 low = 0; uint256 high = len; if (len > 5) { uint256 mid = len - Math.sqrt(len); if (key < _unsafeAccess(self._trace._checkpoints, mid)._key) { high = mid; } else { low = mid + 1; } } uint256 pos = _upperBinaryLookup(self._trace._checkpoints, key, low, high); if (pos == 0) { return (false, 0, 0, 0); } OZCheckpoints.Checkpoint208 memory checkpoint = _unsafeAccess(self._trace._checkpoints, pos - 1); return (true, checkpoint._key, checkpoint._value, uint32(pos - 1)); } /** * @dev Returns whether there is a checkpoint with a key lower or equal than the search key in the structure (i.e. it is not empty), * and if so the key and value in the checkpoint, and its position in the trace. * * NOTE: This is a variant of {upperLookupRecentCheckpoint} that can be optimized by getting the hint * (index of the checkpoint with a key lower or equal than the search key). */ function upperLookupRecentCheckpoint( Trace208 storage self, uint48 key, bytes memory hint_ ) internal view returns (bool, uint48, uint208, uint32) { if (hint_.length == 0) { return upperLookupRecentCheckpoint(self, key); } uint32 hint = abi.decode(hint_, (uint32)); Checkpoint208 memory checkpoint = at(self, hint); if (checkpoint._key == key) { return (true, checkpoint._key, checkpoint._value, hint); } if (checkpoint._key < key && (hint == length(self) - 1 || at(self, hint + 1)._key > key)) { return (true, checkpoint._key, checkpoint._value, hint); } return upperLookupRecentCheckpoint(self, key); } /** * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints. */ function latest( Trace208 storage self ) internal view returns (uint208) { return self._trace.latest(); } /** * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value * in the most recent checkpoint. */ function latestCheckpoint( Trace208 storage self ) internal view returns (bool, uint48, uint208) { return self._trace.latestCheckpoint(); } /** * @dev Returns a total number of checkpoints. */ function length( Trace208 storage self ) internal view returns (uint256) { return self._trace.length(); } /** * @dev Returns checkpoint at a given position. */ function at(Trace208 storage self, uint32 pos) internal view returns (Checkpoint208 memory) { OZCheckpoints.Checkpoint208 memory checkpoint = self._trace.at(pos); return Checkpoint208({_key: checkpoint._key, _value: checkpoint._value}); } /** * @dev Pops the last (most recent) checkpoint. */ function pop( Trace208 storage self ) internal returns (uint208 value) { value = self._trace.latest(); self._trace._checkpoints.pop(); } /** * @dev Pushes a (`key`, `value`) pair into a Trace256 so that it is stored as the checkpoint. * * Returns previous value and new value. */ function push(Trace256 storage self, uint48 key, uint256 value) internal returns (uint256, uint256) { if (self._values.length == 0) { self._values.push(0); } uint256 len = self._values.length; self._trace.push(key, uint208(len)); self._values.push(value); return (self._values[len - 1], value); } /** * @dev Returns the value in the last (most recent) checkpoint with a key lower or equal than the search key, or zero * if there is none. */ function upperLookupRecent(Trace256 storage self, uint48 key) internal view returns (uint256) { uint208 idx = self._trace.upperLookupRecent(key); return idx > 0 ? self._values[idx] : 0; } /** * @dev Returns the value in the last (most recent) checkpoint with a key lower or equal than the search key, or zero * if there is none. * * NOTE: This is a variant of {upperLookupRecent} that can be optimized by getting the hint * (index of the checkpoint with a key lower or equal than the search key). */ function upperLookupRecent(Trace256 storage self, uint48 key, bytes memory hint_) internal view returns (uint256) { if (hint_.length == 0) { return upperLookupRecent(self, key); } uint32 hint = abi.decode(hint_, (uint32)); Checkpoint256 memory checkpoint = at(self, hint); if (checkpoint._key == key) { return checkpoint._value; } if (checkpoint._key < key && (hint == length(self) - 1 || at(self, hint + 1)._key > key)) { return checkpoint._value; } return upperLookupRecent(self, key); } /** * @dev Returns whether there is a checkpoint with a key lower or equal than the search key in the structure (i.e. it is not empty), * and if so the key and value in the checkpoint, and its position in the trace. */ function upperLookupRecentCheckpoint( Trace256 storage self, uint48 key ) internal view returns (bool, uint48, uint256, uint32) { uint256 len = self._trace._checkpoints.length; uint256 low = 0; uint256 high = len; if (len > 5) { uint256 mid = len - Math.sqrt(len); if (key < _unsafeAccess(self._trace._checkpoints, mid)._key) { high = mid; } else { low = mid + 1; } } uint256 pos = _upperBinaryLookup(self._trace._checkpoints, key, low, high); if (pos == 0) { return (false, 0, 0, 0); } OZCheckpoints.Checkpoint208 memory checkpoint = _unsafeAccess(self._trace._checkpoints, pos - 1); return (true, checkpoint._key, self._values[checkpoint._value], uint32(pos - 1)); } /** * @dev Returns whether there is a checkpoint with a key lower or equal than the search key in the structure (i.e. it is not empty), * and if so the key and value in the checkpoint, and its position in the trace. * * NOTE: This is a variant of {upperLookupRecentCheckpoint} that can be optimized by getting the hint * (index of the checkpoint with a key lower or equal than the search key). */ function upperLookupRecentCheckpoint( Trace256 storage self, uint48 key, bytes memory hint_ ) internal view returns (bool, uint48, uint256, uint32) { if (hint_.length == 0) { return upperLookupRecentCheckpoint(self, key); } uint32 hint = abi.decode(hint_, (uint32)); Checkpoint256 memory checkpoint = at(self, hint); if (checkpoint._key == key) { return (true, checkpoint._key, self._values[checkpoint._value], hint); } if (checkpoint._key < key && (hint == length(self) - 1 || at(self, hint + 1)._key > key)) { return (true, checkpoint._key, self._values[checkpoint._value], hint); } return upperLookupRecentCheckpoint(self, key); } /** * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints. */ function latest( Trace256 storage self ) internal view returns (uint256) { uint208 idx = self._trace.latest(); return idx > 0 ? self._values[idx] : 0; } /** * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value * in the most recent checkpoint. */ function latestCheckpoint( Trace256 storage self ) internal view returns (bool exists, uint48 _key, uint256 _value) { uint256 idx; (exists, _key, idx) = self._trace.latestCheckpoint(); _value = exists ? self._values[idx] : 0; } /** * @dev Returns a total number of checkpoints. */ function length( Trace256 storage self ) internal view returns (uint256) { return self._trace.length(); } /** * @dev Returns checkpoint at a given position. */ function at(Trace256 storage self, uint32 pos) internal view returns (Checkpoint256 memory) { OZCheckpoints.Checkpoint208 memory checkpoint = self._trace.at(pos); return Checkpoint256({_key: checkpoint._key, _value: self._values[checkpoint._value]}); } /** * @dev Pops the last (most recent) checkpoint. */ function pop( Trace256 storage self ) internal returns (uint256 value) { uint208 idx = self._trace.latest(); if (idx == 0) { revert SystemCheckpoint(); } value = self._values[idx]; self._trace._checkpoints.pop(); } /** * @dev Return the index of the last (most recent) checkpoint with a key lower or equal than the search key, or `high` * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive * `high`. * * WARNING: `high` should not be greater than the array's length. */ function _upperBinaryLookup( OZCheckpoints.Checkpoint208[] storage self, uint96 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key > key) { high = mid; } else { low = mid + 1; } } return high; } /** * @dev Access an element of the array without performing a bounds check. The position is assumed to be within bounds. */ function _unsafeAccess( OZCheckpoints.Checkpoint208[] storage self, uint256 pos ) private pure returns (OZCheckpoints.Checkpoint208 storage result) { assembly { mstore(0, self.slot) result.slot := add(keccak256(0, 0x20), pos) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/types/Time.sol) pragma solidity ^0.8.20; import {Math} from "../math/Math.sol"; import {SafeCast} from "../math/SafeCast.sol"; /** * @dev This library provides helpers for manipulating time-related objects. * * It uses the following types: * - `uint48` for timepoints * - `uint32` for durations * * While the library doesn't provide specific types for timepoints and duration, it does provide: * - a `Delay` type to represent duration that can be programmed to change value automatically at a given point * - additional helper functions */ library Time { using Time for *; /** * @dev Get the block timestamp as a Timepoint. */ function timestamp() internal view returns (uint48) { return SafeCast.toUint48(block.timestamp); } /** * @dev Get the block number as a Timepoint. */ function blockNumber() internal view returns (uint48) { return SafeCast.toUint48(block.number); } // ==================================================== Delay ===================================================== /** * @dev A `Delay` is a uint32 duration that can be programmed to change value automatically at a given point in the * future. The "effect" timepoint describes when the transitions happens from the "old" value to the "new" value. * This allows updating the delay applied to some operation while keeping some guarantees. * * In particular, the {update} function guarantees that if the delay is reduced, the old delay still applies for * some time. For example if the delay is currently 7 days to do an upgrade, the admin should not be able to set * the delay to 0 and upgrade immediately. If the admin wants to reduce the delay, the old delay (7 days) should * still apply for some time. * * * The `Delay` type is 112 bits long, and packs the following: * * ``` * | [uint48]: effect date (timepoint) * | | [uint32]: value before (duration) * ↓ ↓ ↓ [uint32]: value after (duration) * 0xAAAAAAAAAAAABBBBBBBBCCCCCCCC * ``` * * NOTE: The {get} and {withUpdate} functions operate using timestamps. Block number based delays are not currently * supported. */ type Delay is uint112; /** * @dev Wrap a duration into a Delay to add the one-step "update in the future" feature */ function toDelay(uint32 duration) internal pure returns (Delay) { return Delay.wrap(duration); } /** * @dev Get the value at a given timepoint plus the pending value and effect timepoint if there is a scheduled * change after this timepoint. If the effect timepoint is 0, then the pending value should not be considered. */ function _getFullAt(Delay self, uint48 timepoint) private pure returns (uint32, uint32, uint48) { (uint32 valueBefore, uint32 valueAfter, uint48 effect) = self.unpack(); return effect <= timepoint ? (valueAfter, 0, 0) : (valueBefore, valueAfter, effect); } /** * @dev Get the current value plus the pending value and effect timepoint if there is a scheduled change. If the * effect timepoint is 0, then the pending value should not be considered. */ function getFull(Delay self) internal view returns (uint32, uint32, uint48) { return _getFullAt(self, timestamp()); } /** * @dev Get the current value. */ function get(Delay self) internal view returns (uint32) { (uint32 delay, , ) = self.getFull(); return delay; } /** * @dev Update a Delay object so that it takes a new duration after a timepoint that is automatically computed to * enforce the old delay at the moment of the update. Returns the updated Delay object and the timestamp when the * new delay becomes effective. */ function withUpdate( Delay self, uint32 newValue, uint32 minSetback ) internal view returns (Delay updatedDelay, uint48 effect) { uint32 value = self.get(); uint32 setback = uint32(Math.max(minSetback, value > newValue ? value - newValue : 0)); effect = timestamp() + setback; return (pack(value, newValue, effect), effect); } /** * @dev Split a delay into its components: valueBefore, valueAfter and effect (transition timepoint). */ function unpack(Delay self) internal pure returns (uint32 valueBefore, uint32 valueAfter, uint48 effect) { uint112 raw = Delay.unwrap(self); valueAfter = uint32(raw); valueBefore = uint32(raw >> 32); effect = uint48(raw >> 64); return (valueBefore, valueAfter, effect); } /** * @dev pack the components into a Delay object. */ function pack(uint32 valueBefore, uint32 valueAfter, uint48 effect) internal pure returns (Delay) { return Delay.wrap((uint112(effect) << 64) | (uint112(valueBefore) << 32) | uint112(valueAfter)); } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/Checkpoints.sol) // This file was procedurally generated from scripts/generate/templates/Checkpoints.js. pragma solidity ^0.8.20; import {Math} from "../math/Math.sol"; /** * @dev This library defines the `Trace*` struct, for checkpointing values as they change at different points in * time, and later looking up past values by block number. See {Votes} as an example. * * To create a history of checkpoints define a variable type `Checkpoints.Trace*` in your contract, and store a new * checkpoint for the current transaction block using the {push} function. */ library Checkpoints { /** * @dev A value was attempted to be inserted on a past checkpoint. */ error CheckpointUnorderedInsertion(); struct Trace224 { Checkpoint224[] _checkpoints; } struct Checkpoint224 { uint32 _key; uint224 _value; } /** * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint. * * Returns previous value and new value. * * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint32).max` key set will disable the * library. */ function push(Trace224 storage self, uint32 key, uint224 value) internal returns (uint224, uint224) { return _insert(self._checkpoints, key, value); } /** * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if * there is none. */ function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) { uint256 len = self._checkpoints.length; uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len); return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. */ function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) { uint256 len = self._checkpoints.length; uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. * * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high * keys). */ function upperLookupRecent(Trace224 storage self, uint32 key) internal view returns (uint224) { uint256 len = self._checkpoints.length; uint256 low = 0; uint256 high = len; if (len > 5) { uint256 mid = len - Math.sqrt(len); if (key < _unsafeAccess(self._checkpoints, mid)._key) { high = mid; } else { low = mid + 1; } } uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints. */ function latest(Trace224 storage self) internal view returns (uint224) { uint256 pos = self._checkpoints.length; return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value * in the most recent checkpoint. */ function latestCheckpoint(Trace224 storage self) internal view returns (bool exists, uint32 _key, uint224 _value) { uint256 pos = self._checkpoints.length; if (pos == 0) { return (false, 0, 0); } else { Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1); return (true, ckpt._key, ckpt._value); } } /** * @dev Returns the number of checkpoint. */ function length(Trace224 storage self) internal view returns (uint256) { return self._checkpoints.length; } /** * @dev Returns checkpoint at given position. */ function at(Trace224 storage self, uint32 pos) internal view returns (Checkpoint224 memory) { return self._checkpoints[pos]; } /** * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint, * or by updating the last one. */ function _insert(Checkpoint224[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) { uint256 pos = self.length; if (pos > 0) { // Copying to memory is important here. Checkpoint224 memory last = _unsafeAccess(self, pos - 1); // Checkpoint keys must be non-decreasing. if (last._key > key) { revert CheckpointUnorderedInsertion(); } // Update or push new checkpoint if (last._key == key) { _unsafeAccess(self, pos - 1)._value = value; } else { self.push(Checkpoint224({_key: key, _value: value})); } return (last._value, value); } else { self.push(Checkpoint224({_key: key, _value: value})); return (0, value); } } /** * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high` * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive * `high`. * * WARNING: `high` should not be greater than the array's length. */ function _upperBinaryLookup( Checkpoint224[] storage self, uint32 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key > key) { high = mid; } else { low = mid + 1; } } return high; } /** * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and * exclusive `high`. * * WARNING: `high` should not be greater than the array's length. */ function _lowerBinaryLookup( Checkpoint224[] storage self, uint32 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key < key) { low = mid + 1; } else { high = mid; } } return high; } /** * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds. */ function _unsafeAccess( Checkpoint224[] storage self, uint256 pos ) private pure returns (Checkpoint224 storage result) { assembly { mstore(0, self.slot) result.slot := add(keccak256(0, 0x20), pos) } } struct Trace208 { Checkpoint208[] _checkpoints; } struct Checkpoint208 { uint48 _key; uint208 _value; } /** * @dev Pushes a (`key`, `value`) pair into a Trace208 so that it is stored as the checkpoint. * * Returns previous value and new value. * * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint48).max` key set will disable the * library. */ function push(Trace208 storage self, uint48 key, uint208 value) internal returns (uint208, uint208) { return _insert(self._checkpoints, key, value); } /** * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if * there is none. */ function lowerLookup(Trace208 storage self, uint48 key) internal view returns (uint208) { uint256 len = self._checkpoints.length; uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len); return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. */ function upperLookup(Trace208 storage self, uint48 key) internal view returns (uint208) { uint256 len = self._checkpoints.length; uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. * * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high * keys). */ function upperLookupRecent(Trace208 storage self, uint48 key) internal view returns (uint208) { uint256 len = self._checkpoints.length; uint256 low = 0; uint256 high = len; if (len > 5) { uint256 mid = len - Math.sqrt(len); if (key < _unsafeAccess(self._checkpoints, mid)._key) { high = mid; } else { low = mid + 1; } } uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints. */ function latest(Trace208 storage self) internal view returns (uint208) { uint256 pos = self._checkpoints.length; return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value * in the most recent checkpoint. */ function latestCheckpoint(Trace208 storage self) internal view returns (bool exists, uint48 _key, uint208 _value) { uint256 pos = self._checkpoints.length; if (pos == 0) { return (false, 0, 0); } else { Checkpoint208 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1); return (true, ckpt._key, ckpt._value); } } /** * @dev Returns the number of checkpoint. */ function length(Trace208 storage self) internal view returns (uint256) { return self._checkpoints.length; } /** * @dev Returns checkpoint at given position. */ function at(Trace208 storage self, uint32 pos) internal view returns (Checkpoint208 memory) { return self._checkpoints[pos]; } /** * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint, * or by updating the last one. */ function _insert(Checkpoint208[] storage self, uint48 key, uint208 value) private returns (uint208, uint208) { uint256 pos = self.length; if (pos > 0) { // Copying to memory is important here. Checkpoint208 memory last = _unsafeAccess(self, pos - 1); // Checkpoint keys must be non-decreasing. if (last._key > key) { revert CheckpointUnorderedInsertion(); } // Update or push new checkpoint if (last._key == key) { _unsafeAccess(self, pos - 1)._value = value; } else { self.push(Checkpoint208({_key: key, _value: value})); } return (last._value, value); } else { self.push(Checkpoint208({_key: key, _value: value})); return (0, value); } } /** * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high` * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive * `high`. * * WARNING: `high` should not be greater than the array's length. */ function _upperBinaryLookup( Checkpoint208[] storage self, uint48 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key > key) { high = mid; } else { low = mid + 1; } } return high; } /** * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and * exclusive `high`. * * WARNING: `high` should not be greater than the array's length. */ function _lowerBinaryLookup( Checkpoint208[] storage self, uint48 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key < key) { low = mid + 1; } else { high = mid; } } return high; } /** * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds. */ function _unsafeAccess( Checkpoint208[] storage self, uint256 pos ) private pure returns (Checkpoint208 storage result) { assembly { mstore(0, self.slot) result.slot := add(keccak256(0, 0x20), pos) } } struct Trace160 { Checkpoint160[] _checkpoints; } struct Checkpoint160 { uint96 _key; uint160 _value; } /** * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint. * * Returns previous value and new value. * * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint96).max` key set will disable the * library. */ function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) { return _insert(self._checkpoints, key, value); } /** * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if * there is none. */ function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) { uint256 len = self._checkpoints.length; uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len); return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. */ function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) { uint256 len = self._checkpoints.length; uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero * if there is none. * * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high * keys). */ function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) { uint256 len = self._checkpoints.length; uint256 low = 0; uint256 high = len; if (len > 5) { uint256 mid = len - Math.sqrt(len); if (key < _unsafeAccess(self._checkpoints, mid)._key) { high = mid; } else { low = mid + 1; } } uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high); return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints. */ function latest(Trace160 storage self) internal view returns (uint160) { uint256 pos = self._checkpoints.length; return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value; } /** * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value * in the most recent checkpoint. */ function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) { uint256 pos = self._checkpoints.length; if (pos == 0) { return (false, 0, 0); } else { Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1); return (true, ckpt._key, ckpt._value); } } /** * @dev Returns the number of checkpoint. */ function length(Trace160 storage self) internal view returns (uint256) { return self._checkpoints.length; } /** * @dev Returns checkpoint at given position. */ function at(Trace160 storage self, uint32 pos) internal view returns (Checkpoint160 memory) { return self._checkpoints[pos]; } /** * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint, * or by updating the last one. */ function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) { uint256 pos = self.length; if (pos > 0) { // Copying to memory is important here. Checkpoint160 memory last = _unsafeAccess(self, pos - 1); // Checkpoint keys must be non-decreasing. if (last._key > key) { revert CheckpointUnorderedInsertion(); } // Update or push new checkpoint if (last._key == key) { _unsafeAccess(self, pos - 1)._value = value; } else { self.push(Checkpoint160({_key: key, _value: value})); } return (last._value, value); } else { self.push(Checkpoint160({_key: key, _value: value})); return (0, value); } } /** * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high` * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive * `high`. * * WARNING: `high` should not be greater than the array's length. */ function _upperBinaryLookup( Checkpoint160[] storage self, uint96 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key > key) { high = mid; } else { low = mid + 1; } } return high; } /** * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and * exclusive `high`. * * WARNING: `high` should not be greater than the array's length. */ function _lowerBinaryLookup( Checkpoint160[] storage self, uint96 key, uint256 low, uint256 high ) private view returns (uint256) { while (low < high) { uint256 mid = Math.average(low, high); if (_unsafeAccess(self, mid)._key < key) { low = mid + 1; } else { high = mid; } } return high; } /** * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds. */ function _unsafeAccess( Checkpoint160[] storage self, uint256 pos ) private pure returns (Checkpoint160 storage result) { assembly { mstore(0, self.slot) result.slot := add(keccak256(0, 0x20), pos) } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol) pragma solidity ^0.8.20; /** * @dev Standard math utilities missing in the Solidity language. */ library Math { /** * @dev Muldiv operation overflow. */ error MathOverflowedMulDiv(); 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 overflow flag. */ function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { uint256 c = a + b; if (c < a) return (false, 0); return (true, c); } } /** * @dev Returns the subtraction of two unsigned integers, with an overflow flag. */ function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b > a) return (false, 0); return (true, a - b); } } /** * @dev Returns the multiplication of two unsigned integers, with an overflow flag. */ function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { 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 division by zero flag. */ function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a / b); } } /** * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. */ function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { unchecked { if (b == 0) return (false, 0); return (true, a % b); } } /** * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return a > b ? a : b; } /** * @dev Returns the smallest of two numbers. */ function min(uint256 a, uint256 b) internal pure returns (uint256) { return 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. return a / b; } // (a + b - 1) / b can overflow on addition, so we distribute. return a == 0 ? 0 : (a - 1) / b + 1; } /** * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or * denominator == 0. * @dev 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^256 and mod 2^256 - 1, then use // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2^256 + 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^256. Also prevents denominator == 0. if (denominator <= prod1) { revert MathOverflowedMulDiv(); } /////////////////////////////////////////////// // 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^256 / 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^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for // four bits. That is, denominator * inv = 1 mod 2^4. 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^8 inverse *= 2 - denominator * inverse; // inverse mod 2^16 inverse *= 2 - denominator * inverse; // inverse mod 2^32 inverse *= 2 - denominator * inverse; // inverse mod 2^64 inverse *= 2 - denominator * inverse; // inverse mod 2^128 inverse *= 2 - denominator * inverse; // inverse mod 2^256 // 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^256. Since the preconditions guarantee that the outcome is // less than 2^256, 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; } } /** * @notice 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) { uint256 result = mulDiv(x, y, denominator); if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) { result += 1; } return result; } /** * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded * towards zero. * * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11). */ function sqrt(uint256 a) internal pure returns (uint256) { if (a == 0) { return 0; } // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target. // // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`. // // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)` // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))` // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)` // // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit. uint256 result = 1 << (log2(a) >> 1); // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128, // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision // into the expected uint128 result. unchecked { result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; result = (result + a / result) >> 1; return min(result, a / result); } } /** * @notice 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 + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0); } } /** * @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; unchecked { if (value >> 128 > 0) { value >>= 128; result += 128; } if (value >> 64 > 0) { value >>= 64; result += 64; } if (value >> 32 > 0) { value >>= 32; result += 32; } if (value >> 16 > 0) { value >>= 16; result += 16; } if (value >> 8 > 0) { value >>= 8; result += 8; } if (value >> 4 > 0) { value >>= 4; result += 4; } if (value >> 2 > 0) { value >>= 2; result += 2; } if (value >> 1 > 0) { result += 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 + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0); } } /** * @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 + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0); } } /** * @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; unchecked { if (value >> 128 > 0) { value >>= 128; result += 16; } if (value >> 64 > 0) { value >>= 64; result += 8; } if (value >> 32 > 0) { value >>= 32; result += 4; } if (value >> 16 > 0) { value >>= 16; result += 2; } if (value >> 8 > 0) { result += 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 + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0); } } /** * @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) (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 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); } }
{ "remappings": [ "forge-std/=lib/forge-std/src/", "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/", "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/", "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/", "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/", "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/", "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": "paris", "viaIR": true, "libraries": {} }
[{"inputs":[{"internalType":"address","name":"whoRegistry","type":"address"},{"internalType":"address","name":"whereRegistry","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AlreadyOptedIn","type":"error"},{"inputs":[],"name":"CheckpointUnorderedInsertion","type":"error"},{"inputs":[],"name":"NotOptedIn","type":"error"},{"inputs":[],"name":"NotWhereEntity","type":"error"},{"inputs":[],"name":"NotWho","type":"error"},{"inputs":[],"name":"OptOutCooldown","type":"error"},{"inputs":[{"internalType":"uint8","name":"bits","type":"uint8"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"SafeCastOverflowedUintDowncast","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"who","type":"address"},{"indexed":true,"internalType":"address","name":"where","type":"address"}],"name":"OptIn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"who","type":"address"},{"indexed":true,"internalType":"address","name":"where","type":"address"}],"name":"OptOut","type":"event"},{"inputs":[],"name":"WHERE_REGISTRY","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"WHO_REGISTRY","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"who","type":"address"},{"internalType":"address","name":"where","type":"address"}],"name":"isOptedIn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"who","type":"address"},{"internalType":"address","name":"where","type":"address"},{"internalType":"uint48","name":"timestamp","type":"uint48"},{"internalType":"bytes","name":"hint","type":"bytes"}],"name":"isOptedInAt","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"where","type":"address"}],"name":"optIn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"where","type":"address"}],"name":"optOut","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"target","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"staticDelegateCall","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Contract Creation Code
60c034608157601f610f6138819003918201601f19168301916001600160401b038311848410176086578084926040948552833981010312608157604b6020604583609c565b9201609c565b9060805260a052604051610eb190816100b082396080518181816103800152610521015260a05181818161021f01526103d40152f35b600080fd5b634e487b7160e01b600052604160045260246000fd5b51906001600160a01b038216820360815756fe6080604052600436101561001257600080fd5b60003560e01c8063220d32d414610087578063530e1d43146100825780636d0fae4a1461007d5780639f86fd8514610078578063b1138ad114610073578063c4b5d62c1461006e5763d46104831461006957600080fd5b610550565b61050b565b610341565b61024e565b610209565b61011c565b346100bd5760403660031901126100bd5760206100b36100a56100c2565b6100ad6100d8565b90610646565b6040519015158152f35b600080fd5b600435906001600160a01b03821682036100bd57565b602435906001600160a01b03821682036100bd57565b9181601f840112156100bd5782359167ffffffffffffffff83116100bd57602083818601950101116100bd57565b346100bd5760803660031901126100bd576101356100c2565b61013d6100d8565b6044359065ffffffffffff821682036100bd576064359167ffffffffffffffff83116100bd576101746101a39336906004016100ee565b93909260009560018060a01b0316865285602052604086209060018060a01b0316600052602052604060002090565b926101ad81610712565b926101bb60405194856106e1565b818452368282011161020557916102019560208386946101ec968360019a99013784010152848060d01b03936107a8565b60405191169190911481529081906020820190565b0390f35b8580fd5b346100bd5760003660031901126100bd576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b346100bd576040806003193601126100bd576102686100c2565b60243567ffffffffffffffff81116100bd576102889036906004016100ee565b9182845193849384378201906000938493838580955203915af4903d15610337573d916102b483610712565b926102c1855194856106e1565b83523d82602085013e92905b81519384926020911515602085015280840152835193846060850152825b85811061032057846103186080828988838284010152601f801991011681010360608101845201826106e1565b805190602001fd5b8181018301518782016080015286945082016102eb565b60609192906102cd565b346100bd576020806003193601126100bd5761035b6100c2565b6040516302910f8b60e31b8082523360048301526001600160a01b03939182816024817f000000000000000000000000000000000000000000000000000000000000000089165afa9081156104d7576000916104ee575b50156104dc576040519081526001600160a01b038316600482015281816024817f000000000000000000000000000000000000000000000000000000000000000088165afa9182156104d7576000926104aa575b505015610498576104178133610646565b6104865733600090815260208190526040902061045b9061044c9083905b9060018060a01b0316600052602052604060002090565b61045542610906565b90610b39565b505016337f9b730d5b907ee509de729817a2bb37e404418ba569b3a50f36192372f973cb41600080a3005b60405163dcdeaba360e01b8152600490fd5b6040516321f5408d60e01b8152600490fd5b6104c99250803d106104d0575b6104c181836106e1565b81019061072e565b3880610406565b503d6104b7565b610746565b604051630fb9ff9560e41b8152600490fd5b6105059150833d85116104d0576104c181836106e1565b386103b2565b346100bd5760003660031901126100bd576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b346100bd5760203660031901126100bd576105696100c2565b33600090815260208190526040902061058c90610587908390610435565b61095c565b9091506001600160d01b0316156106345765ffffffffffff6105bb6105b042610906565b65ffffffffffff1690565b911614610622573360009081526020819052604090206105ef906105e0908390610435565b6105e942610906565b90610c48565b50506001600160a01b0316337f1629cd9ad365627cf8408d19c50224af8f3213c1a18ae48062d92e22bddf7de5600080a3005b60405163291bf4b760e21b8152600490fd5b6040516334b09b3960e11b8152600490fd5b6001600160a01b039081166000908152602081815260408083209490931682529290925281208054806106845750506001905b818060d01b03161490565b6000199181838101116106a55783602091600195522001015460301c610679565b61076e565b634e487b7160e01b600052604160045260246000fd5b6040810190811067ffffffffffffffff8211176106dc57604052565b6106aa565b90601f8019910116810190811067ffffffffffffffff8211176106dc57604052565b60405190610710826106c0565b565b67ffffffffffffffff81116106dc57601f01601f191660200190565b908160209103126100bd575180151581036100bd5790565b6040513d6000823e3d90fd5b908160209103126100bd575163ffffffff811681036100bd5790565b634e487b7160e01b600052601160045260246000fd5b6000198101919082116106a557565b90600163ffffffff809316019182116106a557565b9180511561088f57806020806107c393518301019101610752565b916107ce83826108b2565b926107df845165ffffffffffff1690565b9065ffffffffffff80851692168281146108765782119182610821575b505061080f5761080c92506109bf565b90565b5050602001516001600160d01b031690565b90915061082e8354610784565b63ffffffff831614918215610847575b505038806107fc565b61086e91925061086261085c6105b092610793565b856108b2565b5165ffffffffffff1690565b11388061083e565b50505050602091909101516001600160d01b0316905090565b5061080c916109bf565b604051906108a6826106c0565b60006020838281520152565b6108d7916108d1916108c2610899565b506108cb610899565b50610aa8565b50610938565b65ffffffffffff81511690602060018060d01b0391015116604051916108fc836106c0565b8252602082015290565b65ffffffffffff90818111610919571690565b604490604051906306dfcc6560e41b8252603060048301526024820152fd5b90604051610945816106c0565b915465ffffffffffff8116835260301c6020830152565b805480610970575050600090600090600090565b6000199080828101116106a5576109909260005260206000200101610938565b9065ffffffffffff82511691602060018060d01b0391015116906001929190565b90600182018092116106a557565b908154906000918060058111610a5c575b50915b828110610a0c575050806109e8575050600090565b61080c916109f8610a0592610784565b9060005260206000200190565b5460301c90565b90918082169080831860011c82018092116106a5578460005265ffffffffffff8083602060002001541690851610600014610a4a5750915b906109d3565b929150610a56906109b1565b90610a44565b90610a6682610d29565b82039182116106a5578460005265ffffffffffff8083602060002001541690841610600014610a9757505b386109d0565b9250610aa2906109b1565b91610a91565b8054821015610ac05760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b8054680100000000000000008110156106dc57610af891600182018155610aa8565b919091610b2357805160209091015160301b65ffffffffffff191665ffffffffffff91909116179055565b634e487b7160e01b600052600060045260246000fd5b8054908115610c2357610b63610b5e610b5184610784565b8360005260206000200190565b610938565b90610b74825165ffffffffffff1690565b65ffffffffffff8581169491168410610c1157610bdc94602094610ba16105b0865165ffffffffffff1690565b03610be25750610bce916109f8610bb792610784565b660100000000000065ffffffffffff825416179055565b01516001600160d01b031690565b90600190565b9050610c0c91610c01610bf3610703565b65ffffffffffff9093168352565b600182860152610ad6565b610bce565b604051632520601d60e01b8152600490fd5b610c40929150610c34610bf3610703565b60016020830152610ad6565b600090600190565b8054908115610ce457610c60610b5e610b5184610784565b90610c71825165ffffffffffff1690565b65ffffffffffff8581169491168410610c1157610cc294602094610c9e6105b0865165ffffffffffff1690565b03610cc85750610bce916109f8610cb492610784565b65ffffffffffff8154169055565b90600090565b9050610c0c91610cd9610bf3610703565b600085830152610ad6565b610d01929150610cf5610bf3610703565b60006020830152610ad6565b600090600090565b8115610d13570490565b634e487b7160e01b600052601260045260246000fd5b8015610e635780610dfc610df5610deb610de1610dd7610dcd610dc3610db9600161080c9a6000908b60801c80610e57575b508060401c80610e4a575b508060201c80610e3d575b508060101c80610e30575b508060081c80610e23575b508060041c80610e16575b508060021c80610e09575b50821c610e02575b811c1b610db2818b610d09565b0160011c90565b610db2818a610d09565b610db28189610d09565b610db28188610d09565b610db28187610d09565b610db28186610d09565b610db28185610d09565b8092610d09565b90610e69565b8101610da5565b6002915091019038610d9d565b6004915091019038610d92565b6008915091019038610d87565b6010915091019038610d7c565b6020915091019038610d71565b6040915091019038610d66565b91505060809038610d5b565b50600090565b9080821015610e76575090565b90509056fea2646970667358221220f934a4509108cb0ba2614a3bfa56243799c5cfc49ad23c4cdaa6d4d5b296019264736f6c63430008190033000000000000000000000000ac5fe73e2829eef6b408f349e936f9a817e6850d00000000000000000000000090d6bf17d26d975611f20a8e2f15df8eb957526d
Deployed Bytecode
0x6080604052600436101561001257600080fd5b60003560e01c8063220d32d414610087578063530e1d43146100825780636d0fae4a1461007d5780639f86fd8514610078578063b1138ad114610073578063c4b5d62c1461006e5763d46104831461006957600080fd5b610550565b61050b565b610341565b61024e565b610209565b61011c565b346100bd5760403660031901126100bd5760206100b36100a56100c2565b6100ad6100d8565b90610646565b6040519015158152f35b600080fd5b600435906001600160a01b03821682036100bd57565b602435906001600160a01b03821682036100bd57565b9181601f840112156100bd5782359167ffffffffffffffff83116100bd57602083818601950101116100bd57565b346100bd5760803660031901126100bd576101356100c2565b61013d6100d8565b6044359065ffffffffffff821682036100bd576064359167ffffffffffffffff83116100bd576101746101a39336906004016100ee565b93909260009560018060a01b0316865285602052604086209060018060a01b0316600052602052604060002090565b926101ad81610712565b926101bb60405194856106e1565b818452368282011161020557916102019560208386946101ec968360019a99013784010152848060d01b03936107a8565b60405191169190911481529081906020820190565b0390f35b8580fd5b346100bd5760003660031901126100bd576040517f00000000000000000000000090d6bf17d26d975611f20a8e2f15df8eb957526d6001600160a01b03168152602090f35b346100bd576040806003193601126100bd576102686100c2565b60243567ffffffffffffffff81116100bd576102889036906004016100ee565b9182845193849384378201906000938493838580955203915af4903d15610337573d916102b483610712565b926102c1855194856106e1565b83523d82602085013e92905b81519384926020911515602085015280840152835193846060850152825b85811061032057846103186080828988838284010152601f801991011681010360608101845201826106e1565b805190602001fd5b8181018301518782016080015286945082016102eb565b60609192906102cd565b346100bd576020806003193601126100bd5761035b6100c2565b6040516302910f8b60e31b8082523360048301526001600160a01b03939182816024817f000000000000000000000000ac5fe73e2829eef6b408f349e936f9a817e6850d89165afa9081156104d7576000916104ee575b50156104dc576040519081526001600160a01b038316600482015281816024817f00000000000000000000000090d6bf17d26d975611f20a8e2f15df8eb957526d88165afa9182156104d7576000926104aa575b505015610498576104178133610646565b6104865733600090815260208190526040902061045b9061044c9083905b9060018060a01b0316600052602052604060002090565b61045542610906565b90610b39565b505016337f9b730d5b907ee509de729817a2bb37e404418ba569b3a50f36192372f973cb41600080a3005b60405163dcdeaba360e01b8152600490fd5b6040516321f5408d60e01b8152600490fd5b6104c99250803d106104d0575b6104c181836106e1565b81019061072e565b3880610406565b503d6104b7565b610746565b604051630fb9ff9560e41b8152600490fd5b6105059150833d85116104d0576104c181836106e1565b386103b2565b346100bd5760003660031901126100bd576040517f000000000000000000000000ac5fe73e2829eef6b408f349e936f9a817e6850d6001600160a01b03168152602090f35b346100bd5760203660031901126100bd576105696100c2565b33600090815260208190526040902061058c90610587908390610435565b61095c565b9091506001600160d01b0316156106345765ffffffffffff6105bb6105b042610906565b65ffffffffffff1690565b911614610622573360009081526020819052604090206105ef906105e0908390610435565b6105e942610906565b90610c48565b50506001600160a01b0316337f1629cd9ad365627cf8408d19c50224af8f3213c1a18ae48062d92e22bddf7de5600080a3005b60405163291bf4b760e21b8152600490fd5b6040516334b09b3960e11b8152600490fd5b6001600160a01b039081166000908152602081815260408083209490931682529290925281208054806106845750506001905b818060d01b03161490565b6000199181838101116106a55783602091600195522001015460301c610679565b61076e565b634e487b7160e01b600052604160045260246000fd5b6040810190811067ffffffffffffffff8211176106dc57604052565b6106aa565b90601f8019910116810190811067ffffffffffffffff8211176106dc57604052565b60405190610710826106c0565b565b67ffffffffffffffff81116106dc57601f01601f191660200190565b908160209103126100bd575180151581036100bd5790565b6040513d6000823e3d90fd5b908160209103126100bd575163ffffffff811681036100bd5790565b634e487b7160e01b600052601160045260246000fd5b6000198101919082116106a557565b90600163ffffffff809316019182116106a557565b9180511561088f57806020806107c393518301019101610752565b916107ce83826108b2565b926107df845165ffffffffffff1690565b9065ffffffffffff80851692168281146108765782119182610821575b505061080f5761080c92506109bf565b90565b5050602001516001600160d01b031690565b90915061082e8354610784565b63ffffffff831614918215610847575b505038806107fc565b61086e91925061086261085c6105b092610793565b856108b2565b5165ffffffffffff1690565b11388061083e565b50505050602091909101516001600160d01b0316905090565b5061080c916109bf565b604051906108a6826106c0565b60006020838281520152565b6108d7916108d1916108c2610899565b506108cb610899565b50610aa8565b50610938565b65ffffffffffff81511690602060018060d01b0391015116604051916108fc836106c0565b8252602082015290565b65ffffffffffff90818111610919571690565b604490604051906306dfcc6560e41b8252603060048301526024820152fd5b90604051610945816106c0565b915465ffffffffffff8116835260301c6020830152565b805480610970575050600090600090600090565b6000199080828101116106a5576109909260005260206000200101610938565b9065ffffffffffff82511691602060018060d01b0391015116906001929190565b90600182018092116106a557565b908154906000918060058111610a5c575b50915b828110610a0c575050806109e8575050600090565b61080c916109f8610a0592610784565b9060005260206000200190565b5460301c90565b90918082169080831860011c82018092116106a5578460005265ffffffffffff8083602060002001541690851610600014610a4a5750915b906109d3565b929150610a56906109b1565b90610a44565b90610a6682610d29565b82039182116106a5578460005265ffffffffffff8083602060002001541690841610600014610a9757505b386109d0565b9250610aa2906109b1565b91610a91565b8054821015610ac05760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b8054680100000000000000008110156106dc57610af891600182018155610aa8565b919091610b2357805160209091015160301b65ffffffffffff191665ffffffffffff91909116179055565b634e487b7160e01b600052600060045260246000fd5b8054908115610c2357610b63610b5e610b5184610784565b8360005260206000200190565b610938565b90610b74825165ffffffffffff1690565b65ffffffffffff8581169491168410610c1157610bdc94602094610ba16105b0865165ffffffffffff1690565b03610be25750610bce916109f8610bb792610784565b660100000000000065ffffffffffff825416179055565b01516001600160d01b031690565b90600190565b9050610c0c91610c01610bf3610703565b65ffffffffffff9093168352565b600182860152610ad6565b610bce565b604051632520601d60e01b8152600490fd5b610c40929150610c34610bf3610703565b60016020830152610ad6565b600090600190565b8054908115610ce457610c60610b5e610b5184610784565b90610c71825165ffffffffffff1690565b65ffffffffffff8581169491168410610c1157610cc294602094610c9e6105b0865165ffffffffffff1690565b03610cc85750610bce916109f8610cb492610784565b65ffffffffffff8154169055565b90600090565b9050610c0c91610cd9610bf3610703565b600085830152610ad6565b610d01929150610cf5610bf3610703565b60006020830152610ad6565b600090600090565b8115610d13570490565b634e487b7160e01b600052601260045260246000fd5b8015610e635780610dfc610df5610deb610de1610dd7610dcd610dc3610db9600161080c9a6000908b60801c80610e57575b508060401c80610e4a575b508060201c80610e3d575b508060101c80610e30575b508060081c80610e23575b508060041c80610e16575b508060021c80610e09575b50821c610e02575b811c1b610db2818b610d09565b0160011c90565b610db2818a610d09565b610db28189610d09565b610db28188610d09565b610db28187610d09565b610db28186610d09565b610db28185610d09565b8092610d09565b90610e69565b8101610da5565b6002915091019038610d9d565b6004915091019038610d92565b6008915091019038610d87565b6010915091019038610d7c565b6020915091019038610d71565b6040915091019038610d66565b91505060809038610d5b565b50600090565b9080821015610e76575090565b90509056fea2646970667358221220f934a4509108cb0ba2614a3bfa56243799c5cfc49ad23c4cdaa6d4d5b296019264736f6c63430008190033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000ac5fe73e2829eef6b408f349e936f9a817e6850d00000000000000000000000090d6bf17d26d975611f20a8e2f15df8eb957526d
-----Decoded View---------------
Arg [0] : whoRegistry (address): 0xAc5Fe73E2829EeF6B408f349E936f9a817e6850D
Arg [1] : whereRegistry (address): 0x90D6BF17d26D975611f20A8e2f15dF8eb957526D
-----Encoded View---------------
2 Constructor Arguments found :
Arg [0] : 000000000000000000000000ac5fe73e2829eef6b408f349e936f9a817e6850d
Arg [1] : 00000000000000000000000090d6bf17d26d975611f20a8e2f15df8eb957526d
Loading...
Loading
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.