Transaction Hash:
Block:
21507615 at Dec-29-2024 10:44:59 AM +UTC
Transaction Fee:
0.000100849224044134 ETH
$0.27
Gas Used:
26,374 Gas / 3.823812241 Gwei
Emitted Events:
98 |
EGGX.Approval( owner=[Sender] 0x36e578b3560d2a7b6a946a55f33d46ac6ab82e31, spender=0x40aA958d...a374bcD7f, amount=0 )
|
Account State Difference:
Address | Before | After | State Difference | ||
---|---|---|---|---|---|
0x36e578B3...C6AB82e31 |
0.019364388053473286 Eth
Nonce: 20
|
0.019263538829429152 Eth
Nonce: 21
| 0.000100849224044134 | ||
0x4838B106...B0BAD5f97
Miner
| (Titan Builder) | 11.689055171231271349 Eth | 11.689073983131879389 Eth | 0.00001881190060804 | |
0xe2f95ee8...909A6e6b3 |
Execution Trace
EGGX.approve( spender=0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f, amountOrId=0 ) => ( True )
{"egg404-2.sol":{"content":"//SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.0;\n\nimport \"./ERC404.sol\";\nimport \"./strings.sol\";\n\ncontract EGGX is ERC404 {\n string public dataURI;\n string public baseTokenURI;\n\n constructor(address _owner) ERC404(\"EGGX\", \"EGGX\", 18, 100000000, _owner) {\n balanceOf[_owner] = 100000000 * 10 ** 18;\n } \n\n function setDataURI(string memory _dataURI) public onlyOwner {\n dataURI = _dataURI;\n }\n\n function setTokenURI(string memory _tokenURI) public onlyOwner {\n baseTokenURI = _tokenURI;\n }\n\n function setNameSymbol(\n string memory _name,\n string memory _symbol\n ) public onlyOwner {\n _setNameSymbol(_name, _symbol);\n }\n\n function _getImage(uint256 id) internal pure returns (string memory, string memory, string memory, string memory) {\n uint8 seed1 = uint8(bytes1(keccak256(abi.encodePacked(id))));\n uint8 seed2 = uint8(bytes1(keccak256(abi.encodePacked(id + 101))));\n uint8 seed3 = uint8(bytes1(keccak256(abi.encodePacked(id + 202))));\n \n \n string memory color;\n string memory pattern;\n string memory wings;\n\n if (seed1 \u003c= 13) {\n // 5 / 100 * 255 = 13\n pattern = \"scale\";\n } else if (seed1 \u003c= 33) {\n // 13 / 100 * 255 = 33\n pattern = \"nebula\";\n } else if (seed1 \u003c= 61) {\n // 24 / 100 * 255 = 61\n pattern = \"star\";\n } else if (seed1 \u003c= 97) {\n // 38 / 100 * 255 = 97\n pattern = \"cloud\";\n } else if (seed1 \u003c= 140) {\n // 55 / 100 * 255 = 140\n pattern = \"mountain\";\n } else if (seed1 \u003c= 194) {\n // 76 / 100 * 255 = 194\n pattern = \"river\";\n } else {\n pattern = \"flower\";\n }\n \n if (seed2 \u003c= 56) {\n color = \"red\";\n } else if (seed2 \u003c= 107 ) {\n color = \"blue\";\n } else if (seed2 \u003c= 153 ) {\n color = \"green\";\n } else if (seed2 \u003c= 194) {\n color = \"yellow\";\n } else if (seed2 \u003c= 227) {\n color = \"black\";\n } else {\n color = \"camouflage\";\n }\n\n if (seed3 \u003c= 204) {\n wings = \"wingless\";\n } else {\n wings = \"winged\";\n }\n\n string memory _t1 = string.concat(\u0027egg-\u0027, pattern);\n string memory _t2 = string.concat(_t1, color);\n string memory _t3 = string.concat(_t2, wings);\n\n string memory image = string.concat(_t3, \u0027.jpg\u0027);\n return (image, color, pattern, wings);\n }\n\n function tokenURI(uint256 id) public view override returns (string memory) {\n if (bytes(baseTokenURI).length \u003e 0) {\n return string.concat(baseTokenURI, Strings.toString(id));\n } else {\n string memory image;\n string memory color;\n string memory pattern;\n string memory wings;\n\n (image, color, pattern, wings) = _getImage(id);\n\n string memory jsonPreImage = string.concat(\n string.concat(\n string.concat(\u0027{\"name\": \"EGGX #\u0027, Strings.toString(id)),\n \u0027\",\"description\":\"A collection of 10,000 EGGX NFTs enabled by an adjustedversion of ERC404, an experimental token standardenabling persistent liquidity and semi-fungibility forEthereum NFTs.\",\"external_url\":\"https://eggs.build\",\"image\":\"\u0027\n ),\n string.concat(dataURI, image)\n );\n string memory jsonPostImage = string.concat(\n \u0027\",\"attributes\":[{\"trait_type\":\"Color\",\"value\":\"\u0027,\n color\n );\n string memory jsonPostImage1 = string.concat(\n \u0027\"},{\"trait_type\":\"Pattern\",\"value\":\"\u0027,\n pattern\n );\n \n string memory j1 = string.concat(jsonPostImage, jsonPostImage1);\n\n string memory jsonPostImage2 = string.concat(\n \u0027\"},{\"trait_type\":\"Wings\",\"value\":\"\u0027,\n wings\n );\n\n string memory j2 = string.concat(j1, jsonPostImage2);\n\n string memory jsonPostTraits = \u0027\"}]}\u0027;\n\n return\n string.concat(\n \"data:application/json;utf8,\",\n string.concat(\n string.concat(jsonPreImage, j2),\n jsonPostTraits\n )\n );\n }\n }\n}\n"},"ERC404.sol":{"content":"//SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.0;\n\nabstract contract Ownable {\n event OwnershipTransferred(address indexed user, address indexed newOwner);\n\n error Unauthorized();\n error InvalidOwner();\n\n address public owner;\n\n modifier onlyOwner() virtual {\n if (msg.sender != owner) revert Unauthorized();\n\n _;\n }\n\n constructor(address _owner) {\n if (_owner == address(0)) revert InvalidOwner();\n\n owner = _owner;\n\n emit OwnershipTransferred(address(0), _owner);\n }\n\n function transferOwnership(address _owner) public virtual onlyOwner {\n if (_owner == address(0)) revert InvalidOwner();\n\n owner = _owner;\n\n emit OwnershipTransferred(msg.sender, _owner);\n }\n\n function revokeOwnership() public virtual onlyOwner {\n owner = address(0);\n\n emit OwnershipTransferred(msg.sender, address(0));\n }\n}\n\nabstract contract ERC721Receiver {\n function onERC721Received(\n address,\n address,\n uint256,\n bytes calldata\n ) external virtual returns (bytes4) {\n return ERC721Receiver.onERC721Received.selector;\n }\n}\n\n/// @notice ERC404\n/// A gas-efficient, mixed ERC20 / ERC721 implementation\n/// with native liquidity and fractionalization.\n///\n/// This is an experimental standard designed to integrate\n/// with pre-existing ERC20 / ERC721 support as smoothly as\n/// possible.\n///\n/// @dev In order to support full functionality of ERC20 and ERC721\n/// supply assumptions are made that slightly constraint usage.\n/// Ensure decimals are sufficiently large (standard 18 recommended)\n/// as ids are effectively encoded in the lowest range of amounts.\n///\n/// NFTs are spent on ERC20 functions in a FILO queue, this is by\n/// design.\n///\nabstract contract ERC404 is Ownable {\n // Events\n event ERC20Transfer(\n address indexed from,\n address indexed to,\n uint256 amount\n );\n event Approval(\n address indexed owner,\n address indexed spender,\n uint256 amount\n );\n event Transfer(\n address indexed from,\n address indexed to,\n uint256 indexed id\n );\n event ERC721Approval(\n address indexed owner,\n address indexed spender,\n uint256 indexed id\n );\n event ApprovalForAll(\n address indexed owner,\n address indexed operator,\n bool approved\n );\n\n // Errors\n error NotFound();\n error AlreadyExists();\n error InvalidRecipient();\n error InvalidSender();\n error UnsafeRecipient();\n\n // Metadata\n /// @dev Token name\n string public name;\n\n /// @dev Token symbol\n string public symbol;\n\n /// @dev Decimals for fractional representation\n uint8 public immutable decimals;\n\n /// @dev Total supply in fractionalized representation\n uint256 public immutable totalSupply;\n\n /// @dev Current mint counter, monotonically increasing to ensure accurate ownership\n uint256 public minted;\n\n // Mappings\n /// @dev Balance of user in fractional representation\n mapping(address =\u003e uint256) public balanceOf;\n\n /// @dev Allowance of user in fractional representation\n mapping(address =\u003e mapping(address =\u003e uint256)) public allowance;\n\n /// @dev Approval in native representaion\n mapping(uint256 =\u003e address) public getApproved;\n\n /// @dev Approval for all in native representation\n mapping(address =\u003e mapping(address =\u003e bool)) public isApprovedForAll;\n\n /// @dev Owner of id in native representation\n mapping(uint256 =\u003e address) internal _ownerOf;\n\n /// @dev Array of owned ids in native representation\n mapping(address =\u003e uint256[]) internal _owned;\n\n /// @dev Tracks indices for the _owned mapping\n mapping(uint256 =\u003e uint256) internal _ownedIndex;\n\n /// @dev Addresses whitelisted from minting / burning for gas savings (pairs, routers, etc)\n mapping(address =\u003e bool) public whitelist;\n\n // Constructor\n constructor(\n string memory _name,\n string memory _symbol,\n uint8 _decimals,\n uint256 _totalNativeSupply,\n address _owner\n ) Ownable(_owner) {\n name = _name;\n symbol = _symbol;\n decimals = _decimals;\n totalSupply = _totalNativeSupply * (10 ** decimals);\n }\n\n /// @notice Initialization function to set pairs / etc\n /// saving gas by avoiding mint / burn on unnecessary targets\n function setWhitelist(address target, bool state) public onlyOwner {\n whitelist[target] = state;\n }\n\n /// @notice Function to find owner of a given native token\n function ownerOf(uint256 id) public view virtual returns (address owner) {\n owner = _ownerOf[id];\n\n if (owner == address(0)) {\n revert NotFound();\n }\n }\n\n /// @notice tokenURI must be implemented by child contract\n function tokenURI(uint256 id) public view virtual returns (string memory);\n\n /// @notice Function for token approvals\n /// @dev This function assumes id / native if amount less than or equal to current max id\n function approve(\n address spender,\n uint256 amountOrId\n ) public virtual returns (bool) {\n if (amountOrId \u003c= minted \u0026\u0026 amountOrId \u003e 0) {\n address owner = _ownerOf[amountOrId];\n\n if (msg.sender != owner \u0026\u0026 !isApprovedForAll[owner][msg.sender]) {\n revert Unauthorized();\n }\n\n getApproved[amountOrId] = spender;\n\n emit Approval(owner, spender, amountOrId);\n } else {\n allowance[msg.sender][spender] = amountOrId;\n\n emit Approval(msg.sender, spender, amountOrId);\n }\n\n return true;\n }\n\n /// @notice Function native approvals\n function setApprovalForAll(address operator, bool approved) public virtual {\n isApprovedForAll[msg.sender][operator] = approved;\n\n emit ApprovalForAll(msg.sender, operator, approved);\n }\n\n /// @notice Function for mixed transfers\n /// @dev This function assumes id / native if amount less than or equal to current max id\n function transferFrom(\n address from,\n address to,\n uint256 amountOrId\n ) public virtual {\n if (amountOrId \u003c= minted) {\n if (from != _ownerOf[amountOrId]) {\n revert InvalidSender();\n }\n\n if (to == address(0)) {\n revert InvalidRecipient();\n }\n\n if (\n msg.sender != from \u0026\u0026\n !isApprovedForAll[from][msg.sender] \u0026\u0026\n msg.sender != getApproved[amountOrId]\n ) {\n revert Unauthorized();\n }\n\n balanceOf[from] -= _getUnit();\n\n unchecked {\n balanceOf[to] += _getUnit();\n }\n\n _ownerOf[amountOrId] = to;\n delete getApproved[amountOrId];\n\n // update _owned for sender\n uint256 updatedId = _owned[from][_owned[from].length - 1];\n _owned[from][_ownedIndex[amountOrId]] = updatedId;\n // pop\n _owned[from].pop();\n // update index for the moved id\n _ownedIndex[updatedId] = _ownedIndex[amountOrId];\n // push token to to owned\n _owned[to].push(amountOrId);\n // update index for to owned\n _ownedIndex[amountOrId] = _owned[to].length - 1;\n\n emit Transfer(from, to, amountOrId);\n emit ERC20Transfer(from, to, _getUnit());\n } else {\n uint256 allowed = allowance[from][msg.sender];\n\n if (allowed != type(uint256).max)\n allowance[from][msg.sender] = allowed - amountOrId;\n\n _transfer(from, to, amountOrId);\n }\n }\n\n /// @notice Function for fractional transfers\n function transfer(\n address to,\n uint256 amount\n ) public virtual returns (bool) {\n return _transfer(msg.sender, to, amount);\n }\n\n /// @notice Function for native transfers with contract support\n function safeTransferFrom(\n address from,\n address to,\n uint256 id\n ) public virtual {\n transferFrom(from, to, id);\n\n if (\n to.code.length != 0 \u0026\u0026\n ERC721Receiver(to).onERC721Received(msg.sender, from, id, \"\") !=\n ERC721Receiver.onERC721Received.selector\n ) {\n revert UnsafeRecipient();\n }\n }\n\n /// @notice Function for native transfers with contract support and callback data\n function safeTransferFrom(\n address from,\n address to,\n uint256 id,\n bytes calldata data\n ) public virtual {\n transferFrom(from, to, id);\n\n if (\n to.code.length != 0 \u0026\u0026\n ERC721Receiver(to).onERC721Received(msg.sender, from, id, data) !=\n ERC721Receiver.onERC721Received.selector\n ) {\n revert UnsafeRecipient();\n }\n }\n\n /// @notice Internal function for fractional transfers\n function _transfer(\n address from,\n address to,\n uint256 amount\n ) internal returns (bool) {\n uint256 unit = _getUnit();\n uint256 balanceBeforeSender = balanceOf[from];\n uint256 balanceBeforeReceiver = balanceOf[to];\n\n balanceOf[from] -= amount;\n\n unchecked {\n balanceOf[to] += amount;\n }\n\n // Skip burn for certain addresses to save gas\n if (!whitelist[from]) {\n uint256 tokens_to_burn = (balanceBeforeSender / unit) -\n (balanceOf[from] / unit);\n for (uint256 i = 0; i \u003c tokens_to_burn; i++) {\n _burn(from);\n }\n }\n\n // Skip minting for certain addresses to save gas\n if (!whitelist[to]) {\n uint256 tokens_to_mint = (balanceOf[to] / unit) -\n (balanceBeforeReceiver / unit);\n for (uint256 i = 0; i \u003c tokens_to_mint; i++) {\n _mint(to);\n }\n }\n\n emit ERC20Transfer(from, to, amount);\n return true;\n }\n\n // Internal utility logic\n function _getUnit() internal view returns (uint256) {\n return 10000 * 10 ** decimals;\n }\n\n function _mint(address to) internal virtual {\n if (to == address(0)) {\n revert InvalidRecipient();\n }\n\n unchecked {\n minted++;\n }\n\n uint256 id = minted;\n\n if (_ownerOf[id] != address(0)) {\n revert AlreadyExists();\n }\n\n _ownerOf[id] = to;\n _owned[to].push(id);\n _ownedIndex[id] = _owned[to].length - 1;\n\n emit Transfer(address(0), to, id);\n }\n\n function _burn(address from) internal virtual {\n if (from == address(0)) {\n revert InvalidSender();\n }\n\n uint256 id = _owned[from][_owned[from].length - 1];\n _owned[from].pop();\n delete _ownedIndex[id];\n delete _ownerOf[id];\n delete getApproved[id];\n\n emit Transfer(from, address(0), id);\n }\n\n function _setNameSymbol(\n string memory _name,\n string memory _symbol\n ) internal {\n name = _name;\n symbol = _symbol;\n }\n}\n"},"Math.sol":{"content":"// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Standard math utilities missing in the Solidity language.\n */\nlibrary Math {\n /**\n * @dev Muldiv operation overflow.\n */\n error MathOverflowedMulDiv();\n\n enum Rounding {\n Floor, // Toward negative infinity\n Ceil, // Toward positive infinity\n Trunc, // Toward zero\n Expand // Away from zero\n }\n\n /**\n * @dev Returns the addition of two unsigned integers, with an overflow flag.\n */\n function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n uint256 c = a + b;\n if (c \u003c a) return (false, 0);\n return (true, c);\n }\n }\n\n /**\n * @dev Returns the subtraction of two unsigned integers, with an overflow flag.\n */\n function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b \u003e a) return (false, 0);\n return (true, a - b);\n }\n }\n\n /**\n * @dev Returns the multiplication of two unsigned integers, with an overflow flag.\n */\n function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n // Gas optimization: this is cheaper than requiring \u0027a\u0027 not being zero, but the\n // benefit is lost if \u0027b\u0027 is also tested.\n // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522\n if (a == 0) return (true, 0);\n uint256 c = a * b;\n if (c / a != b) return (false, 0);\n return (true, c);\n }\n }\n\n /**\n * @dev Returns the division of two unsigned integers, with a division by zero flag.\n */\n function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b == 0) return (false, 0);\n return (true, a / b);\n }\n }\n\n /**\n * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.\n */\n function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b == 0) return (false, 0);\n return (true, a % b);\n }\n }\n\n /**\n * @dev Returns the largest of two numbers.\n */\n function max(uint256 a, uint256 b) internal pure returns (uint256) {\n return a \u003e b ? a : b;\n }\n\n /**\n * @dev Returns the smallest of two numbers.\n */\n function min(uint256 a, uint256 b) internal pure returns (uint256) {\n return a \u003c b ? a : b;\n }\n\n /**\n * @dev Returns the average of two numbers. The result is rounded towards\n * zero.\n */\n function average(uint256 a, uint256 b) internal pure returns (uint256) {\n // (a + b) / 2 can overflow.\n return (a \u0026 b) + (a ^ b) / 2;\n }\n\n /**\n * @dev Returns the ceiling of the division of two numbers.\n *\n * This differs from standard division with `/` in that it rounds towards infinity instead\n * of rounding towards zero.\n */\n function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {\n if (b == 0) {\n // Guarantee the same behavior as in a regular Solidity division.\n return a / b;\n }\n\n // (a + b - 1) / b can overflow on addition, so we distribute.\n return a == 0 ? 0 : (a - 1) / b + 1;\n }\n\n /**\n * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or\n * denominator == 0.\n * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by\n * Uniswap Labs also under MIT license.\n */\n function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {\n unchecked {\n // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use\n // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256\n // variables such that product = prod1 * 2^256 + prod0.\n uint256 prod0 = x * y; // Least significant 256 bits of the product\n uint256 prod1; // Most significant 256 bits of the product\n assembly {\n let mm := mulmod(x, y, not(0))\n prod1 := sub(sub(mm, prod0), lt(mm, prod0))\n }\n\n // Handle non-overflow cases, 256 by 256 division.\n if (prod1 == 0) {\n // Solidity will revert if denominator == 0, unlike the div opcode on its own.\n // The surrounding unchecked block does not change this fact.\n // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.\n return prod0 / denominator;\n }\n\n // Make sure the result is less than 2^256. Also prevents denominator == 0.\n if (denominator \u003c= prod1) {\n revert MathOverflowedMulDiv();\n }\n\n ///////////////////////////////////////////////\n // 512 by 256 division.\n ///////////////////////////////////////////////\n\n // Make division exact by subtracting the remainder from [prod1 prod0].\n uint256 remainder;\n assembly {\n // Compute remainder using mulmod.\n remainder := mulmod(x, y, denominator)\n\n // Subtract 256 bit number from 512 bit number.\n prod1 := sub(prod1, gt(remainder, prod0))\n prod0 := sub(prod0, remainder)\n }\n\n // Factor powers of two out of denominator and compute largest power of two divisor of denominator.\n // Always \u003e= 1. See https://cs.stackexchange.com/q/138556/92363.\n\n uint256 twos = denominator \u0026 (0 - denominator);\n assembly {\n // Divide denominator by twos.\n denominator := div(denominator, twos)\n\n // Divide [prod1 prod0] by twos.\n prod0 := div(prod0, twos)\n\n // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.\n twos := add(div(sub(0, twos), twos), 1)\n }\n\n // Shift in bits from prod1 into prod0.\n prod0 |= prod1 * twos;\n\n // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such\n // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for\n // four bits. That is, denominator * inv = 1 mod 2^4.\n uint256 inverse = (3 * denominator) ^ 2;\n\n // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel\u0027s lifting lemma, this also\n // works in modular arithmetic, doubling the correct bits in each step.\n inverse *= 2 - denominator * inverse; // inverse mod 2^8\n inverse *= 2 - denominator * inverse; // inverse mod 2^16\n inverse *= 2 - denominator * inverse; // inverse mod 2^32\n inverse *= 2 - denominator * inverse; // inverse mod 2^64\n inverse *= 2 - denominator * inverse; // inverse mod 2^128\n inverse *= 2 - denominator * inverse; // inverse mod 2^256\n\n // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.\n // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is\n // less than 2^256, this is the final result. We don\u0027t need to compute the high bits of the result and prod1\n // is no longer required.\n result = prod0 * inverse;\n return result;\n }\n }\n\n /**\n * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.\n */\n function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {\n uint256 result = mulDiv(x, y, denominator);\n if (unsignedRoundsUp(rounding) \u0026\u0026 mulmod(x, y, denominator) \u003e 0) {\n result += 1;\n }\n return result;\n }\n\n /**\n * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded\n * towards zero.\n *\n * Inspired by Henry S. Warren, Jr.\u0027s \"Hacker\u0027s Delight\" (Chapter 11).\n */\n function sqrt(uint256 a) internal pure returns (uint256) {\n if (a == 0) {\n return 0;\n }\n\n // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.\n //\n // We know that the \"msb\" (most significant bit) of our target number `a` is a power of 2 such that we have\n // `msb(a) \u003c= a \u003c 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.\n //\n // This can be rewritten `2**log2(a) \u003c= a \u003c 2**(log2(a) + 1)`\n // → `sqrt(2**k) \u003c= sqrt(a) \u003c sqrt(2**(k+1))`\n // → `2**(k/2) \u003c= sqrt(a) \u003c 2**((k+1)/2) \u003c= 2**(k/2 + 1)`\n //\n // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.\n uint256 result = 1 \u003c\u003c (log2(a) \u003e\u003e 1);\n\n // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,\n // since it is the square root of a uint256. Newton\u0027s method converges quadratically (precision doubles at\n // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision\n // into the expected uint128 result.\n unchecked {\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n result = (result + a / result) \u003e\u003e 1;\n return min(result, a / result);\n }\n }\n\n /**\n * @notice Calculates sqrt(a), following the selected rounding direction.\n */\n function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {\n unchecked {\n uint256 result = sqrt(a);\n return result + (unsignedRoundsUp(rounding) \u0026\u0026 result * result \u003c a ? 1 : 0);\n }\n }\n\n /**\n * @dev Return the log in base 2 of a positive value rounded towards zero.\n * Returns 0 if given 0.\n */\n function log2(uint256 value) internal pure returns (uint256) {\n uint256 result = 0;\n unchecked {\n if (value \u003e\u003e 128 \u003e 0) {\n value \u003e\u003e= 128;\n result += 128;\n }\n if (value \u003e\u003e 64 \u003e 0) {\n value \u003e\u003e= 64;\n result += 64;\n }\n if (value \u003e\u003e 32 \u003e 0) {\n value \u003e\u003e= 32;\n result += 32;\n }\n if (value \u003e\u003e 16 \u003e 0) {\n value \u003e\u003e= 16;\n result += 16;\n }\n if (value \u003e\u003e 8 \u003e 0) {\n value \u003e\u003e= 8;\n result += 8;\n }\n if (value \u003e\u003e 4 \u003e 0) {\n value \u003e\u003e= 4;\n result += 4;\n }\n if (value \u003e\u003e 2 \u003e 0) {\n value \u003e\u003e= 2;\n result += 2;\n }\n if (value \u003e\u003e 1 \u003e 0) {\n result += 1;\n }\n }\n return result;\n }\n\n /**\n * @dev Return the log in base 2, following the selected rounding direction, of a positive value.\n * Returns 0 if given 0.\n */\n function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {\n unchecked {\n uint256 result = log2(value);\n return result + (unsignedRoundsUp(rounding) \u0026\u0026 1 \u003c\u003c result \u003c value ? 1 : 0);\n }\n }\n\n /**\n * @dev Return the log in base 10 of a positive value rounded towards zero.\n * Returns 0 if given 0.\n */\n function log10(uint256 value) internal pure returns (uint256) {\n uint256 result = 0;\n unchecked {\n if (value \u003e= 10 ** 64) {\n value /= 10 ** 64;\n result += 64;\n }\n if (value \u003e= 10 ** 32) {\n value /= 10 ** 32;\n result += 32;\n }\n if (value \u003e= 10 ** 16) {\n value /= 10 ** 16;\n result += 16;\n }\n if (value \u003e= 10 ** 8) {\n value /= 10 ** 8;\n result += 8;\n }\n if (value \u003e= 10 ** 4) {\n value /= 10 ** 4;\n result += 4;\n }\n if (value \u003e= 10 ** 2) {\n value /= 10 ** 2;\n result += 2;\n }\n if (value \u003e= 10 ** 1) {\n result += 1;\n }\n }\n return result;\n }\n\n /**\n * @dev Return the log in base 10, following the selected rounding direction, of a positive value.\n * Returns 0 if given 0.\n */\n function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {\n unchecked {\n uint256 result = log10(value);\n return result + (unsignedRoundsUp(rounding) \u0026\u0026 10 ** result \u003c value ? 1 : 0);\n }\n }\n\n /**\n * @dev Return the log in base 256 of a positive value rounded towards zero.\n * Returns 0 if given 0.\n *\n * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.\n */\n function log256(uint256 value) internal pure returns (uint256) {\n uint256 result = 0;\n unchecked {\n if (value \u003e\u003e 128 \u003e 0) {\n value \u003e\u003e= 128;\n result += 16;\n }\n if (value \u003e\u003e 64 \u003e 0) {\n value \u003e\u003e= 64;\n result += 8;\n }\n if (value \u003e\u003e 32 \u003e 0) {\n value \u003e\u003e= 32;\n result += 4;\n }\n if (value \u003e\u003e 16 \u003e 0) {\n value \u003e\u003e= 16;\n result += 2;\n }\n if (value \u003e\u003e 8 \u003e 0) {\n result += 1;\n }\n }\n return result;\n }\n\n /**\n * @dev Return the log in base 256, following the selected rounding direction, of a positive value.\n * Returns 0 if given 0.\n */\n function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {\n unchecked {\n uint256 result = log256(value);\n return result + (unsignedRoundsUp(rounding) \u0026\u0026 1 \u003c\u003c (result \u003c\u003c 3) \u003c value ? 1 : 0);\n }\n }\n\n /**\n * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.\n */\n function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {\n return uint8(rounding) % 2 == 1;\n }\n}\n"},"SignedMath.sol":{"content":"// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Standard signed math utilities missing in the Solidity language.\n */\nlibrary SignedMath {\n /**\n * @dev Returns the largest of two signed numbers.\n */\n function max(int256 a, int256 b) internal pure returns (int256) {\n return a \u003e b ? a : b;\n }\n\n /**\n * @dev Returns the smallest of two signed numbers.\n */\n function min(int256 a, int256 b) internal pure returns (int256) {\n return a \u003c b ? a : b;\n }\n\n /**\n * @dev Returns the average of two signed numbers without overflow.\n * The result is rounded towards zero.\n */\n function average(int256 a, int256 b) internal pure returns (int256) {\n // Formula from the book \"Hacker\u0027s Delight\"\n int256 x = (a \u0026 b) + ((a ^ b) \u003e\u003e 1);\n return x + (int256(uint256(x) \u003e\u003e 255) \u0026 (a ^ b));\n }\n\n /**\n * @dev Returns the absolute unsigned value of a signed value.\n */\n function abs(int256 n) internal pure returns (uint256) {\n unchecked {\n // must be unchecked in order to support `n = type(int256).min`\n return uint256(n \u003e= 0 ? n : -n);\n }\n }\n}\n"},"strings.sol":{"content":"\n// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/Strings.sol)\n\npragma solidity ^0.8.20;\n\nimport {Math} from \"./Math.sol\";\nimport {SignedMath} from \"./SignedMath.sol\";\n\n/**\n * @dev String operations.\n */\nlibrary Strings {\n bytes16 private constant HEX_DIGITS = \"0123456789abcdef\";\n uint8 private constant ADDRESS_LENGTH = 20;\n\n /**\n * @dev The `value` string doesn\u0027t fit in the specified `length`.\n */\n error StringsInsufficientHexLength(uint256 value, uint256 length);\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` decimal representation.\n */\n function toString(uint256 value) internal pure returns (string memory) {\n unchecked {\n uint256 length = Math.log10(value) + 1;\n string memory buffer = new string(length);\n uint256 ptr;\n /// @solidity memory-safe-assembly\n assembly {\n ptr := add(buffer, add(32, length))\n }\n while (true) {\n ptr--;\n /// @solidity memory-safe-assembly\n assembly {\n mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))\n }\n value /= 10;\n if (value == 0) break;\n }\n return buffer;\n }\n }\n\n /**\n * @dev Converts a `int256` to its ASCII `string` decimal representation.\n */\n function toStringSigned(int256 value) internal pure returns (string memory) {\n return string.concat(value \u003c 0 ? \"-\" : \"\", toString(SignedMath.abs(value)));\n }\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.\n */\n function toHexString(uint256 value) internal pure returns (string memory) {\n unchecked {\n return toHexString(value, Math.log256(value) + 1);\n }\n }\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.\n */\n function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {\n uint256 localValue = value;\n bytes memory buffer = new bytes(2 * length + 2);\n buffer[0] = \"0\";\n buffer[1] = \"x\";\n for (uint256 i = 2 * length + 1; i \u003e 1; --i) {\n buffer[i] = HEX_DIGITS[localValue \u0026 0xf];\n localValue \u003e\u003e= 4;\n }\n if (localValue != 0) {\n revert StringsInsufficientHexLength(value, length);\n }\n return string(buffer);\n }\n\n /**\n * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal\n * representation.\n */\n function toHexString(address addr) internal pure returns (string memory) {\n return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);\n }\n\n /**\n * @dev Returns true if the two strings are equal.\n */\n function equal(string memory a, string memory b) internal pure returns (bool) {\n return bytes(a).length == bytes(b).length \u0026\u0026 keccak256(bytes(a)) == keccak256(bytes(b));\n }\n}\n"}}