ault-sdk-ts 0.16.1 → 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/README.md CHANGED
@@ -1018,7 +1018,7 @@ interface TxResult {
1018
1018
  txHash: string; // Transaction hash
1019
1019
  code: number; // Result code (0 = success)
1020
1020
  rawLog?: string; // Raw log from transaction
1021
- confirmed?: boolean; // true = DeliverTx observed; false = broadcast-only or poll timeout; undefined = confirmation lookup unavailable
1021
+ confirmed?: boolean; // true = DeliverTx observed; false = broadcast-only or poll timeout; undefined = outcome unknown
1022
1022
  success: boolean; // True only when DeliverTx success was confirmed
1023
1023
  }
1024
1024
  ```
@@ -1355,7 +1355,7 @@ Note: `pnpm gen:eip712` also reads Cosmos SDK protos (staking/distribution) and
1355
1355
  ## Error Handling
1356
1356
 
1357
1357
  ```typescript
1358
- import { ApiError, NetworkError, TimeoutError } from "ault-sdk-ts";
1358
+ import { ApiError, NetworkError, TimeoutError, TxConfirmationUnavailableError } from "ault-sdk-ts";
1359
1359
 
1360
1360
  try {
1361
1361
  const result = await client.transferLicense({ licenseId: 1, to: "0x..." });
@@ -1371,7 +1371,9 @@ try {
1371
1371
  console.error(`Transaction failed: ${result.rawLog}`);
1372
1372
  }
1373
1373
  } catch (error) {
1374
- if (error instanceof ApiError) {
1374
+ if (error instanceof TxConfirmationUnavailableError) {
1375
+ console.warn(`Outcome unknown, look the transaction up first: ${error.txHash}`);
1376
+ } else if (error instanceof ApiError) {
1375
1377
  console.error(`API Error (${error.status}): ${error.message}`);
1376
1378
  } else if (error instanceof NetworkError) {
1377
1379
  console.error(`Network Error: ${error.message}`);
@@ -1381,6 +1383,13 @@ try {
1381
1383
  }
1382
1384
  ```
1383
1385
 
1386
+ A send can also end with the outcome unknown. If the broadcast response is lost
1387
+ or unreadable, or the node cannot answer the confirmation lookup, the SDK
1388
+ throws `TxConfirmationUnavailableError`. The chain may have accepted the
1389
+ transaction, so look it up by `error.txHash` before you sign and send it again.
1390
+ A `TxNotConfirmedError` means the broadcast was accepted but the transaction
1391
+ did not show up in a block before the timeout.
1392
+
1384
1393
  ## Advanced Usage
1385
1394
 
1386
1395
  For advanced use cases, you can access the low-level client:
@@ -1,6 +1,6 @@
1
1
  import { type AccountCache } from "../eip712/sign-and-broadcast";
2
2
  import type { AnyEip712Msg } from "../eip712/builder";
3
- import { type FetchFn, type FetchWithRetryOptions } from "../core/http";
3
+ import type { FetchFn, FetchWithRetryOptions } from "../core/http";
4
4
  import type { NetworkConfig } from "../core/network";
5
5
  export declare const AGENT_DEFAULTS: {
6
6
  readonly GAS_LIMIT: "186000";
@@ -22,7 +22,7 @@ export interface TxErrorParams {
22
22
  /**
23
23
  * - `true` -- chain returned a definitive non-zero code (CheckTx or DeliverTx).
24
24
  * - `false` -- broadcast accepted but inclusion never observed within the timeout.
25
- * - `undefined` -- broadcast accepted but the confirmation lookup endpoint failed.
25
+ * - `undefined` -- broadcast or confirmation response unavailable; acceptance is unknown.
26
26
  */
27
27
  confirmed: boolean | undefined;
28
28
  }
@@ -60,8 +60,9 @@ export declare class TxNotConfirmedError extends TxError {
60
60
  constructor(params: TxErrorParams);
61
61
  }
62
62
  /**
63
- * Thrown when a tx was broadcast but the confirmation lookup endpoint was
64
- * unreachable or returned an unrecognized payload. Distinct from
63
+ * Thrown when the signed broadcast response or the confirmation lookup was
64
+ * unavailable or returned an unrecognized payload. The tx may or may not have
65
+ * been accepted; `txHash` is always set so it can be looked up. Distinct from
65
66
  * `TxNotConfirmedError` so callers can distinguish "still pending" from
66
67
  * "we cannot tell at all".
67
68
  */
@@ -15,6 +15,21 @@ export interface AccountCache {
15
15
  nextSequence: string | null;
16
16
  }
17
17
  export declare function createAccountCache(): AccountCache;
18
+ /**
19
+ * Posts signed tx bytes and returns the CheckTx result.
20
+ *
21
+ * The request is never retried: once the bytes are signed, a lost or
22
+ * unreadable response does not mean the chain rejected them. Any failure to
23
+ * get a recognizable response throws `TxConfirmationUnavailableError` carrying
24
+ * the hash computed from the signed bytes, so the caller can look the tx up.
25
+ *
26
+ * @internal
27
+ */
28
+ export declare function broadcastSignedTx(network: NetworkConfig, txRawBytes: Uint8Array, fetchFn?: FetchFn, fetchOptions?: FetchWithRetryOptions): Promise<{
29
+ txHash: string;
30
+ code: number;
31
+ rawLog?: string;
32
+ }>;
18
33
  /** @internal Retained for compatibility; polling is now stateless. */
19
34
  export declare function _resetIndexingCache(): void;
20
35
  /**
package/dist/index.cjs CHANGED
@@ -281,6 +281,7 @@ __export(index_exports, {
281
281
  aultToEvm: () => aultToEvm,
282
282
  averagePremium: () => averagePremium,
283
283
  base64ToBytes: () => base64ToBytes,
284
+ broadcastSignedTx: () => broadcastSignedTx,
284
285
  buildBorrowMarginMsg: () => buildBorrowMarginMsg,
285
286
  buildConditionalOrderMsg: () => buildConditionalOrderMsg,
286
287
  buildDepositMarginMsg: () => buildDepositMarginMsg,
@@ -635,7 +636,7 @@ __export(index_exports, {
635
636
  module.exports = __toCommonJS(index_exports);
636
637
 
637
638
  // src/core/user-agent.ts
638
- var SDK_VERSION = "0.16.1";
639
+ var SDK_VERSION = "0.17.0";
639
640
  var USER_AGENT = `ault-sdk-ts/${SDK_VERSION}`;
640
641
  function isServerRuntime() {
641
642
  if (typeof document !== "undefined") return false;
@@ -35593,6 +35594,34 @@ function getConfirmationUnavailableLog(txHash, error) {
35593
35594
  }
35594
35595
  return `Transaction ${txHash} was broadcast but could not be confirmed.`;
35595
35596
  }
35597
+ async function broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions) {
35598
+ const txHash = normalizeTxHash((0, import_viem5.sha256)(txRawBytes));
35599
+ try {
35600
+ const result = await postJson(
35601
+ `${network.restUrl}/cosmos/tx/v1beta1/txs`,
35602
+ { tx_bytes: bytesToBase64(txRawBytes), mode: "BROADCAST_MODE_SYNC" },
35603
+ { ...fetchOptions, retries: 0 },
35604
+ fetchFn
35605
+ );
35606
+ const response = result?.tx_response;
35607
+ const code = response?.code;
35608
+ const rawHash = response?.txhash;
35609
+ if (typeof code !== "number" || !Number.isSafeInteger(code) || code < 0 || typeof rawHash !== "string" || !rawHash) {
35610
+ throw new Error("Unrecognized broadcast response");
35611
+ }
35612
+ if (normalizeTxHash(rawHash) !== txHash) {
35613
+ throw new Error("Broadcast response is for a different transaction");
35614
+ }
35615
+ return { txHash, code, rawLog: response?.raw_log };
35616
+ } catch (error) {
35617
+ throw new TxConfirmationUnavailableError({
35618
+ txHash,
35619
+ code: -1,
35620
+ rawLog: error instanceof Error ? error.message : "Broadcast response unavailable",
35621
+ confirmed: void 0
35622
+ });
35623
+ }
35624
+ }
35596
35625
  function _resetIndexingCache() {
35597
35626
  }
35598
35627
  async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs = TIMING_CONSTANTS.TX_POLL_TIMEOUT_MS, intervalMs = 1500) {
@@ -35609,7 +35638,8 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
35609
35638
  { ...fetchOptions, retries: 0, timeout: perRequestTimeout },
35610
35639
  fetchFn
35611
35640
  );
35612
- if (typeof result.tx_response?.code !== "number") {
35641
+ const receipt = result?.tx_response;
35642
+ 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) {
35613
35643
  return {
35614
35644
  code: -1,
35615
35645
  rawLog: `Transaction ${txHash} was broadcast but the confirmation response was invalid.`,
@@ -35617,9 +35647,9 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
35617
35647
  };
35618
35648
  }
35619
35649
  return {
35620
- code: result.tx_response.code,
35621
- rawLog: result.tx_response.raw_log,
35622
- data: result.tx_response.data,
35650
+ code: receipt.code,
35651
+ rawLog: receipt.raw_log,
35652
+ data: receipt.data,
35623
35653
  confirmed: true
35624
35654
  };
35625
35655
  } catch (error) {
@@ -35777,26 +35807,20 @@ async function signAndBroadcastEip712({
35777
35807
  authInfoBytes,
35778
35808
  signatures: [sigWithoutRecovery]
35779
35809
  });
35780
- const txBytesBase64 = bytesToBase64(txRawBytes);
35781
- let result;
35810
+ let checkTx;
35782
35811
  try {
35783
- result = await postJson(
35784
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
35785
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
35786
- { ...fetchOptions, retries: 0 },
35787
- fetchFn
35788
- );
35812
+ checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
35789
35813
  } catch (error) {
35790
35814
  clearCachedSequence(cache);
35791
35815
  throw error;
35792
35816
  }
35793
- const txHash = normalizeTxHash(result.tx_response.txhash);
35794
- if (result.tx_response.code !== 0) {
35817
+ const { txHash } = checkTx;
35818
+ if (checkTx.code !== 0) {
35795
35819
  clearCachedSequence(cache);
35796
35820
  throw new TxError({
35797
35821
  txHash,
35798
- code: result.tx_response.code,
35799
- rawLog: result.tx_response.raw_log,
35822
+ code: checkTx.code,
35823
+ rawLog: checkTx.rawLog,
35800
35824
  confirmed: true
35801
35825
  });
35802
35826
  }
@@ -35805,7 +35829,7 @@ async function signAndBroadcastEip712({
35805
35829
  return {
35806
35830
  txHash,
35807
35831
  code: 0,
35808
- rawLog: result.tx_response.raw_log,
35832
+ rawLog: checkTx.rawLog,
35809
35833
  confirmed: false
35810
35834
  };
35811
35835
  }
@@ -35827,7 +35851,7 @@ async function signAndBroadcastEip712({
35827
35851
  throw new TxConfirmationUnavailableError({
35828
35852
  txHash,
35829
35853
  code: deliverResult.code,
35830
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
35854
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
35831
35855
  confirmed: void 0
35832
35856
  });
35833
35857
  }
@@ -38854,19 +38878,13 @@ async function signAndBroadcastAgentTx({
38854
38878
  authInfoBytes,
38855
38879
  signatures: [signatureBytes]
38856
38880
  });
38857
- const txBytesBase64 = bytesToBase64(txRawBytes);
38858
- const result = await postJson(
38859
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
38860
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
38861
- fetchOptions,
38862
- fetchFn
38863
- );
38864
- const txHash = normalizeTxHash(result.tx_response.txhash);
38865
- if (result.tx_response.code !== 0) {
38881
+ const checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
38882
+ const { txHash } = checkTx;
38883
+ if (checkTx.code !== 0) {
38866
38884
  throw new TxError({
38867
38885
  txHash,
38868
- code: result.tx_response.code,
38869
- rawLog: result.tx_response.raw_log,
38886
+ code: checkTx.code,
38887
+ rawLog: checkTx.rawLog,
38870
38888
  confirmed: true
38871
38889
  });
38872
38890
  }
@@ -38874,7 +38892,7 @@ async function signAndBroadcastAgentTx({
38874
38892
  return {
38875
38893
  txHash,
38876
38894
  code: 0,
38877
- rawLog: result.tx_response.raw_log,
38895
+ rawLog: checkTx.rawLog,
38878
38896
  confirmed: false
38879
38897
  };
38880
38898
  }
@@ -38889,7 +38907,7 @@ async function signAndBroadcastAgentTx({
38889
38907
  throw new TxConfirmationUnavailableError({
38890
38908
  txHash,
38891
38909
  code: deliverResult.code,
38892
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
38910
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
38893
38911
  confirmed: void 0
38894
38912
  });
38895
38913
  }
@@ -42955,6 +42973,7 @@ function createDexWs(url, options) {
42955
42973
  aultToEvm,
42956
42974
  averagePremium,
42957
42975
  base64ToBytes,
42976
+ broadcastSignedTx,
42958
42977
  buildBorrowMarginMsg,
42959
42978
  buildConditionalOrderMsg,
42960
42979
  buildDepositMarginMsg,