Skip to content

A decentralized finance protocol that enables users to earn funding fees on GMX V2 by maintaining a delta-neutral position strategy.

Notifications You must be signed in to change notification settings

roudra323/Delta-Neutral-Vault-GMX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delta-Neutral Vault GMX

A decentralized finance protocol that enables users to earn funding fees on GMX V2 by maintaining a delta-neutral position strategy.

Overview

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.

Architecture

Funding Fee Vault Architecture

The protocol consists of three main components:

1. Vault Contract

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 shares
  • withdraw() - Withdraw WETH by burning shares (requires execution fee)
  • cancel() - Cancel pending withdrawal orders
  • claim() - Claim WETH from executed withdrawal orders

Admin Actions:

  • Transfer funds between vault and strategy
  • Manage execution fees (increase, decrease, claim)

2. Strategy Contract

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 positions
  • decrease() - Close/reduce hedged positions
  • claim() - Claim collateral from executed orders
  • cancel() - Cancel pending orders
  • Execution fee management and refunds

3. WithdrawCallback Contract

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 withdrawals
  • afterOrderCancellation() - Handle cancelled orders
  • afterOrderFrozen() - Handle frozen orders
  • refundExecutionFee() - Return unused execution fees

How It Works

User → Vault → Strategy → GMX V2
 ↓                          ↓
WETH                   Market Orders
 ↓                          ↓
Shares              Delta-Neutral Position
                             ↓
                    WithdrawCallback ← GMX
                             ↓
                      WETH (to User)
  1. Deposit Flow: Users deposit WETH into the Vault and receive shares proportional to their contribution
  2. Strategy Execution: Admin transfers WETH to Strategy, which creates hedged positions on GMX
  3. Funding Fee Accumulation: The delta-neutral position earns funding fees over time
  4. Withdrawal Flow: Users request withdrawals, Strategy decreases positions, and WithdrawCallback handles the GMX callback to return WETH

Delta-Neutral Strategy

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.

Key Benefits

  • 🛡️ 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

Smart Contracts

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

Technology Stack

  • Solidity: 0.8.26
  • Framework: Foundry
  • Protocol: GMX V2 (Arbitrum)
  • Oracles: Chainlink Data Streams
  • Token: WETH (Wrapped Ether)

Development

Prerequisites

Setup

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

Environment Setup

Create 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

Testing

⚠️ Important: All tests require forking Arbitrum mainnet as they interact with live GMX V2 contracts.

Run Tests

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

Test Suites

  • Strategy.test.sol - Tests for position management and GMX integration
  • Vault.test.sol - Tests for deposits, withdrawals, and share accounting
  • WithdrawCallback.test.sol - Tests for GMX callback handling
  • VaultAndStrategy.test.sol - Integration tests for the complete flow

Security Considerations

⚠️ This project is for educational purposes

  • 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

License

MIT

Acknowledgments

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.

About

A decentralized finance protocol that enables users to earn funding fees on GMX V2 by maintaining a delta-neutral position strategy.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published