Skip to content

L9T-Development/DeXFun-smart-contract-hyperliquid

Repository files navigation

dFun Synthetics Contracts

A comprehensive decentralized exchange (DEX) protocol for synthetic asset trading built on Solidity. This project provides a complete infrastructure for trading synthetic assets with advanced features including order management, position tracking, liquidation, and fee distribution.

Contact

If you are interested in building perp dex trading dapp, Contact here: Telegram | Twitter

πŸš€ Features

  • Synthetic Asset Trading: Trade synthetic versions of popular assets (BTC, ETH, USDC, USDT, etc.)
  • Advanced Order Management: Support for limit orders, market orders, and stop-loss orders
  • Position Management: Track and manage long/short positions with leverage
  • Liquidation System: Automated liquidation of undercollateralized positions
  • Oracle Integration: Real-time price feeds for accurate asset pricing
  • Fee Distribution: Automated fee collection and distribution system
  • Role-Based Access Control: Secure multi-role permission system
  • Cross-Chain Support: Deployed on NeoX and NeoXT4 networks

πŸ“ Project Structure

contracts/
β”œβ”€β”€ access/          # Access control and role management
β”œβ”€β”€ adl/            # Automated Dealer Liquidation
β”œβ”€β”€ bank/           # Banking and vault functionality
β”œβ”€β”€ callback/       # Callback mechanisms
β”œβ”€β”€ chain/          # Chain-specific utilities
β”œβ”€β”€ config/         # Configuration management
β”œβ”€β”€ data/           # Data storage and management
β”œβ”€β”€ deposit/        # Deposit handling
β”œβ”€β”€ error/          # Custom error definitions
β”œβ”€β”€ event/          # Event emission utilities
β”œβ”€β”€ exchange/       # Core exchange functionality
β”œβ”€β”€ external/       # External contract integrations
β”œβ”€β”€ feature/        # Feature flags and toggles
β”œβ”€β”€ fee/            # Fee calculation and distribution
β”œβ”€β”€ gas/            # Gas optimization utilities
β”œβ”€β”€ liquidation/    # Liquidation mechanisms
β”œβ”€β”€ market/         # Market management
β”œβ”€β”€ mock/           # Mock contracts for testing
β”œβ”€β”€ nonce/          # Nonce management
β”œβ”€β”€ oracle/         # Price oracle integration
β”œβ”€β”€ order/          # Order management system
β”œβ”€β”€ position/       # Position tracking and management
β”œβ”€β”€ pricing/        # Price calculation utilities
β”œβ”€β”€ reader/         # Data reading utilities
β”œβ”€β”€ referral/       # Referral system
β”œβ”€β”€ role/           # Role-based access control
β”œβ”€β”€ router/         # Exchange router functionality
β”œβ”€β”€ subaccount/     # Subaccount management
β”œβ”€β”€ swap/           # Swap functionality
β”œβ”€β”€ token/          # Token contracts
β”œβ”€β”€ tokens/         # Token implementations
β”œβ”€β”€ utils/          # Utility functions
└── withdrawal/     # Withdrawal handling

πŸ› οΈ Technology Stack

  • Solidity: ^0.8.26
  • Hardhat: Development framework
  • Foundry: Additional testing framework
  • OpenZeppelin: Security libraries
  • TypeScript: Development language
  • Ethers.js: Ethereum interaction library

πŸš€ Quick Start

Prerequisites

  • Node.js (v16 or higher)
  • pnpm or npm
  • Git

Installation

  1. Clone the repository:
git clone https://github.com/L9T-Development/DeXFun-smart-contract-hyperliquid.git
cd DeXFun-smart-contract-hyperliquid
  1. Install dependencies:
pnpm install
# or
npm install
  1. Set up environment variables:
cp .env.example .env
# Edit .env with your configuration

Development

  1. Compile contracts:
npx hardhat compile
  1. Run tests:
npx hardhat test
  1. Start local development network:
npx hardhat node
  1. Deploy contracts:
npx hardhat deploy --network localhost

🌐 Supported Networks

  • NeoX Mainnet (Chain ID: 47763)
  • NeoXT4 Testnet (Chain ID: 12227332)
  • Hardhat Local (Chain ID: 31337)

πŸ“‹ Available Scripts

  • npm run test - Run test suite
  • npm run compile - Compile contracts
  • npm run deploy - Deploy contracts
  • npm run verify - Verify contracts on block explorer
  • npm run lint - Run linting
  • npm run fork:neoxt4 - Fork NeoXT4 testnet

πŸ—οΈ Core Components

Exchange Router

The main entry point for all trading operations, handling deposits, withdrawals, orders, and swaps.

Market System

Manages synthetic asset markets with configurable parameters for each trading pair.

Order Management

Comprehensive order system supporting:

  • Limit orders
  • Market orders
  • Stop-loss orders
  • Conditional orders

Position Management

Track and manage leveraged positions with:

  • Real-time P&L calculation
  • Margin requirements
  • Position sizing

Oracle System

Secure price feed integration with:

  • Multiple price sources
  • Price validation
  • Fallback mechanisms

Liquidation Engine

Automated liquidation system for:

  • Undercollateralized positions
  • Risk management
  • Market stability

πŸ” Security Features

  • Reentrancy Protection: Global reentrancy guards
  • Access Control: Role-based permissions
  • Input Validation: Comprehensive parameter validation
  • Oracle Security: Multiple price source validation
  • Upgrade Safety: Immutable core contracts with proxy patterns

πŸ“Š Token Support

Synthetic Tokens

  • FUNGAS (Synthetic Gas)
  • FUNETH (Synthetic Ethereum)
  • FUNBTC (Synthetic Bitcoin)
  • FUNUSDC (Synthetic USD Coin)
  • FUNUSDT (Synthetic Tether)

Native Tokens

  • WGAS (Wrapped Gas)
  • WETH (Wrapped Ethereum)
  • WBTC (Wrapped Bitcoin)
  • USDC (USD Coin)
  • USDT (Tether)

πŸ§ͺ Testing

The project includes comprehensive test coverage:

# Run all tests
npm run test

# Run specific test file
npx hardhat test test/market/Market.test.ts

# Run with gas reporting
REPORT_GAS=true npm run test

πŸ“ Deployment

Local Development

npx hardhat deploy --network localhost

Testnet Deployment

npx hardhat deploy --network neoXT4

Mainnet Deployment

npx hardhat deploy --network neoX

πŸ”§ Configuration

Environment Variables

  • ACCOUNT_KEY: Private key for deployment
  • NEOX_T4_ACCOUNT_KEY: NeoXT4 specific private key
  • ACCOUNT_KEY_FILE: Path to key file
  • REPORT_GAS: Enable gas reporting

Network Configuration

Networks are configured in hardhat.config.ts with:

  • RPC endpoints
  • Chain IDs
  • Gas settings
  • Account management

πŸ“š Documentation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

πŸ“„ License

This project is licensed under the BUSL-1.1 License - see the LICENSE file for details.

About

DeXFun smart contract on Hyperliquid: smart contracts for the DEXFun perp dex trading dapp

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published