@nktkas/hyperliquid 0.15.3 → 0.16.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.
Files changed (54) hide show
  1. package/README.md +11 -2
  2. package/esm/src/base.d.ts +1 -0
  3. package/esm/src/base.d.ts.map +1 -1
  4. package/esm/src/clients/event.d.ts +2 -0
  5. package/esm/src/clients/event.d.ts.map +1 -1
  6. package/esm/src/clients/event.js +2 -0
  7. package/esm/src/clients/public.d.ts +6 -6
  8. package/esm/src/clients/public.d.ts.map +1 -1
  9. package/esm/src/clients/public.js +237 -54
  10. package/esm/src/clients/wallet.d.ts +57 -6
  11. package/esm/src/clients/wallet.d.ts.map +1 -1
  12. package/esm/src/clients/wallet.js +225 -53
  13. package/esm/src/signing.d.ts +14 -4
  14. package/esm/src/signing.d.ts.map +1 -1
  15. package/esm/src/signing.js +5 -5
  16. package/esm/src/transports/http/http_transport.d.ts +25 -8
  17. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  18. package/esm/src/transports/http/http_transport.js +12 -6
  19. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +1 -3
  20. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  21. package/esm/src/transports/websocket/websocket_transport.d.ts +1 -1
  22. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  23. package/esm/src/transports/websocket/websocket_transport.js +1 -5
  24. package/esm/src/types/exchange/requests.d.ts +120 -0
  25. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  26. package/esm/src/types/info/requests.d.ts +36 -18
  27. package/esm/src/types/info/requests.d.ts.map +1 -1
  28. package/package.json +1 -1
  29. package/script/src/base.d.ts +1 -0
  30. package/script/src/base.d.ts.map +1 -1
  31. package/script/src/clients/event.d.ts +2 -0
  32. package/script/src/clients/event.d.ts.map +1 -1
  33. package/script/src/clients/event.js +2 -0
  34. package/script/src/clients/public.d.ts +6 -6
  35. package/script/src/clients/public.d.ts.map +1 -1
  36. package/script/src/clients/public.js +237 -54
  37. package/script/src/clients/wallet.d.ts +57 -6
  38. package/script/src/clients/wallet.d.ts.map +1 -1
  39. package/script/src/clients/wallet.js +224 -52
  40. package/script/src/signing.d.ts +14 -4
  41. package/script/src/signing.d.ts.map +1 -1
  42. package/script/src/signing.js +5 -0
  43. package/script/src/transports/http/http_transport.d.ts +25 -8
  44. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  45. package/script/src/transports/http/http_transport.js +12 -6
  46. package/script/src/transports/websocket/_reconnecting_websocket.d.ts +1 -3
  47. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  48. package/script/src/transports/websocket/websocket_transport.d.ts +1 -1
  49. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  50. package/script/src/transports/websocket/websocket_transport.js +1 -5
  51. package/script/src/types/exchange/requests.d.ts +120 -0
  52. package/script/src/types/exchange/requests.d.ts.map +1 -1
  53. package/script/src/types/info/requests.d.ts +36 -18
  54. package/script/src/types/info/requests.d.ts.map +1 -1
@@ -1,5 +1,5 @@
1
1
  import { HyperliquidError } from "../base.js";
2
- import { signL1Action, signUserSignedAction, } from "../signing.js";
2
+ import { isAbstractEthersSigner, isAbstractEthersV5Signer, isAbstractViemWalletClient, isAbstractWindowEthereum, signL1Action, signUserSignedAction, } from "../signing.js";
3
3
  // ——————————————— Errors ———————————————
4
4
  /** Error thrown when the API returns an error response. */
5
5
  export class ApiRequestError extends HyperliquidError {
@@ -46,6 +46,17 @@ export class ApiRequestError extends HyperliquidError {
46
46
  * @typeParam W The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
47
47
  */
48
48
  export class WalletClient {
49
+ /** Gets the next nonce for signing transactions. */
50
+ get _nonce() {
51
+ let nonce = Date.now();
52
+ if (nonce <= this._lastNonce) {
53
+ nonce = ++this._lastNonce;
54
+ }
55
+ else {
56
+ this._lastNonce = nonce;
57
+ }
58
+ return nonce;
59
+ }
49
60
  /**
50
61
  * Initialises a new instance.
51
62
  * @param args - The parameters for the client.
@@ -131,13 +142,43 @@ export class WalletClient {
131
142
  writable: true,
132
143
  value: void 0
133
144
  });
145
+ /** The last nonce used for signing transactions. */
146
+ Object.defineProperty(this, "_lastNonce", {
147
+ enumerable: true,
148
+ configurable: true,
149
+ writable: true,
150
+ value: 0
151
+ });
134
152
  this.transport = args.transport;
135
153
  this.wallet = args.wallet;
136
154
  this.isTestnet = args.isTestnet ?? false;
137
155
  this.defaultVaultAddress = args.defaultVaultAddress;
138
- this.signatureChainId = args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1");
156
+ this.signatureChainId = args.signatureChainId ?? (async () => {
157
+ // Trying to get chain id of the wallet
158
+ if (isAbstractViemWalletClient(this.wallet)) {
159
+ if ("getChainId" in this.wallet && typeof this.wallet.getChainId === "function") {
160
+ const chainId = await this.wallet.getChainId();
161
+ return `0x${chainId.toString(16)}`;
162
+ }
163
+ }
164
+ else if (isAbstractEthersSigner(this.wallet) || isAbstractEthersV5Signer(this.wallet)) {
165
+ if ("provider" in this.wallet &&
166
+ typeof this.wallet.provider === "object" && this.wallet.provider !== null &&
167
+ "getNetwork" in this.wallet.provider &&
168
+ typeof this.wallet.provider.getNetwork === "function") {
169
+ const network = await this.wallet.provider.getNetwork();
170
+ return `0x${network.chainId.toString(16)}`;
171
+ }
172
+ }
173
+ else if (isAbstractWindowEthereum(this.wallet)) {
174
+ const [chainId] = await this.wallet.request({ method: "eth_chainId", params: [] });
175
+ return chainId;
176
+ }
177
+ // Trying to guess chain id based on isTestnet
178
+ return this.isTestnet ? "0x66eee" : "0xa4b1";
179
+ });
139
180
  }
140
- // ———————————————Actions———————————————
181
+ // ——————————————— Exchange API ———————————————
141
182
  /**
142
183
  * Approve an agent to sign on behalf of the master or sub-accounts.
143
184
  * @param args - The parameters for the request.
@@ -167,8 +208,10 @@ export class WalletClient {
167
208
  ...args,
168
209
  type: "approveAgent",
169
210
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
170
- signatureChainId: this.signatureChainId,
171
- nonce: args.nonce ?? Date.now(),
211
+ signatureChainId: typeof this.signatureChainId === "string"
212
+ ? this.signatureChainId
213
+ : await this.signatureChainId(),
214
+ nonce: args.nonce ?? this._nonce,
172
215
  };
173
216
  // Sign the action
174
217
  const signature = await signUserSignedAction({
@@ -220,8 +263,10 @@ export class WalletClient {
220
263
  ...args,
221
264
  type: "approveBuilderFee",
222
265
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
223
- signatureChainId: this.signatureChainId,
224
- nonce: args.nonce ?? Date.now(),
266
+ signatureChainId: typeof this.signatureChainId === "string"
267
+ ? this.signatureChainId
268
+ : await this.signatureChainId(),
269
+ nonce: args.nonce ?? this._nonce,
225
270
  };
226
271
  // Sign the action
227
272
  const signature = await signUserSignedAction({
@@ -283,7 +328,7 @@ export class WalletClient {
283
328
  */
284
329
  async batchModify(args, signal) {
285
330
  // Destructure the parameters
286
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
331
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
287
332
  // Construct an action
288
333
  const action = {
289
334
  type: "batchModify",
@@ -293,8 +338,8 @@ export class WalletClient {
293
338
  order: {
294
339
  a: modify.order.a,
295
340
  b: modify.order.b,
296
- p: modify.order.p,
297
- s: modify.order.s,
341
+ p: this._formatDecimal(modify.order.p),
342
+ s: this._formatDecimal(modify.order.s),
298
343
  r: modify.order.r,
299
344
  t: "limit" in modify.order.t
300
345
  ? {
@@ -305,7 +350,7 @@ export class WalletClient {
305
350
  : {
306
351
  trigger: {
307
352
  isMarket: modify.order.t.trigger.isMarket,
308
- triggerPx: modify.order.t.trigger.triggerPx,
353
+ triggerPx: this._formatDecimal(modify.order.t.trigger.triggerPx),
309
354
  tpsl: modify.order.t.trigger.tpsl,
310
355
  },
311
356
  },
@@ -359,7 +404,7 @@ export class WalletClient {
359
404
  */
360
405
  async cancel(args, signal) {
361
406
  // Destructure the parameters
362
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
407
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
363
408
  // Construct an action
364
409
  const action = {
365
410
  type: "cancel",
@@ -409,8 +454,10 @@ export class WalletClient {
409
454
  ...args,
410
455
  type: "cDeposit",
411
456
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
412
- signatureChainId: this.signatureChainId,
413
- nonce: args.nonce ?? Date.now(),
457
+ signatureChainId: typeof this.signatureChainId === "string"
458
+ ? this.signatureChainId
459
+ : await this.signatureChainId(),
460
+ nonce: args.nonce ?? this._nonce,
414
461
  };
415
462
  // Sign the action
416
463
  const signature = await signUserSignedAction({
@@ -454,7 +501,7 @@ export class WalletClient {
454
501
  */
455
502
  async claimRewards(args = {}, signal) {
456
503
  // Destructure the parameters
457
- const { nonce = Date.now() } = args;
504
+ const { nonce = this._nonce, } = args;
458
505
  // Construct an action
459
506
  const sortedAction = { type: "claimRewards" };
460
507
  // Sign the action
@@ -498,7 +545,7 @@ export class WalletClient {
498
545
  */
499
546
  async cancelByCloid(args, signal) {
500
547
  // Destructure the parameters
501
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
548
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
502
549
  // Construct an action
503
550
  const action = {
504
551
  type: "cancelByCloid",
@@ -548,8 +595,10 @@ export class WalletClient {
548
595
  ...args,
549
596
  type: "cWithdraw",
550
597
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
551
- signatureChainId: this.signatureChainId,
552
- nonce: args.nonce ?? Date.now(),
598
+ signatureChainId: typeof this.signatureChainId === "string"
599
+ ? this.signatureChainId
600
+ : await this.signatureChainId(),
601
+ nonce: args.nonce ?? this._nonce,
553
602
  };
554
603
  // Sign the action
555
604
  const signature = await signUserSignedAction({
@@ -593,7 +642,7 @@ export class WalletClient {
593
642
  */
594
643
  async evmUserModify(args, signal) {
595
644
  // Destructure the parameters
596
- const { nonce = Date.now(), ...actionArgs } = args;
645
+ const { nonce = this._nonce, ...actionArgs } = args;
597
646
  // Construct an action
598
647
  const action = {
599
648
  type: "evmUserModify",
@@ -635,7 +684,7 @@ export class WalletClient {
635
684
  */
636
685
  async createSubAccount(args, signal) {
637
686
  // Destructure the parameters
638
- const { nonce = Date.now(), ...actionArgs } = args;
687
+ const { nonce = this._nonce, ...actionArgs } = args;
639
688
  // Construct an action
640
689
  const action = {
641
690
  type: "createSubAccount",
@@ -681,7 +730,7 @@ export class WalletClient {
681
730
  */
682
731
  async createVault(args, signal) {
683
732
  // Destructure the parameters
684
- const { nonce = Date.now(), ...actionArgs } = args;
733
+ const { nonce = this._nonce, ...actionArgs } = args;
685
734
  // Construct an action
686
735
  const action = {
687
736
  type: "createVault",
@@ -741,7 +790,7 @@ export class WalletClient {
741
790
  */
742
791
  async modify(args, signal) {
743
792
  // Destructure the parameters
744
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
793
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
745
794
  // Construct an action
746
795
  const action = {
747
796
  type: "modify",
@@ -749,8 +798,8 @@ export class WalletClient {
749
798
  order: {
750
799
  a: actionArgs.order.a,
751
800
  b: actionArgs.order.b,
752
- p: actionArgs.order.p,
753
- s: actionArgs.order.s,
801
+ p: this._formatDecimal(actionArgs.order.p),
802
+ s: this._formatDecimal(actionArgs.order.s),
754
803
  r: actionArgs.order.r,
755
804
  t: "limit" in actionArgs.order.t
756
805
  ? {
@@ -761,7 +810,7 @@ export class WalletClient {
761
810
  : {
762
811
  trigger: {
763
812
  isMarket: actionArgs.order.t.trigger.isMarket,
764
- triggerPx: actionArgs.order.t.trigger.triggerPx,
813
+ triggerPx: this._formatDecimal(actionArgs.order.t.trigger.triggerPx),
765
814
  tpsl: actionArgs.order.t.trigger.tpsl,
766
815
  },
767
816
  },
@@ -822,7 +871,7 @@ export class WalletClient {
822
871
  */
823
872
  async order(args, signal) {
824
873
  // Destructure the parameters
825
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
874
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
826
875
  // Construct an action
827
876
  const action = {
828
877
  type: "order",
@@ -830,8 +879,8 @@ export class WalletClient {
830
879
  const sortedOrder = {
831
880
  a: order.a,
832
881
  b: order.b,
833
- p: order.p,
834
- s: order.s,
882
+ p: this._formatDecimal(order.p),
883
+ s: this._formatDecimal(order.s),
835
884
  r: order.r,
836
885
  t: "limit" in order.t
837
886
  ? {
@@ -842,7 +891,7 @@ export class WalletClient {
842
891
  : {
843
892
  trigger: {
844
893
  isMarket: order.t.trigger.isMarket,
845
- triggerPx: order.t.trigger.triggerPx,
894
+ triggerPx: this._formatDecimal(order.t.trigger.triggerPx),
846
895
  tpsl: order.t.trigger.tpsl,
847
896
  },
848
897
  },
@@ -899,7 +948,7 @@ export class WalletClient {
899
948
  */
900
949
  async scheduleCancel(args = {}, signal) {
901
950
  // Destructure the parameters
902
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
951
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
903
952
  // Construct an action
904
953
  const action = {
905
954
  type: "scheduleCancel",
@@ -944,7 +993,7 @@ export class WalletClient {
944
993
  */
945
994
  async setDisplayName(args, signal) {
946
995
  // Destructure the parameters
947
- const { nonce = Date.now(), ...actionArgs } = args;
996
+ const { nonce = this._nonce, ...actionArgs } = args;
948
997
  // Construct an action
949
998
  const action = {
950
999
  type: "setDisplayName",
@@ -986,7 +1035,7 @@ export class WalletClient {
986
1035
  */
987
1036
  async setReferrer(args, signal) {
988
1037
  // Destructure the parameters
989
- const { nonce = Date.now(), ...actionArgs } = args;
1038
+ const { nonce = this._nonce, ...actionArgs } = args;
990
1039
  // Construct an action
991
1040
  const action = {
992
1041
  type: "setReferrer",
@@ -1006,6 +1055,110 @@ export class WalletClient {
1006
1055
  this._validateResponse(response);
1007
1056
  return response;
1008
1057
  }
1058
+ /**
1059
+ * Deploying HIP-1 and HIP-2 assets.
1060
+ * @param args - The parameters for the request.
1061
+ * @param signal - An optional abort signal.
1062
+ * @returns Successful response without specific data.
1063
+ * @throws {ApiRequestError} When the API returns an error response.
1064
+ * @untested
1065
+ *
1066
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-1-and-hip-2-assets
1067
+ * @example
1068
+ * ```ts
1069
+ * import * as hl from "@nktkas/hyperliquid";
1070
+ * import { privateKeyToAccount } from "viem/accounts";
1071
+ *
1072
+ * const wallet = privateKeyToAccount("0x...");
1073
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1074
+ * const client = new hl.WalletClient({ wallet, transport });
1075
+ *
1076
+ * // Unknown what the successful response will be
1077
+ * const result = await client.spotDeploy({
1078
+ * registerToken2: {
1079
+ * spec: {
1080
+ * name: "TestToken",
1081
+ * szDecimals: 8,
1082
+ * weiDecimals: 8,
1083
+ * },
1084
+ * maxGas: 1000000,
1085
+ * fullName: "TestToken (TT)"
1086
+ * }
1087
+ * });
1088
+ * ```
1089
+ */
1090
+ async spotDeploy(args, signal) {
1091
+ // Destructure the parameters
1092
+ const { nonce = this._nonce, ...actionArgs } = args;
1093
+ // Construct an action
1094
+ let action;
1095
+ if ("registerToken2" in actionArgs) {
1096
+ action = {
1097
+ type: "spotDeploy",
1098
+ registerToken2: {
1099
+ spec: {
1100
+ name: actionArgs.registerToken2.spec.name,
1101
+ szDecimals: actionArgs.registerToken2.spec.szDecimals,
1102
+ weiDecimals: actionArgs.registerToken2.spec.weiDecimals,
1103
+ },
1104
+ maxGas: actionArgs.registerToken2.maxGas,
1105
+ fullName: actionArgs.registerToken2.fullName,
1106
+ },
1107
+ };
1108
+ }
1109
+ else if ("userGenesis" in actionArgs) {
1110
+ action = {
1111
+ type: "spotDeploy",
1112
+ userGenesis: {
1113
+ token: actionArgs.userGenesis.token,
1114
+ userAndWei: actionArgs.userGenesis.userAndWei,
1115
+ existingTokenAndWei: actionArgs.userGenesis.existingTokenAndWei,
1116
+ },
1117
+ };
1118
+ }
1119
+ else if ("genesis" in actionArgs) {
1120
+ action = {
1121
+ type: "spotDeploy",
1122
+ genesis: {
1123
+ token: actionArgs.genesis.token,
1124
+ maxSupply: actionArgs.genesis.maxSupply,
1125
+ },
1126
+ };
1127
+ }
1128
+ else if ("registerSpot" in actionArgs) {
1129
+ action = {
1130
+ type: "spotDeploy",
1131
+ registerSpot: {
1132
+ tokens: actionArgs.registerSpot.tokens,
1133
+ },
1134
+ };
1135
+ }
1136
+ else {
1137
+ action = {
1138
+ type: "spotDeploy",
1139
+ registerHyperliquidity: {
1140
+ spot: actionArgs.registerHyperliquidity.spot,
1141
+ startPx: actionArgs.registerHyperliquidity.startPx,
1142
+ orderSz: actionArgs.registerHyperliquidity.orderSz,
1143
+ nOrders: actionArgs.registerHyperliquidity.nOrders,
1144
+ nSeededLevels: actionArgs.registerHyperliquidity.nSeededLevels,
1145
+ },
1146
+ };
1147
+ }
1148
+ // Sign the action
1149
+ const signature = await signL1Action({
1150
+ wallet: this.wallet,
1151
+ action,
1152
+ nonce,
1153
+ isTestnet: this.isTestnet,
1154
+ });
1155
+ // Send a request
1156
+ const request = { action, signature, nonce };
1157
+ const response = await this.transport.request("exchange", request, signal);
1158
+ // Validate a response
1159
+ this._validateResponse(response);
1160
+ return response;
1161
+ }
1009
1162
  /**
1010
1163
  * Transfer a spot asset on L1 to another address.
1011
1164
  * @param args - The parameters for the request.
@@ -1036,8 +1189,10 @@ export class WalletClient {
1036
1189
  ...args,
1037
1190
  type: "spotSend",
1038
1191
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1039
- signatureChainId: this.signatureChainId,
1040
- time: args.time ?? Date.now(),
1192
+ signatureChainId: typeof this.signatureChainId === "string"
1193
+ ? this.signatureChainId
1194
+ : await this.signatureChainId(),
1195
+ time: args.time ?? this._nonce,
1041
1196
  };
1042
1197
  // Sign the action
1043
1198
  const signature = await signUserSignedAction({
@@ -1085,7 +1240,7 @@ export class WalletClient {
1085
1240
  */
1086
1241
  async spotUser(args, signal) {
1087
1242
  // Destructure the parameters
1088
- const { nonce = Date.now(), ...actionArgs } = args;
1243
+ const { nonce = this._nonce, ...actionArgs } = args;
1089
1244
  // Construct an action
1090
1245
  const action = {
1091
1246
  type: "spotUser",
@@ -1134,7 +1289,7 @@ export class WalletClient {
1134
1289
  */
1135
1290
  async subAccountSpotTransfer(args, signal) {
1136
1291
  // Destructure the parameters
1137
- const { nonce = Date.now(), ...actionArgs } = args;
1292
+ const { nonce = this._nonce, ...actionArgs } = args;
1138
1293
  // Construct an action
1139
1294
  const action = {
1140
1295
  type: "subAccountSpotTransfer",
@@ -1183,7 +1338,7 @@ export class WalletClient {
1183
1338
  */
1184
1339
  async subAccountTransfer(args, signal) {
1185
1340
  // Destructure the parameters
1186
- const { nonce = Date.now(), ...actionArgs } = args;
1341
+ const { nonce = this._nonce, ...actionArgs } = args;
1187
1342
  // Construct an action
1188
1343
  const action = {
1189
1344
  type: "subAccountTransfer",
@@ -1235,8 +1390,10 @@ export class WalletClient {
1235
1390
  ...args,
1236
1391
  type: "tokenDelegate",
1237
1392
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1238
- signatureChainId: this.signatureChainId,
1239
- nonce: args.nonce ?? Date.now(),
1393
+ signatureChainId: typeof this.signatureChainId === "string"
1394
+ ? this.signatureChainId
1395
+ : await this.signatureChainId(),
1396
+ nonce: args.nonce ?? this._nonce,
1240
1397
  };
1241
1398
  // Sign the action
1242
1399
  const signature = await signUserSignedAction({
@@ -1285,7 +1442,7 @@ export class WalletClient {
1285
1442
  */
1286
1443
  async twapCancel(args, signal) {
1287
1444
  // Destructure the parameters
1288
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1445
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1289
1446
  // Construct an action
1290
1447
  const action = {
1291
1448
  type: "twapCancel",
@@ -1336,14 +1493,14 @@ export class WalletClient {
1336
1493
  */
1337
1494
  async twapOrder(args, signal) {
1338
1495
  // Destructure the parameters
1339
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1496
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1340
1497
  // Construct an action
1341
1498
  const action = {
1342
1499
  type: "twapOrder",
1343
1500
  twap: {
1344
1501
  a: actionArgs.a,
1345
1502
  b: actionArgs.b,
1346
- s: actionArgs.s,
1503
+ s: this._formatDecimal(actionArgs.s),
1347
1504
  r: actionArgs.r,
1348
1505
  m: actionArgs.m,
1349
1506
  t: actionArgs.t,
@@ -1390,7 +1547,7 @@ export class WalletClient {
1390
1547
  */
1391
1548
  async updateIsolatedMargin(args, signal) {
1392
1549
  // Destructure the parameters
1393
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1550
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1394
1551
  // Construct an action
1395
1552
  const action = {
1396
1553
  type: "updateIsolatedMargin",
@@ -1439,7 +1596,7 @@ export class WalletClient {
1439
1596
  */
1440
1597
  async updateLeverage(args, signal) {
1441
1598
  // Destructure the parameters
1442
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1599
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1443
1600
  // Construct an action
1444
1601
  const action = {
1445
1602
  type: "updateLeverage",
@@ -1491,8 +1648,10 @@ export class WalletClient {
1491
1648
  ...args,
1492
1649
  type: "usdClassTransfer",
1493
1650
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1494
- signatureChainId: this.signatureChainId,
1495
- nonce: args.nonce ?? Date.now(),
1651
+ signatureChainId: typeof this.signatureChainId === "string"
1652
+ ? this.signatureChainId
1653
+ : await this.signatureChainId(),
1654
+ nonce: args.nonce ?? this._nonce,
1496
1655
  };
1497
1656
  // Sign the action
1498
1657
  const signature = await signUserSignedAction({
@@ -1544,8 +1703,10 @@ export class WalletClient {
1544
1703
  ...args,
1545
1704
  type: "usdSend",
1546
1705
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1547
- signatureChainId: this.signatureChainId,
1548
- time: args.time ?? Date.now(),
1706
+ signatureChainId: typeof this.signatureChainId === "string"
1707
+ ? this.signatureChainId
1708
+ : await this.signatureChainId(),
1709
+ time: args.time ?? this._nonce,
1549
1710
  };
1550
1711
  // Sign the action
1551
1712
  const signature = await signUserSignedAction({
@@ -1593,7 +1754,7 @@ export class WalletClient {
1593
1754
  */
1594
1755
  async vaultDistribute(args, signal) {
1595
1756
  // Destructure the parameters
1596
- const { nonce = Date.now(), ...actionArgs } = args;
1757
+ const { nonce = this._nonce, ...actionArgs } = args;
1597
1758
  // Construct an action
1598
1759
  const action = {
1599
1760
  type: "vaultDistribute",
@@ -1640,7 +1801,7 @@ export class WalletClient {
1640
1801
  */
1641
1802
  async vaultModify(args, signal) {
1642
1803
  // Destructure the parameters
1643
- const { nonce = Date.now(), ...actionArgs } = args;
1804
+ const { nonce = this._nonce, ...actionArgs } = args;
1644
1805
  // Construct an action
1645
1806
  const action = {
1646
1807
  type: "vaultModify",
@@ -1688,7 +1849,7 @@ export class WalletClient {
1688
1849
  */
1689
1850
  async vaultTransfer(args, signal) {
1690
1851
  // Destructure the parameters
1691
- const { nonce = Date.now(), ...actionArgs } = args;
1852
+ const { nonce = this._nonce, ...actionArgs } = args;
1692
1853
  // Construct an action
1693
1854
  const action = {
1694
1855
  type: "vaultTransfer",
@@ -1739,8 +1900,10 @@ export class WalletClient {
1739
1900
  ...args,
1740
1901
  type: "withdraw3",
1741
1902
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1742
- signatureChainId: this.signatureChainId,
1743
- time: args.time ?? Date.now(),
1903
+ signatureChainId: typeof this.signatureChainId === "string"
1904
+ ? this.signatureChainId
1905
+ : await this.signatureChainId(),
1906
+ time: args.time ?? this._nonce,
1744
1907
  };
1745
1908
  // Sign the action
1746
1909
  const signature = await signUserSignedAction({
@@ -1763,6 +1926,15 @@ export class WalletClient {
1763
1926
  this._validateResponse(response);
1764
1927
  return response;
1765
1928
  }
1929
+ // ——————————————— Private methods ———————————————
1930
+ /** Formats a decimal number as a string, removing trailing zeros. */
1931
+ _formatDecimal(numStr) {
1932
+ if (!numStr.includes("."))
1933
+ return numStr;
1934
+ const [intPart, fracPart] = numStr.split(".");
1935
+ const newFrac = fracPart.replace(/0+$/, "");
1936
+ return newFrac ? `${intPart}.${newFrac}` : intPart;
1937
+ }
1766
1938
  /** Validate a response from the API. */
1767
1939
  _validateResponse(response) {
1768
1940
  if (response.status === "err") {
@@ -70,10 +70,10 @@ export interface AbstractEthersV5Signer {
70
70
  }
71
71
  /** Abstract interface for a [window.ethereum](https://eips.ethereum.org/EIPS/eip-1193) object. */
72
72
  export interface AbstractWindowEthereum {
73
- request: (args: {
74
- method: string;
75
- params: unknown[];
76
- }) => Promise<unknown>;
73
+ request(args: {
74
+ method: any;
75
+ params: any;
76
+ }): Promise<any>;
77
77
  }
78
78
  /**
79
79
  * Create a hash of the L1 action.
@@ -132,4 +132,14 @@ export declare function signUserSignedAction(args: {
132
132
  s: Hex;
133
133
  v: number;
134
134
  }>;
135
+ /** Checks if the given value is an abstract viem wallet. */
136
+ export declare function isAbstractViemWalletClient(client: unknown): client is AbstractViemWalletClient;
137
+ /** Checks if the given value is an abstract extended viem wallet (e.g. privy `useSignTypedData`). */
138
+ export declare function isAbstractExtendedViemWalletClient(client: unknown): client is AbstractViemWalletClient;
139
+ /** Checks if the given value is an abstract ethers signer. */
140
+ export declare function isAbstractEthersSigner(client: unknown): client is AbstractEthersSigner;
141
+ /** Checks if the given value is an abstract ethers v5 signer. */
142
+ export declare function isAbstractEthersV5Signer(client: unknown): client is AbstractEthersV5Signer;
143
+ /** Checks if the given value is an abstract `window.ethereum` object. */
144
+ export declare function isAbstractWindowEthereum(client: unknown): client is AbstractWindowEthereum;
135
145
  //# sourceMappingURL=signing.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/src/signing.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,6CAA6C,CAAC;AAEpG,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAErC,YAAY,EAAE,GAAG,EAAE,CAAC;AACpB,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;AAEpC,mFAAmF;AACnF,MAAM,WAAW,wBAAwB;IACrC,aAAa,CAAC,MAAM,EAAE;QAClB,MAAM,EAAE;YACJ,IAAI,EAAE,MAAM,CAAC;YACb,OAAO,EAAE,MAAM,CAAC;YAChB,OAAO,EAAE,MAAM,CAAC;YAChB,iBAAiB,EAAE,GAAG,CAAC;SAC1B,CAAC;QACF,KAAK,EAAE;YACH,CAAC,GAAG,EAAE,MAAM,GAAG;gBACX,IAAI,EAAE,MAAM,CAAC;gBACb,IAAI,EAAE,MAAM,CAAC;aAChB,EAAE,CAAC;SACP,CAAC;QACF,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACpC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;CACpB;AAED,gNAAgN;AAChN,MAAM,WAAW,gCAAgC;IAC7C,aAAa,CACT,MAAM,EAAE;QACJ,MAAM,EAAE;YACJ,IAAI,EAAE,MAAM,CAAC;YACb,OAAO,EAAE,MAAM,CAAC;YAChB,OAAO,EAAE,MAAM,CAAC;YAChB,iBAAiB,EAAE,GAAG,CAAC;SAC1B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;AACjE,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,sBAAsB,CAI1F;AAED,yEAAyE;AACzE,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,sBAAsB,CAI1F"}
@@ -157,31 +157,31 @@ function splitSignature(signature) {
157
157
  return { r, s, v };
158
158
  }
159
159
  /** Checks if the given value is an abstract viem wallet. */
160
- function isAbstractViemWalletClient(client) {
160
+ export function isAbstractViemWalletClient(client) {
161
161
  return typeof client === "object" && client !== null &&
162
162
  "signTypedData" in client && typeof client.signTypedData === "function" &&
163
163
  client.signTypedData.length === 1;
164
164
  }
165
165
  /** Checks if the given value is an abstract extended viem wallet (e.g. privy `useSignTypedData`). */
166
- function isAbstractExtendedViemWalletClient(client) {
166
+ export function isAbstractExtendedViemWalletClient(client) {
167
167
  return typeof client === "object" && client !== null &&
168
168
  "signTypedData" in client && typeof client.signTypedData === "function" &&
169
169
  client.signTypedData.length === 2;
170
170
  }
171
171
  /** Checks if the given value is an abstract ethers signer. */
172
- function isAbstractEthersSigner(client) {
172
+ export function isAbstractEthersSigner(client) {
173
173
  return typeof client === "object" && client !== null &&
174
174
  "signTypedData" in client && typeof client.signTypedData === "function" &&
175
175
  client.signTypedData.length === 3;
176
176
  }
177
177
  /** Checks if the given value is an abstract ethers v5 signer. */
178
- function isAbstractEthersV5Signer(client) {
178
+ export function isAbstractEthersV5Signer(client) {
179
179
  return typeof client === "object" && client !== null &&
180
180
  "_signTypedData" in client && typeof client._signTypedData === "function" &&
181
181
  client._signTypedData.length === 3;
182
182
  }
183
183
  /** Checks if the given value is an abstract `window.ethereum` object. */
184
- function isAbstractWindowEthereum(client) {
184
+ export function isAbstractWindowEthereum(client) {
185
185
  return typeof client === "object" && client !== null &&
186
186
  "request" in client && typeof client.request === "function" &&
187
187
  client.request.length >= 1;