Providing a hedging mechanism on the Ethereum blockchain. When certain price triggers are hit, the contract either performs a hedge by swapping tokens or unwinds the hedge. The contract integrates with the Uniswap V4 core and leverages the BaseHook
mechanism for after-swap activities.
Please follow these steps, if you all ready have Foundry setup ignore steps 1 and 2
Step 1: Install Rust https://doc.rust-lang.org/book/ch01-01-installation.html
Step 2: Install Foundry https://book.getfoundry.sh/getting-started/installation#using-foundryup
Step 3: Build
Please run the command below to build your contracts
forge build
If you get a stack to deep error try running the command below
forge build --via-ir
Step 4: Test
Please run the command below to test your contracts, the given tests are examples please generate your own.
forge test
-
BaseHook Integration: Inherits from the
BaseHook
contract to interface with Uniswap V4 core functionality. -
Price Triggers: Users can set price triggers which, when breached, will initiate a hedge or unwind the hedge based on conditions.
-
Binary Search Algorithm: Implements a binary search method to efficiently determine the index to insert a new price trigger.
-
Swap Mechanism: Utilizes the Uniswap V4 pool to swap tokens based on set triggers.
-
SafeERC20: Uses the
SafeERC20
library from OpenZeppelin to ensure safe operations with ERC20 tokens.
- Trigger: Contains details about the hedging trigger, including the price, unwind conditions, and associated token details.
- setTrigger: Allows users to set a price trigger with specified conditions.
- _findIndex: Implements a binary search algorithm to find the index for inserting a price in an ordered array.
- _performHedge: Executes the hedge based on the given triggers.
- _performUnwind: Unwinds the hedge based on the given triggers.
- lockAcquiredUnwind: Unwinds the hedge by swapping back the tokens when called.
- lockAcquiredHedge: Initiates the hedge by swapping tokens when called.
- afterSwap: Executed after a swap on the Uniswap V4 pool. It checks price conditions and decides whether to hedge or unwind.
The contract imports and interacts with various other contracts and libraries, such as:
- Uniswap V4 Core: For core functionalities related to pools, swaps, and liquidity.
- OpenZeppelin: For standard interfaces (
IERC20
) and utility libraries (SafeERC20
).
- Setting Triggers: Users can define their hedging triggers by calling the
setTrigger
function. - Trading: The main functionality revolves around the
afterSwap
function. After a token swap in the associated Uniswap V4 pool, the function checks if any hedge or unwind conditions are met and acts accordingly.
- Users need to be aware of the hedging conditions they set and the implications of those conditions.
- The hedging mechanism relies on the price of the token in the Uniswap V4 pool.