A decentralized finance protocol that enables users to earn funding fees on GMX V2 by maintaining a delta-neutral position strategy.
The Delta-Neutral Vault is a sophisticated DeFi protocol built on top of GMX V2 that allows users to deposit WETH and earn funding fees while maintaining a market-neutral position. The protocol requires admin management to actively maintain hedged positions and eliminate directional market risk.
The protocol consists of three main components:
The main entry point for user interactions, managing deposits, withdrawals, and share accounting.
Key Features:
- User deposits and withdrawals in WETH
- ERC20-like share token system
- Execution fee management for GMX operations
- Secure order tracking and validation
User Actions:
deposit()- Deposit WETH to receive vault shareswithdraw()- Withdraw WETH by burning shares (requires execution fee)cancel()- Cancel pending withdrawal ordersclaim()- Claim WETH from executed withdrawal orders
Admin Actions:
- Transfer funds between vault and strategy
- Manage execution fees (increase, decrease, claim)
Executes the delta-neutral strategy by managing positions on GMX V2.
Key Features:
- Creates and manages market increase/decrease orders on GMX
- Maintains delta-neutral positions through automated hedging
- Calculates total portfolio value including unrealized PnL
- Integrates with GMX order handlers and oracles
Operations:
increase()- Open/increase hedged positionsdecrease()- Close/reduce hedged positionsclaim()- Claim collateral from executed orderscancel()- Cancel pending orders- Execution fee management and refunds
Handles GMX order execution callbacks for withdrawal operations.
Responsibilities:
- Processes successful order executions
- Handles order cancellations and frozen orders
- Manages execution fee refunds to users
- Distributes WETH to withdrawing users and vault
Callback Handlers:
afterOrderExecution()- Process successful withdrawalsafterOrderCancellation()- Handle cancelled ordersafterOrderFrozen()- Handle frozen ordersrefundExecutionFee()- Return unused execution fees
User → Vault → Strategy → GMX V2
↓ ↓
WETH Market Orders
↓ ↓
Shares Delta-Neutral Position
↓
WithdrawCallback ← GMX
↓
WETH (to User)
- Deposit Flow: Users deposit WETH into the Vault and receive shares proportional to their contribution
- Strategy Execution: Admin transfers WETH to Strategy, which creates hedged positions on GMX
- Funding Fee Accumulation: The delta-neutral position earns funding fees over time
- Withdrawal Flow: Users request withdrawals, Strategy decreases positions, and WithdrawCallback handles the GMX callback to return WETH
The protocol maintains a delta-neutral position through admin-managed operations:
- Holding long positions in the underlying asset (ETH)
- Simultaneously holding equivalent short positions
- Admin actively balances collateral to minimize directional exposure
- Earning funding fees from the perpetual futures market
This approach allows users to earn yields from funding rates without exposure to price volatility, while the admin handles position management.
- 🛡️ Market Neutral: Eliminates directional price risk through hedging
- 💰 Funding Fee Rewards: Earn from GMX V2 funding rates
- 🔒 Non-Custodial: Users maintain control through share tokens
- 👨💼 Admin Managed: Dedicated admin handles position rebalancing
- 🔐 Secure: Multi-contract architecture with access controls
| Contract | Purpose |
|---|---|
Vault.sol |
User interface and share management |
Strategy.sol |
Position management and GMX integration |
WithdrawCallback.sol |
GMX callback handler for withdrawals |
GmxHelper.sol |
Helper functions for GMX V2 interactions |
- Solidity: 0.8.26
- Framework: Foundry
- Protocol: GMX V2 (Arbitrum)
- Oracles: Chainlink Data Streams
- Token: WETH (Wrapped Ether)
- Foundry
- Git
# Clone the repository
git clone https://github.com/roudra323/Delta-Neutral-Vault-GMX.git
cd Delta-Neutral-Vault-GMX
# Install dependencies
forge install
# Build contracts
forge buildCreate a .env file with required variables for testing:
# Arbitrum RPC URL for forking (required for tests)
FORK_URL=https://arb1.arbitrum.io/rpc
# or use a provider like Alchemy/Infura:
# FORK_URL=https://arb-mainnet.g.alchemy.com/v2/YOUR_API_KEY# Load environment variables
source .env
# Get current block number
FORK_BLOCK_NUM=$(cast block-number --rpc-url $FORK_URL)
# Run all test suites
forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/Strategy.test.sol -vvv
forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/WithdrawCallback.test.sol -vvv
forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/Vault.test.sol -vvv
forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/VaultAndStrategy.test.sol -vvvStrategy.test.sol- Tests for position management and GMX integrationVault.test.sol- Tests for deposits, withdrawals, and share accountingWithdrawCallback.test.sol- Tests for GMX callback handlingVaultAndStrategy.test.sol- Integration tests for the complete flow
- All contracts should undergo professional security audits before mainnet deployment
- Execution fees are required for GMX operations and can fluctuate
- Smart contract risk exists in both this protocol and underlying GMX protocol
- Oracle dependencies introduce additional trust assumptions
MIT
Built with GMX V2 on Arbitrum. Part of the Cyfrin Updraft course curriculum.
Disclaimer: This is experimental software. Use at your own risk. Not financial advice.
