A smart contract system designed to hedge impermanent loss for Uniswap V2 liquidity providers using perpetual futures on Binance. 📈
This project implements an automated trading bot that:
- 🔄 Monitors Uniswap V2 LP positions
- 🧮 Calculates impermanent loss in real-time
- 🛡️ Manages hedging positions
- 🤖 Automatically rebalances when needed
- TradingBot:🎮 Main contract handling LP positions and hedging strategy
- ImpermanentLossCalculator:🔢 Calculates IL and optimal hedge positions
- PriceCalculator:💹 Handles price calculations and TWAP
- HedgingStrategy:🎯 Implements the hedging logic and decision making
# Clone the repository
git clone https://github.com/yourusername/trading-bot-hedging.git
cd trading-bot-hedging
# Install dependencies
forge install
Create a .env file :
PRIVATE_KEY=your_private_key_without_0x
RPC_URL=your_sepolia_rpc_url
ETHERSCAN_API_KEY=your_etherscan_api_key
Deploy all contracts to Sepolia testnet:
forge script script/Deploy.s.sol:DeployScript --rpc-url $RPC_URL --broadcast --verify
📝 ImpermanentLossCalculator: 0x426638105fbd5B246068DF0cA672366b50255820 🧮 PriceCalculator: 0xEBD5B476aEd1a7f8301575270D75edefA0dbFf5E 🛡️ HedgingStrategy: 0xda4E73cfED29b817BE36A07bd82958BF9322f74a 🤖 TradingBot: 0x1bce275dC0f3bDe0e96B60fDe8f4dE12a4dEaBbd
Opening a Position 📈:
cast send $TRADING_BOT_ADDRESS "openPosition(uint256,uint256,uint256)" \
$TOKEN0_AMOUNT $TOKEN1_AMOUNT $MIN_LIQUIDITY \
--rpc-url $RPC_URL --private-key $PRIVATE_KEY
Checking Rebalance Status 🔍:
Copycast call $TRADING_BOT_ADDRESS "checkRebalance()" --rpc-url $RPC_URL
Closing a Position 📉:
Copycast send $TRADING_BOT_ADDRESS "closePosition(uint256,uint256)" \
$MIN_TOKEN0 $MIN_TOKEN1 \
--rpc-url $RPC_URL --private-key $PRIVATE_KEY
Run the test suite:
Copyforge test
With verbosity for more details:
Copyforge test -vv
graph TD
A[TradingBot] --> B[ImpermanentLossCalculator]
A --> C[PriceCalculator]
A --> D[HedgingStrategy]
B --> E[Uniswap V2 Pool]
C --> E
D --> B
D --> C
This project includes several security features:
🛡️ Reentrancy protection 🔐 Access control ✅ Input validation 🔰 Slippage protection
- 🍴 Fork the repository
- 🌿 Create your feature branch (git checkout -b feature/amazing-feature)
- 💾 Commit your changes (git commit -m 'Add some amazing feature')
- 🚀 Push to the branch (git push origin feature/amazing-feature)
- 🎯 Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details. Acknowledgments
🦊 OpenZeppelin for secure contract implementations 🦄 Uniswap V2 for DEX infrastructure ⚒️ Foundry for development framework