// SPDX-License-Identifier: MIT pragma solidity >=0.6.2 <0.9.0; import "./console2.sol"; abstract contract StdUtils { uint256 private constant UINT256_MAX = 115792089237316195423570985008687907853269984665640564039457584007913129639935; function _bound(uint256 x, uint256 min, uint256 max) internal pure virtual returns (uint256 result) { require(min <= max, "StdUtils bound(uint256,uint256,uint256): Max is less than min."); // If x is between min and max, return x directly. This is to ensure that dictionary values // do not get shifted if the min is nonzero. More info: https://github.com/foundry-rs/forge-std/issues/188 if (x >= min && x <= max) return x; uint256 size = max - min + 1; // If the value is 0, 1, 2, 3, warp that to min, min+1, min+2, min+3. Similarly for the UINT256_MAX side. // This helps ensure coverage of the min/max values. if (x <= 3 && size > x) return min + x; if (x >= UINT256_MAX - 3 && size > UINT256_MAX - x) return max - (UINT256_MAX - x); // Otherwise, wrap x into the range [min, max], i.e. the range is inclusive. if (x > max) { uint256 diff = x - max; uint256 rem = diff % size; if (rem == 0) return max; result = min + rem - 1; } else if (x < max) { uint256 diff = min - x; uint256 rem = diff % size; if (rem == 0) return min; result = max - rem + 1; } } function bound(uint256 x, uint256 min, uint256 max) internal view virtual returns (uint256 result) { result = _bound(x, min, max); console2.log("Bound Result", result); } /// @dev Compute the address a contract will be deployed at for a given deployer address and nonce /// @notice adapated from Solmate implementation (https://github.com/Rari-Capital/solmate/blob/main/src/utils/LibRLP.sol) function computeCreateAddress(address deployer, uint256 nonce) internal pure virtual returns (address) { // forgefmt: disable-start // The integer zero is treated as an empty byte string, and as a result it only has a length prefix, 0x80, computed via 0x80 + 0. // A one byte integer uses its own value as its length prefix, there is no additional "0x80 + length" prefix that comes before it. if (nonce == 0x00) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, bytes1(0x80)))); if (nonce <= 0x7f) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, uint8(nonce)))); // Nonces greater than 1 byte all follow a consistent encoding scheme, where each value is preceded by a prefix of 0x80 + length. if (nonce <= 2**8 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployer, bytes1(0x81), uint8(nonce)))); if (nonce <= 2**16 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployer, bytes1(0x82), uint16(nonce)))); if (nonce <= 2**24 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployer, bytes1(0x83), uint24(nonce)))); // forgefmt: disable-end // More details about RLP encoding can be found here: https://eth.wiki/fundamentals/rlp // 0xda = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x84 ++ nonce) // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex) // 0x84 = 0x80 + 0x04 (0x04 = the bytes length of the nonce, 4 bytes, in hex) // We assume nobody can have a nonce large enough to require more than 32 bytes. return addressFromLast20Bytes( keccak256(abi.encodePacked(bytes1(0xda), bytes1(0x94), deployer, bytes1(0x84), uint32(nonce))) ); } function computeCreate2Address(bytes32 salt, bytes32 initcodeHash, address deployer) internal pure virtual returns (address) { return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xff), deployer, salt, initcodeHash))); } function addressFromLast20Bytes(bytes32 bytesValue) private pure returns (address) { return address(uint160(uint256(bytesValue))); } }