Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uniswapfoundation/v4-template
Template repository for writing Uniswap v4 Hooks
https://github.com/uniswapfoundation/v4-template
Last synced: about 1 month ago
JSON representation
Template repository for writing Uniswap v4 Hooks
- Host: GitHub
- URL: https://github.com/uniswapfoundation/v4-template
- Owner: uniswapfoundation
- License: mit
- Created: 2023-06-13T17:56:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-07T18:57:24.000Z (7 months ago)
- Last Synced: 2024-05-21T01:09:14.317Z (7 months ago)
- Language: Solidity
- Homepage:
- Size: 122 KB
- Stars: 190
- Watchers: 3
- Forks: 20
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-uniswap-v4-hooks - Saucepoint's Template
- awesome-uniswap-hooks - Uniswap Foundation's Template
- awesome-uniswap-v4-resources - Uniswap Foundation's Template
- awesome-uniswap-v4-resources - Uniswap Foundation's Template
README
# v4-template
### **A template for writing Uniswap v4 Hooks 🦄**[`Use this Template`](https://github.com/uniswapfoundation/v4-template/generate)
1. The example hook [Counter.sol](src/Counter.sol) demonstrates the `beforeSwap()` and `afterSwap()` hooks
2. The test template [Counter.t.sol](test/Counter.t.sol) preconfigures the v4 pool manager, test tokens, and test liquidity.Updating to v4-template:latest
This template is actively maintained -- you can update the v4 dependencies, scripts, and helpers:
```bash
git remote add template https://github.com/uniswapfoundation/v4-template
git fetch template
git merge template/main --allow-unrelated-histories
```---
### Check Forge Installation
*Ensure that you have correctly installed Foundry (Forge) and that it's up to date. You can update Foundry by running:*```
foundryup
```## Set up
*requires [foundry](https://book.getfoundry.sh)*
```
forge install
forge test
```### Local Development (Anvil)
Other than writing unit tests (recommended!), you can only deploy & test hooks on [anvil](https://book.getfoundry.sh/anvil/)
```bash
# start anvil, a local EVM chain
anvil# in a new terminal
forge script script/Anvil.s.sol \
--rpc-url http://localhost:8545 \
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--broadcast
```See [script/](script/) for hook deployment, pool creation, liquidity provision, and swapping.
---
Troubleshooting
### *Permission Denied*
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](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh)
Or [adding the keys to your ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent), if you have already uploaded SSH keys
### Hook deployment failures
Hook deployment failures are caused by incorrect flags or incorrect salt mining
1. Verify the flags are in agreement:
* `getHookCalls()` returns the correct flags
* `flags` provided to `HookMiner.find(...)`
2. Verify salt mining is correct:
* In **forge test**: the *deployer* for: `new Hook{salt: salt}(...)` and `HookMiner.find(deployer, ...)` are the same. This will be `address(this)`. If using `vm.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`---
Additional resources:
[Uniswap v4 docs](https://docs.uniswap.org/contracts/v4/overview)
[v4-periphery](https://github.com/uniswap/v4-periphery) contains advanced hook implementations that serve as a great reference
[v4-core](https://github.com/uniswap/v4-core)
[v4-by-example](https://v4-by-example.org)