Skip to content

yug49/OptionAMM

Repository files navigation

OptionAMM

It is a decentralized options trading platform built with Solidity using Foundry and a modern Next.js frontend.

  • It is a comprehensive Automated Market Maker (AMM) for options trading that I developed leveraging Foundry and React.
  • It consists of multiple sub-components:
    • Smart Contract (OptionAMM): Core options trading contract with liquidity management
    • Frontend Application: Modern Next.js interface for seamless options trading
    • Liquidity Management: Users can provide liquidity and earn profits from options trading
    • Real-time Price Feeds: Integration with Chainlink oracles for accurate price data
  • The platform enables users to create and trade both call and put options with customizable parameters including strike price, lot size, premium, and expiry.
  • It implements advanced DeFi strategies including:
    • Automated options creation and settlement
    • Dynamic premium calculation with minimum thresholds
    • Liquidity pool management for options backing
    • Real-time ETH/USD price integration via Chainlink
  • Supports Ethereum Mainnet and Sepolia testnet with configurable network settings.

Key Features

1. Options Trading

  • Call & Put Options: Create and trade both types of options
  • Flexible Parameters: Customizable strike price, lot size, premium, and expiry
  • Option Settlement: Options can be settled after expiry whenever owner wants based on current market price

2. Liquidity Management

  • Liquidity Provision: Users can add USDC liquidity to back options
  • Dynamic Management: Add and remove liquidity as needed

3. Real-time Price Integration

  • Chainlink Oracles: Reliable ETH/USD price feeds
  • Gas Optimized: Price fetching optimized to use view functions (0 gas cost)
  • Accurate Settlement: Options settle based on real market prices

Architecture

Smart Contract (dexCode/)

  • OptionAMM Contract: Main trading contract handling options creation, trading, and settlement
  • IERC20 Integration: USDC token support for premium payments and settlements
  • Chainlink Integration: Real-time price feeds for accurate option pricing

Frontend (option-amm-app/)

  • Next.js Application: Modern React-based user interface
  • Wallet Integration: RainbowKit for seamless wallet connections
  • Real-time Updates: Live price feeds and portfolio tracking
  • Responsive Design: Mobile-friendly interface

Getting Started

  • git: You'll know you did it right if you can run git --version and you see a response like git version x.x.x
  • foundry: You'll know you did it right if you can run forge --version and you see a response like forge 0.2.0 (816e00b 2023-03-16T00:05:26.396218Z)
  • Node.js: You'll know you did it right if you can run node --version and you see a response like v18.x.x
  • npm: You'll know you did it right if you can run npm --version and you see a response like 8.x.x

Installation

  • Install OptionAMM
    git clone https://github.com/yug49/OptionAMM
    cd OptionAMM

Smart Contract Setup (dexCode/)

  • Navigate to the smart contract directory
    cd dexCode
  • Make a .env file
    touch .env
  • Open the .env file and fill in the details similar to:
    SEPOLIA_RPC_URL=<YOUR SEPOLIA RPC URL>
    PRIVATE_KEY=<YOUR PRIVATE KEY>
    ETHERSCAN_API_KEY=<YOUR ETHERSCAN API KEY>
  • Install dependencies and libraries
    forge install
  • Build Smart Contract
    forge build

Frontend Setup (option-amm-app/)

  • Navigate to the frontend directory
    cd ../option-amm-app
  • Install frontend dependencies
    npm install
  • Create frontend environment file
    touch .env.local
  • Configure your .env.local file:
    NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=<YOUR_WALLETCONNECT_PROJECT_ID>
    NEXT_PUBLIC_CONTRACT_ADDRESS=<DEPLOYED_CONTRACT_ADDRESS>

Deployment

Smart Contract Deployment

For Sepolia testnet deployment:

    cd dexCode
    forge script script/DeployOptionAMM.s.sol --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY --etherscan-api-key $ETHERSCAN_API_KEY --broadcast --verify --legacy -vvv

once deployed, grab the deployed contract address of your OptionAMM and replace the previous one in option-amm-app/.env.local

Frontend Deployment

  • Build the frontend application:
    cd option-amm-app
    npm run build
  • Start the development server:
    npm run dev
  • Access the application at http://localhost:3000

Usage

Creating Options

  1. Connect Wallet: Use a browser or external wallet to connect with the app
  2. Set Parameters: Choose strike price, lot size, premium, and expiry
  3. Create Option: Submit transaction to create the option
  4. Monitor: Track your created options in the dashboard

Trading Options

  1. Browse Options: View available options in the marketplace
  2. Purchase Options: Buy options by paying the premium
  3. Manage Portfolio: Track your option positions
  4. Settlement: Options can be settled after expiry

Providing Liquidity

  1. Add Liquidity: Deposit USDC to the liquidity pool
  2. Manage Position: Add or remove liquidity as needed

Smart Contract Tests

    cd dexCode
    source .env
    forge test --fork-url $SEPOLIA_RPC_URL

Configuration

Network Setup

  • The platform supports Sepolia testnet
  • Network configuration is handled in the frontend application
  • Update contract addresses in the frontend environment variables

Price Feed Configuration

  • Chainlink ETH/USD price feeds are configured in the smart contract
  • Supports multiple networks with different price feed addresses
  • Automatic price updates in the market info tab after every 30 seconds.

🔗 Links

Loved it? Let's connect on:

twitter linkedin