hyperliquid 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +21 -2
- data/CHANGELOG.md +34 -4
- data/CLAUDE.md +202 -0
- data/README.md +19 -343
- data/docs/API.md +95 -0
- data/docs/CONFIGURATION.md +53 -0
- data/docs/DEVELOPMENT.md +54 -0
- data/docs/ERRORS.md +38 -0
- data/docs/EXAMPLES.md +342 -0
- data/example.rb +131 -28
- data/lib/hyperliquid/cloid.rb +102 -0
- data/lib/hyperliquid/constants.rb +1 -0
- data/lib/hyperliquid/exchange.rb +410 -0
- data/lib/hyperliquid/signing/eip712.rb +56 -0
- data/lib/hyperliquid/signing/signer.rb +122 -0
- data/lib/hyperliquid/version.rb +1 -1
- data/lib/hyperliquid.rb +33 -5
- data/test_integration.rb +255 -0
- metadata +40 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 184e215d2d80056cb19bd6a20b273134d9997979d160d154318c5e155cdf02bb
|
|
4
|
+
data.tar.gz: 1f2bd8b08ef2e99e3732bd7775eeb70fe00ad7c2f4210312d07d237d28e670ed
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1b8645b6f40ae9163343fbc6d7d5c814417643469d75ac320073546243d601055c26f92e1ecb272baf9d839dc4b983b19d56be01cc2bcf4709de9f0e6bf81116
|
|
7
|
+
data.tar.gz: 462b64553e8ee0939bf0284b0647ec602f9be2f3b2389144e64188813cf52d1bca1daf3feb233eab1167fb1337bda7028208acc38398e55a6279e2593ab26181
|
data/.rubocop.yml
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
AllCops:
|
|
2
2
|
NewCops: enable
|
|
3
3
|
TargetRubyVersion: 3.4.3
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
Exclude:
|
|
6
|
+
- 'test_*.rb' # Exclude ad-hoc integration test scripts
|
|
7
|
+
- 'vendor/**/*' # Exclude vendored gems (CI bundles here)
|
|
4
8
|
|
|
5
9
|
# Allow longer methods for complex logic
|
|
6
10
|
Metrics/MethodLength:
|
|
@@ -14,7 +18,22 @@ Metrics/AbcSize:
|
|
|
14
18
|
Metrics/ClassLength:
|
|
15
19
|
Enabled: false
|
|
16
20
|
|
|
17
|
-
# Allow longer blocks in specs - this is normal for tests
|
|
21
|
+
# Allow longer blocks in specs and gemspec - this is normal for tests and gem configs
|
|
18
22
|
Metrics/BlockLength:
|
|
19
23
|
Exclude:
|
|
20
|
-
- 'spec/**/*'
|
|
24
|
+
- 'spec/**/*'
|
|
25
|
+
- '*.gemspec'
|
|
26
|
+
|
|
27
|
+
# Exchange API methods require many parameters
|
|
28
|
+
Metrics/ParameterLists:
|
|
29
|
+
Exclude:
|
|
30
|
+
- 'lib/hyperliquid/exchange.rb'
|
|
31
|
+
|
|
32
|
+
# Allow higher complexity for order type conversion logic
|
|
33
|
+
Metrics/CyclomaticComplexity:
|
|
34
|
+
Exclude:
|
|
35
|
+
- 'lib/hyperliquid/exchange.rb'
|
|
36
|
+
|
|
37
|
+
Metrics/PerceivedComplexity:
|
|
38
|
+
Exclude:
|
|
39
|
+
- 'lib/hyperliquid/exchange.rb'
|
data/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,43 @@
|
|
|
1
1
|
## [Ruby Hyperliquid SDK Changelog]
|
|
2
2
|
|
|
3
|
-
## [0.1
|
|
3
|
+
## [0.4.1] - 2025-01-28
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- Reorganize documentation for improved readability
|
|
6
|
+
- Streamline README with basic setup and links to detailed docs
|
|
7
|
+
- Add `docs/API.md` with complete method reference
|
|
8
|
+
- Add `docs/EXAMPLES.md` with Info and Exchange code examples
|
|
9
|
+
- Add `docs/CONFIGURATION.md` with SDK options and retry settings
|
|
10
|
+
- Add `docs/ERRORS.md` with error handling guide
|
|
11
|
+
- Add `docs/DEVELOPMENT.md` with setup and testing instructions
|
|
6
12
|
|
|
7
|
-
## [0.
|
|
13
|
+
## [0.4.0] - 2025-01-27
|
|
8
14
|
|
|
9
|
-
-
|
|
15
|
+
- Add Exchange API for authenticated write operations (trading)
|
|
16
|
+
- Order placement: `order`, `bulk_orders`, `market_order`
|
|
17
|
+
- Order cancellation: `cancel`, `cancel_by_cloid`, `bulk_cancel`, `bulk_cancel_by_cloid`
|
|
18
|
+
- Trigger orders (stop loss / take profit) support
|
|
19
|
+
- Vault trading support via optional `vault_address` parameter
|
|
20
|
+
- Order expiration support via `expires_after` parameter
|
|
21
|
+
- Add EIP-712 signing infrastructure matching official Python SDK
|
|
22
|
+
- Phantom agent signing scheme
|
|
23
|
+
- Full parity with Python SDK signature generation
|
|
24
|
+
- Add new dependencies: `eth` (~> 0.5), `msgpack` (~> 1.7)
|
|
25
|
+
- SDK now accepts optional `private_key` and `expires_after` parameters
|
|
26
|
+
|
|
27
|
+
## [0.3.0] - 2025-09-24
|
|
28
|
+
|
|
29
|
+
- Full parity with all Hyperliquid Info APIs
|
|
30
|
+
- All Info APIs implemented
|
|
31
|
+
- Code, tests, and docs reflect structure of official Hyperliquid API documentation
|
|
10
32
|
|
|
11
33
|
## [0.2.0] - 2025-09-24
|
|
12
34
|
|
|
13
35
|
- Add info endpoints for user spot data
|
|
36
|
+
|
|
37
|
+
## [0.1.1] - 2025-09-23
|
|
38
|
+
|
|
39
|
+
- Fixed retry logic, make retry logic disabled by default
|
|
40
|
+
|
|
41
|
+
## [0.1.0] - 2025-08-21
|
|
42
|
+
|
|
43
|
+
- Initial release which includes info endpoints for market and user perps data
|
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This is a Ruby SDK for the Hyperliquid decentralized exchange API. The current version (0.4.0) supports both **read operations** (Info API) and **authenticated write operations** (Exchange API) for trading.
|
|
8
|
+
|
|
9
|
+
**Target Ruby Version**: 3.4.0+
|
|
10
|
+
|
|
11
|
+
## Development Commands
|
|
12
|
+
|
|
13
|
+
### Running Tests
|
|
14
|
+
```bash
|
|
15
|
+
# Run all tests
|
|
16
|
+
rake spec
|
|
17
|
+
|
|
18
|
+
# Run tests and linting together (default rake task)
|
|
19
|
+
rake
|
|
20
|
+
|
|
21
|
+
# Run a single test file
|
|
22
|
+
bundle exec rspec spec/hyperliquid/cloid_spec.rb
|
|
23
|
+
|
|
24
|
+
# Run a specific test by line number
|
|
25
|
+
bundle exec rspec spec/hyperliquid/cloid_spec.rb:62
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Linting
|
|
29
|
+
```bash
|
|
30
|
+
# Run RuboCop linter
|
|
31
|
+
rake rubocop
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Interactive Console
|
|
35
|
+
```bash
|
|
36
|
+
# Open an interactive console with the SDK loaded
|
|
37
|
+
bin/console
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Example Script
|
|
41
|
+
```bash
|
|
42
|
+
# Run the example usage script
|
|
43
|
+
ruby example.rb
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Integration Testing (Testnet)
|
|
47
|
+
```bash
|
|
48
|
+
# Run the testnet integration test (requires private key)
|
|
49
|
+
# Get testnet funds from: https://app.hyperliquid-testnet.xyz
|
|
50
|
+
HYPERLIQUID_PRIVATE_KEY=0x... ruby test_integration.rb
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The integration test executes real trades on testnet:
|
|
54
|
+
1. Spot market roundtrip (buy/sell PURR/USDC)
|
|
55
|
+
2. Spot limit order (place and cancel)
|
|
56
|
+
3. Perp market roundtrip (long/close BTC)
|
|
57
|
+
4. Perp limit order (place short, cancel)
|
|
58
|
+
|
|
59
|
+
### Setup
|
|
60
|
+
```bash
|
|
61
|
+
# Install dependencies
|
|
62
|
+
bin/setup
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Architecture
|
|
66
|
+
|
|
67
|
+
### Core Components
|
|
68
|
+
|
|
69
|
+
**Hyperliquid::SDK** (`lib/hyperliquid.rb`)
|
|
70
|
+
- Main entry point created via `Hyperliquid.new(testnet:, timeout:, retry_enabled:, private_key:, expires_after:)`
|
|
71
|
+
- Manages environment selection (mainnet vs testnet)
|
|
72
|
+
- Exposes the `info` API client (always available)
|
|
73
|
+
- Exposes the `exchange` API client (when `private_key` provided)
|
|
74
|
+
|
|
75
|
+
**Hyperliquid::Client** (`lib/hyperliquid/client.rb`)
|
|
76
|
+
- Low-level HTTP client built on Faraday
|
|
77
|
+
- Handles all POST requests to the Hyperliquid API
|
|
78
|
+
- Manages retry logic (disabled by default, opt-in via `retry_enabled: true`)
|
|
79
|
+
- Converts HTTP errors into typed exceptions
|
|
80
|
+
|
|
81
|
+
**Hyperliquid::Info** (`lib/hyperliquid/info.rb`)
|
|
82
|
+
- High-level API client for all Info endpoints (read-only)
|
|
83
|
+
- Organized into three sections:
|
|
84
|
+
1. **General Info**: Market data, user orders, fills, rate limits, portfolios, referrals, fees, staking
|
|
85
|
+
2. **Perpetuals**: Perp DEXs, metadata, user state, funding rates, open interest
|
|
86
|
+
3. **Spot**: Spot tokens, balances, deploy auctions, token details
|
|
87
|
+
- All methods accept user wallet addresses and return parsed JSON responses
|
|
88
|
+
|
|
89
|
+
**Hyperliquid::Exchange** (`lib/hyperliquid/exchange.rb`)
|
|
90
|
+
- High-level API client for Exchange endpoints (authenticated write operations)
|
|
91
|
+
- Order placement: `order`, `bulk_orders`, `market_order`
|
|
92
|
+
- Order cancellation: `cancel`, `cancel_by_cloid`, `bulk_cancel`, `bulk_cancel_by_cloid`
|
|
93
|
+
- Supports trigger orders (stop loss / take profit)
|
|
94
|
+
- Supports vault trading via `vault_address` parameter
|
|
95
|
+
- Caches asset metadata for efficient lookups
|
|
96
|
+
|
|
97
|
+
**Hyperliquid::Signing::Signer** (`lib/hyperliquid/signing/signer.rb`)
|
|
98
|
+
- EIP-712 signature generation using phantom agent scheme
|
|
99
|
+
- Matches official Python SDK signing algorithm exactly
|
|
100
|
+
- Supports vault address and expiration in signature
|
|
101
|
+
|
|
102
|
+
**Hyperliquid::Signing::EIP712** (`lib/hyperliquid/signing/eip712.rb`)
|
|
103
|
+
- EIP-712 domain and type definitions
|
|
104
|
+
- L1 chain ID (1337) and source identifiers ('a' mainnet, 'b' testnet)
|
|
105
|
+
|
|
106
|
+
**Hyperliquid::Cloid** (`lib/hyperliquid/cloid.rb`)
|
|
107
|
+
- Type-safe client order ID class
|
|
108
|
+
- Validates 16-byte hex format (0x + 32 hex characters)
|
|
109
|
+
- Factory methods: `from_int`, `from_str`, `from_uuid`, `random`
|
|
110
|
+
|
|
111
|
+
**Hyperliquid::Constants** (`lib/hyperliquid/constants.rb`)
|
|
112
|
+
- API URLs for mainnet and testnet
|
|
113
|
+
- Endpoint paths (`/info`, `/exchange`)
|
|
114
|
+
- Default timeout values
|
|
115
|
+
|
|
116
|
+
**Hyperliquid::Errors** (`lib/hyperliquid/errors.rb`)
|
|
117
|
+
- Typed exception hierarchy for API errors
|
|
118
|
+
- Base class: `Hyperliquid::Error`
|
|
119
|
+
- Specific errors: `ClientError`, `ServerError`, `AuthenticationError`, `RateLimitError`, `BadRequestError`, `NotFoundError`, `TimeoutError`, `NetworkError`
|
|
120
|
+
|
|
121
|
+
### API Request Pattern
|
|
122
|
+
|
|
123
|
+
**Info API (read-only):**
|
|
124
|
+
1. SDK method called (e.g., `sdk.info.all_mids`)
|
|
125
|
+
2. Info class builds request body with `type` field (e.g., `{ type: 'allMids' }`)
|
|
126
|
+
3. Client POSTs JSON body to `/info` endpoint
|
|
127
|
+
4. Client parses response and handles errors
|
|
128
|
+
5. Parsed JSON returned to caller
|
|
129
|
+
|
|
130
|
+
**Exchange API (authenticated):**
|
|
131
|
+
1. SDK method called (e.g., `sdk.exchange.order(...)`)
|
|
132
|
+
2. Exchange class builds action payload with order/cancel details
|
|
133
|
+
3. Signer generates EIP-712 signature over msgpack-encoded action
|
|
134
|
+
4. Exchange POSTs signed payload to `/exchange` endpoint
|
|
135
|
+
5. Client parses response and handles errors
|
|
136
|
+
6. Parsed JSON returned to caller
|
|
137
|
+
|
|
138
|
+
### Testing
|
|
139
|
+
|
|
140
|
+
- Uses RSpec for testing
|
|
141
|
+
- WebMock for HTTP mocking
|
|
142
|
+
- Spec helper configures WebMock to reset between tests
|
|
143
|
+
- Test files mirror source structure in `spec/`
|
|
144
|
+
|
|
145
|
+
### Code Style
|
|
146
|
+
|
|
147
|
+
RuboCop configuration (`.rubocop.yml`):
|
|
148
|
+
- Targets Ruby 3.4.0+
|
|
149
|
+
- Allows longer methods (max 50 lines) for complex logic
|
|
150
|
+
- Disables class length checks (Info/Exchange classes implement many endpoints)
|
|
151
|
+
- Excludes block length checks for specs
|
|
152
|
+
- Enables NewCops by default
|
|
153
|
+
|
|
154
|
+
## Key Implementation Details
|
|
155
|
+
|
|
156
|
+
### Retry Logic
|
|
157
|
+
- **Disabled by default** for predictable behavior in time-sensitive trading
|
|
158
|
+
- When enabled: max 2 retries, 0.5s base interval, exponential backoff (2x), ±50% randomness
|
|
159
|
+
- Retries on: connection failures, timeouts, 429 (rate limit), 5xx errors
|
|
160
|
+
|
|
161
|
+
### API Endpoints
|
|
162
|
+
- Info requests POST to `/info` endpoint
|
|
163
|
+
- Exchange requests POST to `/exchange` endpoint
|
|
164
|
+
- Request body includes `type` field indicating the operation
|
|
165
|
+
|
|
166
|
+
### Time Parameters
|
|
167
|
+
- All timestamps are in **milliseconds** (not seconds)
|
|
168
|
+
- Methods with time ranges support optional `end_time` parameter
|
|
169
|
+
- `expires_after` is an absolute timestamp in milliseconds
|
|
170
|
+
|
|
171
|
+
### Signature Generation (Python SDK Parity)
|
|
172
|
+
The signing implementation matches the official Python SDK exactly:
|
|
173
|
+
- **Action hash**: `keccak256(msgpack(action) + nonce(8B BE) + vault_flag + [vault_addr] + [expires_flag + expires_after])`
|
|
174
|
+
- **Phantom agent**: `{ source: 'a'|'b', connectionId: action_hash }`
|
|
175
|
+
- **EIP-712 signature** over phantom agent with Exchange domain
|
|
176
|
+
|
|
177
|
+
### Float to Wire Format
|
|
178
|
+
Numeric values are converted to strings matching Python SDK `float_to_wire`:
|
|
179
|
+
- 8 decimal precision
|
|
180
|
+
- Rounding tolerance validation (1e-12)
|
|
181
|
+
- Trailing zero normalization (no scientific notation)
|
|
182
|
+
|
|
183
|
+
### Market Order Price Calculation
|
|
184
|
+
Market orders use Python SDK `_slippage_price` algorithm:
|
|
185
|
+
- Apply slippage to mid price
|
|
186
|
+
- Round to 5 significant figures
|
|
187
|
+
- Round to asset-specific decimal places: `(6 for perp, 8 for spot) - szDecimals`
|
|
188
|
+
|
|
189
|
+
### Client Order IDs (Cloid)
|
|
190
|
+
- Must be 16 bytes in hex format: `0x` + 32 hex characters
|
|
191
|
+
- Use `Hyperliquid::Cloid` class for type safety and validation
|
|
192
|
+
- Factory methods: `from_int(n)`, `from_str(s)`, `from_uuid(uuid)`, `random`
|
|
193
|
+
|
|
194
|
+
## Development Workflow
|
|
195
|
+
|
|
196
|
+
1. Make changes to library code in `lib/hyperliquid/`
|
|
197
|
+
2. Add/update tests in `spec/`
|
|
198
|
+
3. Run tests: `rake spec`
|
|
199
|
+
4. Run linter: `rake rubocop`
|
|
200
|
+
5. Test in console: `bin/console`
|
|
201
|
+
6. Run example script: `ruby example.rb`
|
|
202
|
+
|