Skip to content

Commit 84495d8

Browse files
nomeidaclaude
andcommitted
feat: import hyperliquid SDK codebase into sma-emulator
Copies the full hyperliquid SDK source including expanded API coverage, exchange actions, order builder, validation, WebSocket improvements, tests, scripts, docs, and CI workflows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0cb96c6 commit 84495d8

534 files changed

Lines changed: 79629 additions & 4379 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
private_key=private_key
2-
user_address=xxx
3-
cloid=cloid
1+
# These env vars are only used in the test suite/test scripts
2+
TEST_WALLET_ADDRESS="" #Main wallet, NOT API Agent wallet
3+
TEST_KEY="" #API Agent Wallet or Private Key
4+
5+
TEST_PERP_SYMBOL=""
6+
TEST_SPOT_TOKEN_ID=""
7+
TEST_BUILDER_ADDRESS=""
8+
TEST_AGENT_ADDRESS=""
9+
TEST_AGENT_NAME=""
10+

.github/workflows/integration.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Integration Tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
integration:
10+
runs-on: ubuntu-latest
11+
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20.x'
21+
cache: 'npm'
22+
23+
- name: Install dependencies
24+
run: npm ci --legacy-peer-deps
25+
26+
- name: Run integration tests
27+
run: npx vitest run tests/integration/
28+
env:
29+
HL_INTEGRATION_TEST: 'true'
30+
PRIVATE_KEY: ${{ secrets.HL_TESTNET_PRIVATE_KEY }}

.github/workflows/schema-drift.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Schema Drift Check
2+
3+
on:
4+
schedule:
5+
- cron: '0 6 * * 1' # Every Monday at 6am UTC
6+
workflow_dispatch:
7+
8+
jobs:
9+
schema-drift:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '20.x'
20+
cache: 'npm'
21+
22+
- name: Install dependencies
23+
run: npm ci --legacy-peer-deps
24+
25+
- name: Run schema coverage tests
26+
run: npx vitest run tests/integration/schemaCoverage.test.ts
27+
env:
28+
HL_INTEGRATION_TEST: 'true'

.github/workflows/test.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node-version: [18.x, 20.x, 22.x]
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: 'npm'
25+
26+
- name: Install dependencies
27+
run: npm ci --legacy-peer-deps
28+
29+
- name: Type check
30+
run: npx tsc --noEmit
31+
32+
- name: Run tests
33+
run: npx vitest run --coverage
34+
35+
- name: Build
36+
run: npm run build
37+
38+
- name: Verify package
39+
run: npm pack --dry-run

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ src/**/*.js
1010
.DS_Store
1111
.idea
1212
csdk/
13+
id/
14+
CLAUDE.md

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx lint-staged

PLAN.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Hyperliquid SDK Parity Plan (nktkas Alignment)
2+
3+
## Goals
4+
5+
- Match nktkas SDK coverage for all REST `/info`, REST `/exchange`, and WebSocket methods.
6+
- Implement schema validation for responses and structured error handling.
7+
- Preserve raw/parsed response behavior (parsed default, raw opt-in).
8+
- Keep undocumented endpoints when present in nktkas.
9+
10+
## Scope
11+
12+
- New first-class methods for missing info/exchange endpoints.
13+
- WebSocket subscription and post-message parity.
14+
- Types, response parsing, schema validation, and error taxonomy.
15+
- Minimal docs and tests to cover new functionality.
16+
17+
## Non-goals
18+
19+
- Large architectural rewrite or runtime portability changes.
20+
- Dropping existing behavior without a compatible path.
21+
22+
## Plan of Record
23+
24+
1. Inventory and Mapping
25+
26+
- Extract full method list from nktkas (info/exchange/ws).
27+
- Compare against current SDK exports and document gaps.
28+
- Identify undocumented endpoints that must be included.
29+
30+
2. Types and Constants Baseline
31+
32+
- Add missing `InfoType`, `ExchangeType`, and WS subscription constants.
33+
- Define request/response types for all new methods.
34+
- Add shared payload fragments for reuse (e.g. user/subaccount, dex).
35+
36+
3. REST `/info` Parity
37+
38+
- Implement all missing info endpoints as first-class methods.
39+
- Add missing documented params and optional fields.
40+
- Ensure per-call raw override and default parsing behavior.
41+
42+
4. REST `/exchange` Parity
43+
44+
- Implement all missing exchange actions and method wrappers.
45+
- Ensure signing supports new action payload shapes.
46+
- Add any required subaccount overrides where applicable.
47+
48+
5. WebSocket Parity
49+
50+
- Add missing subscriptions, params, and payload builders.
51+
- Add response type support and raw/parsed handling.
52+
- Align WS errors and reconnect behavior with structured errors.
53+
54+
6. Schema Validation
55+
56+
- Add a validation layer for REST and WS responses using valibot.
57+
- Support: parsed (transform) and raw (shape-only) validation.
58+
- Allow global config toggle for validation with per-call override.
59+
60+
7. Structured Errors
61+
62+
- Introduce base `HyperliquidError` with typed subclasses:
63+
- `HyperliquidHttpError`, `HyperliquidSchemaError`, `HyperliquidWsError`,
64+
`HyperliquidRateLimitError`, `HyperliquidTimeoutError`, `HyperliquidRequestError`.
65+
- Attach context: method name, endpoint, requestId, status, payload.
66+
- Normalize server error payloads into consistent error codes.
67+
68+
8. Tests and Docs
69+
70+
- Add tests for new methods, validation, and error mapping.
71+
- Update README and changelog for new coverage and options.
72+
73+
9. Rollout Strategy
74+
- Implement in phases: types/constants -> info -> exchange -> ws -> validation -> errors -> docs/tests.
75+
- Ship incremental commits to reduce review and integration risk.

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All info on the Hyperliquid API can be found here: [HyperLiquid API Documentatio
1212
- Automatic handling of trailing zeros in price and size fields
1313
- Rate limiting support
1414
- Comprehensive error handling
15+
- Schema validation with configurable strictness
1516

1617
## Installation
1718

@@ -78,7 +79,11 @@ const sdk = new Hyperliquid({
7879
vaultAddress: <vaultAddress - string (OPTIONAL)>,
7980
maxReconnectAttempts: <number (OPTIONAL)>, // Default is 5, controls WebSocket reconnection attempts
8081
disableAssetMapRefresh: <boolean (OPTIONAL)>, // Default is false, set to true to disable automatic asset map refresh
81-
assetMapRefreshIntervalMs: <number (OPTIONAL)> // Default is 60000 (1 minute), controls how often asset maps are refreshed
82+
assetMapRefreshIntervalMs: <number (OPTIONAL)>, // Default is 60000 (1 minute), controls how often asset maps are refreshed
83+
rawResponses: <boolean (OPTIONAL)>, // Default is false, return raw API responses without numeric parsing
84+
validationMode: <'strict' | 'warn' | 'off' (OPTIONAL)>, // Default is 'strict'
85+
wsUrl: <string (OPTIONAL)>, // Override WebSocket URL (e.g. wss://rpc.hyperliquid.xyz/ws for explorer subs)
86+
explorerUrl: <string (OPTIONAL)> // Override explorer HTTP base (default https://rpc.hyperliquid.xyz)
8287
});
8388

8489
// Use the SDK methods
@@ -90,6 +95,15 @@ sdk.info.getAllMids().then(allMids => {
9095
**Note:** You don't have to provide your private key, but it is required if you want to
9196
use the exchange API to place, cancel or modify orders or access your accounts assets.
9297
WebSocket functionality is enabled by default but can be disabled by setting `enableWs: false` in the constructor options.
98+
Validation runs in `strict` mode by default; use `validationMode: 'warn'` or `validationMode: 'off'` to change behavior.
99+
100+
### Response Validation
101+
102+
To override validation for a single call, wrap it with `withValidationMode`:
103+
104+
```typescript
105+
await sdk.withValidationMode('off', () => sdk.info.getAllMids());
106+
```
93107

94108
## Key Features
95109

0 commit comments

Comments
 (0)