@metaflux-dex/client 0.0.3 → 0.0.5

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.
Files changed (137) hide show
  1. package/README.md +196 -16
  2. package/dist/client.d.ts +173 -3
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +264 -11
  5. package/dist/client.js.map +1 -1
  6. package/dist/faucet.js +1 -1
  7. package/dist/faucet.js.map +1 -1
  8. package/dist/index.d.ts +8 -7
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +14 -9
  11. package/dist/index.js.map +1 -1
  12. package/dist/native/actions.d.ts +46 -0
  13. package/dist/native/actions.d.ts.map +1 -0
  14. package/dist/native/actions.js +593 -0
  15. package/dist/native/actions.js.map +1 -0
  16. package/dist/native/digest.d.ts +28 -0
  17. package/dist/native/digest.d.ts.map +1 -0
  18. package/dist/{native.js → native/digest.js} +71 -86
  19. package/dist/native/digest.js.map +1 -0
  20. package/dist/native/index.d.ts +3 -0
  21. package/dist/native/index.d.ts.map +1 -0
  22. package/dist/native/index.js +5 -0
  23. package/dist/native/index.js.map +1 -0
  24. package/dist/rest/http.d.ts.map +1 -0
  25. package/dist/rest/http.js.map +1 -0
  26. package/dist/{info.d.ts → rest/info.d.ts} +1 -1
  27. package/dist/rest/info.d.ts.map +1 -0
  28. package/dist/{info.js → rest/info.js} +8 -5
  29. package/dist/rest/info.js.map +1 -0
  30. package/dist/types/account.d.ts +52 -0
  31. package/dist/types/account.d.ts.map +1 -0
  32. package/dist/types/account.js +8 -0
  33. package/dist/types/account.js.map +1 -0
  34. package/dist/types/encrypted.d.ts +8 -0
  35. package/dist/types/encrypted.d.ts.map +1 -0
  36. package/dist/types/encrypted.js +7 -0
  37. package/dist/types/encrypted.js.map +1 -0
  38. package/dist/types/governance.d.ts +11 -0
  39. package/dist/types/governance.d.ts.map +1 -0
  40. package/dist/types/governance.js +7 -0
  41. package/dist/types/governance.js.map +1 -0
  42. package/dist/types/index.d.ts +10 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types/index.js +6 -0
  45. package/dist/types/index.js.map +1 -0
  46. package/dist/types/info/core.d.ts +97 -0
  47. package/dist/types/info/core.d.ts.map +1 -0
  48. package/dist/types/info/core.js +15 -0
  49. package/dist/types/info/core.js.map +1 -0
  50. package/dist/{info-types.d.ts → types/info/hl-parity.d.ts} +8 -177
  51. package/dist/types/info/hl-parity.d.ts.map +1 -0
  52. package/dist/types/info/hl-parity.js +8 -0
  53. package/dist/types/info/hl-parity.js.map +1 -0
  54. package/dist/types/info/index.d.ts +4 -0
  55. package/dist/types/info/index.d.ts.map +1 -0
  56. package/dist/types/info/index.js +6 -0
  57. package/dist/types/info/index.js.map +1 -0
  58. package/dist/types/info/reads.d.ts +81 -0
  59. package/dist/types/info/reads.d.ts.map +1 -0
  60. package/dist/types/info/reads.js +7 -0
  61. package/dist/types/info/reads.js.map +1 -0
  62. package/dist/types/meta-bridge.d.ts +8 -0
  63. package/dist/types/meta-bridge.d.ts.map +1 -0
  64. package/dist/types/meta-bridge.js +7 -0
  65. package/dist/types/meta-bridge.js.map +1 -0
  66. package/dist/types/spot.d.ts +41 -0
  67. package/dist/types/spot.d.ts.map +1 -0
  68. package/dist/types/spot.js +7 -0
  69. package/dist/types/spot.js.map +1 -0
  70. package/dist/types/staking.d.ts +12 -0
  71. package/dist/types/staking.d.ts.map +1 -0
  72. package/dist/types/staking.js +6 -0
  73. package/dist/types/staking.js.map +1 -0
  74. package/dist/{types.d.ts → types/trading.d.ts} +33 -1
  75. package/dist/types/trading.d.ts.map +1 -0
  76. package/dist/{types.js → types/trading.js} +3 -3
  77. package/dist/types/trading.js.map +1 -0
  78. package/dist/types/twap.d.ts +13 -0
  79. package/dist/types/twap.d.ts.map +1 -0
  80. package/dist/types/twap.js +7 -0
  81. package/dist/types/twap.js.map +1 -0
  82. package/dist/types/vault.d.ts +24 -0
  83. package/dist/types/vault.d.ts.map +1 -0
  84. package/dist/types/vault.js +6 -0
  85. package/dist/types/vault.js.map +1 -0
  86. package/dist/{wasm.d.ts → wallet/wasm.d.ts} +1 -1
  87. package/dist/wallet/wasm.d.ts.map +1 -0
  88. package/dist/{wasm.js → wallet/wasm.js} +9 -8
  89. package/dist/wallet/wasm.js.map +1 -0
  90. package/dist/{ws.d.ts → ws/ws.d.ts} +1 -1
  91. package/dist/ws/ws.d.ts.map +1 -0
  92. package/dist/{ws.js → ws/ws.js} +11 -10
  93. package/dist/ws/ws.js.map +1 -0
  94. package/package.json +3 -1
  95. package/src/client.ts +610 -12
  96. package/src/faucet.ts +1 -1
  97. package/src/index.ts +115 -15
  98. package/src/native/actions.ts +820 -0
  99. package/src/{native.ts → native/digest.ts} +78 -95
  100. package/src/native/index.ts +5 -0
  101. package/src/{http.ts → rest/http.ts} +1 -1
  102. package/src/{info.ts → rest/info.ts} +9 -6
  103. package/src/types/account.ts +111 -0
  104. package/src/types/encrypted.ts +21 -0
  105. package/src/types/governance.ts +27 -0
  106. package/src/types/index.ts +79 -0
  107. package/src/types/info/core.ts +214 -0
  108. package/src/types/info/hl-parity.ts +428 -0
  109. package/src/types/info/index.ts +78 -0
  110. package/src/types/info/reads.ts +165 -0
  111. package/src/types/meta-bridge.ts +22 -0
  112. package/src/types/spot.ts +114 -0
  113. package/src/types/staking.ts +27 -0
  114. package/src/{types.ts → types/trading.ts} +92 -8
  115. package/src/types/twap.ts +29 -0
  116. package/src/types/vault.ts +55 -0
  117. package/src/{wasm.ts → wallet/wasm.ts} +10 -9
  118. package/src/{ws.ts → ws/ws.ts} +16 -14
  119. package/dist/http.d.ts.map +0 -1
  120. package/dist/http.js.map +0 -1
  121. package/dist/info-types.d.ts.map +0 -1
  122. package/dist/info-types.js +0 -16
  123. package/dist/info-types.js.map +0 -1
  124. package/dist/info.d.ts.map +0 -1
  125. package/dist/info.js.map +0 -1
  126. package/dist/native.d.ts +0 -12
  127. package/dist/native.d.ts.map +0 -1
  128. package/dist/native.js.map +0 -1
  129. package/dist/types.d.ts.map +0 -1
  130. package/dist/types.js.map +0 -1
  131. package/dist/wasm.d.ts.map +0 -1
  132. package/dist/wasm.js.map +0 -1
  133. package/dist/ws.d.ts.map +0 -1
  134. package/dist/ws.js.map +0 -1
  135. package/src/info-types.ts +0 -783
  136. /package/dist/{http.d.ts → rest/http.d.ts} +0 -0
  137. /package/dist/{http.js → rest/http.js} +0 -0
package/src/client.ts CHANGED
@@ -15,28 +15,114 @@ import {
15
15
  keccak256,
16
16
  recoverPubkey,
17
17
  signSecp256k1,
18
- } from './wasm.js';
19
- import { httpRequest } from './http.js';
18
+ } from './wallet/wasm.js';
19
+ import { httpRequest } from './rest/http.js';
20
20
  import {
21
+ buildNativeAgentSetAbstractionAction,
22
+ buildNativeApproveAgentAction,
23
+ buildNativeApproveBuilderFeeAction,
24
+ buildNativeBatchCancelAction,
25
+ buildNativeBatchModifyAction,
26
+ buildNativeBatchOrderAction,
21
27
  buildNativeCancelAction,
28
+ buildNativeCancelAllOrdersAction,
29
+ buildNativeCancelByCloidAction,
30
+ buildNativeClaimRewardsAction,
31
+ buildNativeConvertToMultiSigUserAction,
32
+ buildNativeCreateVaultAction,
33
+ buildNativeEarnDepositAction,
34
+ buildNativeEarnWithdrawAction,
35
+ buildNativeLinkStakingUserAction,
36
+ buildNativeMbWithdrawAction,
37
+ buildNativeModifyAction,
22
38
  buildNativeOrderAction,
39
+ buildNativePriorityBidAction,
40
+ buildNativeRegisterMetaliquidityOperatorAction,
41
+ buildNativeScheduleCancelAction,
42
+ buildNativeSetDisplayNameAction,
43
+ buildNativeSetMetaliquidityWhitelistAction,
44
+ buildNativeSetPositionModeAction,
45
+ buildNativeSetReferrerAction,
46
+ buildNativeSpotCancelAction,
47
+ buildNativeSpotMarginCloseAction,
48
+ buildNativeSpotMarginDepositAction,
49
+ buildNativeSpotMarginOpenAction,
50
+ buildNativeSpotMarginWithdrawAction,
51
+ buildNativeSpotOrderAction,
52
+ buildNativeSubmitEncryptedOrderAction,
53
+ buildNativeTokenDelegateAction,
54
+ buildNativeTopUpIsolatedOnlyMarginAction,
55
+ buildNativeTwapCancelAction,
56
+ buildNativeTwapOrderAction,
57
+ buildNativeUpdateIsolatedMarginAction,
58
+ buildNativeUpdateLeverageAction,
59
+ buildNativeUserDexAbstractionAction,
60
+ buildNativeUserPortfolioMarginAction,
61
+ buildNativeUserSetAbstractionAction,
62
+ buildNativeVaultModifyAction,
63
+ buildNativeVaultTransferAction,
64
+ buildNativeVaultWithdrawAction,
65
+ } from './native/actions.js';
66
+ import {
23
67
  nativeRequestBody,
24
68
  nextNonce,
25
69
  recoverNativeSigner,
26
70
  signNativeAction,
27
- } from './native.js';
28
- import { InfoApi } from './info.js';
29
- import { WsClient, type WsConfig } from './ws.js';
71
+ } from './native/digest.js';
72
+ import { InfoApi } from './rest/info.js';
73
+ import { WsClient, type WsConfig } from './ws/ws.js';
30
74
  import type {
75
+ AgentSetAbstraction,
76
+ ApproveAgent,
77
+ ApproveBuilderFee,
78
+ BatchCancel,
79
+ BatchModify,
80
+ BatchOrder,
81
+ CancelAllOrders,
82
+ CancelByCloid,
83
+ ClaimRewards,
84
+ ConvertToMultiSigUser,
85
+ CreateVault,
86
+ LinkStakingUser,
31
87
  Market,
88
+ MbWithdraw,
89
+ Modify,
32
90
  NativeCancel,
91
+ NativeEarnDeposit,
92
+ NativeEarnWithdraw,
33
93
  NativeExchangeAck,
34
94
  NativeOrder,
95
+ NativeSetPositionMode,
96
+ NativeSpotCancel,
97
+ NativeSpotMarginClose,
98
+ NativeSpotMarginDeposit,
99
+ NativeSpotMarginOpen,
100
+ NativeSpotMarginWithdraw,
101
+ NativeSpotOrder,
35
102
  Order,
36
103
  OrderAck,
37
104
  Position,
105
+ PriorityBid,
106
+ RegisterMetaliquidityOperator,
107
+ ScheduleCancel,
108
+ SetDisplayName,
109
+ SetMetaliquidityWhitelist,
110
+ SetReferrer,
38
111
  SignedOrder,
39
- } from './types.js';
112
+ SubmitEncryptedOrder,
113
+ TokenDelegate,
114
+ TopUpIsolatedOnlyMargin,
115
+ TwapCancel,
116
+ TwapOrder,
117
+ UpdateIsolatedMargin,
118
+ UpdateLeverage,
119
+ UserDexAbstraction,
120
+ UserPortfolioMargin,
121
+ UserSetAbstraction,
122
+ VaultModify,
123
+ VaultTransfer,
124
+ VaultWithdraw,
125
+ } from './types/index.js';
40
126
 
41
127
  /// Options accepted by the `Client` constructor.
42
128
  export interface ClientOpts {
@@ -53,10 +139,9 @@ export interface ClientOpts {
53
139
  chainId?: number;
54
140
  }
55
141
 
56
- /// Default chain ID for the EIP-712 domain. The MetaFlux mainnet ID is
57
- /// reserved-but-not-yet-assigned (PLAN.md §M.1 "TBD pre-mainnet, avoid
58
- /// 999 which is HL"); the devnet default `31337` matches what the
59
- /// existing core-state tests use.
142
+ /// Default chain ID for the EIP-712 domain. The devnet default `31337`
143
+ /// matches the node's devnet configuration; production deployments
144
+ /// override via `chainId`.
60
145
  const DEFAULT_CHAIN_ID = 31337;
61
146
 
62
147
  /// `keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")`
@@ -353,12 +438,525 @@ export class Client {
353
438
  });
354
439
  }
355
440
 
441
+ /// Toggle one-way / hedge position mode via `POST /exchange`.
442
+ ///
443
+ /// `setPositionMode({ hedge: true })` switches the account to hedge / two-way
444
+ /// mode; `{ hedge: false }` switches back to one-way / net. Same signed-action
445
+ /// envelope as the order paths, but SENDER-AUTHORIZED: the recovered signer IS
446
+ /// the account, so there is no `owner` to cross-check. The node only permits
447
+ /// the switch while the account is flat on every market (else it 4xxs).
448
+ ///
449
+ /// After switching to hedge mode, perp orders MUST carry `position_side`
450
+ /// (`"long"` / `"short"`); after switching back to one-way they MUST omit it.
451
+ async setPositionMode(
452
+ mode: NativeSetPositionMode,
453
+ opts: { nonce?: bigint; chainId?: number } = {},
454
+ ): Promise<NativeExchangeAck> {
455
+ return this.postSenderAuthorized(
456
+ buildNativeSetPositionModeAction(mode),
457
+ opts,
458
+ );
459
+ }
460
+
461
+ /// Submit an SE-0 spot CLOB order via `POST /exchange`.
462
+ ///
463
+ /// v0 is IOC-limit only: `tif` defaults to `"ioc"` and `limit_px` must be
464
+ /// `> 0` (the builder + node both enforce it). Sender-authorized: the signer
465
+ /// is the trader, so there is no `owner` field and no local owner check.
466
+ async submitSpotOrderNative(
467
+ order: NativeSpotOrder,
468
+ opts: { nonce?: bigint; chainId?: number } = {},
469
+ ): Promise<NativeExchangeAck> {
470
+ return this.postSenderAuthorized(buildNativeSpotOrderAction(order), opts);
471
+ }
472
+
473
+ /// Cancel a resting SE-0 spot order via `POST /exchange`.
474
+ ///
475
+ /// Cancels by `(pair, oid)`; the node cancels spot orders by `oid`. Sender-
476
+ /// authorized, same envelope as the other native actions.
477
+ async cancelSpotOrderNative(
478
+ cancel: NativeSpotCancel,
479
+ opts: { nonce?: bigint; chainId?: number } = {},
480
+ ): Promise<NativeExchangeAck> {
481
+ return this.postSenderAuthorized(buildNativeSpotCancelAction(cancel), opts);
482
+ }
483
+
484
+ // ── spot margin & Earn actions (devnet preview) ───────────────────────────
485
+ //
486
+ // Leveraged spot borrows quote from the Earn lending pool. All SENDER-
487
+ // AUTHORIZED (the signer is the actor). Each returns the 202 admission ack,
488
+ // NOT a synchronous oid; observe committed state via `/info` `spot_margin_state`
489
+ // / `earn_state`. Preview: forced-liquidation settlement is not yet wired and
490
+ // per-pair maintenance ratios are still being calibrated.
491
+
492
+ /// Post quote collateral into a spot-margin account via `POST /exchange`.
493
+ /// Margin must be enabled for the pair (else the node rejects).
494
+ async spotMarginDeposit(
495
+ params: NativeSpotMarginDeposit,
496
+ opts: { nonce?: bigint; chainId?: number } = {},
497
+ ): Promise<NativeExchangeAck> {
498
+ return this.postSenderAuthorized(
499
+ buildNativeSpotMarginDepositAction(params),
500
+ opts,
501
+ );
502
+ }
503
+
504
+ /// Withdraw free collateral from a spot-margin account via `POST /exchange`.
505
+ /// Full while flat; initial-margin-gated while a position is open.
506
+ async spotMarginWithdraw(
507
+ params: NativeSpotMarginWithdraw,
508
+ opts: { nonce?: bigint; chainId?: number } = {},
509
+ ): Promise<NativeExchangeAck> {
510
+ return this.postSenderAuthorized(
511
+ buildNativeSpotMarginWithdrawAction(params),
512
+ opts,
513
+ );
514
+ }
515
+
516
+ /// Open a leveraged spot position via `POST /exchange`: borrow quote from the
517
+ /// pair's Earn pool and IOC-buy base. Gated by the initial-margin requirement.
518
+ async spotMarginOpen(
519
+ params: NativeSpotMarginOpen,
520
+ opts: { nonce?: bigint; chainId?: number } = {},
521
+ ): Promise<NativeExchangeAck> {
522
+ return this.postSenderAuthorized(
523
+ buildNativeSpotMarginOpenAction(params),
524
+ opts,
525
+ );
526
+ }
527
+
528
+ /// Close a leveraged spot position via `POST /exchange`: IOC-sell the held
529
+ /// base, repay principal + interest, return the remainder (partial keeps open).
530
+ async spotMarginClose(
531
+ params: NativeSpotMarginClose,
532
+ opts: { nonce?: bigint; chainId?: number } = {},
533
+ ): Promise<NativeExchangeAck> {
534
+ return this.postSenderAuthorized(
535
+ buildNativeSpotMarginCloseAction(params),
536
+ opts,
537
+ );
538
+ }
539
+
540
+ /// Supply quote into an Earn lending pool for shares via `POST /exchange`.
541
+ /// 1:1 on a fresh pool, else priced off NAV; the pool auto-creates.
542
+ async earnDeposit(
543
+ params: NativeEarnDeposit,
544
+ opts: { nonce?: bigint; chainId?: number } = {},
545
+ ): Promise<NativeExchangeAck> {
546
+ return this.postSenderAuthorized(buildNativeEarnDepositAction(params), opts);
547
+ }
548
+
549
+ /// Redeem Earn pool shares back to quote via `POST /exchange`. The payout is
550
+ /// clamped to the pool's idle liquidity (`supplied − borrowed`).
551
+ async earnWithdraw(
552
+ params: NativeEarnWithdraw,
553
+ opts: { nonce?: bigint; chainId?: number } = {},
554
+ ): Promise<NativeExchangeAck> {
555
+ return this.postSenderAuthorized(
556
+ buildNativeEarnWithdrawAction(params),
557
+ opts,
558
+ );
559
+ }
560
+
561
+ // ── order management ──────────────────────────────
562
+
563
+ /// Cancel a resting order by its client order id via `POST /exchange`.
564
+ async cancelByCloid(
565
+ params: CancelByCloid,
566
+ opts: { nonce?: bigint; chainId?: number } = {},
567
+ ): Promise<NativeExchangeAck> {
568
+ return this.postSenderAuthorized(buildNativeCancelByCloidAction(params), opts);
569
+ }
570
+
571
+ /// Amend a resting order's price and/or size in place via `POST /exchange`.
572
+ async modify(
573
+ params: Modify,
574
+ opts: { nonce?: bigint; chainId?: number } = {},
575
+ ): Promise<NativeExchangeAck> {
576
+ return this.postSenderAuthorized(buildNativeModifyAction(params), opts);
577
+ }
578
+
579
+ /// Apply N modifications under one signature via `POST /exchange`.
580
+ async batchModify(
581
+ params: BatchModify,
582
+ opts: { nonce?: bigint; chainId?: number } = {},
583
+ ): Promise<NativeExchangeAck> {
584
+ return this.postSenderAuthorized(buildNativeBatchModifyAction(params), opts);
585
+ }
586
+
587
+ /// Place N orders under one signature via `POST /exchange`. Each order's
588
+ /// `owner` must equal the signing wallet; the batch returns the admission
589
+ /// envelope (not per-order statuses).
590
+ async batchOrder(
591
+ batch: BatchOrder,
592
+ opts: { nonce?: bigint; chainId?: number } = {},
593
+ ): Promise<NativeExchangeAck> {
594
+ return this.postBatchOwnerChecked(
595
+ buildNativeBatchOrderAction(batch),
596
+ batch.orders.map((o) => o.owner),
597
+ opts,
598
+ );
599
+ }
600
+
601
+ /// Apply N cancels under one signature via `POST /exchange`. Each cancel's
602
+ /// `owner` must equal the signing wallet.
603
+ async batchCancel(
604
+ batch: BatchCancel,
605
+ opts: { nonce?: bigint; chainId?: number } = {},
606
+ ): Promise<NativeExchangeAck> {
607
+ return this.postBatchOwnerChecked(
608
+ buildNativeBatchCancelAction(batch),
609
+ batch.cancels.map((c) => c.owner),
610
+ opts,
611
+ );
612
+ }
613
+
614
+ /// Schedule a cancel-all of the sender's open orders at a future block.
615
+ async scheduleCancel(
616
+ params: ScheduleCancel,
617
+ opts: { nonce?: bigint; chainId?: number } = {},
618
+ ): Promise<NativeExchangeAck> {
619
+ return this.postSenderAuthorized(buildNativeScheduleCancelAction(params), opts);
620
+ }
621
+
622
+ /// Cancel all of the sender's open orders (optionally one asset) via
623
+ /// `POST /exchange`.
624
+ async cancelAllOrders(
625
+ params: CancelAllOrders = {},
626
+ opts: { nonce?: bigint; chainId?: number } = {},
627
+ ): Promise<NativeExchangeAck> {
628
+ return this.postSenderAuthorized(buildNativeCancelAllOrdersAction(params), opts);
629
+ }
630
+
631
+ // ── TWAP ──────────────────────────────────────
632
+
633
+ /// Submit a sliced (TWAP) order via `POST /exchange`.
634
+ async twapOrder(
635
+ params: TwapOrder,
636
+ opts: { nonce?: bigint; chainId?: number } = {},
637
+ ): Promise<NativeExchangeAck> {
638
+ return this.postSenderAuthorized(buildNativeTwapOrderAction(params), opts);
639
+ }
640
+
641
+ /// Cancel a running TWAP parent by id via `POST /exchange`.
642
+ async twapCancel(
643
+ params: TwapCancel,
644
+ opts: { nonce?: bigint; chainId?: number } = {},
645
+ ): Promise<NativeExchangeAck> {
646
+ return this.postSenderAuthorized(buildNativeTwapCancelAction(params), opts);
647
+ }
648
+
649
+ // ── leverage & margin ──────────────────────────
650
+
651
+ /// Set per-asset leverage (and optionally flip to isolated) via `POST /exchange`.
652
+ async updateLeverage(
653
+ params: UpdateLeverage,
654
+ opts: { nonce?: bigint; chainId?: number } = {},
655
+ ): Promise<NativeExchangeAck> {
656
+ return this.postSenderAuthorized(buildNativeUpdateLeverageAction(params), opts);
657
+ }
658
+
659
+ /// Add or remove isolated margin on an open position via `POST /exchange`.
660
+ async updateIsolatedMargin(
661
+ params: UpdateIsolatedMargin,
662
+ opts: { nonce?: bigint; chainId?: number } = {},
663
+ ): Promise<NativeExchangeAck> {
664
+ return this.postSenderAuthorized(
665
+ buildNativeUpdateIsolatedMarginAction(params),
666
+ opts,
667
+ );
668
+ }
669
+
670
+ /// Top up the margin of a strict-isolated-only position via `POST /exchange`.
671
+ async topUpIsolatedOnlyMargin(
672
+ params: TopUpIsolatedOnlyMargin,
673
+ opts: { nonce?: bigint; chainId?: number } = {},
674
+ ): Promise<NativeExchangeAck> {
675
+ return this.postSenderAuthorized(
676
+ buildNativeTopUpIsolatedOnlyMarginAction(params),
677
+ opts,
678
+ );
679
+ }
680
+
681
+ /// Enroll into or out of portfolio margin via `POST /exchange`.
682
+ async userPortfolioMargin(
683
+ params: UserPortfolioMargin,
684
+ opts: { nonce?: bigint; chainId?: number } = {},
685
+ ): Promise<NativeExchangeAck> {
686
+ return this.postSenderAuthorized(
687
+ buildNativeUserPortfolioMarginAction(params),
688
+ opts,
689
+ );
690
+ }
691
+
692
+ // ── account & agent settings ───────────────────
693
+
694
+ /// Set the account display name via `POST /exchange`.
695
+ async setDisplayName(
696
+ params: SetDisplayName,
697
+ opts: { nonce?: bigint; chainId?: number } = {},
698
+ ): Promise<NativeExchangeAck> {
699
+ return this.postSenderAuthorized(buildNativeSetDisplayNameAction(params), opts);
700
+ }
701
+
702
+ /// Set the account referrer (one-time) via `POST /exchange`.
703
+ async setReferrer(
704
+ params: SetReferrer,
705
+ opts: { nonce?: bigint; chainId?: number } = {},
706
+ ): Promise<NativeExchangeAck> {
707
+ return this.postSenderAuthorized(buildNativeSetReferrerAction(params), opts);
708
+ }
709
+
710
+ /// Approve an agent wallet to sign on this account's behalf via `POST /exchange`.
711
+ async approveAgent(
712
+ params: ApproveAgent,
713
+ opts: { nonce?: bigint; chainId?: number } = {},
714
+ ): Promise<NativeExchangeAck> {
715
+ return this.postSenderAuthorized(buildNativeApproveAgentAction(params), opts);
716
+ }
717
+
718
+ /// Approve a builder fee ceiling (`max_bps`; `0` revokes) via `POST /exchange`.
719
+ async approveBuilderFee(
720
+ params: ApproveBuilderFee,
721
+ opts: { nonce?: bigint; chainId?: number } = {},
722
+ ): Promise<NativeExchangeAck> {
723
+ return this.postSenderAuthorized(
724
+ buildNativeApproveBuilderFeeAction(params),
725
+ opts,
726
+ );
727
+ }
728
+
729
+ /// Convert the account to an M-of-N multisig via `POST /exchange`.
730
+ async convertToMultiSigUser(
731
+ params: ConvertToMultiSigUser,
732
+ opts: { nonce?: bigint; chainId?: number } = {},
733
+ ): Promise<NativeExchangeAck> {
734
+ return this.postSenderAuthorized(
735
+ buildNativeConvertToMultiSigUserAction(params),
736
+ opts,
737
+ );
738
+ }
739
+
740
+ /// Toggle the account's DEX-abstraction opt-in flag via `POST /exchange`.
741
+ async userDexAbstraction(
742
+ params: UserDexAbstraction,
743
+ opts: { nonce?: bigint; chainId?: number } = {},
744
+ ): Promise<NativeExchangeAck> {
745
+ return this.postSenderAuthorized(
746
+ buildNativeUserDexAbstractionAction(params),
747
+ opts,
748
+ );
749
+ }
750
+
751
+ /// Set a self-scoped abstraction config value via `POST /exchange`.
752
+ async userSetAbstraction(
753
+ params: UserSetAbstraction,
754
+ opts: { nonce?: bigint; chainId?: number } = {},
755
+ ): Promise<NativeExchangeAck> {
756
+ return this.postSenderAuthorized(
757
+ buildNativeUserSetAbstractionAction(params),
758
+ opts,
759
+ );
760
+ }
761
+
762
+ /// As an approved agent, set an abstraction config value for `params.user`
763
+ /// via `POST /exchange`.
764
+ async agentSetAbstraction(
765
+ params: AgentSetAbstraction,
766
+ opts: { nonce?: bigint; chainId?: number } = {},
767
+ ): Promise<NativeExchangeAck> {
768
+ return this.postSenderAuthorized(
769
+ buildNativeAgentSetAbstractionAction(params),
770
+ opts,
771
+ );
772
+ }
773
+
774
+ /// Pay a priority fee (bps) for block-front placement via `POST /exchange`.
775
+ async priorityBid(
776
+ params: PriorityBid,
777
+ opts: { nonce?: bigint; chainId?: number } = {},
778
+ ): Promise<NativeExchangeAck> {
779
+ return this.postSenderAuthorized(buildNativePriorityBidAction(params), opts);
780
+ }
781
+
782
+ // ── staking ──────────────────────────────────
783
+
784
+ /// Delegate stake to a validator, or queue an undelegation, via `POST /exchange`.
785
+ async tokenDelegate(
786
+ params: TokenDelegate,
787
+ opts: { nonce?: bigint; chainId?: number } = {},
788
+ ): Promise<NativeExchangeAck> {
789
+ return this.postSenderAuthorized(buildNativeTokenDelegateAction(params), opts);
790
+ }
791
+
792
+ /// Claim accrued staking rewards via `POST /exchange`.
793
+ async claimRewards(
794
+ params: ClaimRewards = {},
795
+ opts: { nonce?: bigint; chainId?: number } = {},
796
+ ): Promise<NativeExchangeAck> {
797
+ return this.postSenderAuthorized(buildNativeClaimRewardsAction(params), opts);
798
+ }
799
+
800
+ /// Alias another account as this account's staking target via `POST /exchange`.
801
+ async linkStakingUser(
802
+ params: LinkStakingUser,
803
+ opts: { nonce?: bigint; chainId?: number } = {},
804
+ ): Promise<NativeExchangeAck> {
805
+ return this.postSenderAuthorized(buildNativeLinkStakingUserAction(params), opts);
806
+ }
807
+
808
+ // ── encrypted orders ───────────────────────────
809
+
810
+ /// Submit a threshold-encrypted order ciphertext via `POST /exchange`.
811
+ async submitEncryptedOrder(
812
+ params: SubmitEncryptedOrder,
813
+ opts: { nonce?: bigint; chainId?: number } = {},
814
+ ): Promise<NativeExchangeAck> {
815
+ return this.postSenderAuthorized(
816
+ buildNativeSubmitEncryptedOrderAction(params),
817
+ opts,
818
+ );
819
+ }
820
+
821
+ // ── vaults ───────────────────────────────────
822
+
823
+ /// Create a new vault via `POST /exchange`. The signing wallet becomes the
824
+ /// leader. SENDER-AUTHORIZED.
825
+ async createVault(
826
+ params: CreateVault,
827
+ opts: { nonce?: bigint; chainId?: number } = {},
828
+ ): Promise<NativeExchangeAck> {
829
+ return this.postSenderAuthorized(buildNativeCreateVaultAction(params), opts);
830
+ }
831
+
832
+ /// Leader moves capital into / out of a vault via `POST /exchange`.
833
+ async vaultTransfer(
834
+ params: VaultTransfer,
835
+ opts: { nonce?: bigint; chainId?: number } = {},
836
+ ): Promise<NativeExchangeAck> {
837
+ return this.postSenderAuthorized(buildNativeVaultTransferAction(params), opts);
838
+ }
839
+
840
+ /// Leader updates vault configuration via `POST /exchange`.
841
+ async vaultModify(
842
+ params: VaultModify,
843
+ opts: { nonce?: bigint; chainId?: number } = {},
844
+ ): Promise<NativeExchangeAck> {
845
+ return this.postSenderAuthorized(buildNativeVaultModifyAction(params), opts);
846
+ }
847
+
848
+ /// Follower redeems shares from a vault via `POST /exchange`. SENDER-AUTHORIZED.
849
+ async vaultWithdraw(
850
+ params: VaultWithdraw,
851
+ opts: { nonce?: bigint; chainId?: number } = {},
852
+ ): Promise<NativeExchangeAck> {
853
+ return this.postSenderAuthorized(buildNativeVaultWithdrawAction(params), opts);
854
+ }
855
+
856
+ // ── MetaBridge ───────────────────────────────
857
+
858
+ /// Withdraw cross-collateral to a destination chain via `POST /exchange`.
859
+ async mbWithdraw(
860
+ params: MbWithdraw,
861
+ opts: { nonce?: bigint; chainId?: number } = {},
862
+ ): Promise<NativeExchangeAck> {
863
+ return this.postSenderAuthorized(buildNativeMbWithdrawAction(params), opts);
864
+ }
865
+
866
+ // ── governance / operator ─────────────────────
867
+
868
+ /// Set a metaliquidity-provider whitelist membership (validator-authorized)
869
+ /// via `POST /exchange`.
870
+ async setMetaliquidityWhitelist(
871
+ params: SetMetaliquidityWhitelist,
872
+ opts: { nonce?: bigint; chainId?: number } = {},
873
+ ): Promise<NativeExchangeAck> {
874
+ return this.postSenderAuthorized(
875
+ buildNativeSetMetaliquidityWhitelistAction(params),
876
+ opts,
877
+ );
878
+ }
879
+
880
+ /// Register or revoke an external strategy operator for a vault
881
+ /// (vault-leader-authorized) via `POST /exchange`.
882
+ async registerMetaliquidityOperator(
883
+ params: RegisterMetaliquidityOperator,
884
+ opts: { nonce?: bigint; chainId?: number } = {},
885
+ ): Promise<NativeExchangeAck> {
886
+ return this.postSenderAuthorized(
887
+ buildNativeRegisterMetaliquidityOperatorAction(params),
888
+ opts,
889
+ );
890
+ }
891
+
892
+ /// Sign a pre-built sender-authorized action JSON and POST it to `/exchange`.
893
+ ///
894
+ /// Shared by the actions where the recovered signer IS the actor (no `owner`
895
+ /// to cross-check): `set_position_mode`, `spot_order`, `spot_cancel`. Mirrors
896
+ /// `submitOrderNative`'s flow minus the owner-vs-signer guard.
897
+ private async postSenderAuthorized(
898
+ actionJson: string,
899
+ opts: { nonce?: bigint; chainId?: number },
900
+ ): Promise<NativeExchangeAck> {
901
+ if (this.privateKey === undefined) {
902
+ throw new Error(
903
+ 'this action requires a privateKey in ClientOpts (this Client is read-only)',
904
+ );
905
+ }
906
+ const nonce = opts.nonce ?? nextNonce();
907
+ const signed = await signNativeAction(
908
+ this.privateKey,
909
+ actionJson,
910
+ nonce,
911
+ opts.chainId,
912
+ );
913
+ return httpRequest<NativeExchangeAck>(this.baseUrl, '/exchange', {
914
+ method: 'POST',
915
+ rawJson: nativeRequestBody(signed),
916
+ bearer: this.jwt,
917
+ });
918
+ }
919
+
920
+ /// Sign a pre-built batch action whose inner orders / cancels each carry an
921
+ /// `owner`, and POST it to `/exchange`. Recovers the signer once and rejects
922
+ /// unless EVERY inner `owner` equals it (the node sets the sender to the
923
+ /// recovered signer for all of them).
924
+ private async postBatchOwnerChecked(
925
+ actionJson: string,
926
+ owners: string[],
927
+ opts: { nonce?: bigint; chainId?: number },
928
+ ): Promise<NativeExchangeAck> {
929
+ if (this.privateKey === undefined) {
930
+ throw new Error(
931
+ 'this action requires a privateKey in ClientOpts (this Client is read-only)',
932
+ );
933
+ }
934
+ const nonce = opts.nonce ?? nextNonce();
935
+ const signed = await signNativeAction(
936
+ this.privateKey,
937
+ actionJson,
938
+ nonce,
939
+ opts.chainId,
940
+ );
941
+ const signer = await recoverNativeSigner(signed, opts.chainId);
942
+ for (const owner of owners) {
943
+ if (signer.toLowerCase() !== owner.toLowerCase()) {
944
+ throw new Error(`batch item owner ${owner} != recovered signer ${signer}`);
945
+ }
946
+ }
947
+ return httpRequest<NativeExchangeAck>(this.baseUrl, '/exchange', {
948
+ method: 'POST',
949
+ rawJson: nativeRequestBody(signed),
950
+ bearer: this.jwt,
951
+ });
952
+ }
953
+
356
954
  /// Open an MTF-native WebSocket connection to `<baseUrl>/ws`.
357
955
  ///
358
956
  /// Derives the `ws(s)://` URL from the client's `http(s)://` base, mounts the
359
957
  /// `/ws` path (the node's upgrade route), and returns a connected
360
958
  /// [`WsClient`]. Register handlers via `ws.onMessage` and subscribe with
361
- /// `ws.subscribe({ type: 'l2Book', coin: 'BTC' })`.
959
+ /// `ws.subscribe({ type: 'l2_book', coin: 'BTC' })`.
362
960
  async connectWs(config: Partial<WsConfig> = {}): Promise<WsClient> {
363
961
  const ws = new WsClient(httpToWsUrl(this.baseUrl), config);
364
962
  await ws.connect();
@@ -401,7 +999,7 @@ export class Client {
401
999
  /// Encode a `Uint8Array` as a base64url string (no padding).
402
1000
  ///
403
1001
  /// Matches the encoding the gateway's `LoginEnvelope.signature` field
404
- /// expects (see `metaflux/crates/api-gateway/src/ccxt/auth.rs`). We
1002
+ /// expects. We
405
1003
  /// avoid Buffer/global polyfills here because the SDK targets both
406
1004
  /// node and the browser; manual base64 is small and avoids the
407
1005
  /// dependency.
package/src/faucet.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  // synchronously. Devnet / testnet only — mainnet refuses (surfaced as a
12
12
  // `MetaFluxApiError`).
13
13
 
14
- import { httpRequest } from './http.js';
14
+ import { httpRequest } from './rest/http.js';
15
15
 
16
16
  /// Successful faucet response (200). `status` is `"queued"` — the credit is
17
17
  /// staged for the next block, so the balance updates after ~1 block rather