|
1 | | -## HyperEVM Orderbook |
| 1 | +# HyperEVM Limit Order Book |
2 | 2 |
|
3 | | -- HyperEVM uses cancun hardfork without blobs |
4 | | -- Mainnet Chain ID: 999 |
5 | | -- JSON-RPC endpoint: https://rpc.hyperliquid.xyz/evm for mainnet |
6 | | -- Testnet Chain ID: 998 |
7 | | -- JSON-RPC endpoint: https://rpc.hyperliquid-testnet.xyz/evm |
| 3 | +[](https://opensource.org/licenses/MIT) |
| 4 | +[](https://soliditylang.org/) |
| 5 | +[](https://getfoundry.sh/) |
| 6 | +[](#testing) |
8 | 7 |
|
9 | | -## Goal: |
| 8 | +A decentralized limit order book smart contract designed for HyperEVM, enabling users to place limit orders that are executed by off-chain bots when price conditions are met. |
10 | 9 |
|
11 | | -- On-Chain limit order then off chain component to execute it |
12 | | -- save orders on chain, once price hit, execute |
13 | | -- so there's a bot that will execute it |
14 | | -- If cannot execute, we don't execute and can have on chain revert |
| 10 | +## 🌟 Features |
15 | 11 |
|
16 | | -## UserFlow: |
| 12 | +- **On-chain Order Management**: Secure storage of limit orders on the blockchain |
| 13 | +- **Off-chain Execution**: Permissionless bot execution with optional authorization controls |
| 14 | +- **Flexible Authorization**: Support for both permissionless and authorized executor models |
| 15 | +- **Comprehensive Testing**: 95%+ test coverage with unit and fuzz tests |
| 16 | +- **Gas Optimized**: Efficient storage patterns and minimal gas consumption |
| 17 | +- **Reentrancy Protection**: Built-in security against reentrancy attacks |
17 | 18 |
|
18 | | -- User places order on-chain > emits OrderPlaced |
19 | | -- Off-chain bots listens, tracks order |
20 | | -- Once price meets condition, bot sends markExecuted |
21 | | -- If slippage or gas fails, tx reverts |
22 | | -- Successful tx emits OrderExecuted |
| 19 | +## 🏗️ Architecture |
23 | 20 |
|
24 | | -## SC design: |
| 21 | +### User Flow |
25 | 22 |
|
26 | | -- placeOrder |
27 | | -- cancelOrder |
28 | | -- markExecuted |
29 | | -- Emit OrderPlaced, OrderExecuted, OrderCancelled |
| 23 | +1. **Order Placement**: Users place limit orders on-chain → emits `OrderPlaced` event |
| 24 | +2. **Off-chain Monitoring**: Bots listen to events and track order conditions |
| 25 | +3. **Price Monitoring**: Bots monitor price feeds (Hyperliquid or custom oracles) |
| 26 | +4. **Order Execution**: When price conditions are met, bots call `markExecuted(orderId)` |
| 27 | +5. **Failure Handling**: Failed transactions revert with appropriate error messages |
| 28 | +6. **Success Confirmation**: Successful execution emits `OrderExecuted` event |
30 | 29 |
|
31 | | -## Off-chain bot (Executor Service) |
| 30 | +### Contract Design |
32 | 31 |
|
33 | | -- Listen to the OrderPlaced events |
34 | | -- Monitor Price Feed (Hyperliquid off-chain price or custom oracle) |
35 | | -- When price hits, call markExecuted(orderId) via relayer |
36 | | -- Handle failed tx reverts (e.g. order already filled, bad slippage, front-run etc.) |
| 32 | +- **placeOrder**: Create new limit orders with price and amount validation |
| 33 | +- **cancelOrder**: Cancel existing orders (owner-only) |
| 34 | +- **markExecuted**: Mark orders as executed (bot/executor function) |
| 35 | +- **Access Control**: Owner-controlled executor authorization system |
| 36 | + |
| 37 | +## 🌐 HyperEVM Network |
| 38 | + |
| 39 | +- **Mainnet Chain ID**: 999 |
| 40 | +- **Mainnet RPC**: `https://rpc.hyperliquid.xyz/evm` |
| 41 | +- **Testnet Chain ID**: 998 |
| 42 | +- **Testnet RPC**: `https://rpc.hyperliquid-testnet.xyz/evm` |
| 43 | +- **Hardfork**: Cancun (without blobs) |
| 44 | + |
| 45 | +## 📋 Prerequisites |
| 46 | + |
| 47 | +- [Foundry](https://getfoundry.sh/) (latest version) |
| 48 | +- [Git](https://git-scm.com/) |
| 49 | +- Node.js (for optional tooling) |
| 50 | + |
| 51 | +## 🚀 Quick Start |
| 52 | + |
| 53 | +### Installation |
| 54 | + |
| 55 | +```bash |
| 56 | +# Clone the repository |
| 57 | +git clone https://github.com/hougangdev/hyperliquid-limit-order-sc.git |
| 58 | +cd hyperliquid-limit-order-sc |
| 59 | + |
| 60 | +# Install dependencies |
| 61 | +forge install |
| 62 | + |
| 63 | +# Build the project |
| 64 | +forge build |
| 65 | +``` |
| 66 | + |
| 67 | +### Testing |
| 68 | + |
| 69 | +```bash |
| 70 | +# Run all tests |
| 71 | +forge test |
| 72 | + |
| 73 | +# Run tests with coverage |
| 74 | +forge coverage --no-match-coverage "test/mocks/" |
| 75 | + |
| 76 | +# Run specific test file |
| 77 | +forge test --match-path test/unit/LimitOrderBook.t.sol |
| 78 | + |
| 79 | +# Run fuzz tests with more iterations |
| 80 | +forge test --match-path test/fuzz/ --fuzz-runs 1000 |
| 81 | +``` |
| 82 | + |
| 83 | +### Deployment |
| 84 | + |
| 85 | +```bash |
| 86 | +# Deploy to HyperEVM testnet |
| 87 | +forge script script/Deploy.s.sol --rpc-url https://rpc.hyperliquid-testnet.xyz/evm --broadcast |
| 88 | + |
| 89 | +# Deploy to HyperEVM mainnet (replace with your key) |
| 90 | +forge script script/Deploy.s.sol --rpc-url https://rpc.hyperliquid.xyz/evm --broadcast --private-key $PRIVATE_KEY |
| 91 | +``` |
| 92 | + |
| 93 | +## 📖 Usage Examples |
| 94 | + |
| 95 | +### Basic Order Operations |
| 96 | + |
| 97 | +```solidity |
| 98 | +// Deploy the contract |
| 99 | +LimitOrderBook orderBook = new LimitOrderBook(owner); |
| 100 | +
|
| 101 | +// Place a limit order |
| 102 | +uint256 orderId = orderBook.placeOrder(1000e18, 100e18); // Price: 1000, Amount: 100 |
| 103 | +
|
| 104 | +// Check order details |
| 105 | +LimitOrderBook.Order memory order = orderBook.getOrder(orderId); |
| 106 | +
|
| 107 | +// Cancel an order (owner only) |
| 108 | +orderBook.cancelOrder(orderId); |
| 109 | +
|
| 110 | +// Execute an order (bot/executor) |
| 111 | +orderBook.markExecuted(orderId); |
| 112 | +``` |
| 113 | + |
| 114 | +### Executor Authorization |
| 115 | + |
| 116 | +```solidity |
| 117 | +// Authorize an executor (owner only) |
| 118 | +orderBook.authorizeExecutor(executorAddress); |
| 119 | +
|
| 120 | +// Require authorization for execution |
| 121 | +orderBook.setExecutorAuthRequired(true); |
| 122 | +
|
| 123 | +// Revoke executor authorization |
| 124 | +orderBook.revokeExecutor(executorAddress); |
| 125 | +``` |
| 126 | + |
| 127 | +## 🔧 API Reference |
| 128 | + |
| 129 | +### Core Functions |
| 130 | + |
| 131 | +#### `placeOrder(uint256 price, uint256 amount) → uint256 orderId` |
| 132 | + |
| 133 | +Creates a new limit order. |
| 134 | + |
| 135 | +- **Parameters**: `price` - Target execution price, `amount` - Order quantity |
| 136 | +- **Returns**: Unique order ID |
| 137 | +- **Events**: `OrderPlaced(orderId, user, price, amount)` |
| 138 | + |
| 139 | +#### `cancelOrder(uint256 orderId)` |
| 140 | + |
| 141 | +Cancels an existing order. |
| 142 | + |
| 143 | +- **Parameters**: `orderId` - Order to cancel |
| 144 | +- **Requirements**: Must be order owner, order must not be executed |
| 145 | +- **Events**: `OrderCancelled(orderId)` |
| 146 | + |
| 147 | +#### `markExecuted(uint256 orderId)` |
| 148 | + |
| 149 | +Marks an order as executed. |
| 150 | + |
| 151 | +- **Parameters**: `orderId` - Order to execute |
| 152 | +- **Requirements**: Order must exist and not be executed |
| 153 | +- **Events**: `OrderExecuted(orderId)` |
| 154 | + |
| 155 | +### View Functions |
| 156 | + |
| 157 | +- `getOrder(uint256 orderId) → Order` - Get order details |
| 158 | +- `isOrderActive(uint256 orderId) → bool` - Check if order is active |
| 159 | +- `getOrderCount() → uint256` - Get total number of orders |
| 160 | +- `s_authorizedExecutors(address) → bool` - Check executor authorization |
| 161 | + |
| 162 | +### Admin Functions |
| 163 | + |
| 164 | +- `authorizeExecutor(address executor)` - Authorize an executor |
| 165 | +- `revokeExecutor(address executor)` - Revoke executor authorization |
| 166 | +- `setExecutorAuthRequired(bool requireAuth)` - Toggle authorization requirement |
| 167 | + |
| 168 | +## 🧪 Testing |
| 169 | + |
| 170 | +The project includes comprehensive testing: |
| 171 | + |
| 172 | +- **Unit Tests**: 26 tests covering all functionality |
| 173 | +- **Fuzz Tests**: 10 fuzz tests with 256 iterations each |
| 174 | +- **Coverage**: 95.35% line coverage, 95.12% statement coverage |
| 175 | +- **Mock Contracts**: Test utilities for executor bots and price oracles |
| 176 | + |
| 177 | +### Test Structure |
| 178 | + |
| 179 | +``` |
| 180 | +test/ |
| 181 | +├── unit/ # Unit tests for core functionality |
| 182 | +├── fuzz/ # Fuzz tests for edge cases |
| 183 | +└── mocks/ # Mock contracts for testing |
| 184 | + ├── MockExecutorBot.sol |
| 185 | + └── MockPriceOracle.sol |
| 186 | +``` |
| 187 | + |
| 188 | +### Known Risks |
| 189 | + |
| 190 | +- **Centralization**: Owner has admin privileges (intended design) |
| 191 | +- **Oracle Dependency**: Relies on external price feeds for execution |
| 192 | + |
| 193 | +### Development Guidelines |
| 194 | + |
| 195 | +- Follow Solidity style guide |
| 196 | +- Add comprehensive tests for new features |
| 197 | +- Update documentation for API changes |
| 198 | +- Ensure 100% test coverage for new code |
| 199 | + |
| 200 | +## 📄 License |
| 201 | + |
| 202 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 203 | + |
| 204 | +## 🙏 Acknowledgments |
| 205 | + |
| 206 | +- [Hyperliquid](https://hyperliquid.xyz/) for the HyperEVM network |
| 207 | +- [OpenZeppelin](https://openzeppelin.com/) for secure contract libraries |
| 208 | +- [Foundry](https://getfoundry.sh/) for the development framework |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +**⚠️ Disclaimer**: This software is provided "as is" without warranty. Use at your own risk. Always conduct thorough testing before deploying to mainnet. |
0 commit comments