ault-sdk-ts 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/core/user-agent.ts
8
- var SDK_VERSION = "0.16.0";
8
+ var SDK_VERSION = "0.17.0";
9
9
  var USER_AGENT = `ault-sdk-ts/${SDK_VERSION}`;
10
10
  function isServerRuntime() {
11
11
  if (typeof document !== "undefined") return false;
@@ -21590,6 +21590,14 @@ function compareBytes(a, b) {
21590
21590
  return a.length - b.length;
21591
21591
  }
21592
21592
 
21593
+ // src/rest/market-id.ts
21594
+ var MARKET_ID_HEX_RE = /^(0[xX])?([0-9a-fA-F]{2})+$/;
21595
+ function marketIdToBase64(marketId) {
21596
+ if (marketId instanceof MarketId) return marketId.toBase64();
21597
+ if (typeof marketId !== "string") return bytesToBase64(marketId);
21598
+ return MARKET_ID_HEX_RE.test(marketId) ? MarketId.fromHex(marketId).toBase64() : marketId;
21599
+ }
21600
+
21593
21601
  // src/proto/gen/ault/clob/v1/query.ts
21594
21602
  import { BinaryReader as BinaryReader45, BinaryWriter as BinaryWriter45 } from "@bufbuild/protobuf/wire";
21595
21603
 
@@ -22580,9 +22588,6 @@ function normalizeOrderId(orderId) {
22580
22588
  if (typeof orderId !== "string") return bytesToBase64(orderId);
22581
22589
  return ORDER_ID_HEX_RE.test(orderId) ? OrderId.fromHex(orderId).toBase64() : orderId;
22582
22590
  }
22583
- function normalizeMarketId(marketId) {
22584
- return typeof marketId === "string" ? marketId : bytesToBase64(marketId);
22585
- }
22586
22591
  var LIMIT_TYPE_URL = "/ault.clob.v1.MsgPlaceLimitOrder";
22587
22592
  var MARKET_TYPE_URL = "/ault.clob.v1.MsgPlaceMarketOrder";
22588
22593
  var OPTION_CODECS = {
@@ -22674,7 +22679,7 @@ function createClobApi(context) {
22674
22679
  },
22675
22680
  async getOrders(params = {}) {
22676
22681
  const query = buildQuery({
22677
- market_id: params.marketId ? normalizeMarketId(params.marketId) : void 0,
22682
+ market_id: params.marketId ? marketIdToBase64(params.marketId) : void 0,
22678
22683
  ...params.pagination
22679
22684
  });
22680
22685
  return fetchProtoJson(
@@ -25101,10 +25106,6 @@ function isSet46(value) {
25101
25106
  }
25102
25107
 
25103
25108
  // src/rest/margin.ts
25104
- function marketIdParam(marketId) {
25105
- if (marketId instanceof MarketId) return marketId.toBase64();
25106
- return typeof marketId === "string" ? marketId : bytesToBase64(marketId);
25107
- }
25108
25109
  var enc2 = encodeURIComponent;
25109
25110
  function createMarginApi(context) {
25110
25111
  return {
@@ -25121,7 +25122,7 @@ function createMarginApi(context) {
25121
25122
  },
25122
25123
  async getMarginAccount(owner, marketId) {
25123
25124
  const query = buildQuery({
25124
- market_id: marketId === void 0 ? void 0 : marketIdParam(marketId)
25125
+ market_id: marketId === void 0 ? void 0 : marketIdToBase64(marketId)
25125
25126
  });
25126
25127
  return fetchProtoJson(
25127
25128
  context,
@@ -25511,9 +25512,6 @@ function isSet48(value) {
25511
25512
  }
25512
25513
 
25513
25514
  // src/rest/market.ts
25514
- function normalizeMarketId2(marketId) {
25515
- return typeof marketId === "string" ? marketId : bytesToBase64(marketId);
25516
- }
25517
25515
  function createMarketApi(context) {
25518
25516
  return {
25519
25517
  async getParams() {
@@ -25526,7 +25524,7 @@ function createMarketApi(context) {
25526
25524
  async getMarket(marketId) {
25527
25525
  return fetchProtoJson(
25528
25526
  context,
25529
- `/ault/market/v1/markets/${encodeURIComponent(normalizeMarketId2(marketId))}`,
25527
+ `/ault/market/v1/markets/${encodeURIComponent(marketIdToBase64(marketId))}`,
25530
25528
  QueryMarketResponse
25531
25529
  );
25532
25530
  }
@@ -28741,10 +28739,6 @@ function isSet53(value) {
28741
28739
  }
28742
28740
 
28743
28741
  // src/rest/perpetual.ts
28744
- function normalizeMarketId3(marketId) {
28745
- if (marketId instanceof MarketId) return marketId.toBase64();
28746
- return typeof marketId === "string" ? marketId : bytesToBase64(marketId);
28747
- }
28748
28742
  function createPerpetualApi(context) {
28749
28743
  return {
28750
28744
  async getParams() {
@@ -28759,7 +28753,7 @@ function createPerpetualApi(context) {
28759
28753
  },
28760
28754
  async getAccount(owner, marketId) {
28761
28755
  const query = buildQuery({
28762
- market_id: marketId === void 0 ? void 0 : normalizeMarketId3(marketId)
28756
+ market_id: marketId === void 0 ? void 0 : marketIdToBase64(marketId)
28763
28757
  });
28764
28758
  return fetchProtoJson(
28765
28759
  context,
@@ -28768,7 +28762,7 @@ function createPerpetualApi(context) {
28768
28762
  );
28769
28763
  },
28770
28764
  async getPosition(owner, marketId, cross) {
28771
- const query = buildQuery({ market_id: normalizeMarketId3(marketId), cross });
28765
+ const query = buildQuery({ market_id: marketIdToBase64(marketId), cross });
28772
28766
  return fetchProtoJson(
28773
28767
  context,
28774
28768
  `/ault/perpetual/v1/position/${encodeURIComponent(normalizeAddress(owner))}${query}`,
@@ -28777,7 +28771,7 @@ function createPerpetualApi(context) {
28777
28771
  },
28778
28772
  async getAccountRisk(owner, marketId) {
28779
28773
  const query = buildQuery({
28780
- market_id: marketId === void 0 ? void 0 : normalizeMarketId3(marketId)
28774
+ market_id: marketId === void 0 ? void 0 : marketIdToBase64(marketId)
28781
28775
  });
28782
28776
  return fetchProtoJson(
28783
28777
  context,
@@ -28786,11 +28780,11 @@ function createPerpetualApi(context) {
28786
28780
  );
28787
28781
  },
28788
28782
  async getFunding(marketId) {
28789
- const query = buildQuery({ market_id: normalizeMarketId3(marketId) });
28783
+ const query = buildQuery({ market_id: marketIdToBase64(marketId) });
28790
28784
  return fetchProtoJson(context, `/ault/perpetual/v1/funding${query}`, QueryFundingResponse);
28791
28785
  },
28792
28786
  async getPrices(marketId) {
28793
- const id = encodeURIComponent(normalizeMarketId3(marketId));
28787
+ const id = encodeURIComponent(marketIdToBase64(marketId));
28794
28788
  return fetchProtoJson(
28795
28789
  context,
28796
28790
  `/ault/perpetual/v1/markets/${id}/prices`,
@@ -28798,7 +28792,7 @@ function createPerpetualApi(context) {
28798
28792
  );
28799
28793
  },
28800
28794
  async getImpactPrices(marketId) {
28801
- const id = encodeURIComponent(normalizeMarketId3(marketId));
28795
+ const id = encodeURIComponent(marketIdToBase64(marketId));
28802
28796
  return fetchProtoJson(
28803
28797
  context,
28804
28798
  `/ault/perpetual/v1/markets/${id}/impact_prices`,
@@ -29532,7 +29526,7 @@ function createRewardApi(context) {
29532
29526
  );
29533
29527
  },
29534
29528
  async getDeployerShare(marketId) {
29535
- const encoded = typeof marketId === "string" ? marketId : bytesToBase64(marketId);
29529
+ const encoded = marketIdToBase64(marketId);
29536
29530
  return fetchProtoJson(
29537
29531
  context,
29538
29532
  `/ault/reward/v1/deployer_share/${encodeURIComponent(encoded)}`,
@@ -34313,7 +34307,7 @@ function createVaultApi(context) {
34313
34307
  }
34314
34308
 
34315
34309
  // src/eip712/sign-and-broadcast.ts
34316
- import { hexToBytes, hashTypedData, recoverPublicKey } from "viem";
34310
+ import { hexToBytes, hashTypedData, recoverPublicKey, sha256 as sha2562 } from "viem";
34317
34311
 
34318
34312
  // src/proto/tx-encode.ts
34319
34313
  var WIRE_VARINT = 0;
@@ -34969,6 +34963,34 @@ function getConfirmationUnavailableLog(txHash, error) {
34969
34963
  }
34970
34964
  return `Transaction ${txHash} was broadcast but could not be confirmed.`;
34971
34965
  }
34966
+ async function broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions) {
34967
+ const txHash = normalizeTxHash(sha2562(txRawBytes));
34968
+ try {
34969
+ const result = await postJson(
34970
+ `${network.restUrl}/cosmos/tx/v1beta1/txs`,
34971
+ { tx_bytes: bytesToBase64(txRawBytes), mode: "BROADCAST_MODE_SYNC" },
34972
+ { ...fetchOptions, retries: 0 },
34973
+ fetchFn
34974
+ );
34975
+ const response = result?.tx_response;
34976
+ const code = response?.code;
34977
+ const rawHash = response?.txhash;
34978
+ if (typeof code !== "number" || !Number.isSafeInteger(code) || code < 0 || typeof rawHash !== "string" || !rawHash) {
34979
+ throw new Error("Unrecognized broadcast response");
34980
+ }
34981
+ if (normalizeTxHash(rawHash) !== txHash) {
34982
+ throw new Error("Broadcast response is for a different transaction");
34983
+ }
34984
+ return { txHash, code, rawLog: response?.raw_log };
34985
+ } catch (error) {
34986
+ throw new TxConfirmationUnavailableError({
34987
+ txHash,
34988
+ code: -1,
34989
+ rawLog: error instanceof Error ? error.message : "Broadcast response unavailable",
34990
+ confirmed: void 0
34991
+ });
34992
+ }
34993
+ }
34972
34994
  function _resetIndexingCache() {
34973
34995
  }
34974
34996
  async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs = TIMING_CONSTANTS.TX_POLL_TIMEOUT_MS, intervalMs = 1500) {
@@ -34985,7 +35007,8 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
34985
35007
  { ...fetchOptions, retries: 0, timeout: perRequestTimeout },
34986
35008
  fetchFn
34987
35009
  );
34988
- if (typeof result.tx_response?.code !== "number") {
35010
+ const receipt = result?.tx_response;
35011
+ if (!receipt || !Number.isSafeInteger(receipt.code) || receipt.code < 0 || typeof receipt.txhash !== "string" || normalizeTxHash(receipt.txhash) !== normalizeTxHash(txHash) || typeof receipt.height !== "string" || !/^\d+$/.test(receipt.height) || BigInt(receipt.height) <= 0n) {
34989
35012
  return {
34990
35013
  code: -1,
34991
35014
  rawLog: `Transaction ${txHash} was broadcast but the confirmation response was invalid.`,
@@ -34993,9 +35016,9 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
34993
35016
  };
34994
35017
  }
34995
35018
  return {
34996
- code: result.tx_response.code,
34997
- rawLog: result.tx_response.raw_log,
34998
- data: result.tx_response.data,
35019
+ code: receipt.code,
35020
+ rawLog: receipt.raw_log,
35021
+ data: receipt.data,
34999
35022
  confirmed: true
35000
35023
  };
35001
35024
  } catch (error) {
@@ -35153,26 +35176,20 @@ async function signAndBroadcastEip712({
35153
35176
  authInfoBytes,
35154
35177
  signatures: [sigWithoutRecovery]
35155
35178
  });
35156
- const txBytesBase64 = bytesToBase64(txRawBytes);
35157
- let result;
35179
+ let checkTx;
35158
35180
  try {
35159
- result = await postJson(
35160
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
35161
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
35162
- { ...fetchOptions, retries: 0 },
35163
- fetchFn
35164
- );
35181
+ checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
35165
35182
  } catch (error) {
35166
35183
  clearCachedSequence(cache);
35167
35184
  throw error;
35168
35185
  }
35169
- const txHash = normalizeTxHash(result.tx_response.txhash);
35170
- if (result.tx_response.code !== 0) {
35186
+ const { txHash } = checkTx;
35187
+ if (checkTx.code !== 0) {
35171
35188
  clearCachedSequence(cache);
35172
35189
  throw new TxError({
35173
35190
  txHash,
35174
- code: result.tx_response.code,
35175
- rawLog: result.tx_response.raw_log,
35191
+ code: checkTx.code,
35192
+ rawLog: checkTx.rawLog,
35176
35193
  confirmed: true
35177
35194
  });
35178
35195
  }
@@ -35181,7 +35198,7 @@ async function signAndBroadcastEip712({
35181
35198
  return {
35182
35199
  txHash,
35183
35200
  code: 0,
35184
- rawLog: result.tx_response.raw_log,
35201
+ rawLog: checkTx.rawLog,
35185
35202
  confirmed: false
35186
35203
  };
35187
35204
  }
@@ -35203,7 +35220,7 @@ async function signAndBroadcastEip712({
35203
35220
  throw new TxConfirmationUnavailableError({
35204
35221
  txHash,
35205
35222
  code: deliverResult.code,
35206
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
35223
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
35207
35224
  confirmed: void 0
35208
35225
  });
35209
35226
  }
@@ -38230,19 +38247,13 @@ async function signAndBroadcastAgentTx({
38230
38247
  authInfoBytes,
38231
38248
  signatures: [signatureBytes]
38232
38249
  });
38233
- const txBytesBase64 = bytesToBase64(txRawBytes);
38234
- const result = await postJson(
38235
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
38236
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
38237
- fetchOptions,
38238
- fetchFn
38239
- );
38240
- const txHash = normalizeTxHash(result.tx_response.txhash);
38241
- if (result.tx_response.code !== 0) {
38250
+ const checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
38251
+ const { txHash } = checkTx;
38252
+ if (checkTx.code !== 0) {
38242
38253
  throw new TxError({
38243
38254
  txHash,
38244
- code: result.tx_response.code,
38245
- rawLog: result.tx_response.raw_log,
38255
+ code: checkTx.code,
38256
+ rawLog: checkTx.rawLog,
38246
38257
  confirmed: true
38247
38258
  });
38248
38259
  }
@@ -38250,7 +38261,7 @@ async function signAndBroadcastAgentTx({
38250
38261
  return {
38251
38262
  txHash,
38252
38263
  code: 0,
38253
- rawLog: result.tx_response.raw_log,
38264
+ rawLog: checkTx.rawLog,
38254
38265
  confirmed: false
38255
38266
  };
38256
38267
  }
@@ -38265,7 +38276,7 @@ async function signAndBroadcastAgentTx({
38265
38276
  throw new TxConfirmationUnavailableError({
38266
38277
  txHash,
38267
38278
  code: deliverResult.code,
38268
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
38279
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
38269
38280
  confirmed: void 0
38270
38281
  });
38271
38282
  }
@@ -42330,6 +42341,7 @@ export {
42330
42341
  aultToEvm,
42331
42342
  averagePremium,
42332
42343
  base64ToBytes,
42344
+ broadcastSignedTx,
42333
42345
  buildBorrowMarginMsg,
42334
42346
  buildConditionalOrderMsg,
42335
42347
  buildDepositMarginMsg,