Skip to content

kairos1205/DeXFun-smart-contract-hyperliquid

Repository files navigation

dFun Synthetics Contracts

A fully-featured decentralized perpetual DEX for trading synthetic assets, built using Solidity. The protocol enables leveraged long/short trading, advanced order types, and secure oracle integration—designed for performance, security, and cross-chain extensibility.


📬 Contact

Interested in building a perpetual DEX or integrating synthetic asset trading?


🚀 Key Features

  • 🔁 Synthetic Asset Trading — Trade synthetic assets like BTC, ETH, USDT, USDC, and more
  • 📊 Leverage & Position Management — Manage long/short positions with real-time PnL and margin calculations
  • 📈 Advanced Order Types — Market, limit, stop-loss, and conditional orders
  • 🧠 Oracle Integration — Secure and flexible real-time price feeds
  • 💥 Liquidation Engine — Automatic liquidation of undercollateralized positions
  • 💸 Fee Distribution — Built-in fee routing to stakeholders and protocols
  • 🧩 Modular Architecture — Role-based access, feature toggles, and upgradability
  • 🌐 Cross-Chain Support — Currently supports NeoX and NeoXT4

🧱 Project Structure

contracts/
├── access/         # Role-based access control
├── adl/            # Auto Dealer Liquidation system
├── bank/           # Vaults and balance accounting
├── callback/       # Callback hooks for external contracts
├── chain/          # Chain-specific logic
├── config/         # Config registries and management
├── data/           # Data storage layout
├── deposit/        # Deposit logic
├── error/          # Custom errors
├── event/          # Event definitions
├── exchange/       # Core exchange logic
├── external/       # 3rd-party contract integration
├── feature/        # Feature flags & toggles
├── fee/            # Fee logic and distribution
├── gas/            # Gas usage optimizations
├── liquidation/    # Liquidation engine
├── market/         # Market creation and control
├── mock/           # Mocks for testing
├── nonce/          # Nonce tracking
├── oracle/         # Price oracle integrations
├── order/          # Order creation, matching, validation
├── position/       # Position tracking and PnL logic
├── pricing/        # Pricing mechanisms
├── reader/         # Read-only views and aggregators
├── referral/       # Referral incentives
├── role/           # RBAC utilities
├── router/         # High-level interaction gateway
├── subaccount/     # Subaccount architecture
├── swap/           # Asset swap functionality
├── token/          # Token interfaces and utilities
├── tokens/         # Token implementations
├── utils/          # Common utilities
└── withdrawal/     # Withdrawal processing

🛠 Technology Stack

  • Solidity ^0.8.26
  • Hardhat — Ethereum development framework
  • Foundry — High-performance testing and fuzzing
  • TypeScript — Project scripting and config
  • Ethers.js — Ethereum contract interaction
  • OpenZeppelin — Secure contract libraries

⚡ Quick Start

Prerequisites

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

Installation

git clone https://github.com/kairos1205/DeXFun-smart-contract-hyperliquid.git
cd DeXFun-smart-contract-hyperliquid

pnpm install
# or
npm install

Set Up Environment

cp .env.example .env
# Configure your environment variables in `.env`

🔧 Development

Compile Contracts

npx hardhat compile

Run Tests

npx hardhat test
# or
npm run test

Local Development Network

npx hardhat node

Deploy to Localhost

npx hardhat deploy --network localhost

🌐 Supported Networks

Network Chain ID
NeoX Mainnet 47763
NeoXT4 Testnet 12227332
Hardhat Local 31337

🧩 Core Modules

🛠 Exchange Router

Central gateway for all user interactions: deposits, orders, swaps, and withdrawals.

📉 Market Engine

Supports synthetic markets with configurable risk, margin, and pricing parameters.

📝 Order System

Handles order creation, matching, cancellation, and conditional triggers:

  • Market orders
  • Limit orders
  • Stop-loss / take-profit
  • Conditional triggers

📌 Position Tracking

Supports multiple positions with leverage:

  • Dynamic margin and PnL
  • Liquidation thresholds
  • Subaccount management

📡 Oracle Aggregator

Integrates with multiple price feeds for high-fidelity pricing:

  • Redundant data sources
  • Fallback logic
  • Tamper resistance

⚠️ Liquidation Engine

Automated forced liquidation of undercollateralized positions to maintain protocol solvency.


🔐 Security Highlights

  • Global Reentrancy Guards
  • RBAC via Role Manager
  • Oracle Fallbacks and Integrity Checks
  • Immutable Logic + Upgradeable Proxies
  • Strict Input Validation

💰 Token Support

Synthetic Tokens

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

Native Tokens

  • WGAS, WETH, WBTC, USDC, USDT

🧪 Testing

# Run full test suite
npm run test

# Specific test file
npx hardhat test test/market/Market.test.ts

# With gas reporting
REPORT_GAS=true npm run test

📦 Scripts

Command Description
npm run test Run all tests
npm run compile Compile all contracts
npm run deploy Deploy to selected network
npm run verify Verify contracts (e.g. Etherscan)
npm run lint Run linter on TypeScript code
npm run fork:neoxt4 Fork NeoXT4 network locally

🚀 Deployment

Localhost

npx hardhat deploy --network localhost

Testnet (NeoXT4)

npx hardhat deploy --network neoXT4

Mainnet (NeoX)

npx hardhat deploy --network neoX

⚙️ Configuration

Environment Variables (.env)

Variable Description
ACCOUNT_KEY Private key for deployment
NEOX_T4_ACCOUNT_KEY Private key for NeoXT4 testnet
ACCOUNT_KEY_FILE Path to private key file
REPORT_GAS Enable gas usage reporting

Network Config

Configured in hardhat.config.ts:

  • RPC URLs
  • Chain IDs
  • Gas settings
  • Account access

📚 Documentation


🤝 Contribution Guide

  1. Fork the repo
  2. Create a new feature branch
  3. Write code + tests
  4. Commit with clear messages
  5. Open a pull request 🎉

📄 License

This project is licensed under the Business Source License 1.1 (BUSL-1.1). See LICENSE for details.

About

DeXFun smartcontract on Hyperliquid - smartcontracts for the DeXFun perp dex trading

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors