@nktkas/hyperliquid 0.15.3 → 0.15.4

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 (30) hide show
  1. package/README.md +5 -2
  2. package/esm/src/clients/event.d.ts +2 -0
  3. package/esm/src/clients/event.d.ts.map +1 -1
  4. package/esm/src/clients/event.js +2 -0
  5. package/esm/src/clients/wallet.d.ts +53 -2
  6. package/esm/src/clients/wallet.d.ts.map +1 -1
  7. package/esm/src/clients/wallet.js +173 -42
  8. package/esm/src/transports/http/http_transport.d.ts +24 -5
  9. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  10. package/esm/src/transports/http/http_transport.js +12 -6
  11. package/esm/src/transports/websocket/websocket_transport.d.ts +1 -1
  12. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  13. package/esm/src/transports/websocket/websocket_transport.js +1 -5
  14. package/esm/src/types/exchange/requests.d.ts +120 -0
  15. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  16. package/package.json +1 -1
  17. package/script/src/clients/event.d.ts +2 -0
  18. package/script/src/clients/event.d.ts.map +1 -1
  19. package/script/src/clients/event.js +2 -0
  20. package/script/src/clients/wallet.d.ts +53 -2
  21. package/script/src/clients/wallet.d.ts.map +1 -1
  22. package/script/src/clients/wallet.js +173 -42
  23. package/script/src/transports/http/http_transport.d.ts +24 -5
  24. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  25. package/script/src/transports/http/http_transport.js +12 -6
  26. package/script/src/transports/websocket/websocket_transport.d.ts +1 -1
  27. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  28. package/script/src/transports/websocket/websocket_transport.js +1 -5
  29. package/script/src/types/exchange/requests.d.ts +120 -0
  30. package/script/src/types/exchange/requests.d.ts.map +1 -1
@@ -59,6 +59,17 @@
59
59
  * @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.
60
60
  */
61
61
  class WalletClient {
62
+ /** Gets the next nonce for signing transactions. */
63
+ get _nonce() {
64
+ let nonce = Date.now();
65
+ if (nonce <= this._lastNonce) {
66
+ nonce = ++this._lastNonce;
67
+ }
68
+ else {
69
+ this._lastNonce = nonce;
70
+ }
71
+ return nonce;
72
+ }
62
73
  /**
63
74
  * Initialises a new instance.
64
75
  * @param args - The parameters for the client.
@@ -144,13 +155,20 @@
144
155
  writable: true,
145
156
  value: void 0
146
157
  });
158
+ /** The last nonce used for signing transactions. */
159
+ Object.defineProperty(this, "_lastNonce", {
160
+ enumerable: true,
161
+ configurable: true,
162
+ writable: true,
163
+ value: 0
164
+ });
147
165
  this.transport = args.transport;
148
166
  this.wallet = args.wallet;
149
167
  this.isTestnet = args.isTestnet ?? false;
150
168
  this.defaultVaultAddress = args.defaultVaultAddress;
151
169
  this.signatureChainId = args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1");
152
170
  }
153
- // ———————————————Actions———————————————
171
+ // ——————————————— Exchange API ———————————————
154
172
  /**
155
173
  * Approve an agent to sign on behalf of the master or sub-accounts.
156
174
  * @param args - The parameters for the request.
@@ -181,7 +199,7 @@
181
199
  type: "approveAgent",
182
200
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
183
201
  signatureChainId: this.signatureChainId,
184
- nonce: args.nonce ?? Date.now(),
202
+ nonce: args.nonce ?? this._nonce,
185
203
  };
186
204
  // Sign the action
187
205
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -234,7 +252,7 @@
234
252
  type: "approveBuilderFee",
235
253
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
236
254
  signatureChainId: this.signatureChainId,
237
- nonce: args.nonce ?? Date.now(),
255
+ nonce: args.nonce ?? this._nonce,
238
256
  };
239
257
  // Sign the action
240
258
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -296,7 +314,7 @@
296
314
  */
297
315
  async batchModify(args, signal) {
298
316
  // Destructure the parameters
299
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
317
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
300
318
  // Construct an action
301
319
  const action = {
302
320
  type: "batchModify",
@@ -306,8 +324,8 @@
306
324
  order: {
307
325
  a: modify.order.a,
308
326
  b: modify.order.b,
309
- p: modify.order.p,
310
- s: modify.order.s,
327
+ p: this._formatDecimal(modify.order.p),
328
+ s: this._formatDecimal(modify.order.s),
311
329
  r: modify.order.r,
312
330
  t: "limit" in modify.order.t
313
331
  ? {
@@ -318,7 +336,7 @@
318
336
  : {
319
337
  trigger: {
320
338
  isMarket: modify.order.t.trigger.isMarket,
321
- triggerPx: modify.order.t.trigger.triggerPx,
339
+ triggerPx: this._formatDecimal(modify.order.t.trigger.triggerPx),
322
340
  tpsl: modify.order.t.trigger.tpsl,
323
341
  },
324
342
  },
@@ -372,7 +390,7 @@
372
390
  */
373
391
  async cancel(args, signal) {
374
392
  // Destructure the parameters
375
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
393
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
376
394
  // Construct an action
377
395
  const action = {
378
396
  type: "cancel",
@@ -423,7 +441,7 @@
423
441
  type: "cDeposit",
424
442
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
425
443
  signatureChainId: this.signatureChainId,
426
- nonce: args.nonce ?? Date.now(),
444
+ nonce: args.nonce ?? this._nonce,
427
445
  };
428
446
  // Sign the action
429
447
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -467,7 +485,7 @@
467
485
  */
468
486
  async claimRewards(args = {}, signal) {
469
487
  // Destructure the parameters
470
- const { nonce = Date.now() } = args;
488
+ const { nonce = this._nonce, } = args;
471
489
  // Construct an action
472
490
  const sortedAction = { type: "claimRewards" };
473
491
  // Sign the action
@@ -511,7 +529,7 @@
511
529
  */
512
530
  async cancelByCloid(args, signal) {
513
531
  // Destructure the parameters
514
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
532
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
515
533
  // Construct an action
516
534
  const action = {
517
535
  type: "cancelByCloid",
@@ -562,7 +580,7 @@
562
580
  type: "cWithdraw",
563
581
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
564
582
  signatureChainId: this.signatureChainId,
565
- nonce: args.nonce ?? Date.now(),
583
+ nonce: args.nonce ?? this._nonce,
566
584
  };
567
585
  // Sign the action
568
586
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -606,7 +624,7 @@
606
624
  */
607
625
  async evmUserModify(args, signal) {
608
626
  // Destructure the parameters
609
- const { nonce = Date.now(), ...actionArgs } = args;
627
+ const { nonce = this._nonce, ...actionArgs } = args;
610
628
  // Construct an action
611
629
  const action = {
612
630
  type: "evmUserModify",
@@ -648,7 +666,7 @@
648
666
  */
649
667
  async createSubAccount(args, signal) {
650
668
  // Destructure the parameters
651
- const { nonce = Date.now(), ...actionArgs } = args;
669
+ const { nonce = this._nonce, ...actionArgs } = args;
652
670
  // Construct an action
653
671
  const action = {
654
672
  type: "createSubAccount",
@@ -694,7 +712,7 @@
694
712
  */
695
713
  async createVault(args, signal) {
696
714
  // Destructure the parameters
697
- const { nonce = Date.now(), ...actionArgs } = args;
715
+ const { nonce = this._nonce, ...actionArgs } = args;
698
716
  // Construct an action
699
717
  const action = {
700
718
  type: "createVault",
@@ -754,7 +772,7 @@
754
772
  */
755
773
  async modify(args, signal) {
756
774
  // Destructure the parameters
757
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
775
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
758
776
  // Construct an action
759
777
  const action = {
760
778
  type: "modify",
@@ -762,8 +780,8 @@
762
780
  order: {
763
781
  a: actionArgs.order.a,
764
782
  b: actionArgs.order.b,
765
- p: actionArgs.order.p,
766
- s: actionArgs.order.s,
783
+ p: this._formatDecimal(actionArgs.order.p),
784
+ s: this._formatDecimal(actionArgs.order.s),
767
785
  r: actionArgs.order.r,
768
786
  t: "limit" in actionArgs.order.t
769
787
  ? {
@@ -774,7 +792,7 @@
774
792
  : {
775
793
  trigger: {
776
794
  isMarket: actionArgs.order.t.trigger.isMarket,
777
- triggerPx: actionArgs.order.t.trigger.triggerPx,
795
+ triggerPx: this._formatDecimal(actionArgs.order.t.trigger.triggerPx),
778
796
  tpsl: actionArgs.order.t.trigger.tpsl,
779
797
  },
780
798
  },
@@ -835,7 +853,7 @@
835
853
  */
836
854
  async order(args, signal) {
837
855
  // Destructure the parameters
838
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
856
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
839
857
  // Construct an action
840
858
  const action = {
841
859
  type: "order",
@@ -843,8 +861,8 @@
843
861
  const sortedOrder = {
844
862
  a: order.a,
845
863
  b: order.b,
846
- p: order.p,
847
- s: order.s,
864
+ p: this._formatDecimal(order.p),
865
+ s: this._formatDecimal(order.s),
848
866
  r: order.r,
849
867
  t: "limit" in order.t
850
868
  ? {
@@ -855,7 +873,7 @@
855
873
  : {
856
874
  trigger: {
857
875
  isMarket: order.t.trigger.isMarket,
858
- triggerPx: order.t.trigger.triggerPx,
876
+ triggerPx: this._formatDecimal(order.t.trigger.triggerPx),
859
877
  tpsl: order.t.trigger.tpsl,
860
878
  },
861
879
  },
@@ -912,7 +930,7 @@
912
930
  */
913
931
  async scheduleCancel(args = {}, signal) {
914
932
  // Destructure the parameters
915
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
933
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
916
934
  // Construct an action
917
935
  const action = {
918
936
  type: "scheduleCancel",
@@ -957,7 +975,7 @@
957
975
  */
958
976
  async setDisplayName(args, signal) {
959
977
  // Destructure the parameters
960
- const { nonce = Date.now(), ...actionArgs } = args;
978
+ const { nonce = this._nonce, ...actionArgs } = args;
961
979
  // Construct an action
962
980
  const action = {
963
981
  type: "setDisplayName",
@@ -999,7 +1017,7 @@
999
1017
  */
1000
1018
  async setReferrer(args, signal) {
1001
1019
  // Destructure the parameters
1002
- const { nonce = Date.now(), ...actionArgs } = args;
1020
+ const { nonce = this._nonce, ...actionArgs } = args;
1003
1021
  // Construct an action
1004
1022
  const action = {
1005
1023
  type: "setReferrer",
@@ -1019,6 +1037,110 @@
1019
1037
  this._validateResponse(response);
1020
1038
  return response;
1021
1039
  }
1040
+ /**
1041
+ * Deploying HIP-1 and HIP-2 assets.
1042
+ * @param args - The parameters for the request.
1043
+ * @param signal - An optional abort signal.
1044
+ * @returns Successful response without specific data.
1045
+ * @throws {ApiRequestError} When the API returns an error response.
1046
+ * @untested
1047
+ *
1048
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-1-and-hip-2-assets
1049
+ * @example
1050
+ * ```ts
1051
+ * import * as hl from "@nktkas/hyperliquid";
1052
+ * import { privateKeyToAccount } from "viem/accounts";
1053
+ *
1054
+ * const wallet = privateKeyToAccount("0x...");
1055
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1056
+ * const client = new hl.WalletClient({ wallet, transport });
1057
+ *
1058
+ * // Unknown what the successful response will be
1059
+ * const result = await client.spotDeploy({
1060
+ * registerToken2: {
1061
+ * spec: {
1062
+ * name: "TestToken",
1063
+ * szDecimals: 8,
1064
+ * weiDecimals: 8,
1065
+ * },
1066
+ * maxGas: 1000000,
1067
+ * fullName: "TestToken (TT)"
1068
+ * }
1069
+ * });
1070
+ * ```
1071
+ */
1072
+ async spotDeploy(args, signal) {
1073
+ // Destructure the parameters
1074
+ const { nonce = this._nonce, ...actionArgs } = args;
1075
+ // Construct an action
1076
+ let action;
1077
+ if ("registerToken2" in actionArgs) {
1078
+ action = {
1079
+ type: "spotDeploy",
1080
+ registerToken2: {
1081
+ spec: {
1082
+ name: actionArgs.registerToken2.spec.name,
1083
+ szDecimals: actionArgs.registerToken2.spec.szDecimals,
1084
+ weiDecimals: actionArgs.registerToken2.spec.weiDecimals,
1085
+ },
1086
+ maxGas: actionArgs.registerToken2.maxGas,
1087
+ fullName: actionArgs.registerToken2.fullName,
1088
+ },
1089
+ };
1090
+ }
1091
+ else if ("userGenesis" in actionArgs) {
1092
+ action = {
1093
+ type: "spotDeploy",
1094
+ userGenesis: {
1095
+ token: actionArgs.userGenesis.token,
1096
+ userAndWei: actionArgs.userGenesis.userAndWei,
1097
+ existingTokenAndWei: actionArgs.userGenesis.existingTokenAndWei,
1098
+ },
1099
+ };
1100
+ }
1101
+ else if ("genesis" in actionArgs) {
1102
+ action = {
1103
+ type: "spotDeploy",
1104
+ genesis: {
1105
+ token: actionArgs.genesis.token,
1106
+ maxSupply: actionArgs.genesis.maxSupply,
1107
+ },
1108
+ };
1109
+ }
1110
+ else if ("registerSpot" in actionArgs) {
1111
+ action = {
1112
+ type: "spotDeploy",
1113
+ registerSpot: {
1114
+ tokens: actionArgs.registerSpot.tokens,
1115
+ },
1116
+ };
1117
+ }
1118
+ else {
1119
+ action = {
1120
+ type: "spotDeploy",
1121
+ registerHyperliquidity: {
1122
+ spot: actionArgs.registerHyperliquidity.spot,
1123
+ startPx: actionArgs.registerHyperliquidity.startPx,
1124
+ orderSz: actionArgs.registerHyperliquidity.orderSz,
1125
+ nOrders: actionArgs.registerHyperliquidity.nOrders,
1126
+ nSeededLevels: actionArgs.registerHyperliquidity.nSeededLevels,
1127
+ },
1128
+ };
1129
+ }
1130
+ // Sign the action
1131
+ const signature = await (0, signing_js_1.signL1Action)({
1132
+ wallet: this.wallet,
1133
+ action,
1134
+ nonce,
1135
+ isTestnet: this.isTestnet,
1136
+ });
1137
+ // Send a request
1138
+ const request = { action, signature, nonce };
1139
+ const response = await this.transport.request("exchange", request, signal);
1140
+ // Validate a response
1141
+ this._validateResponse(response);
1142
+ return response;
1143
+ }
1022
1144
  /**
1023
1145
  * Transfer a spot asset on L1 to another address.
1024
1146
  * @param args - The parameters for the request.
@@ -1050,7 +1172,7 @@
1050
1172
  type: "spotSend",
1051
1173
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1052
1174
  signatureChainId: this.signatureChainId,
1053
- time: args.time ?? Date.now(),
1175
+ time: args.time ?? this._nonce,
1054
1176
  };
1055
1177
  // Sign the action
1056
1178
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -1098,7 +1220,7 @@
1098
1220
  */
1099
1221
  async spotUser(args, signal) {
1100
1222
  // Destructure the parameters
1101
- const { nonce = Date.now(), ...actionArgs } = args;
1223
+ const { nonce = this._nonce, ...actionArgs } = args;
1102
1224
  // Construct an action
1103
1225
  const action = {
1104
1226
  type: "spotUser",
@@ -1147,7 +1269,7 @@
1147
1269
  */
1148
1270
  async subAccountSpotTransfer(args, signal) {
1149
1271
  // Destructure the parameters
1150
- const { nonce = Date.now(), ...actionArgs } = args;
1272
+ const { nonce = this._nonce, ...actionArgs } = args;
1151
1273
  // Construct an action
1152
1274
  const action = {
1153
1275
  type: "subAccountSpotTransfer",
@@ -1196,7 +1318,7 @@
1196
1318
  */
1197
1319
  async subAccountTransfer(args, signal) {
1198
1320
  // Destructure the parameters
1199
- const { nonce = Date.now(), ...actionArgs } = args;
1321
+ const { nonce = this._nonce, ...actionArgs } = args;
1200
1322
  // Construct an action
1201
1323
  const action = {
1202
1324
  type: "subAccountTransfer",
@@ -1249,7 +1371,7 @@
1249
1371
  type: "tokenDelegate",
1250
1372
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1251
1373
  signatureChainId: this.signatureChainId,
1252
- nonce: args.nonce ?? Date.now(),
1374
+ nonce: args.nonce ?? this._nonce,
1253
1375
  };
1254
1376
  // Sign the action
1255
1377
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -1298,7 +1420,7 @@
1298
1420
  */
1299
1421
  async twapCancel(args, signal) {
1300
1422
  // Destructure the parameters
1301
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1423
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1302
1424
  // Construct an action
1303
1425
  const action = {
1304
1426
  type: "twapCancel",
@@ -1349,14 +1471,14 @@
1349
1471
  */
1350
1472
  async twapOrder(args, signal) {
1351
1473
  // Destructure the parameters
1352
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1474
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1353
1475
  // Construct an action
1354
1476
  const action = {
1355
1477
  type: "twapOrder",
1356
1478
  twap: {
1357
1479
  a: actionArgs.a,
1358
1480
  b: actionArgs.b,
1359
- s: actionArgs.s,
1481
+ s: this._formatDecimal(actionArgs.s),
1360
1482
  r: actionArgs.r,
1361
1483
  m: actionArgs.m,
1362
1484
  t: actionArgs.t,
@@ -1403,7 +1525,7 @@
1403
1525
  */
1404
1526
  async updateIsolatedMargin(args, signal) {
1405
1527
  // Destructure the parameters
1406
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1528
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1407
1529
  // Construct an action
1408
1530
  const action = {
1409
1531
  type: "updateIsolatedMargin",
@@ -1452,7 +1574,7 @@
1452
1574
  */
1453
1575
  async updateLeverage(args, signal) {
1454
1576
  // Destructure the parameters
1455
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
1577
+ const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
1456
1578
  // Construct an action
1457
1579
  const action = {
1458
1580
  type: "updateLeverage",
@@ -1505,7 +1627,7 @@
1505
1627
  type: "usdClassTransfer",
1506
1628
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1507
1629
  signatureChainId: this.signatureChainId,
1508
- nonce: args.nonce ?? Date.now(),
1630
+ nonce: args.nonce ?? this._nonce,
1509
1631
  };
1510
1632
  // Sign the action
1511
1633
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -1558,7 +1680,7 @@
1558
1680
  type: "usdSend",
1559
1681
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1560
1682
  signatureChainId: this.signatureChainId,
1561
- time: args.time ?? Date.now(),
1683
+ time: args.time ?? this._nonce,
1562
1684
  };
1563
1685
  // Sign the action
1564
1686
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -1606,7 +1728,7 @@
1606
1728
  */
1607
1729
  async vaultDistribute(args, signal) {
1608
1730
  // Destructure the parameters
1609
- const { nonce = Date.now(), ...actionArgs } = args;
1731
+ const { nonce = this._nonce, ...actionArgs } = args;
1610
1732
  // Construct an action
1611
1733
  const action = {
1612
1734
  type: "vaultDistribute",
@@ -1653,7 +1775,7 @@
1653
1775
  */
1654
1776
  async vaultModify(args, signal) {
1655
1777
  // Destructure the parameters
1656
- const { nonce = Date.now(), ...actionArgs } = args;
1778
+ const { nonce = this._nonce, ...actionArgs } = args;
1657
1779
  // Construct an action
1658
1780
  const action = {
1659
1781
  type: "vaultModify",
@@ -1701,7 +1823,7 @@
1701
1823
  */
1702
1824
  async vaultTransfer(args, signal) {
1703
1825
  // Destructure the parameters
1704
- const { nonce = Date.now(), ...actionArgs } = args;
1826
+ const { nonce = this._nonce, ...actionArgs } = args;
1705
1827
  // Construct an action
1706
1828
  const action = {
1707
1829
  type: "vaultTransfer",
@@ -1753,7 +1875,7 @@
1753
1875
  type: "withdraw3",
1754
1876
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1755
1877
  signatureChainId: this.signatureChainId,
1756
- time: args.time ?? Date.now(),
1878
+ time: args.time ?? this._nonce,
1757
1879
  };
1758
1880
  // Sign the action
1759
1881
  const signature = await (0, signing_js_1.signUserSignedAction)({
@@ -1776,6 +1898,15 @@
1776
1898
  this._validateResponse(response);
1777
1899
  return response;
1778
1900
  }
1901
+ // ——————————————— Private methods ———————————————
1902
+ /** Formats a decimal number as a string, removing trailing zeros. */
1903
+ _formatDecimal(numStr) {
1904
+ if (!numStr.includes("."))
1905
+ return numStr;
1906
+ const [intPart, fracPart] = numStr.split(".");
1907
+ const newFrac = fracPart.replace(/0+$/, "");
1908
+ return newFrac ? `${intPart}.${newFrac}` : intPart;
1909
+ }
1779
1910
  /** Validate a response from the API. */
1780
1911
  _validateResponse(response) {
1781
1912
  if (response.status === "err") {
@@ -19,11 +19,27 @@ export declare class HttpRequestError extends TransportError {
19
19
  export interface HttpTransportOptions {
20
20
  /**
21
21
  * Base URL for API endpoints.
22
- * - Mainnet: `https://api.hyperliquid.xyz`
23
- * - Testnet: `https://api.hyperliquid-testnet.xyz`
24
- * @defaultValue `https://api.hyperliquid.xyz`
22
+ *
23
+ * When given as a string or URL, the URL is modified based on the request type:
24
+ * - For `"info"` and `"exchange"`, the hostname is replaced with `api.<baseDomain>`.
25
+ * - For `"explorer"`, the hostname is replaced with `rpc.<baseDomain>`.
26
+ *
27
+ * Alternatively, you can supply an object to specify separate URLs:
28
+ * ```ts
29
+ * {
30
+ * api: "https://api.hyperliquid.xyz",
31
+ * rpc: "https://rpc.hyperliquid.xyz"
32
+ * }
33
+ * ```
34
+ *
35
+ * - Mainnet: `https://hyperliquid.xyz`
36
+ * - Testnet: `https://hyperliquid-testnet.xyz`
37
+ * @defaultValue `https://hyperliquid.xyz`
25
38
  */
26
- url?: string | URL;
39
+ url?: string | URL | {
40
+ api?: string | URL;
41
+ rpc?: string | URL;
42
+ };
27
43
  /**
28
44
  * Request timeout in ms.
29
45
  * Set to `null` to disable.
@@ -47,7 +63,10 @@ export interface HttpTransportOptions {
47
63
  }
48
64
  /** HTTP implementation of the REST transport interface. */
49
65
  export declare class HttpTransport implements IRequestTransport, HttpTransportOptions {
50
- url: string | URL;
66
+ url: string | URL | {
67
+ api?: string | URL;
68
+ rpc?: string | URL;
69
+ };
51
70
  timeout: number | null;
52
71
  fetchOptions: Omit<RequestInit, "body" | "method">;
53
72
  onRequest?: (request: Request) => MaybePromise<Request | void | null | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"http_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/http/http_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEvE,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtC;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,cAAc;IAM7B,QAAQ,EAAE,QAAQ;IAAS,YAAY,CAAC,EAAE,MAAM;IALnE;;;;OAIG;gBACgB,QAAQ,EAAE,QAAQ,EAAS,YAAY,CAAC,EAAE,MAAM,YAAA;CAOtE;AAED,0DAA0D;AAC1D,MAAM,WAAW,oBAAoB;IACjC;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAEnB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAExB,2IAA2I;IAC3I,YAAY,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC;IAEpD;;;;OAIG;IACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAElF;;;;OAIG;IACH,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,YAAY,CAAC,QAAQ,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;CACzF;AAED,2DAA2D;AAC3D,qBAAa,aAAc,YAAW,iBAAiB,EAAE,oBAAoB;IACzE,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IAClB,OAAO,EAA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97,18 @@
97
97
  */
98
98
  async request(endpoint, payload, signal) {
99
99
  // Construct a Request
100
- const url = new URL(endpoint, this.url);
101
- // FIXME: Temporary hack: replace `api.hyperliquid-testnet.xyz/explorer` with `rpc.hyperliquid-testnet.xyz/explorer`
102
- // until the new rpc url becomes the standard for mainnet.
103
- // TODO: Maybe after that should split the url property into api and rpc variants.
104
- if (url.hostname === "api.hyperliquid-testnet.xyz" && url.pathname === "/explorer") {
105
- url.hostname = "rpc.hyperliquid-testnet.xyz";
100
+ let url;
101
+ if (typeof this.url === "string" || this.url instanceof URL) {
102
+ // If a string is provided, get the base domain and apply the modification to it
103
+ const parsedUrl = new URL(this.url);
104
+ const parts = parsedUrl.hostname.split(".");
105
+ const baseDomain = parts.slice(-2).join(".");
106
+ const baseUrl = `${parsedUrl.protocol}//${endpoint === "explorer" ? "rpc" : "api"}.${baseDomain}`;
107
+ url = new URL(endpoint, baseUrl);
108
+ }
109
+ else {
110
+ // If an object is specified, use the specific url and add a path to it
111
+ url = new URL(endpoint, endpoint === "explorer" ? this.url.rpc : this.url.api);
106
112
  }
107
113
  const init = mergeRequestInit({
108
114
  body: JSON.stringify(payload),
@@ -76,7 +76,7 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
76
76
  constructor(options?: WebSocketTransportOptions);
77
77
  /**
78
78
  * Sends a request to the Hyperliquid API via WebSocket.
79
- * @param endpoint - The API endpoint to send the request to.
79
+ * @param endpoint - The API endpoint to send the request to (`explorer` requests are not supported).
80
80
  * @param payload - The payload to send with the request.
81
81
  * @param signal - An optional abort signal.
82
82
  * @returns A promise that resolves with parsed JSON response body.
@@ -1 +1 @@
1
- {"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,qBAAqB,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,4BAA4B,EACpC,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAA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AAC;IAEvB,yCAAyC;IACzC,QAAQ,CAAC,SAAS,EAAE;QAChB;;;WAGG;QACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;KACpC,CAAC;IAEF,oDAAoD;IACpD,QAAQ,CAAC,MAAM,EAAE,qBAAqB,CAAC;IAEvC;;;OAGG;gBACS,OAAO,CAAC,EAAE,yBAAyB;IAuC/C;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAwBzG;;;;;;;OAOG;IACG,SAAS,CACX,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,EACrC,MAAM,CAAC,EAAE,WAAW,GACrB,OAAO,CAAC,YAAY,CAAC;IAwExB;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAuB1C;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAoB7C"}
1
+ {"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,qBAAqB,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,4BAA4B,EACpC,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AACvG,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7F,OAAO,EAAE,qBAAqB,EAAE,CAAC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,EAAE,yBAAyB;IAuC/C;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBzG;;;;;;;OAOG;IACG,SAAS,CACX,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,EACrC,MAAM,CAAC,EAAE,WAAW,GACrB,OAAO,CAAC,YAAY,CAAC;IAwExB;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAuB1C;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAoB7C"}
@@ -111,7 +111,7 @@
111
111
  }
112
112
  /**
113
113
  * Sends a request to the Hyperliquid API via WebSocket.
114
- * @param endpoint - The API endpoint to send the request to.
114
+ * @param endpoint - The API endpoint to send the request to (`explorer` requests are not supported).
115
115
  * @param payload - The payload to send with the request.
116
116
  * @param signal - An optional abort signal.
117
117
  * @returns A promise that resolves with parsed JSON response body.
@@ -119,10 +119,6 @@
119
119
  * @note Explorer requests are not supported in the Hyperliquid WebSocket API.
120
120
  */
121
121
  request(type, payload, signal) {
122
- // Reject explorer requests because they are not supported by the Hyperliquid WebSocket API
123
- if (type === "explorer") {
124
- return Promise.reject(new _websocket_request_dispatcher_js_1.WebSocketRequestError("Explorer requests are not supported in the Hyperliquid WebSocket API."));
125
- }
126
122
  // Send the request and wait for a response
127
123
  const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
128
124
  const combinedSignal = signal && timeoutSignal