Skip to content

Repository files navigation

Demo Video

https://youtu.be/Ejt-1HBWPbI

Market Sentiment Dynamic Fee Hook

This is a Uniswap V4 hook that dynamically adjusts pool fees based on market sentiment data from CoinMarketCap. The hook DECREASES fees during bullish market conditions and INCREASES them during bearish conditions in order to optimize pool performance!

Features

  • Dynamic fee adjustment based on market sentiment
  • Integration with CoinMarketCap API for real-time market data
  • Configurable sentiment thresholds and fee ranges
  • Pausable oracle for emergency situations
  • Confidence scoring for sentiment data

Prerequisites

  • Node.js v16 or higher
  • A CoinMarketCap API key

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/sweet-hook.git
cd sweet-hook
  1. Install dependencies:
npm install
  1. Copy the environment file and fill in your values:
cp .env.example .env

Configuration

Edit the .env file with your values:

  • COINMARKETCAP_API_KEY: Your CoinMarketCap API key
  • ORACLE_ADDRESS: The deployed MarketSentimentOracle contract address
  • MAINNET_RPC_URL: Your Ethereum node RPC URL
  • PRIVATE_KEY: Your wallet private key (for deployment)

Usage

First deploy the poolmanager

npx hardhat run scripts/deploy-poolmanager.ts --network unichain-sepolia

Then deploy the Contracts

  1. Deploy the MarketSentimentOracle:
npx hardhat run scripts/deploy-oracle.ts --network mainnet
  1. Deploy the MarketSentimentHook:
npx hardhat run scripts/deploy-hook.ts --network mainnet
  1. Deploy the hook
npx hardhat run scripts/create-pool.ts --network unichain-sepolia

Updating Market Sentiment

Run the script to update market sentiment data:

npm run update-sentiment

This script will:

  1. Fetch current market data from CoinMarketCap
  2. Calculate sentiment based on price and volume changes
  3. Update the oracle contract with new sentiment values

Fee Adjustment Logic

The hook adjusts fees based on the following rules:

  • Base fee: 0.03%
  • Maximum fee: 1%
  • Minimum fee: 0.03%
  • Bullish threshold: 70 (decreases fee by 20%)
  • Bearish threshold: 30 (increases fee by 20%)

Security Considerations

  • The oracle is pausable in case of emergencies
  • Sentiment data has a maximum age of 1 hour
  • Confidence scoring helps filter out unreliable data
  • Only the owner can update sentiment data

License

MIT

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.
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

Check Forge Installation

Ensure that you have correctly installed Foundry (Forge) Stable. You can update Foundry by running:

foundryup

v4-template appears to be incompatible with Foundry Nightly. See foundry announcements to revert back to the stable build

Set up

requires foundry

forge install
forge test

Local Development (Anvil)

Other than writing unit tests (recommended!), you can only deploy & test hooks on anvil

# 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/ 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

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:

Uniswap v4 docs

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

v4-core

v4-by-example

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

Generated from Uniswap/v4-template