From 2c777e8ff2f280ea4138aaa97eb4d535b65fce9c Mon Sep 17 00:00:00 2001 From: devtooligan Date: Fri, 16 Jun 2023 19:51:05 -0700 Subject: [PATCH] fix: add code size check --- .gitmodules | 9 +++-- README.md | 4 +-- foundry.toml | 2 +- script/Deploy.s.sol | 2 +- src/HookDeployerLib.sol | 0 src/ReferenceImplementations/SaltMiner.sol | 2 +- src/SaltMiner.huff | 18 ++++++++++ test/SaltMiner.t.sol | 38 ++++++++++++++++++---- 8 files changed, 61 insertions(+), 14 deletions(-) delete mode 100644 src/HookDeployerLib.sol diff --git a/.gitmodules b/.gitmodules index 33e8fe4..ded3488 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,9 +4,12 @@ [submodule "lib/forge-std"] path = lib/forge-std url = https://github.com/foundry-rs/forge-std -[submodule "lib/v4-periphery"] - path = lib/v4-periphery - url = https://github.com/Uniswap/v4-periphery [submodule "lib/v4-core"] path = lib/v4-core url = https://github.com/Uniswap/v4-core +[submodule "lib/openzeppelin-contracts"] + path = lib/openzeppelin-contracts + url = https://github.com/OpenZeppelin/openzeppelin-contracts +[submodule "lib/v4-periphery"] + path = lib/v4-periphery + url = https://github.com/Uniswap/v4-periphery diff --git a/README.md b/README.md index f8d9b06..d937d49 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# huff-project-template • [![ci](https://github.com/huff-language/huff-project-template/actions/workflows/ci.yaml/badge.svg)](https://github.com/huff-language/huff-project-template/actions/workflows/ci.yaml) ![license](https://img.shields.io/github/license/huff-language/huff-project-template.svg) ![solidity](https://img.shields.io/badge/solidity-^0.8.15-lightgrey) +# huff-project-template • [![ci](https://github.com/huff-language/huff-project-template/actions/workflows/ci.yaml/badge.svg)](https://github.com/huff-language/huff-project-template/actions/workflows/ci.yaml) ![license](https://img.shields.io/github/license/huff-language/huff-project-template.svg) ![solidity](https://img.shields.io/badge/solidity-^0.8.19-lightgrey) Versatile Huff Project Template using Foundry. @@ -11,7 +11,7 @@ Versatile Huff Project Template using Foundry. The following will need to be installed in order to use this template. Please follow the links and instructions. -- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) +- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) - You'll know you've done it right if you can run `git --version` - [Foundry / Foundryup](https://github.com/gakonst/foundry) - This will install `forge`, `cast`, and `anvil` diff --git a/foundry.toml b/foundry.toml index 6750a9a..e7eafaf 100644 --- a/foundry.toml +++ b/foundry.toml @@ -1,6 +1,6 @@ [profile.default] -solc_version = '0.8.15' +solc_version = '0.8.19' auto_detect_solc = false optimizer = true optimizer_runs = 200 # Default amount diff --git a/script/Deploy.s.sol b/script/Deploy.s.sol index 1b204c8..5052374 100644 --- a/script/Deploy.s.sol +++ b/script/Deploy.s.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Unlicense -pragma solidity ^0.8.15; +pragma solidity ^0.8.19; import "foundry-huff/HuffDeployer.sol"; import "forge-std/Script.sol"; diff --git a/src/HookDeployerLib.sol b/src/HookDeployerLib.sol deleted file mode 100644 index e69de29..0000000 diff --git a/src/ReferenceImplementations/SaltMiner.sol b/src/ReferenceImplementations/SaltMiner.sol index fd8cea8..39fa9c0 100644 --- a/src/ReferenceImplementations/SaltMiner.sol +++ b/src/ReferenceImplementations/SaltMiner.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: UNLICENSED -pragma solidity =0.8.15; +pragma solidity =0.8.19; contract HookMineAndSinker { uint256 constant PREFIX_MASK = 0x00ff00000000000000000000000000000000000000; diff --git a/src/SaltMiner.huff b/src/SaltMiner.huff index 6134280..626fcd8 100644 --- a/src/SaltMiner.huff +++ b/src/SaltMiner.huff @@ -1,3 +1,17 @@ +/* + +███████╗ █████╗ ██╗ ████████╗███╗ ███╗██╗███╗ ██╗███████╗██████╗ +██╔════╝██╔══██╗██║ ╚══██╔══╝████╗ ████║██║████╗ ██║██╔════╝██╔══██╗ +███████╗███████║██║ ██║ ██╔████╔██║██║██╔██╗ ██║█████╗ ██████╔╝ +╚════██║██╔══██║██║ ██║ ██║╚██╔╝██║██║██║╚██╗██║██╔══╝ ██╔══██╗ +███████║██║ ██║███████╗██║ ██║ ╚═╝ ██║██║██║ ╚████║███████╗██║ ██║ +╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝ + +*/ + +/// @notice A utility for finding the salt for a CREATE2 address for a UniV4 Hook +/// @author devtooligan + /* Interface */ #define function mineSalt(bytes32,uint256,address) view returns (uint256,address) @@ -32,6 +46,10 @@ [PREFIX_MASK] and // [maskedAddress, newAddress, prefix] dup3 // [prefix, maskedAddress, newAddress, prefix] eq // [matchesPrefix, newAddress, prefix] + dup2 // [newAddress, matchesPrefix, newAddress, prefix] + extcodesize // [codeSize, matchesPrefix, newAddress, prefix] + iszero // [noCode, matchesPrefix, newAddress, prefix] + and // [matchesPrefix && noCode, newAddress, prefix] found jumpi // [newAddress, prefix] // use remaining gas as new salt, cheaper than maintining a counter diff --git a/test/SaltMiner.t.sol b/test/SaltMiner.t.sol index 01747b2..aec54bb 100644 --- a/test/SaltMiner.t.sol +++ b/test/SaltMiner.t.sol @@ -1,24 +1,50 @@ // SPDX-License-Identifier: Unlicense -pragma solidity ^0.8.15; +pragma solidity ^0.8.19; import "foundry-huff/HuffDeployer.sol"; +import {HookDeployer} from "../src/HookDeployer.sol"; +import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol"; +import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol"; +import {PoolManager} from "@uniswap/v4-core/contracts/PoolManager.sol"; +import {LimitOrder} from "@uniswap/v4-periphery/contracts/hooks/examples/LimitOrder.sol"; import "forge-std/Test.sol"; import "forge-std/console.sol"; contract SaltMinerTest is Test { - /// @dev Address of the SaltMiner contract. ISaltMiner public saltMiner; + PoolManager manager; /// @dev Setup the testing environment. function setUp() public { saltMiner = ISaltMiner(HuffDeployer.deploy("SaltMiner")); + manager = new PoolManager(500000); + } /// @dev Ensure that you can set and get the value. - function testMineSalt() public { - (uint256 salt, address newAddress) = saltMiner.mineSalt(keccak256("myCode"), uint(0x004400000000000000000000000000000000000000), address(this)); - console.log(salt); - console.log(newAddress); + function testMineSalt__LimitOrder() public { + // LimitOrder + bytes memory initCode = abi.encodePacked(type(LimitOrder).creationCode, abi.encode(manager)); + bytes32 initCodeHash = keccak256(initCode); + uint prefix = HookDeployer.getPrefix(Hooks.Calls({ + beforeInitialize: false, + afterInitialize: true, + beforeModifyPosition: false, + afterModifyPosition: false, + beforeSwap: false, + afterSwap: true, + beforeDonate: false, + afterDonate: false + })); + + uint start = gasleft(); + (uint256 salt, address expectedAddress) = saltMiner.mineSalt(initCodeHash, prefix, address(this)); + console.log("gas used: %s", salt); + address actualAddress = HookDeployer.deployHook(initCode, salt); + + // console.log(salt); + // console.log(expectedAddress); + // console.log(actualAddress); } }