@madeinusmate/grvt-cli 0.1.0

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.
package/README.md ADDED
@@ -0,0 +1,1154 @@
1
+ # grvt-cli
2
+
3
+ CLI tool and library to trade on [GRVT](https://grvt.io) markets.
4
+
5
+ ## Features
6
+
7
+ - Full trading lifecycle: create, cancel, and query orders
8
+ - Fund management: transfers, withdrawals, and deposit history
9
+ - Market data: instruments, tickers, orderbook, trades, candles, funding rates, margin rules, currency metadata
10
+ - Account summaries, history, and sub-account management
11
+ - Leverage get/set and derisk MM ratio management
12
+ - Cancel-on-disconnect for automated trading protection
13
+ - EIP-712 signing for orders, transfers, withdrawals, and derisk (Ethereum private key via viem)
14
+ - Cursor-based pagination with streaming `--all` support
15
+ - Multiple output formats: `json`, `ndjson`, `table`, `raw`
16
+ - Scriptable with consistent exit codes and `--silent` mode
17
+ - `--dry-run` on all write commands
18
+ - JSON file/stdin input for advanced payloads
19
+ - Library exports for programmatic use
20
+
21
+ ## Requirements
22
+
23
+ - Node.js 20+
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ pnpm add -g grvt-cli
29
+ # or
30
+ npm install -g grvt-cli
31
+ ```
32
+
33
+ ---
34
+
35
+ ## Quick Start
36
+
37
+ ### 1. Set your environment
38
+
39
+ ```bash
40
+ grvt config set env testnet
41
+ ```
42
+
43
+ Supported environments: `dev`, `staging`, `testnet`, `prod` (default: `prod`).
44
+
45
+ ### 2. Authenticate
46
+
47
+ ```bash
48
+ grvt auth login --api-key YOUR_API_KEY --private-key 0xYOUR_PRIVATE_KEY
49
+ ```
50
+
51
+ The private key is required for order signing (EIP-712). If you only need read access, you can omit it and add it later:
52
+
53
+ ```bash
54
+ grvt auth login --api-key YOUR_API_KEY
55
+ grvt config set privateKey 0xYOUR_PRIVATE_KEY
56
+ ```
57
+
58
+ Credentials are stored in `~/.config/grvt/config.toml` with `0600` permissions.
59
+
60
+ ### 3. Set your default sub-account
61
+
62
+ ```bash
63
+ grvt config set subAccountId YOUR_SUB_ACCOUNT_ID
64
+ ```
65
+
66
+ This saves you from passing `--sub-account-id` on every command.
67
+
68
+ ### 4. Start trading
69
+
70
+ ```bash
71
+ # List all active instruments
72
+ grvt market instruments
73
+
74
+ # Get BTC orderbook
75
+ grvt market orderbook --instrument BTC_USDT_Perp
76
+
77
+ # Place a limit buy order
78
+ grvt trade order create \
79
+ --instrument BTC_USDT_Perp \
80
+ --side buy \
81
+ --type limit \
82
+ --qty 0.01 \
83
+ --price 68000
84
+
85
+ # View open orders
86
+ grvt trade order open
87
+
88
+ # Check positions
89
+ grvt trade positions
90
+
91
+ # View account summary
92
+ grvt account summary
93
+ ```
94
+
95
+ ---
96
+
97
+ ## Global Options
98
+
99
+ These flags are available on every command:
100
+
101
+ | Flag | Description | Default |
102
+ |---|---|---|
103
+ | `--output <format>` | Output format: `json`, `ndjson`, `table`, `raw` | `table` (TTY) / `json` (piped) |
104
+ | `--pretty` | Pretty-print JSON output with indentation | `false` |
105
+ | `--silent` | Suppress info logs; only data goes to stdout | `false` |
106
+ | `--no-color` | Disable colored output | auto-detect |
107
+ | `--yes` | Skip all confirmation prompts | `false` |
108
+ | `--retries <n>` | Number of retries on network/server failure | `3` |
109
+ | `--timeout-ms <n>` | Request timeout in milliseconds | `10000` |
110
+
111
+ Write commands (`order create`, `order cancel`, `transfer create`, `withdraw create`) additionally support:
112
+
113
+ | Flag | Description |
114
+ |---|---|
115
+ | `--dry-run` | Validate and show the exact payload that would be sent, without sending |
116
+ | `--json <path>` | Read the full request body from a file (`@file.json`) or stdin (`-`) |
117
+
118
+ ---
119
+
120
+ ## Commands
121
+
122
+ ### `grvt config` -- Configuration Management
123
+
124
+ Configuration is stored as TOML at `~/.config/grvt/config.toml` (or `$XDG_CONFIG_HOME/grvt/config.toml`).
125
+
126
+ #### `config path`
127
+
128
+ Print the absolute path to your config file.
129
+
130
+ ```bash
131
+ grvt config path
132
+ # /Users/you/.config/grvt/config.toml
133
+ ```
134
+
135
+ #### `config get [key]`
136
+
137
+ Get a single config value. Supports dot-notation for nested keys. Without a key, prints the full config (secrets redacted).
138
+
139
+ ```bash
140
+ grvt config get env
141
+ # testnet
142
+
143
+ grvt config get http.timeoutMs
144
+ # 10000
145
+
146
+ grvt config get --output json
147
+ # {"env":"testnet","subAccountId":"12345",...}
148
+ ```
149
+
150
+ #### `config set <key> <value>`
151
+
152
+ Set a config value. Validated against the config schema on save.
153
+
154
+ ```bash
155
+ grvt config set env testnet
156
+ grvt config set subAccountId 924180738198039
157
+ grvt config set privateKey 0xabcdef...
158
+ grvt config set http.timeoutMs 15000
159
+ grvt config set outputDefaults.pretty true
160
+ ```
161
+
162
+ **Available config keys:**
163
+
164
+ | Key | Type | Description |
165
+ |---|---|---|
166
+ | `env` | `dev\|staging\|testnet\|prod` | GRVT environment |
167
+ | `apiKey` | string | API key for authentication |
168
+ | `privateKey` | string | Ethereum private key for EIP-712 signing |
169
+ | `subAccountId` | string | Default sub-account ID |
170
+ | `accountId` | string | Main account ID (set automatically on login) |
171
+ | `cookie` | string | Session cookie (set automatically on login) |
172
+ | `outputDefaults.output` | `json\|ndjson\|table\|raw` | Default output format |
173
+ | `outputDefaults.pretty` | boolean | Default pretty-print JSON |
174
+ | `outputDefaults.silent` | boolean | Default silent mode |
175
+ | `http.timeoutMs` | number | HTTP request timeout (ms) |
176
+ | `http.retries` | number | Number of retries on failure |
177
+ | `http.backoffMs` | number | Initial retry backoff (ms) |
178
+ | `http.maxBackoffMs` | number | Maximum retry backoff (ms) |
179
+
180
+ #### `config unset <key>`
181
+
182
+ Remove a config value, resetting it to its default.
183
+
184
+ ```bash
185
+ grvt config unset apiKey
186
+ ```
187
+
188
+ #### `config list`
189
+
190
+ Print the full config with secret values redacted.
191
+
192
+ ```bash
193
+ grvt config list --output json --pretty
194
+ ```
195
+
196
+ #### `config export --file <path>`
197
+
198
+ Export the config to a file. By default secrets are redacted.
199
+
200
+ ```bash
201
+ # Export without secrets
202
+ grvt config export --file backup.toml
203
+
204
+ # Export with secrets (prompts for confirmation)
205
+ grvt config export --file backup.toml --include-secrets --yes
206
+ ```
207
+
208
+ #### `config import --file <path>`
209
+
210
+ Import config from a TOML file. Validates the file against the config schema.
211
+
212
+ ```bash
213
+ grvt config import --file backup.toml
214
+ ```
215
+
216
+ ---
217
+
218
+ ### `grvt auth` -- Authentication
219
+
220
+ GRVT uses API key authentication. Login creates a session cookie that is stored locally and sent with every authenticated request.
221
+
222
+ #### `auth login`
223
+
224
+ Authenticate with the GRVT API. Stores the session cookie, account ID, API key, and optionally the private key in config.
225
+
226
+ ```bash
227
+ # Full login (API key + private key for order signing)
228
+ grvt auth login --api-key YOUR_API_KEY --private-key 0xYOUR_KEY
229
+
230
+ # Login with a specific environment
231
+ grvt auth login --api-key YOUR_API_KEY --env testnet
232
+
233
+ # API key can also come from config
234
+ grvt config set apiKey YOUR_API_KEY
235
+ grvt auth login
236
+ ```
237
+
238
+ | Option | Description |
239
+ |---|---|
240
+ | `--api-key <key>` | GRVT API key (falls back to config `apiKey`) |
241
+ | `--private-key <key>` | Ethereum private key for EIP-712 signing (optional) |
242
+ | `--env <env>` | Environment override: `dev\|staging\|testnet\|prod` |
243
+
244
+ #### `auth status`
245
+
246
+ Check whether your current session is valid by making a test API call.
247
+
248
+ ```bash
249
+ grvt auth status --output json
250
+ # {"valid":true,"env":"testnet","accountId":"0xabc..."}
251
+ ```
252
+
253
+ #### `auth whoami`
254
+
255
+ Show your current authentication state without making API calls.
256
+
257
+ ```bash
258
+ grvt auth whoami --output json --pretty
259
+ # {
260
+ # "env": "testnet",
261
+ # "accountId": "0xabc...",
262
+ # "subAccountId": "924180738198039",
263
+ # "hasApiKey": true,
264
+ # "hasPrivateKey": true,
265
+ # "hasSession": true
266
+ # }
267
+ ```
268
+
269
+ #### `auth logout`
270
+
271
+ Clear all stored credentials (API key, private key, session cookie, account ID). Other settings like `env`, `subAccountId`, and `http` config are preserved.
272
+
273
+ ```bash
274
+ grvt auth logout
275
+ ```
276
+
277
+ ---
278
+
279
+ ### `grvt market` -- Market Data
280
+
281
+ Market data commands do **not** require authentication.
282
+
283
+ #### `market instruments`
284
+
285
+ List available trading instruments. Without filters, returns all active instruments.
286
+
287
+ ```bash
288
+ # All active instruments
289
+ grvt market instruments
290
+
291
+ # Filter by kind
292
+ grvt market instruments --kind PERPETUAL
293
+
294
+ # Filter by base and quote currency
295
+ grvt market instruments --base BTC,ETH --quote USDT
296
+
297
+ # Combined filters with JSON output
298
+ grvt market instruments --kind PERPETUAL --base BTC --output json --pretty
299
+ ```
300
+
301
+ | Option | Description |
302
+ |---|---|
303
+ | `--kind <kinds>` | Comma-separated: `PERPETUAL`, `FUTURE`, `CALL`, `PUT` |
304
+ | `--base <currencies>` | Comma-separated base currencies: `BTC`, `ETH`, etc. |
305
+ | `--quote <currencies>` | Comma-separated quote currencies: `USDT`, etc. |
306
+ | `--active` | Only active instruments (default: `true`) |
307
+ | `--limit <n>` | Maximum number of results |
308
+
309
+ #### `market orderbook`
310
+
311
+ Get the order book (bids and asks) for an instrument.
312
+
313
+ ```bash
314
+ grvt market orderbook --instrument BTC_USDT_Perp
315
+ grvt market orderbook --instrument BTC_USDT_Perp --depth 50 --output json
316
+ ```
317
+
318
+ | Option | Description |
319
+ |---|---|
320
+ | `--instrument <name>` | **Required.** Instrument symbol (e.g. `BTC_USDT_Perp`) |
321
+ | `--depth <n>` | Number of levels: `10`, `50`, `100`, or `500` (default: `10`) |
322
+
323
+ #### `market trades`
324
+
325
+ Get recent trade history for an instrument. Supports pagination.
326
+
327
+ ```bash
328
+ grvt market trades --instrument BTC_USDT_Perp --limit 10
329
+ grvt market trades --instrument ETH_USDT_Perp --start-time 2025-01-01T00:00:00Z --limit 50
330
+ ```
331
+
332
+ | Option | Description |
333
+ |---|---|
334
+ | `--instrument <name>` | **Required.** Instrument symbol |
335
+ | `--start-time <time>` | Start time (see [Timestamp Formats](#timestamp-formats)) |
336
+ | `--end-time <time>` | End time |
337
+ | `--limit <n>` | Maximum trades per page |
338
+ | `--cursor <cursor>` | Pagination cursor from a previous response |
339
+ | `--all` | Auto-paginate through all results |
340
+
341
+ #### `market candles`
342
+
343
+ Get candlestick (OHLCV) data for an instrument. Supports pagination.
344
+
345
+ ```bash
346
+ grvt market candles --instrument BTC_USDT_Perp --interval CI_1_H --limit 24
347
+ grvt market candles --instrument BTC_USDT_Perp --interval CI_1_D --type MARK
348
+ ```
349
+
350
+ | Option | Description |
351
+ |---|---|
352
+ | `--instrument <name>` | **Required.** Instrument symbol |
353
+ | `--interval <interval>` | **Required.** Candle interval (see below) |
354
+ | `--type <type>` | Price type: `TRADE`, `MARK`, `INDEX`, `MID` (default: `TRADE`) |
355
+ | `--start-time <time>` | Start time |
356
+ | `--end-time <time>` | End time |
357
+ | `--limit <n>` | Maximum candles per page |
358
+ | `--cursor <cursor>` | Pagination cursor |
359
+ | `--all` | Auto-paginate all results |
360
+
361
+ **Candle intervals:** `CI_1_M`, `CI_5_M`, `CI_15_M`, `CI_30_M`, `CI_1_H`, `CI_2_H`, `CI_4_H`, `CI_6_H`, `CI_8_H`, `CI_12_H`, `CI_1_D`, `CI_1_W`
362
+
363
+ #### `market funding-rate`
364
+
365
+ Get funding rate history for a perpetual instrument. Supports pagination.
366
+
367
+ ```bash
368
+ grvt market funding-rate --instrument BTC_USDT_Perp --limit 10
369
+ ```
370
+
371
+ | Option | Description |
372
+ |---|---|
373
+ | `--instrument <name>` | **Required.** Instrument symbol |
374
+ | `--start-time <time>` | Start time |
375
+ | `--end-time <time>` | End time |
376
+ | `--limit <n>` | Maximum entries per page |
377
+ | `--cursor <cursor>` | Pagination cursor |
378
+ | `--all` | Auto-paginate all results |
379
+
380
+ #### `market instrument`
381
+
382
+ Get full metadata for a single instrument by name.
383
+
384
+ ```bash
385
+ grvt market instrument --instrument BTC_USDT_Perp --output json --pretty
386
+ ```
387
+
388
+ | Option | Description |
389
+ |---|---|
390
+ | `--instrument <name>` | **Required.** Instrument symbol (e.g. `BTC_USDT_Perp`) |
391
+
392
+ #### `market currency`
393
+
394
+ List all supported currencies with their IDs, symbols, and decimal precision.
395
+
396
+ ```bash
397
+ grvt market currency --output json --pretty
398
+ ```
399
+
400
+ No options required.
401
+
402
+ #### `market mini-ticker`
403
+
404
+ Get lightweight price data for an instrument: last price, mark price, index price, 24h change, open interest, and funding rate.
405
+
406
+ ```bash
407
+ grvt market mini-ticker --instrument BTC_USDT_Perp --output json --pretty
408
+ ```
409
+
410
+ | Option | Description |
411
+ |---|---|
412
+ | `--instrument <name>` | **Required.** Instrument symbol |
413
+
414
+ #### `market ticker`
415
+
416
+ Get full ticker data with 24h volume, bid/ask, and optionally greeks (for options) and derived statistics.
417
+
418
+ ```bash
419
+ grvt market ticker --instrument BTC_USDT_Perp --output json --pretty
420
+ grvt market ticker --instrument BTC_USDT_Perp --greeks --derived
421
+ ```
422
+
423
+ | Option | Description |
424
+ |---|---|
425
+ | `--instrument <name>` | **Required.** Instrument symbol |
426
+ | `--greeks` | Include greeks data (relevant for options) |
427
+ | `--derived` | Include derived statistics (24h volume, buy/sell ratio) |
428
+
429
+ #### `market margin-rules`
430
+
431
+ Get margin rules (initial margin, maintenance margin, risk bracket tiers) for an instrument.
432
+
433
+ ```bash
434
+ grvt market margin-rules --instrument BTC_USDT_Perp --output json --pretty
435
+ ```
436
+
437
+ | Option | Description |
438
+ |---|---|
439
+ | `--instrument <name>` | **Required.** Instrument symbol (e.g. `BTC_USDT_Perp`) |
440
+
441
+ ---
442
+
443
+ ### `grvt account` -- Account Information
444
+
445
+ All account commands require authentication.
446
+
447
+ #### `account funding`
448
+
449
+ Get your funding (main) account summary -- balances and margin info.
450
+
451
+ ```bash
452
+ grvt account funding --output json --pretty
453
+ ```
454
+
455
+ #### `account summary`
456
+
457
+ Get a specific sub-account summary including balances, margin, and P&L.
458
+
459
+ ```bash
460
+ grvt account summary
461
+ grvt account summary --sub-account-id 924180738198039 --output json
462
+ ```
463
+
464
+ | Option | Description |
465
+ |---|---|
466
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config `subAccountId`) |
467
+
468
+ #### `account aggregated`
469
+
470
+ Get an aggregated summary across all your sub-accounts.
471
+
472
+ ```bash
473
+ grvt account aggregated --output json --pretty
474
+ ```
475
+
476
+ #### `account history`
477
+
478
+ Get hourly snapshots of sub-account state (balances, margin, P&L) over time. Supports pagination. History retained for 30 days.
479
+
480
+ ```bash
481
+ grvt account history --limit 10
482
+ grvt account history --sub-account-id 924180738198039 --start-time 2025-01-01 --all
483
+ ```
484
+
485
+ | Option | Description |
486
+ |---|---|
487
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config `subAccountId`) |
488
+ | `--start-time <time>` | Start time |
489
+ | `--end-time <time>` | End time |
490
+ | `--limit <n>` | Max results per page |
491
+ | `--cursor <cursor>` | Pagination cursor |
492
+ | `--all` | Auto-paginate all results |
493
+
494
+ ---
495
+
496
+ ### `grvt trade` -- Trading
497
+
498
+ All trade commands require authentication. Write commands (`order create`, `order cancel`, `order cancel-all`) also require a private key for EIP-712 signing.
499
+
500
+ #### `trade order create`
501
+
502
+ Create a new order. Requires a private key for EIP-712 signing.
503
+
504
+ ```bash
505
+ # Limit buy order
506
+ grvt trade order create \
507
+ --instrument BTC_USDT_Perp \
508
+ --side buy \
509
+ --type limit \
510
+ --qty 0.01 \
511
+ --price 68000
512
+
513
+ # Market sell order
514
+ grvt trade order create \
515
+ --instrument BTC_USDT_Perp \
516
+ --side sell \
517
+ --type market \
518
+ --qty 0.01 \
519
+ --time-in-force IOC
520
+
521
+ # Post-only limit order
522
+ grvt trade order create \
523
+ --instrument ETH_USDT_Perp \
524
+ --side buy \
525
+ --type limit \
526
+ --qty 0.5 \
527
+ --price 2500 \
528
+ --post-only
529
+
530
+ # Reduce-only order (only decreases position)
531
+ grvt trade order create \
532
+ --instrument BTC_USDT_Perp \
533
+ --side sell \
534
+ --type limit \
535
+ --qty 0.01 \
536
+ --price 100000 \
537
+ --reduce-only
538
+
539
+ # Preview the exact API payload without sending
540
+ grvt trade order create \
541
+ --instrument BTC_USDT_Perp \
542
+ --side buy --type limit --qty 0.01 --price 68000 \
543
+ --dry-run --output json --pretty
544
+
545
+ # From a JSON file
546
+ grvt trade order create --json @order.json
547
+ ```
548
+
549
+ | Option | Description |
550
+ |---|---|
551
+ | `--instrument <name>` | **Required.** Instrument symbol (e.g. `BTC_USDT_Perp`) |
552
+ | `--side <side>` | **Required.** `buy` or `sell` |
553
+ | `--type <type>` | **Required.** `limit` or `market` |
554
+ | `--qty <amount>` | **Required.** Order quantity in base asset (e.g. `0.01` = 0.01 BTC) |
555
+ | `--price <price>` | Limit price in quote asset. **Required for limit orders.** |
556
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
557
+ | `--time-in-force <tif>` | `GTT`, `IOC`, `FOK`, or `AON` (default: `GTT`) |
558
+ | `--post-only` | Order must be a maker order or it's cancelled |
559
+ | `--reduce-only` | Order can only reduce an existing position |
560
+ | `--client-order-id <id>` | Custom numeric order ID (auto-generated if omitted) |
561
+ | `--expiration-seconds <n>` | Order expiration in seconds (default: `3600`) |
562
+ | `--json <path>` | Read full request body from file or stdin |
563
+ | `--dry-run` | Show payload without sending |
564
+
565
+ **Time-in-force values:**
566
+
567
+ | Value | Aliases | Description |
568
+ |---|---|---|
569
+ | `GOOD_TILL_TIME` | `GTT` | Order stays open until cancelled or expired |
570
+ | `IMMEDIATE_OR_CANCEL` | `IOC` | Fill as much as possible immediately, cancel the rest |
571
+ | `FILL_OR_KILL` | `FOK` | Fill the entire order immediately, or cancel it |
572
+ | `ALL_OR_NONE` | `AON` | Fill the entire order or not at all (block trades only) |
573
+
574
+ **How signing works:**
575
+
576
+ When you create an order, the CLI:
577
+
578
+ 1. Fetches instrument metadata (cached for 1 hour) to get the `instrument_hash` and `base_decimals`
579
+ 2. Builds an EIP-712 typed data structure with the order parameters
580
+ 3. Signs it with your Ethereum private key using viem
581
+ 4. Sends the order with a structured signature (`signer`, `r`, `s`, `v`, `expiration`, `nonce`) to the API
582
+
583
+ The `--dry-run` flag lets you inspect the exact payload before it hits the API.
584
+
585
+ #### `trade order get`
586
+
587
+ Look up a specific order by order ID or client order ID.
588
+
589
+ ```bash
590
+ grvt trade order get --order-id 0xabc123...
591
+ grvt trade order get --client-order-id 18160428530557423435
592
+ ```
593
+
594
+ | Option | Description |
595
+ |---|---|
596
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
597
+ | `--order-id <id>` | GRVT-assigned order ID |
598
+ | `--client-order-id <id>` | Your client-specified order ID |
599
+
600
+ One of `--order-id` or `--client-order-id` is required.
601
+
602
+ #### `trade order open`
603
+
604
+ List all currently open orders.
605
+
606
+ ```bash
607
+ grvt trade order open
608
+ grvt trade order open --kind PERPETUAL --base BTC --output json
609
+ ```
610
+
611
+ | Option | Description |
612
+ |---|---|
613
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
614
+ | `--kind <kinds>` | Comma-separated filter: `PERPETUAL`, `FUTURE`, `CALL`, `PUT` |
615
+ | `--base <currencies>` | Comma-separated base currency filter |
616
+ | `--quote <currencies>` | Comma-separated quote currency filter |
617
+
618
+ #### `trade order cancel`
619
+
620
+ Cancel a single order by its order ID or client order ID.
621
+
622
+ ```bash
623
+ grvt trade order cancel --order-id 0xabc123...
624
+ grvt trade order cancel --client-order-id 18160428530557423435
625
+
626
+ # Preview what would be sent
627
+ grvt trade order cancel --order-id 0xabc123... --dry-run
628
+ ```
629
+
630
+ | Option | Description |
631
+ |---|---|
632
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
633
+ | `--order-id <id>` | GRVT-assigned order ID |
634
+ | `--client-order-id <id>` | Your client-specified order ID |
635
+ | `--dry-run` | Show payload without sending |
636
+
637
+ #### `trade order cancel-all`
638
+
639
+ Cancel all open orders. Prompts for confirmation unless `--yes` is passed.
640
+
641
+ ```bash
642
+ grvt trade order cancel-all
643
+ grvt trade order cancel-all --instrument BTC_USDT_Perp --yes
644
+ ```
645
+
646
+ | Option | Description |
647
+ |---|---|
648
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
649
+ | `--instrument <name>` | Only cancel orders for this instrument |
650
+ | `--dry-run` | Show payload without sending |
651
+
652
+ #### `trade order history`
653
+
654
+ Get historical orders (filled, cancelled, rejected). Supports pagination.
655
+
656
+ ```bash
657
+ grvt trade order history --limit 20
658
+ grvt trade order history --kind PERPETUAL --start-time 2025-01-01 --output ndjson
659
+ ```
660
+
661
+ | Option | Description |
662
+ |---|---|
663
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
664
+ | `--kind <kinds>` | Comma-separated filter |
665
+ | `--base <currencies>` | Base currency filter |
666
+ | `--quote <currencies>` | Quote currency filter |
667
+ | `--start-time <time>` | Start time |
668
+ | `--end-time <time>` | End time |
669
+ | `--limit <n>` | Max results per page |
670
+ | `--cursor <cursor>` | Pagination cursor |
671
+ | `--all` | Auto-paginate all results |
672
+
673
+ #### `trade fills`
674
+
675
+ Get fill (trade execution) history. Supports pagination.
676
+
677
+ ```bash
678
+ grvt trade fills --limit 10
679
+ grvt trade fills --kind PERPETUAL --base BTC --start-time 2025-01-01 --all
680
+ ```
681
+
682
+ | Option | Description |
683
+ |---|---|
684
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
685
+ | `--kind <kinds>` | Comma-separated filter |
686
+ | `--base <currencies>` | Base currency filter |
687
+ | `--quote <currencies>` | Quote currency filter |
688
+ | `--start-time <time>` | Start time |
689
+ | `--end-time <time>` | End time |
690
+ | `--limit <n>` | Max results per page |
691
+ | `--cursor <cursor>` | Pagination cursor |
692
+ | `--all` | Auto-paginate all results |
693
+
694
+ #### `trade positions`
695
+
696
+ Get current open positions.
697
+
698
+ ```bash
699
+ grvt trade positions
700
+ grvt trade positions --kind PERPETUAL --output json --pretty
701
+ ```
702
+
703
+ | Option | Description |
704
+ |---|---|
705
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
706
+ | `--kind <kinds>` | Comma-separated filter |
707
+ | `--base <currencies>` | Base currency filter |
708
+ | `--quote <currencies>` | Quote currency filter |
709
+
710
+ #### `trade funding-payments`
711
+
712
+ Get funding payment history. Supports pagination.
713
+
714
+ ```bash
715
+ grvt trade funding-payments --limit 20
716
+ grvt trade funding-payments --start-time 2025-01-01 --all --output ndjson
717
+ ```
718
+
719
+ | Option | Description |
720
+ |---|---|
721
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
722
+ | `--kind <kinds>` | Comma-separated filter |
723
+ | `--base <currencies>` | Base currency filter |
724
+ | `--quote <currencies>` | Quote currency filter |
725
+ | `--start-time <time>` | Start time |
726
+ | `--end-time <time>` | End time |
727
+ | `--limit <n>` | Max results per page |
728
+ | `--cursor <cursor>` | Pagination cursor |
729
+ | `--all` | Auto-paginate all results |
730
+
731
+ #### `trade leverage get`
732
+
733
+ Get initial leverage settings for all instruments on a sub-account. Optionally filter to a single instrument.
734
+
735
+ ```bash
736
+ grvt trade leverage get
737
+ grvt trade leverage get --instrument BTC_USDT_Perp --output json --pretty
738
+ ```
739
+
740
+ | Option | Description |
741
+ |---|---|
742
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
743
+ | `--instrument <name>` | Filter to a single instrument (client-side) |
744
+
745
+ #### `trade leverage set`
746
+
747
+ Set the initial leverage for a specific instrument on a sub-account.
748
+
749
+ ```bash
750
+ grvt trade leverage set --instrument BTC_USDT_Perp --leverage 10
751
+ grvt trade leverage set --instrument ETH_USDT_Perp --leverage 5 --output json
752
+ ```
753
+
754
+ | Option | Description |
755
+ |---|---|
756
+ | `--instrument <name>` | **Required.** Instrument symbol |
757
+ | `--leverage <value>` | **Required.** Leverage value (e.g. `10`) |
758
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
759
+
760
+ #### `trade cancel-on-disconnect`
761
+
762
+ Set, refresh, or disable the cancel-on-disconnect countdown. When active, all open orders are automatically cancelled if no heartbeat is received within the countdown period. Used by market makers to protect against connection drops.
763
+
764
+ ```bash
765
+ # Set countdown to 5 seconds
766
+ grvt trade cancel-on-disconnect --countdown 5000
767
+
768
+ # Disable cancel-on-disconnect
769
+ grvt trade cancel-on-disconnect --countdown 0
770
+ ```
771
+
772
+ | Option | Description |
773
+ |---|---|
774
+ | `--countdown <ms>` | **Required.** Countdown in milliseconds (1000–300000, or `0` to disable) |
775
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
776
+
777
+ #### `trade derisk`
778
+
779
+ Set the derisk-to-maintenance-margin ratio for a sub-account. Requires a private key for EIP-712 signing.
780
+
781
+ ```bash
782
+ grvt trade derisk --ratio 0.5
783
+ grvt trade derisk --ratio 0.8 --dry-run --output json --pretty
784
+ ```
785
+
786
+ | Option | Description |
787
+ |---|---|
788
+ | `--ratio <value>` | **Required.** Derisk MM ratio (decimal, e.g. `0.5`) |
789
+ | `--sub-account-id <id>` | Sub-account ID (falls back to config) |
790
+ | `--dry-run` | Show payload without sending |
791
+
792
+ ---
793
+
794
+ ### `grvt funds` -- Fund Management
795
+
796
+ All fund commands require authentication.
797
+
798
+ #### `funds deposit history`
799
+
800
+ Get deposit history. Supports pagination.
801
+
802
+ ```bash
803
+ grvt funds deposit history --limit 10 --output json
804
+ ```
805
+
806
+ Deposit creation is not available via REST API -- use the [GRVT web interface](https://grvt.io).
807
+
808
+ | Option | Description |
809
+ |---|---|
810
+ | `--start-time <time>` | Start time |
811
+ | `--end-time <time>` | End time |
812
+ | `--limit <n>` | Max results per page |
813
+ | `--cursor <cursor>` | Pagination cursor |
814
+ | `--all` | Auto-paginate all results |
815
+
816
+ #### `funds transfer create`
817
+
818
+ Transfer funds between sub-accounts. Requires a private key for EIP-712 signing. Prompts for confirmation.
819
+
820
+ ```bash
821
+ grvt funds transfer create \
822
+ --from-sub-account-id 111111 \
823
+ --to-sub-account-id 222222 \
824
+ --currency USDT \
825
+ --amount 100
826
+
827
+ # Preview without sending
828
+ grvt funds transfer create \
829
+ --from-sub-account-id 111111 \
830
+ --to-sub-account-id 222222 \
831
+ --currency USDT --amount 100 \
832
+ --dry-run
833
+
834
+ # From JSON file
835
+ grvt funds transfer create --json @transfer.json
836
+ ```
837
+
838
+ | Option | Description |
839
+ |---|---|
840
+ | `--from-sub-account-id <id>` | **Required.** Source sub-account ID |
841
+ | `--to-sub-account-id <id>` | **Required.** Destination sub-account ID |
842
+ | `--currency <symbol>` | **Required.** Currency symbol (e.g. `USDT`) |
843
+ | `--amount <amount>` | **Required.** Amount to transfer |
844
+ | `--json <path>` | Read request body from file or stdin |
845
+ | `--dry-run` | Show payload without sending |
846
+
847
+ #### `funds transfer history`
848
+
849
+ Get transfer history. Supports pagination.
850
+
851
+ ```bash
852
+ grvt funds transfer history --limit 10
853
+ ```
854
+
855
+ | Option | Description |
856
+ |---|---|
857
+ | `--start-time <time>` | Start time |
858
+ | `--end-time <time>` | End time |
859
+ | `--limit <n>` | Max results per page |
860
+ | `--cursor <cursor>` | Pagination cursor |
861
+ | `--all` | Auto-paginate all results |
862
+
863
+ #### `funds withdraw create`
864
+
865
+ Withdraw funds from a sub-account to an Ethereum address. Requires a private key for EIP-712 signing. Prompts for confirmation.
866
+
867
+ ```bash
868
+ grvt funds withdraw create \
869
+ --to-address 0xYOUR_ETH_ADDRESS \
870
+ --currency USDT \
871
+ --amount 100
872
+
873
+ # With explicit sub-account
874
+ grvt funds withdraw create \
875
+ --sub-account-id 111111 \
876
+ --to-address 0xYOUR_ETH_ADDRESS \
877
+ --currency USDT \
878
+ --amount 50 \
879
+ --dry-run
880
+ ```
881
+
882
+ | Option | Description |
883
+ |---|---|
884
+ | `--sub-account-id <id>` | Source sub-account ID (falls back to config) |
885
+ | `--to-address <address>` | **Required.** Destination Ethereum address (`0x...`, 42 chars) |
886
+ | `--currency <symbol>` | **Required.** Currency symbol (e.g. `USDT`) |
887
+ | `--amount <amount>` | **Required.** Amount to withdraw |
888
+ | `--main-account-id <id>` | Main account ID (defaults to current account from login) |
889
+ | `--json <path>` | Read request body from file or stdin |
890
+ | `--dry-run` | Show payload without sending |
891
+
892
+ #### `funds withdraw history`
893
+
894
+ Get withdrawal history. Supports pagination.
895
+
896
+ ```bash
897
+ grvt funds withdraw history --limit 10
898
+ ```
899
+
900
+ | Option | Description |
901
+ |---|---|
902
+ | `--start-time <time>` | Start time |
903
+ | `--end-time <time>` | End time |
904
+ | `--limit <n>` | Max results per page |
905
+ | `--cursor <cursor>` | Pagination cursor |
906
+ | `--all` | Auto-paginate all results |
907
+
908
+ ---
909
+
910
+ ## Timestamp Formats
911
+
912
+ All `--start-time` and `--end-time` options accept these formats:
913
+
914
+ | Format | Example | Description |
915
+ |---|---|---|
916
+ | Unix seconds | `1704067200` | 10-digit integer |
917
+ | Unix milliseconds | `1704067200000` | 13-digit integer |
918
+ | Unix nanoseconds | `1704067200000000000` | 19-digit integer |
919
+ | ISO 8601 | `2025-01-01T00:00:00Z` | UTC datetime string |
920
+
921
+ All timestamps are converted to nanoseconds internally for the GRVT API.
922
+
923
+ ---
924
+
925
+ ## Pagination
926
+
927
+ Commands that return lists support cursor-based pagination:
928
+
929
+ ```bash
930
+ # Get first page with a limit
931
+ grvt trade fills --limit 100
932
+
933
+ # Continue from a specific cursor (returned in previous response)
934
+ grvt trade fills --limit 100 --cursor "eyJhZnRlciI6..."
935
+
936
+ # Auto-paginate all results into a single array
937
+ grvt trade fills --all --output json
938
+
939
+ # Stream results as NDJSON (one JSON object per line, ideal for piping)
940
+ grvt trade fills --all --output ndjson | jq '.trade_id'
941
+
942
+ # Time-bounded paginated query
943
+ grvt trade fills --start-time 2025-01-01 --end-time 2025-02-01 --all
944
+ ```
945
+
946
+ When using `--all` with `--output ndjson`, results stream as they are fetched (no buffering). With other output formats, all pages are collected before output.
947
+
948
+ ---
949
+
950
+ ## Output Formats
951
+
952
+ ### `json` (default when piped)
953
+
954
+ Compact JSON on a single line. Add `--pretty` for indented output.
955
+
956
+ ```bash
957
+ grvt trade positions --output json
958
+ # [{"instrument":"BTC_USDT_Perp","size":"0.01",...}]
959
+
960
+ grvt trade positions --output json --pretty
961
+ # [
962
+ # {
963
+ # "instrument": "BTC_USDT_Perp",
964
+ # "size": "0.01",
965
+ # ...
966
+ # }
967
+ # ]
968
+ ```
969
+
970
+ ### `ndjson`
971
+
972
+ One JSON object per line. Best for streaming and piping to `jq`, `grep`, etc.
973
+
974
+ ```bash
975
+ grvt trade fills --all --output ndjson | jq '.instrument'
976
+ ```
977
+
978
+ ### `table` (default in terminal)
979
+
980
+ Unicode box-drawing table with colored headers.
981
+
982
+ ```bash
983
+ grvt trade positions
984
+ # ┌─────────────────┬──────┬──────────┐
985
+ # │ instrument │ size │ ... │
986
+ # ├─────────────────┼──────┼──────────┤
987
+ # │ BTC_USDT_Perp │ 0.01 │ ... │
988
+ # └─────────────────┴──────┴──────────┘
989
+ ```
990
+
991
+ ### `raw`
992
+
993
+ The raw JSON response from the GRVT API, without any transformation.
994
+
995
+ ```bash
996
+ grvt account funding --output raw
997
+ ```
998
+
999
+ ---
1000
+
1001
+ ## Exit Codes
1002
+
1003
+ | Code | Meaning |
1004
+ |---|---|
1005
+ | `0` | Success |
1006
+ | `2` | Usage / validation error (bad arguments, missing options) |
1007
+ | `3` | Authentication / permission error |
1008
+ | `4` | Partial failure (batch operations) |
1009
+ | `5` | API / network error |
1010
+
1011
+ Use exit codes in scripts:
1012
+
1013
+ ```bash
1014
+ grvt auth status --silent && echo "Logged in" || echo "Not logged in"
1015
+ ```
1016
+
1017
+ ---
1018
+
1019
+ ## JSON Input
1020
+
1021
+ Write commands support reading the full request body from a file or stdin with `--json`:
1022
+
1023
+ ```bash
1024
+ # From a file
1025
+ grvt trade order create --json @order.json
1026
+
1027
+ # From stdin
1028
+ echo '{"order":{...}}' | grvt trade order create --json -
1029
+
1030
+ # Pipe from another command
1031
+ cat order.json | grvt trade order create --json -
1032
+ ```
1033
+
1034
+ When `--json` is used, all other command-specific options (like `--instrument`, `--side`, etc.) are ignored -- the JSON payload is sent as-is.
1035
+
1036
+ ---
1037
+
1038
+ ## Configuration File
1039
+
1040
+ Full config file example (`~/.config/grvt/config.toml`):
1041
+
1042
+ ```toml
1043
+ env = "testnet"
1044
+ apiKey = "your-api-key"
1045
+ privateKey = "0xyour-private-key"
1046
+ subAccountId = "924180738198039"
1047
+
1048
+ [outputDefaults]
1049
+ output = "table"
1050
+ pretty = false
1051
+ silent = false
1052
+
1053
+ [http]
1054
+ timeoutMs = 10000
1055
+ retries = 3
1056
+ backoffMs = 200
1057
+ maxBackoffMs = 10000
1058
+ ```
1059
+
1060
+ The `apiKey`, `privateKey`, and `cookie` fields are stored with `0600` file permissions and redacted in `config list` output.
1061
+
1062
+ ---
1063
+
1064
+ ## Environments
1065
+
1066
+ | Environment | Market Data | Trading | Edge |
1067
+ |---|---|---|---|
1068
+ | `dev` | `market-data.dev.gravitymarkets.io` | `trades.dev.gravitymarkets.io` | `edge.dev.gravitymarkets.io` |
1069
+ | `staging` | `market-data.staging.gravitymarkets.io` | `trades.staging.gravitymarkets.io` | `edge.staging.gravitymarkets.io` |
1070
+ | `testnet` | `market-data.testnet.grvt.io` | `trades.testnet.grvt.io` | `edge.testnet.grvt.io` |
1071
+ | `prod` | `market-data.grvt.io` | `trades.grvt.io` | `edge.grvt.io` |
1072
+
1073
+ ---
1074
+
1075
+ ## Library Usage
1076
+
1077
+ The package exports all core functions for programmatic use:
1078
+
1079
+ ```typescript
1080
+ import {
1081
+ loadConfig,
1082
+ createHttpClient,
1083
+ getOpenOrders,
1084
+ getPositions,
1085
+ getFundingAccountSummary,
1086
+ ENDPOINTS,
1087
+ } from "grvt-cli";
1088
+
1089
+ const config = loadConfig();
1090
+ const client = createHttpClient({
1091
+ env: config.env,
1092
+ cookie: config.cookie,
1093
+ accountId: config.accountId,
1094
+ });
1095
+
1096
+ const positions = await getPositions(client, {
1097
+ sub_account_id: config.subAccountId!,
1098
+ });
1099
+
1100
+ const orders = await getOpenOrders(client, {
1101
+ sub_account_id: config.subAccountId!,
1102
+ kind: ["PERPETUAL"],
1103
+ });
1104
+
1105
+ const funding = await getFundingAccountSummary(client);
1106
+ ```
1107
+
1108
+ **Exported functions:**
1109
+
1110
+ | Module | Exports |
1111
+ |---|---|
1112
+ | Config | `loadConfig`, `saveConfig`, `configSchema`, `GrvtConfig` |
1113
+ | Auth | `login`, `logout`, `verifySession` |
1114
+ | Trading | `createOrder`, `cancelOrder`, `cancelAllOrders`, `getOrder`, `getOpenOrders`, `getOrderHistory` |
1115
+ | Fills | `getFillHistory` |
1116
+ | Positions | `getPositions` |
1117
+ | Funding | `getFundingPayments` |
1118
+ | Account | `getFundingAccountSummary`, `getSubAccountSummary`, `getAggregatedAccountSummary`, `getAccountHistory` |
1119
+ | Funds | `createDeposit`, `getDepositHistory`, `createTransfer`, `getTransferHistory`, `createWithdrawal`, `getWithdrawalHistory` |
1120
+ | Currencies | `getCurrencies`, `getCurrencyId`, `getCurrencyDecimals` |
1121
+ | Signing | `buildTransferTypedData`, `buildWithdrawalTypedData`, `buildDeriskTypedData` |
1122
+ | Client | `createHttpClient`, `ENDPOINTS`, `GrvtEnvironment` |
1123
+ | Pagination | `paginateCursor` |
1124
+
1125
+ ---
1126
+
1127
+ ## Instrument Names
1128
+
1129
+ GRVT instruments follow this naming convention:
1130
+
1131
+ | Type | Format | Example |
1132
+ |---|---|---|
1133
+ | Perpetual | `{BASE}_{QUOTE}_Perp` | `BTC_USDT_Perp` |
1134
+ | Future | `{BASE}_{QUOTE}_Fut_{DATE}` | `BTC_USDT_Fut_20Oct23` |
1135
+ | Call | `{BASE}_{QUOTE}_Call_{DATE}_{STRIKE}` | `ETH_USDT_Call_20Oct23_2800` |
1136
+ | Put | `{BASE}_{QUOTE}_Put_{DATE}_{STRIKE}` | `ETH_USDT_Put_20Oct23_2800` |
1137
+
1138
+ Use `grvt market instruments` to discover available instruments.
1139
+
1140
+ ---
1141
+
1142
+ ## Testing
1143
+
1144
+ ```bash
1145
+ # Unit tests
1146
+ pnpm test
1147
+
1148
+ # E2E tests (requires testnet credentials)
1149
+ pnpm test:e2e --api-key YOUR_KEY --private-key 0xYOUR_KEY --sub-account-id YOUR_ID
1150
+ ```
1151
+
1152
+ ## License
1153
+
1154
+ MIT