- The example hook Counter.sol demonstrates the
beforeSwap()
andafterSwap()
hooks - The test template Counter.t.sol preconfigures the v4 pool manager, test tokens, and test liquidity.
requires foundry
forge install
forge test
Because v4 exceeds the bytecode limit of Ethereum and it's business licensed, we can only deploy & test hooks on anvil.
# start anvil, with a larger code limit
anvil --code-size-limit 30000
# in a new terminal
forge script script/Counter.s.sol \
--rpc-url http://localhost:8545 \
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--code-size-limit 30000 \
--broadcast
When installing dependencies with forge install
, Github may throw a Permission Denied
error
Typically caused by missing Github SSH keys, and can be resolved by following the steps here
Or adding the keys to your ssh-agent, if you have already uploaded SSH keys
Hook deployment failures are caused by incorrect flags or incorrect salt mining
- Verify the flags are in agreement:
getHookCalls()
returns the correct flagsflags
provided toHookMiner.find(...)
- In obscure cases where you're deploying multiple hooks (with the same flags), try setting
seed=1000
forHookMiner.find
- Verify salt mining is correct:
- In forge test: the deployer for:
new Hook{salt: salt}(...)
andHookMiner.find(deployer, ...)
are the same. This will beaddress(this)
. If usingvm.prank
, the deployer will be the pranking address - In forge script: the deployer must be the CREATE2 Proxy:
0x4e59b44847b379578588920cA78FbF26c0B4956C
- If anvil does not have the CREATE2 deployer, your foundry may be out of date. You can update it with
foundryup
- If anvil does not have the CREATE2 deployer, your foundry may be out of date. You can update it with
- In forge test: the deployer for:
Additional resources:
v4-periphery contains advanced hook implementations that serve as a great reference