Skip to content

tyllenb/v4-btc-hook

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WBTC-Hook

v4-wbtc-hook

This Hook Template Includes a Hook Concept for only allowing WBTC Pools To Be Created

The v4-wbtc-hook is a specialized Uniswap v4 hook implementation designed with a specific focus: it enforces that only pools containing Wrapped Bitcoin (WBTC) can be created. This constraint ensures that liquidity pools are limited to those involving WBTC, thereby catering to a Bitcoin-centric trading ecosystem on Uniswap v4.

Key Features:

  • WBTC Pool Restriction: The hook logic in v4-btc-hook checks the currencies involved in the pool during initialization. If either of the pool's currencies is not WBTC, the initialization is reverted, effectively preventing the creation of non-WBTC pools.
  • Flexible Integration: This hook can be integrated into existing Uniswap v4 infrastructure, allowing for seamless deployment and interaction with the Uniswap v4 protocol.

Applications:

  • Bitcoin-Centric DeFi Applications: Ideal for DeFi platforms looking to focus exclusively on Bitcoin as a trading asset.
  • Specialized Liquidity Pools: Enables the creation of liquidity pools for projects or platforms that want to ensure WBTC is always part of their trading pairs.
  • Enhanced Trading Experience: Traders and liquidity providers interested in WBTC can operate in an environment tailored to their preferences.

This hook was created using the Uniswap Foundation V4 Template which you can learn more about below!

v4-template

A template for writing Uniswap v4 Hooks 🦄

Use this Template

  1. The example hook Counter.sol demonstrates the beforeSwap() and afterSwap() hooks
  2. The test template Counter.t.sol preconfigures the v4 pool manager, test tokens, and test liquidity.
  3. The scripts in the v4-template are written so that you can
    • Designed for Goerli, but usable for other networks
    • Deploy a hook contract
    • Create a liquidity pool on V4
    • Add liquidity to a pool
    • Swap tokens on a pool
  4. This template is built using Foundry
Updating to v4-template:latest

This template is actively maintained -- you can update the v4 dependencies, scripts, and helpers:

git remote add template https://github.com/uniswapfoundation/v4-template
git fetch template
git merge template/main <BRANCH> --allow-unrelated-histories

Linux / WSL2 (TSTORE/TLOAD)

Please update foundry.toml to use the linux solc

Mac users do not need to change anything by default

Set up

requires foundry

forge install
forge test

Local Development (Anvil)

Because v4 depends on TSTORE and its business licensed, you can only deploy & test hooks on anvil

# start anvil with TSTORE support
# (`foundryup`` to update if cancun is not an option)
anvil --hardfork cancun

# in a new terminal
forge script script/Anvil.s.sol \
    --rpc-url http://localhost:8545 \
    --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
    --broadcast

Goerli Testnet

NOTE: 11/21/2023, the Goerli deployment is out of sync with the latest v4. It is recommend to use local testing instead

For testing on Goerli Testnet the Uniswap Foundation team has deployed a slimmed down version of the V4 contract (due to current contract size limits) on the network.

The relevant addresses for testing on Goerli are the ones below

POOL_MANAGER = 0x3A9D48AB9751398BbFa63ad67599Bb04e4BdF98b
POOL_MODIFY_POSITION_TEST = 0x83feDBeD11B3667f40263a88e8435fca51A03F8C
SWAP_ROUTER = 0xF8AADC65Bf1Ec1645ef931317fD48ffa734a185c

Update the following command with your own private key:

forge script script/00_Counter.s.sol \
--rpc-url https://rpc.ankr.com/eth_goerli \
--private-key [your_private_key_on_goerli_here] \
--broadcast

Deploying your own Tokens For Testing

Because V4 is still in testing mode, most networks don't have liquidity pools live on V4 testnets. We recommend launching your own test tokens and expirementing with them that. We've included in the templace a Mock UNI and Mock USDC contract for easier testing. You can deploy the contracts and when you do you'll have 1 million mock tokens to test with for each contract. See deployment commands below

forge create script/mocks/mUNI.sol:MockUNI \
--rpc-url [your_rpc_url_here] \
--private-key [your_private_key_on_goerli_here]
forge create script/mocks/mUSDC.sol:MockUSDC \
--rpc-url [your_rpc_url_here] \
--private-key [your_private_key_on_goerli_here]

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

Or adding the keys to your 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:

v4-periphery contains advanced hook implementations that serve as a great reference

v4-core

About

Template repository for writing Uniswap v4 Hooks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Solidity 100.0%