hypercall-client 0.4.0

Client library for interacting with Hypercall trading platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# hypercall-client


Rust API client for Hypercall.

## Responsibilities

- HTTP and websocket clients for Hypercall API surfaces.
- EIP-712 wallet helpers for public options, RFQ, and quote-provider signing.
- TEE perp trading: enrollment, builder approval, orders, triggers, batches,
  cancels, leverage, preview, and markets.
- RFQ V2: EIP-712 signing for every action, the `/private/rfqs*` transport,
  ambiguous-submission recovery, and the private `rfq_v2` WebSocket channel.
- Request and response convenience types re-exported from `hypercall-sdk-types`.

## Getting Started

Add the client from the public Git repository:

```toml
[dependencies]
hypercall-client = { git = "https://github.com/hypercall-public/hypercall-rust" }
rust_decimal = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
```

Create a production API client:

```rust,no_run
use hypercall_client::HypercallClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let instruments = client.get_instrument_specs("BTC").await?;

    println!("BTC instruments: {}", instruments.len());
    Ok(())
}
```

## WebSocket recovery

`WsClient` reconnects established `/ws` sessions with capped exponential
backoff and jitter. It identifies the wallet again, restores the exact desired
subscriptions and filters, and waits for subscription confirmations before
reporting that the transport is connected.

```rust,no_run
use hypercall_client::{WsClient, WsConnectionState};

#[tokio::main]
async fn main() -> hypercall_client::Result<()> {
    let ws = WsClient::new();
    ws.connect("https://api.hypercall.xyz", None).await?;
    ws.subscribe_with_options(
        "indicative_market_data",
        Some(vec!["BTC".to_string()]),
        None,
        None,
    )
    .await?;

    if let WsConnectionState::RecoveryRequired(plan) = ws.connection_state() {
        // Fetch the snapshots or REST state selected by `plan`, then acknowledge it.
        ws.mark_recovered()?;
    }
    Ok(())
}
```

Replaceable public streams become ready after resubscription. Ordered public
and private streams enter `RecoveryRequired` because reconnecting cannot prove
that no events were missed. Callers must fetch the required order book snapshot,
portfolio snapshot, or durable REST state before calling `mark_recovered()`.
Portfolio subscribers must do both because durable position-expiry events share
the portfolio channel.
Use `connection_state_receiver()` to watch transitions and
`last_disconnect_reason()` to inspect structured slow-consumer closes.

## Batched indicative market data

`subscribe_indicative_market_data(symbols)` subscribes to
`indicative_market_data_v2`, which delivers one `IndicativeMarketDataBatch`
frame per batch window (25 ms by default). Rows arrive up to one window later,
in exchange for far fewer frames that compress much better when the connection
uses `permessage-deflate`. If the server confirms the channel without
`"batch": true` or rejects it as `unknown_channel`, the client moves the
subscription to `indicative_market_data`, one `IndicativeMarketData` frame per
row. Opt out with `WsClient::new().with_batched_indicative(false)`. Read rows
through `hypercall_client::indicative`, which handles both:

```rust,no_run
use hypercall_client::{indicative::indicative_rows_from_value, WsClient};

#[tokio::main]
async fn main() -> hypercall_client::Result<()> {
    let ws = WsClient::new();
    ws.connect("https://api.hypercall.xyz", None).await?;
    ws.subscribe_indicative_market_data(Some(vec!["BTC-20261002-87000-C".to_string()]))
        .await?;

    for message in ws.get_messages().await {
        if let Some(rows) = indicative_rows_from_value(&message)? {
            for row in rows {
                println!("{} bid {:?} ask {:?}", row.instrument, row.best_bid, row.best_ask);
            }
        }
    }
    Ok(())
}
```

## Wallet Signing

`HypercallWallet` supports local private-key signing in every build:

```rust,no_run
use hypercall_client::{HypercallSigner, HypercallWallet};

fn main() -> hypercall_client::Result<()> {
    let wallet = HypercallWallet::from_private_key("0xYOUR_PRIVATE_KEY", 999)?;

    println!("wallet: {}", wallet.address());
    Ok(())
}
```

Enable the `kms` feature to use AWS KMS-backed signing through the AWS SDK
default credential chain:

```bash
cargo check -p hypercall-client --features kms
```

Private key export only works for local private-key wallets. KMS wallets never
expose private key material.

## Unified-account setup

Create and register the managed `Account.sol` before selecting portfolio
margin. The caller supplies the HyperEVM RPC URL and the Factory address for
the target deployment. The SDK reads the Factory's live minimum HYPE deposit
and applies the same 1% deposit and 20% gas-limit buffers as the Hypercall app.

```rust,no_run
use hypercall_client::{
    AccountAddress, CreateAccountParams, HypercallClient, HypercallWallet, MarginMode,
    UpdateApiWalletParams,
};
use alloy::primitives::keccak256;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let manager = HypercallWallet::from_private_key("0xYOUR_MANAGER_WALLET_KEY", 999)?;
    let api_wallet = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
    let factory: AccountAddress = "0xYOUR_FACTORY_ADDRESS".parse()?;

    let created = client
        .create_account(
            &manager,
            CreateAccountParams {
                rpc_url: "https://YOUR_HYPEREVM_RPC".to_string(),
                factory_address: factory,
                salt: None,
            },
        )
        .await?;
    client
        .update_api_wallet(
            &manager,
            UpdateApiWalletParams {
                account: created.account,
                name: keccak256("primary-api-wallet"),
                api_wallet: api_wallet.address,
                nonce: None,
            },
        )
        .await?;
    client
        .set_margin_mode_typed(&manager, MarginMode::Portfolio)
        .await?;

    println!("account: {}", created.account);
    println!("transaction: {}", created.transaction_hash);
    Ok(())
}
```

The manager and salt select a deterministic CREATE2 address. Account creation
is not idempotent: submitting again with an already-used manager/salt pair
reverts. `salt: None` uses salt zero to match the Hypercall app. Supply another
salt only when deliberately creating a distinct managed account.

API-wallet updates are manager-authorized directives. Reusing a name replaces
its current API wallet. Passing the zero address removes that named wallet.

Account abstraction changes are manager-authorized. The SDK binds the signed
action target to the supplied managed Account address.

```rust,no_run
use hypercall_client::{
    AccountAddress, DirectiveDeliveryStatus, DirectiveStage, HypercallClient, HypercallWallet,
    HypercoreAccountAbstraction, SetAccountAbstractionParams,
};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let manager = HypercallWallet::from_private_key("0xYOUR_MANAGER_WALLET_KEY", 999)?;
    let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;

    let submitted = client
        .set_account_abstraction(
            &manager,
            SetAccountAbstractionParams {
                account,
                abstraction: HypercoreAccountAbstraction::UnifiedAccount,
                nonce: None,
            },
        )
        .await?;
    if submitted.stage == DirectiveStage::Rejected {
        return Err(format!("account abstraction rejected: {:?}", submitted.rejection).into());
    }

    loop {
        let status = client.get_directive_status(&submitted.directive_id).await?;
        match status.delivery_status {
            DirectiveDeliveryStatus::Finalized => break,
            DirectiveDeliveryStatus::CoreRejected
            | DirectiveDeliveryStatus::Reverted
            | DirectiveDeliveryStatus::Expired
            | DirectiveDeliveryStatus::DeadLettered => {
                return Err(format!(
                    "account abstraction delivery failed: {:?}",
                    status.delivery_status
                )
                .into());
            }
            _ => tokio::time::sleep(Duration::from_millis(500)).await,
        }
    }

    Ok(())
}
```

`Finalized` confirms transaction delivery, not that HyperCore has exposed the
new account mode. After finalization, poll Hyperliquid's authoritative Info API
until `userAbstraction` is exactly `unifiedAccount`. Do not submit a
risk-increasing perp order before that read confirms the mode. The typed SDK
deliberately exposes only unified-account mode because the server and Account
contract reject all other `hl_set_abstraction` values.

## Portfolio margin perp orders

Managed perp methods take an existing Account.sol address and the numeric
HyperCore asset ID. Creating or discovering the account and authorizing the API
wallet are separate setup steps. This example assumes the authoritative
Hyperliquid Info API already reports `userAbstraction` as `unifiedAccount`.
Prices and sizes must be positive and exactly representable with 8 decimal
places. The client rejects unsupported precision or overflow instead of
rounding.

```rust,no_run
use hypercall_client::{
    AccountAddress, DirectiveDeliveryStatus, DirectiveStage, HypercallClient, HypercallWallet,
    PerpCancelByCloidParams, PerpLimitOrderParams, PerpTimeInForce, Side,
};
use rust_decimal::Decimal;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let signer = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
    let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;
    let asset = 0; // Numeric HyperCore asset ID supplied by your integration.
    let client_order_id = 42_u128;

    let submitted = client
        .place_perp_limit_order(
            &signer,
            PerpLimitOrderParams {
                account,
                asset,
                side: Side::Buy,
                price: Decimal::new(100_000_125, 3),
                size: Decimal::new(1, 2),
                tif: PerpTimeInForce::Gtc,
                reduce_only: false,
                client_order_id: Some(client_order_id),
                nonce: None,
            },
        )
        .await?;

    match submitted.stage {
        DirectiveStage::Rejected => {
            println!("rejected: {:?}", submitted.rejection);
            return Ok(());
        }
        DirectiveStage::Enqueued | DirectiveStage::Submitted => {}
    }

    let status = loop {
        let status = client
            .get_directive_status(&submitted.directive_id)
            .await?;
        if matches!(
            status.delivery_status,
            DirectiveDeliveryStatus::Included
                | DirectiveDeliveryStatus::CoreRejected
                | DirectiveDeliveryStatus::Finalized
                | DirectiveDeliveryStatus::Reverted
                | DirectiveDeliveryStatus::Expired
                | DirectiveDeliveryStatus::DeadLettered
        ) {
            break status;
        }
        tokio::time::sleep(Duration::from_millis(500)).await;
    };
    println!("delivery status: {:?}", status.delivery_status);

    let portfolio = client.get_portfolio_snapshot(account).await?;
    println!("margin mode: {:?}", portfolio.margin_mode_kind()?);

    let orders = client
        .get_orders_typed(account, Some("open"), Some(50), None)
        .await?;
    for order in &orders.data {
        println!("{}: {:?}", order.symbol, order.instrument_kind()?);
    }

    client
        .cancel_perp_order_by_cloid(
            &signer,
            PerpCancelByCloidParams {
                account,
                asset,
                client_order_id,
                nonce: None,
            },
        )
        .await?;

    Ok(())
}
```

When `client_order_id` is omitted for placement, the selected nonce is used as
the `u128` client order ID. An explicit nonce is available on every managed perp
request for deterministic orchestration. Allocate nonces durably and atomically
per signer. Never reuse a nonce, including when retrying a request. The response
always reports the server's actual stage, directive ID, recovered signer,
rejection, transaction hash, and any fills it knows about.

## TEE perp trading

TEE perp methods submit Hyperliquid perp orders that Hypercall executes through
a TEE-held Hyperliquid API wallet, after Portfolio Margin admission. They are
separate from the managed `/v1/actions/*` directive methods above and use the
same app key (an active Account.sol API wallet) to sign.

An options agent key is not a perps app key. A key approved with
`ApproveAgent` (for example one made with "Create agent key" in account
settings) signs options and REST requests only. Perp trading directives and
TEE perp routes accept only a signer that is an active on-chain API wallet of
the Account (`Account.isApiWalletActive`). The manager signs setup actions such
as `update_api_wallet` but cannot sign trades itself, and cannot be an API
wallet. So the manager must register the key with `update_api_wallet`. Until then every TEE perp
request it signs is rejected with `invalid_signature`, and the message says the
key is an options/REST key that is not enabled for perps. The settings page can
do this step too: tick "Also enable for perps" when creating a key, or press
"Enable for perps" on an existing one.

Setup, once per account, with the manager wallet:

0. `update_api_wallet` registers the app key as an Account.sol API wallet
   (`hc_update_api_wallet`, domain `HypercallManagerSign`). Skip it when the
   app key is already active (`Account.isApiWalletActive`).
1. `enroll_tee_api_wallet` requests the account's TEE API wallet, signs the
   `HLAddApiWallet` approval (domain `HypercallManagerSign`), submits it, and
   waits until Hyperliquid binds the wallet. `HyperliquidInfoClient::is_tee_api_wallet_active`
   reads the enrollment status.
2. `approve_hypercall_builder` sends `Account.approveZeroFeeBuilder(builder)`
   (Account 0.2.4 or newer) with the builder from `/exchange-info`
   `hl_builder_address`. Until then orders come back with
   `errorCode = builder_not_approved` (`requires_builder_approval()`).

Trading, with the app key (domain `HypercallApiSign`):

| Method | Route | Signed type |
| --- | --- | --- |
| `place_tee_perp_limit_order` (GTC, IOC, ALO), `place_tee_perp_market_order` (bounded IOC) | `POST /v1/tee-perp/orders/submit` | `HLOrder` |
| `place_tee_perp_trigger_order` (stop or take-profit, market or limit) | `POST /v1/tee-perp/orders/submit-trigger` | `HCPerpTriggerOrder` |
| `place_tee_perp_bracket_order` (`normalTpsl`), `place_tee_perp_position_tpsl` (`positionTpsl`), `place_tee_perp_scale_order` and `place_tee_perp_order_batch` (`na`) | `POST /v1/tee-perp/orders/submit-batch` | `HCPerpOrderBatch` |
| `cancel_tee_perp_order_by_oid` | `POST /v1/tee-perp/cancels/submit-by-oid` | `HLCancel` |
| `cancel_tee_perp_order_by_cloid` | `POST /v1/tee-perp/cancels/submit` | `HLCancelByCloid` |
| `update_tee_perp_leverage` | `POST /v1/tee-perp/leverage/submit` | `HCPerpLeverageChange` |
| `modify_tee_perp_order` | `POST /v1/tee-perp/orders/modify` | `HCPerpOrderModify` |
| `batch_modify_tee_perp_orders` | `POST /v1/tee-perp/orders/batch-modify` | `HCPerpOrderBatchModify` |
| `place_tee_perp_twap_order` | `POST /v1/tee-perp/twap/submit` | `HCPerpTwapOrder` |
| `cancel_tee_perp_twap` | `POST /v1/tee-perp/twap/cancel` | `HCPerpTwapCancel` |
| `preview_tee_perp_action` | `POST /v1/tee-perp/preview` | none (read-only) |
| `get_tee_perp_markets` | `GET /v1/tee-perp/markets` | none |

Nonces are Unix milliseconds on the API server's clock. The server derives the
venue expiry as `nonce + 9000` and accepts it only within the next 10 seconds,
so a nonce must lie in `(server_now - 9000, server_now + 1000]`. Leave `nonce`
as `None` and the client stamps it from the `/health` `Date` header (cached for
a minute), strictly increasing per signer. Batch child cloids are
`(nonce << 8) + index + 1` (`tee_perp_batch_cloid`).

Every submit returns a stage. `unknown` means the venue may have received the
action: reconcile before acting again, and never resubmit the same intent on
another route. A signed submission that fails in transit (transport error,
timeout, `5xx`, or an undecodable `2xx`) returns
`ClientError::TeeSubmissionAmbiguous` carrying the exact signed body; resend
those bytes with `resend_tee_submission` (the server deduplicates them) or
reconcile. Calling the placing method again signs a new nonce, and new
nonce-derived cloids, so it is a second order. Rejections carry a typed `PerpRejectionReasonCode`, the same
code the preview returns.

```rust,no_run
use hypercall_client::{
    AccountAddress, ApproveHypercallBuilderParams, DirectiveDeliveryStatus, HypercallClient,
    HypercallWallet,
    HyperliquidInfoClient, PerpCancelByCloidParams, PerpLimitOrderParams, PerpPreviewAction,
    PerpPreviewRequest, PerpTimeInForce, Side, TeeApiWalletEnrollmentPolling, TeePerpBracketEntry,
    TeePerpBracketOrderParams, TeePerpExit, TeePerpLeverageParams, TeePerpMarketOrderParams,
    TeePerpTpsl, TeePerpTriggerExecution, TeePerpTriggerOrderParams, UpdateApiWalletParams,
    PERP_MARKET_SLIPPAGE_BPS,
};
use alloy::primitives::keccak256;
use rust_decimal::Decimal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let manager = HypercallWallet::from_private_key("0xYOUR_MANAGER_KEY", 999)?;
    let app_key = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
    let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;
    let hyperliquid = HyperliquidInfoClient::mainnet();

    // One-time setup with the manager wallet. An options agent key is not
    // enough: register the app key as an on-chain API wallet first.
    let registration = client
        .update_api_wallet(
            &manager,
            UpdateApiWalletParams {
                account,
                name: keccak256("tee-perps-app-key"),
                api_wallet: app_key.address,
                nonce: None,
            },
        )
        .await?;
    // Submission only enqueues the directive. Wait until it is final on
    // chain before the app key signs anything.
    loop {
        let status = client
            .get_directive_status(&registration.directive_id)
            .await?;
        match status.delivery_status {
            DirectiveDeliveryStatus::Finalized => break,
            DirectiveDeliveryStatus::CoreRejected
            | DirectiveDeliveryStatus::Reverted
            | DirectiveDeliveryStatus::Expired
            | DirectiveDeliveryStatus::DeadLettered => {
                return Err(format!("API wallet registration failed: {status:?}").into())
            }
            _ => tokio::time::sleep(std::time::Duration::from_secs(1)).await,
        }
    }
    let enrollment = client
        .enroll_tee_api_wallet(
            &manager,
            account,
            &hyperliquid,
            TeeApiWalletEnrollmentPolling::default(),
        )
        .await?;
    println!("TEE API wallet: {}", enrollment.api_wallet);
    client
        .approve_hypercall_builder(
            &manager,
            ApproveHypercallBuilderParams {
                rpc_url: "https://rpc.hyperliquid.xyz/evm".into(),
                account,
                builder: None, // read from /exchange-info hl_builder_address
            },
        )
        .await?;

    // Market metadata and a read-only preview.
    let markets = client.get_tee_perp_markets().await?;
    let btc = markets
        .by_hyperliquid_coin("BTC")
        .ok_or("BTC is not configured")?;
    let preview = client
        .preview_tee_perp_action(&PerpPreviewRequest {
            account: account.into_wallet_address(),
            action: PerpPreviewAction::Leverage {
                asset: btc.asset_id,
                is_cross: true,
                leverage: 5,
            },
        })
        .await?;
    println!(
        "{:?} {:?}, liquidation after {:?}",
        preview.decision, preview.reason_code, preview.liquidation_px_after
    );

    // Leverage, then read the venue's setting back.
    client
        .update_tee_perp_leverage(
            &app_key,
            TeePerpLeverageParams {
                account,
                asset: btc.asset_id,
                is_cross: true,
                leverage: 5,
                nonce: None,
            },
        )
        .await?;
    let venue = hyperliquid
        .active_asset_leverage(account, &btc.hyperliquid_coin)
        .await?;
    println!("venue leverage: {venue:?}");

    // Limit (GTC, IOC, or ALO) and market (bounded IOC) orders.
    let limit = client
        .place_tee_perp_limit_order(
            &app_key,
            PerpLimitOrderParams {
                account,
                asset: btc.asset_id,
                side: Side::Buy,
                price: Decimal::new(95_000, 0),
                size: Decimal::new(1, 3),
                tif: PerpTimeInForce::Alo,
                reduce_only: false,
                client_order_id: Some(42),
                nonce: None,
            },
        )
        .await?;
    if limit.requires_builder_approval() {
        return Err("approve the Hypercall builder first".into());
    }
    client
        .place_tee_perp_market_order(
            &app_key,
            TeePerpMarketOrderParams {
                account,
                asset: btc.asset_id,
                side: Side::Buy,
                size: Decimal::new(1, 3),
                reference_price: Decimal::new(100_000, 0), // best ask
                size_decimals: 5,
                slippage_bps: Some(50),
                reduce_only: false,
                client_order_id: None,
                nonce: None,
            },
        )
        .await?;

    // A stop-market order, and an entry with attached TP/SL.
    client
        .place_tee_perp_trigger_order(
            &app_key,
            TeePerpTriggerOrderParams {
                account,
                asset: btc.asset_id,
                side: Side::Sell,
                size: Decimal::new(1, 3),
                trigger_price: Decimal::new(90_000, 0),
                tpsl: TeePerpTpsl::Sl,
                execution: TeePerpTriggerExecution::Market {
                    size_decimals: 5,
                    slippage_bps: None,
                },
                reduce_only: true,
                client_order_id: None,
                nonce: None,
            },
        )
        .await?;
    let exit = |trigger: i64| {
        TeePerpExit::market(
            Decimal::new(trigger, 0),
            Side::Sell,
            5,
            PERP_MARKET_SLIPPAGE_BPS,
        )
    };
    let bracket = client
        .place_tee_perp_bracket_order(
            &app_key,
            TeePerpBracketOrderParams {
                account,
                entry: TeePerpBracketEntry {
                    asset: btc.asset_id,
                    side: Side::Buy,
                    price: Decimal::new(99_000, 0),
                    size: Decimal::new(1, 3),
                    tif: PerpTimeInForce::Gtc,
                    reduce_only: false,
                },
                take_profit: Some(exit(110_000)?),
                stop_loss: Some(exit(92_000)?),
                nonce: None,
            },
        )
        .await?;
    println!("bracket {:?}: {} children", bracket.stage, bracket.children.len());

    client
        .cancel_tee_perp_order_by_cloid(
            &app_key,
            PerpCancelByCloidParams {
                account,
                asset: btc.asset_id,
                client_order_id: 42,
                nonce: None,
            },
        )
        .await?;
    Ok(())
}
```

Market and market-trigger prices follow the Hypercall order panel: the
reference price moved `slippage_bps` against the trader (default
`PERP_MARKET_SLIPPAGE_BPS`, 2000 bps), rounded to a valid Hyperliquid tick
inside that bound, refused when no such tick still crosses. Scale legs come
from `build_perp_scale_order_legs`. Schedule-cancel and prepared cancels are not
part of the TEE perp SDK surface.

### Modify, batch modify and TWAP

`modify_tee_perp_order` replaces one resting limit order's price, size, side,
TIF and reduce-only flag. `batch_modify_tee_perp_orders` replaces several under
one signature and one venue nonce (Hyperliquid `batchModify`). The engine
admits every child or none, then each child settles on its own, so read
`children` (each with its `index`, `requestId`, `targetDirectiveId`, `cloid`,
`stage`, `message` and `venueOrder`). Whenever Hyperliquid answered every child,
the whole-action stage is definite: `accepted` or `rejected` when every child
agrees, `partial` for a mix. It is `unknown` only when some child is genuinely
unknown (timeout, transport failure, unverifiable venue response).
`children_to_reconcile()` lists the children whose outcome is not definitive; a
stage this SDK does not know parses as `Unrecognized` and is treated as
unknown.

Hyperliquid applies a modify as a cancel of the original order followed by the
placement of the replacement. When the placement fails after the cancel (the
venue's `Error placing new order during modify: ...`, for example an ALO
replacement that would cross), the original order is gone too. The response
then reports `stage: "rejected"` with `venueOrder: {"status": "cancelled",
"reason": "modify rejected; venue cancelled original"}`
(`venue_cancelled_original()`), and the engine treats the order as cancelled:
its reservation is released once a newer position snapshot has absorbed any
fill before the cancel. Any other `rejected` or `not_submitted` outcome leaves
the original order resting.

Modify and batch modify return only after Hyperliquid answered. An accepted
modify's `venueOrder` carries the replacement's NEW `oid` (`resting` or
`filled`); the old `oid` is cancelled. Trust the response over an immediate
`orderStatus` read: Hyperliquid's info endpoint can show the pre-modify order
for a few seconds after the exchange applied the modify.

The client refuses a batch before signing when it is empty, repeats a target or
cloid, or exceeds `max_children` (default 50, the server's default
`api.tee_perp_batch_modify_max_children`; never above 100).

TEE perp orders, modifies and batch modifies carry no Hypercall rate limit;
Hyperliquid's address-based request limit governs them. After Hyperliquid
refuses an Account's order action for that limit, the API answers that
Account's further order actions with `429 venue_rate_limited` and
`Retry-After` for 10 seconds without sending them. Cancels are never held back.

TWAP (`place_tee_perp_twap_order`, `cancel_tee_perp_twap`) runs 5 to 1440
minutes, and the engine reserves the full size until fills arrive. TWAP and
batch modify are gated by the enclave release: a deployment that has not
enabled one answers `400 unsupported_action`, which
`ClientError::is_unsupported_action()` detects. Nothing was reserved or sent to
the venue in that case.

Hyperliquid parity. TWAP submit and cancel carry every `twapOrder` and
`twapCancel` field (`a`, `b`, `s`, `r`, `m`, `t`; `a`, `t`). Modify and batch
modify have these scope cuts:

- Modify replaces limit orders only (GTC, IOC, ALO). Hyperliquid's
  `modify` can also turn an order into a trigger order; the TEE route does not
  accept that, so a resting trigger order must be cancelled and re-placed.
- The order is addressed by its cloid plus its engine `directiveId`, never by
  `oid`, and the modify keeps the cloid. Hyperliquid also accepts an `oid`
  target and a new cloid.
- A batch modify carries at most the instance cap (50 by default, 100 at
  most), where Hyperliquid documents no per-call limit.
- No builder field: Hyperliquid documents none on `modify`, `batchModify`,
  `twapOrder` or `twapCancel`.

```rust,no_run
use hypercall_client::{
    AccountAddress, HypercallClient, HypercallWallet, PerpTimeInForce, Side,
    TeePerpBatchModifyParams, TeePerpModifyParams, TeePerpOrderReplacement, TeePerpOrderStage,
    TeePerpTwapCancelParams, TeePerpTwapParams,
};
use rust_decimal::Decimal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let app_key = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
    let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;

    // `directiveId` and cloid returned when each order was placed.
    let bid = TeePerpOrderReplacement {
        asset: 0,
        target_directive_id: "01999b5a-7c1e-7000-8000-000000000042".parse()?,
        cloid: 42,
        side: Side::Buy,
        price: Decimal::new(99_500, 0),
        size: Decimal::new(2, 3),
        tif: PerpTimeInForce::Alo,
        reduce_only: false,
    };
    let ask = TeePerpOrderReplacement {
        target_directive_id: "01999b5a-7c1e-7000-8000-000000000043".parse()?,
        cloid: 43,
        side: Side::Sell,
        price: Decimal::new(100_500, 0),
        ..bid
    };

    // One order.
    let single = client
        .modify_tee_perp_order(
            &app_key,
            TeePerpModifyParams {
                account,
                replacement: bid,
                nonce: None,
            },
        )
        .await?;
    println!("modify {:?}", single.stage);

    // Both quotes in one signed batchModify; each child settles on its own.
    let batch = client
        .batch_modify_tee_perp_orders(
            &app_key,
            TeePerpBatchModifyParams {
                account,
                replacements: vec![bid, ask],
                max_children: None, // the server default, 50
                nonce: None,
            },
        )
        .await?;
    for child in &batch.children {
        match child.stage {
            TeePerpOrderStage::Accepted => println!("{} moved", child.cloid),
            // Hyperliquid cancelled the original before the replacement failed.
            _ if child.venue_cancelled_original() => {
                println!("{} cancelled: nothing rests any more", child.cloid)
            }
            TeePerpOrderStage::Rejected | TeePerpOrderStage::NotSubmitted => {
                println!("{} unchanged: the old order still rests", child.cloid)
            }
            _ => println!("{} unknown: reconcile before touching it", child.cloid),
        }
    }

    // A native TWAP. Deployments that have not enabled it answer
    // `400 unsupported_action`.
    match client
        .place_tee_perp_twap_order(
            &app_key,
            TeePerpTwapParams {
                account,
                asset: 0,
                side: Side::Sell,
                size: Decimal::new(5, 2),
                reduce_only: true,
                minutes: 30,
                randomize: true,
                nonce: None,
            },
        )
        .await
    {
        Ok(twap) => {
            if let Some(twap_id) = twap.twap_id {
                client
                    .cancel_tee_perp_twap(
                        &app_key,
                        TeePerpTwapCancelParams {
                            account,
                            asset: 0,
                            twap_id,
                            nonce: None,
                        },
                    )
                    .await?;
            }
        }
        Err(error) if error.is_unsupported_action() => println!("TWAP is not enabled here"),
        Err(error) => return Err(error.into()),
    }
    Ok(())
}
```

## Funding

Direct deposits are HyperEVM transactions. Withdrawals are signed Hypercall API
requests. The methods are explicit about margin mode so a caller can never fall
back to the wrong custody route.

The caller supplies the HyperEVM RPC URL and environment-specific USDC address.
The SDK fetches Exchange and Router from `/exchange-info`, verifies the API,
wallet, and RPC chain IDs match, and verifies the current account identity and
margin mode before approving funds.

### Standard Margin

```rust,no_run
use hypercall_client::{
    AccountAddress, DepositUsdcParams, HypercallClient, HypercallWallet,
    WithdrawUsdcParams,
};
use rust_decimal_macros::dec;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let wallet = HypercallWallet::from_private_key("0xYOUR_PRIVATE_KEY", 999)?;
    let usdc: AccountAddress = "0xYOUR_HYPEREVM_USDC_ADDRESS".parse()?;
    let destination: AccountAddress = "0xYOUR_WITHDRAWAL_DESTINATION".parse()?;

    let deposit = client
        .deposit_standard_usdc(
            &wallet,
            DepositUsdcParams {
                rpc_url: "https://YOUR_HYPEREVM_RPC".to_string(),
                usdc_address: usdc,
                account: wallet.address,
                amount: dec!(100),
            },
        )
        .await?;
    println!("deposit transaction: {}", deposit.deposit_transaction_hash);

    let accepted = client
        .withdraw_standard_usdc(
            &wallet,
            WithdrawUsdcParams {
                account: wallet.address,
                destination,
                amount: dec!(10),
                nonce: None,
            },
        )
        .await?;
    println!("withdrawal directive: {}", accepted.directive_id);
    Ok(())
}
```

Standard deposits approve the exact amount only when the existing allowance is
insufficient, then call `Exchange.depositUsdcFor(account, amount)` and wait for
both transactions to confirm successfully.

### Portfolio Margin

Portfolio deposits must be funded by the manager of the canonical Account.sol.
The SDK resolves that identity through the portfolio API, requires Portfolio
Margin and an enabled Router, approves the exact amount only when needed, then
calls `Router.deposit(account, usdc, amount, true)`.

```rust,no_run
use hypercall_client::{
    AccountAddress, DepositUsdcParams, HypercallClient, HypercallWallet,
    WithdrawUsdcParams,
};
use rust_decimal_macros::dec;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let manager = HypercallWallet::from_private_key("0xYOUR_MANAGER_KEY", 999)?;
    let account: AccountAddress = "0xYOUR_ACCOUNT_SOL".parse()?;
    let usdc: AccountAddress = "0xYOUR_HYPEREVM_USDC_ADDRESS".parse()?;
    let destination: AccountAddress = "0xYOUR_WITHDRAWAL_DESTINATION".parse()?;

    client
        .deposit_portfolio_usdc(
            &manager,
            DepositUsdcParams {
                rpc_url: "https://YOUR_HYPEREVM_RPC".to_string(),
                usdc_address: usdc,
                account,
                amount: dec!(100),
            },
        )
        .await?;

    let accepted = client
        .withdraw_portfolio_usdc(
            &manager,
            WithdrawUsdcParams {
                account,
                destination,
                amount: dec!(10.123456),
                nonce: None,
            },
        )
        .await?;
    let status = client.get_directive_status(&accepted.directive_id).await?;
    println!("withdrawal delivery: {:?}", status.delivery_status);
    Ok(())
}
```

Deposit methods return only after the deposit receipt succeeds. A withdrawal
response means Hypercall accepted the request, not that delivery is final.
Poll `get_directive_status` with the returned directive ID. For durable
orchestration, allocate nonces atomically per signer and never use a nonce for a
different request. If a withdrawal uses an SDK-generated nonce and the HTTP
outcome is ambiguous, the error includes the selected nonce and canonical
request fields. Reconcile first. Any retry of that ambiguous request must reuse
the original nonce and every other canonical field exactly.

## Development

Run the focused client checks before changing this crate:

```bash
cargo test -p hypercall-client
cargo test -p hypercall-client --no-default-features
cargo check -p hypercall-client --features kms
cargo test -p hypercall-client --no-default-features --features alloy-rustls
cargo build -p hypercall-client --examples
```

## Feature Flags

| feature | used by | why |
| --- | --- | --- |
| default | client users | Enables the Alloy rustls backend and `rfq-v2`. |
| alloy-rustls | live integrations | Enables rustls for the HTTP client, trusting the bundled webpki-roots set (1.0.10 or later), and Alloy's reqwest rustls backend. |
| alloy-native-tls | native TLS users | Enables native TLS (the platform trust store) for the HTTP client and Alloy's reqwest native TLS backend. |
| kms | AWS/client KMS users | Enables Alloy AWS signer support. |
| rfq-v2 (default) | RFQ V2 takers and makers | RFQ V2 client: `rfq_v2`, `rfq_v2_recovery`, `rfq_v2_signing`, signed read grants, and the private `rfq_v2` WebSocket channel. |

### RFQ V2 (`rfq-v2`)

RFQ V2 is a signed RFQ contract under `/private/rfqs*`: the taker signs the
RFQ, the maker signs the quote, and the taker signs the execution. It adds
anonymous takers, selected-maker routing, indicative quotes with firm-up,
repeated partial fills, atomic sweeps across up to 1,024 quotes, bulk quote
withdrawal, and a maker dead-man switch. V1 RFQ (`/rfq/*` and the `rfq`
channel) is unchanged.

The `rfq-v2` feature is on by default since 0.3.0. It also enables `rfq-v2` on
`hypercall-sdk-types` (the RFQ V2 DTOs and the `AuthenticateRfqV2`,
`RfqV2Authenticated`, and `RfqV2Changed` WebSocket messages) and on
`hypercall-ws-protocol` (the `rfq_v2` channel). With
`default-features = false`, list it explicitly:

```toml
[dependencies]
hypercall-client = { git = "https://github.com/hypercall-public/hypercall-rust", tag = "v0.4.0", default-features = false, features = ["alloy-rustls", "rfq-v2"] }
```

| Module | What it does |
| --- | --- |
| `rfq_v2_signing` | Builds and signs every RFQ V2 action with a `HypercallWallet`, and exposes the exact EIP-712 messages and digests for remote signers. |
| `rfq_v2` | HTTP transport for create, quote, firm-up, decline, execute, sweep, cancel, withdraw, replace, the cancel schedule, and authorized reads. `select_best_rfq_v2_quote` picks the best executable firm quote locally. |
| `rfq_v2_recovery` | Exact-retry reconciliation of ambiguous executions and sweeps, the change-feed cursor, a private mirror, and maker hold tracking. |
| `WsClient::connect_rfq_v2` | The private `rfq_v2` change-hint channel, authenticated with a signed read grant and re-authenticated on reconnect. |

Take a quote as a taker:

```rust,no_run
use alloy::primitives::B256;
use hypercall_client::rfq_v2::{
    encode_rfq_v2_read_grant, select_best_rfq_v2_quote, RfqV2BestQuoteCriteria,
    RfqV2ReadCredentials,
};
use hypercall_client::rfq_v2_signing::RfqV2ActionAuth;
use hypercall_client::{HypercallClient, HypercallWallet};
use hypercall_sdk_types::{
    ListRfqV2QuotesQuery, RfqV2Digest, RfqV2ExecutionMode, RfqV2FeeLimits, RfqV2FillPolicy,
    RfqV2PackageLeg, RfqV2QuotePolicy, RfqV2QuoteView, RfqV2Routing, RfqV2Side,
    RfqV2TakerIdentity, RfqV2Terms,
};
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = HypercallClient::new("https://api.hypercall.xyz");
    let wallet = HypercallWallet::from_private_key("0xYOUR_PRIVATE_KEY", 999)?;
    let taker = wallet.address.into_wallet_address();
    let now = client.server_now_ms().await?;

    let terms = RfqV2Terms {
        id: Uuid::now_v7(),
        taker_account: taker,
        legs: vec![RfqV2PackageLeg {
            instrument: "BTC-20261225-100000-C".into(),
            taker_side: RfqV2Side::Buy,
            quantity_micros_per_unit: 1_000_000, // 1 contract per package
        }],
        target: 1_000_000, // 1 package
        fill_policy: RfqV2FillPolicy::FullSizeOnce,
        execution_mode: RfqV2ExecutionMode::Manual,
        auto_execution_limit: None,
        quote_policy: RfqV2QuotePolicy::FirmOnly,
        taker_identity: RfqV2TakerIdentity::Disclosed,
        routing: RfqV2Routing::AllEligible,
        quote_deadline_ms: now + 20_000,
        expires_at_ms: now + 60_000,
        fee_limits: RfqV2FeeLimits { per_execution: 0, cumulative: 0 },
        builder: None,
        private_terms_blinding: RfqV2Digest(B256::ZERO),
    };
    let created = client
        .create_rfq_v2(&wallet.sign_create_rfq_v2(terms, now).await?)
        .await?;

    // Private reads take a signed read grant as a bearer token.
    let grant = wallet
        .sign_rfq_v2_read_grant(taker, now, now + 300_000, now + 1)
        .await?;
    let token = encode_rfq_v2_read_grant(&grant)?;
    let read = || RfqV2ReadCredentials { bearer_token: &token };

    // Poll until a quote arrives (the full example also handles the deadline).
    let parent = client.get_rfq_v2(created.rfq_id, read()).await?;
    let quotes = client
        .list_rfq_v2_quotes(created.rfq_id, &ListRfqV2QuotesQuery::default(), read())
        .await?;
    let criteria = RfqV2BestQuoteCriteria::for_parent(&parent, parent.remaining_target, now);
    if let Ok(RfqV2QuoteView::Firm { terms, .. }) =
        select_best_rfq_v2_quote(&quotes.quotes, criteria)
    {
        let auth = RfqV2ActionAuth::new(now + 30_000, now + 2);
        let execution = wallet
            .sign_rfq_v2_execution(created.rfq_id, terms.id, 1_000_000, 0, auth)
            .await?;
        let result = client.execute_rfq_v2(created.rfq_id, &execution).await?;
        println!("{} fill(s)", result.executions.len());
    }
    Ok(())
}
```

Runnable versions, with deadline handling, ambiguous-submission
reconciliation, the maker dead-man switch, and quote pricing, are in
[`examples/rfq_v2_taker.rs`](examples/rfq_v2_taker.rs) and
[`examples/rfq_v2_maker.rs`](examples/rfq_v2_maker.rs):

```bash
HYPERCALL_PRIVATE_KEY=0x... RFQ_INSTRUMENT=BTC-20261225-100000-C \
  cargo run -p hypercall-client --example rfq_v2_taker
```

Rules the server enforces:

- **Nonces** are millisecond timestamps within two days before and one day
  after server time, unique per signer. `HypercallClient::server_now_ms` reads
  the server clock.
- **Operation IDs** on execution, sweep, cancel, withdrawal, bulk withdrawal,
  the cancel schedule, and replacement must be UUIDv7s at most one hour old
  and at most 30 seconds in the future by the server clock, or the server
  answers 400 `rfq_v2_invalid_operation_id`. `RfqV2ActionAuth::new` mints a
  fresh one. Reuse an ID only to retry the identical signed request inside
  that window.
- **Selected makers** are signed as a sorted, duplicate-free set. Build them
  with `RfqV2Routing::selected_makers`; the signer refuses an unsorted list.
- **Quote premiums** must equal what the server derives from the leg prices.
  `rfq_v2_signing::rfq_v2_reference_premium` computes it.
- **Ambiguous submissions** (transport error, timeout, 5xx) on a mutation
  return `ClientError::RfqV2SubmissionAmbiguous`. Do not sign a new request.
  Resend the original with `reconcile_rfq_v2_execution` or
  `reconcile_rfq_v2_sweep`, which resend the identical bytes under the
  original operation ID.
- **Availability**: RFQ V2 is open to every account on
  `https://api.hypercall.xyz`. A non-production environment can run it
  disabled, which answers every V2 route with 403 `rfq_v2_disabled`
  (`ClientError::RfqV2Api`), or limited to an allowlist, which answers 403
  `rfq_v2_not_allowlisted` to accounts not on it.

## WebSocket compression

`WsClient` offers RFC 7692 `permessage-deflate` on every connection and
reconnect. When the server accepts, compressed frames are inflated before they
reach `get_messages()` or `wait_for_message()`, so the JSON is identical either
way. When the server does not accept, the session runs uncompressed. An
inflated message larger than 64 MiB, the same limit as an uncompressed one,
fails the connection before more output is allocated. The client sends its own
messages uncompressed, which RFC 7692 allows.

```rust,no_run
use hypercall_client::WsClient;

#[tokio::main]
async fn main() -> hypercall_client::Result<()> {
    // Opt out, for example to rule compression out while debugging.
    let ws = WsClient::new().with_permessage_deflate(false);
    ws.connect("https://api.hypercall.xyz", None).await?;
    Ok(())
}
```

TLS uses the platform trust store through `native-tls`, for both compressed
and uncompressed sessions.