ault-sdk-ts 0.7.0 → 0.7.2

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
@@ -265,15 +265,16 @@ dropped silently and the embedded baseline keeps applying.
265
265
  The keys most consumers will touch are the CLOB ones plus the fee-free
266
266
  multiplier:
267
267
 
268
- | Key | Used by |
269
- | ------------------------- | ---------------------------------------- |
270
- | `FEE_FREE_GAS_MULTIPLIER` | fee-free gas computation across all msgs |
271
- | `LIMIT_ORDER` | `placeLimitOrder` |
272
- | `MARKET_ORDER` | `placeMarketOrder` |
273
- | `SCALE_ORDER_BASE` | `placeScaleOrder` (base cost) |
274
- | `SCALE_ORDER_PER_SUB` | `placeScaleOrder` (per sub-order) |
275
- | `CANCEL_ORDER_BASE` | `cancelOrders` (base cost) |
276
- | `CANCEL_ORDER_PER_ID` | `cancelOrders` (per order id) |
268
+ | Key | Used by |
269
+ | -------------------------- | ------------------------------------------ |
270
+ | `FEE_FREE_GAS_MULTIPLIER` | fee-free gas computation across all msgs |
271
+ | `SIMULATED_GAS_MULTIPLIER` | headroom over simulated gas (staking msgs) |
272
+ | `LIMIT_ORDER` | `placeLimitOrder` |
273
+ | `MARKET_ORDER` | `placeMarketOrder` |
274
+ | `SCALE_ORDER_BASE` | `placeScaleOrder` (base cost) |
275
+ | `SCALE_ORDER_PER_SUB` | `placeScaleOrder` (per sub-order) |
276
+ | `CANCEL_ORDER_BASE` | `cancelOrders` (base cost) |
277
+ | `CANCEL_ORDER_PER_ID` | `cancelOrders` (per order id) |
277
278
 
278
279
  The schema also accepts per-message gas constants for the license,
279
280
  miner, agent, reward, and accountx modules (`PER_LICENSE`,
@@ -580,9 +581,15 @@ const ws = createDexWs("wss://my-dex-api.example.com/ws", {
580
581
  reconnectDelay: 1000, // default: 1000ms
581
582
  maxReconnectDelay: 30000, // default: 30000ms
582
583
  maxReconnectAttempts: Infinity, // default: Infinity
584
+ userAgent: "my-bot/1.0", // default: ault-sdk-ts/<version>; Node and Bun only
583
585
  });
584
586
  ```
585
587
 
588
+ The DEX API rejects handshakes that carry no `User-Agent`. Browsers send their
589
+ own. Under Node or Bun the SDK sends `ault-sdk-ts/<version>`, or whatever you
590
+ put in `userAgent`. If you pass your own `createWebSocket` factory you are on
591
+ your own for the header.
592
+
586
593
  ### Disconnecting
587
594
 
588
595
  ```typescript
@@ -855,6 +862,26 @@ await client.delegateMining({
855
862
  });
856
863
  ```
857
864
 
865
+ ### Staking gas
866
+
867
+ Cosmos staking messages (`delegate`, `undelegate`, `redelegate`,
868
+ `withdrawRewards`) are metered on execution. What a redelegation costs
869
+ depends on the delegator's state, for example whether a delegation to the
870
+ destination already exists or how many redelegation entries are open, so
871
+ no fixed number is right for every account. These methods simulate the tx
872
+ on the chain first (`POST /cosmos/tx/v1beta1/simulate`) and sign
873
+ `gas_used × SIMULATED_GAS_MULTIPLIER / 100` as the gas limit (1.3× by
874
+ default, overridable through the manifest). The fee is computed from that
875
+ gas.
876
+
877
+ If the chain rejects the simulated tx, the call throws `TxSimulationError`
878
+ with the chain's message. Nothing is signed or broadcast, so you don't pay
879
+ a fee for a tx that would have failed in the block. If the simulate
880
+ endpoint is down or a proxy doesn't route it, the method logs a warning
881
+ and falls back to the embedded per-message constant (`DELEGATE`,
882
+ `BEGIN_REDELEGATE`, ...) times `FEE_FREE_GAS_MULTIPLIER`. Passing an
883
+ explicit `gasLimit` skips the simulation.
884
+
858
885
  ## Agent-Signed Transactions
859
886
 
860
887
  The chain's `agent` module lets a master account approve a separate keypair to
@@ -38,6 +38,19 @@ export declare class TxError extends Error {
38
38
  confirmed: boolean | undefined;
39
39
  constructor(params: TxErrorParams, message?: string);
40
40
  }
41
+ /**
42
+ * The chain rejected the tx during gas simulation. Nothing was signed,
43
+ * broadcast or charged. `rawLog` is the chain's message, `chainCode` the gRPC
44
+ * code.
45
+ */
46
+ export declare class TxSimulationError extends Error {
47
+ chainCode?: number;
48
+ rawLog: string;
49
+ constructor(params: {
50
+ rawLog: string;
51
+ chainCode?: number;
52
+ });
53
+ }
41
54
  /**
42
55
  * Thrown when a tx was broadcast but block inclusion could not be confirmed
43
56
  * within the deadline. The tx may still commit later — callers can re-query
@@ -8,7 +8,17 @@ export interface FetchWithRetryOptions {
8
8
  retryableMethods?: string[];
9
9
  retryableStatuses?: number[];
10
10
  retryOnTimeout?: boolean;
11
+ /**
12
+ * `User-Agent` sent on every request under Node or Bun (default:
13
+ * `ault-sdk-ts/<version>`). A `User-Agent` already present in the request
14
+ * headers wins. Browsers are left alone: they send their own, and a custom
15
+ * one would only force a CORS preflight. The hosted DEX API sits behind a
16
+ * WAF that rejects requests without one.
17
+ */
18
+ userAgent?: string;
11
19
  }
20
+ /** Statuses retried by default when the method is retryable. */
21
+ export declare const DEFAULT_RETRYABLE_STATUSES: readonly number[];
12
22
  export declare function fetchWithRetry(url: string, options?: RequestInit, fetchOptions?: FetchWithRetryOptions, fetchFn?: FetchFn): Promise<Response>;
13
23
  export declare function fetchJson<T>(url: string, options?: RequestInit, fetchOptions?: FetchWithRetryOptions, fetchFn?: FetchFn): Promise<T>;
14
24
  export declare function postJson<T, R>(url: string, data: T, fetchOptions?: FetchWithRetryOptions, fetchFn?: FetchFn): Promise<R>;
@@ -9,6 +9,7 @@ import { z } from "zod";
9
9
  */
10
10
  export declare const ManifestGasSchema: z.ZodObject<{
11
11
  FEE_FREE_GAS_MULTIPLIER: z.ZodOptional<z.ZodNumber>;
12
+ SIMULATED_GAS_MULTIPLIER: z.ZodOptional<z.ZodNumber>;
12
13
  PER_LICENSE: z.ZodOptional<z.ZodNumber>;
13
14
  PER_KYC_MEMBER: z.ZodOptional<z.ZodNumber>;
14
15
  PER_DELEGATE_MINING: z.ZodOptional<z.ZodNumber>;
@@ -62,6 +63,7 @@ export declare const ManifestSchema: z.ZodObject<{
62
63
  schemaVersion: z.ZodNumber;
63
64
  gas: z.ZodOptional<z.ZodObject<{
64
65
  FEE_FREE_GAS_MULTIPLIER: z.ZodOptional<z.ZodNumber>;
66
+ SIMULATED_GAS_MULTIPLIER: z.ZodOptional<z.ZodNumber>;
65
67
  PER_LICENSE: z.ZodOptional<z.ZodNumber>;
66
68
  PER_KYC_MEMBER: z.ZodOptional<z.ZodNumber>;
67
69
  PER_DELEGATE_MINING: z.ZodOptional<z.ZodNumber>;
@@ -40,6 +40,8 @@ export declare const GAS_CONSTANTS: {
40
40
  readonly DENOM: "aault";
41
41
  /** Chain multiplier applied to min gas to derive max gas (120 = 1.2×). */
42
42
  readonly FEE_FREE_GAS_MULTIPLIER: 120;
43
+ /** Headroom over simulated `gas_used` (130 = 1.3×). */
44
+ readonly SIMULATED_GAS_MULTIPLIER: 130;
43
45
  readonly PER_LICENSE: 200000;
44
46
  readonly PER_KYC_MEMBER: 200000;
45
47
  readonly PER_DELEGATE_MINING: 200000;
@@ -61,10 +63,12 @@ export declare const GAS_CONSTANTS: {
61
63
  readonly MODIFY_ORDER_PER_ID: 130000;
62
64
  readonly OPEN_ACCOUNT: 96000;
63
65
  readonly DELEGATE: 220000;
64
- readonly UNDELEGATE: 220000;
65
- readonly BEGIN_REDELEGATE: 280000;
66
+ readonly UNDELEGATE: 260000;
67
+ readonly BEGIN_REDELEGATE: 400000;
66
68
  readonly WITHDRAW_DELEGATOR_REWARD: 220000;
67
69
  };
70
+ /** Gas limit to sign for a simulated `gas_used` (percent multiplier, floored). */
71
+ export declare function computeSimulatedGas(gasUsed: bigint, multiplier?: number): string;
68
72
  /**
69
73
  * Returns the per-tx gas allowance for a uniform-cost batch (in wire-format
70
74
  * integer string). The chain interprets `gas` as an unsigned bigint, so the
@@ -0,0 +1,15 @@
1
+ export declare const SDK_VERSION: string;
2
+ /**
3
+ * Default `User-Agent` sent on every REST request and WebSocket handshake
4
+ * when running under Node or Bun. The hosted DEX API sits behind a WAF that
5
+ * rejects requests without one. Browsers always send their own, and setting
6
+ * it there would only add a CORS preflight, so the SDK leaves them alone.
7
+ */
8
+ export declare const USER_AGENT: string;
9
+ /**
10
+ * True under Node or Bun, where the SDK owns the `User-Agent`: `fetch` accepts
11
+ * the header and `WebSocket` accepts `{ headers }` as its second argument.
12
+ * False in browsers (which send their own and would preflight on a custom
13
+ * one) and Deno (whose `WebSocket` only takes a protocol list).
14
+ */
15
+ export declare function isServerRuntime(): boolean;
@@ -46,6 +46,15 @@ export interface SignAndBroadcastParams {
46
46
  feeDenom?: string;
47
47
  feeAmount?: string;
48
48
  confirmationTimeoutMs?: number;
49
+ /**
50
+ * Simulate before signing and sign `gas_used × multiplier / 100`; `gasLimit`
51
+ * becomes the fallback. A chain rejection throws `TxSimulationError` before
52
+ * anything is signed. Staking only: fee-free msgs must keep the chain's
53
+ * fixed gas window.
54
+ */
55
+ simulateGas?: boolean;
56
+ /** Percent headroom over the simulated gas; default from GAS_CONSTANTS. */
57
+ simulatedGasMultiplier?: number;
49
58
  /**
50
59
  * When `true` (default), poll for DeliverTx after a successful CheckTx and
51
60
  * return the committed result (throwing on any non-success). When `false`,
@@ -76,4 +85,4 @@ export interface SignAndBroadcastResult {
76
85
  data?: string;
77
86
  confirmed: boolean;
78
87
  }
79
- export declare function signAndBroadcastEip712({ network, signerAddress, signer, msgs, memo, gasLimit, feeDenom, feeAmount, confirmationTimeoutMs, wait, fetchFn, fetchOptions, cache, feeMarketCache, }: SignAndBroadcastParams): Promise<SignAndBroadcastResult>;
88
+ export declare function signAndBroadcastEip712({ network, signerAddress, signer, msgs, memo, gasLimit, feeDenom, feeAmount, confirmationTimeoutMs, simulateGas, simulatedGasMultiplier, wait, fetchFn, fetchOptions, cache, feeMarketCache, }: SignAndBroadcastParams): Promise<SignAndBroadcastResult>;
@@ -0,0 +1,41 @@
1
+ import { type FetchFn, type FetchWithRetryOptions } from "../core/http";
2
+ import type { NetworkConfig } from "../core/network";
3
+ import type { AnyEip712Msg } from "./builder";
4
+ /**
5
+ * secp256k1 generator point. Used when the account has no pubkey on chain yet;
6
+ * simulate skips the address/signature checks, so any ethsecp256k1 key gives
7
+ * the same gas.
8
+ */
9
+ export declare const SIMULATION_PLACEHOLDER_PUBKEY: Uint8Array<ArrayBuffer>;
10
+ export interface SimulateTxParams {
11
+ network: NetworkConfig;
12
+ msgs: AnyEip712Msg[];
13
+ memo?: string;
14
+ /** Checked by the chain even in simulate mode. */
15
+ sequence: string;
16
+ /** `null` uses the placeholder pubkey. */
17
+ pubkeyBase64: string | null;
18
+ /** Ignored by the simulate gas meter, but must be non-zero for the fee message to be emitted. */
19
+ placeholderGasLimit: string;
20
+ /**
21
+ * Without a fee coin the ante skips the fee deduction (~19k gas) and the msg
22
+ * runs against a pre-fee balance.
23
+ */
24
+ placeholderFee: {
25
+ denom: string;
26
+ amount: string;
27
+ };
28
+ fetchFn?: FetchFn;
29
+ fetchOptions?: FetchWithRetryOptions;
30
+ }
31
+ export interface SimulateTxResult {
32
+ gasUsed: bigint;
33
+ }
34
+ /**
35
+ * Simulates `msgs` on chain and returns `gas_used`.
36
+ *
37
+ * Throws {@link TxSimulationError} when the chain rejects the tx. Returns
38
+ * `null` (after a warning) when the endpoint is unavailable, so the caller can
39
+ * fall back to a static gas limit.
40
+ */
41
+ export declare function simulateTx({ network, msgs, memo, sequence, pubkeyBase64, placeholderGasLimit, placeholderFee, fetchFn, fetchOptions, }: SimulateTxParams): Promise<SimulateTxResult | null>;
@@ -7,11 +7,12 @@ import { type FeeMarketCache } from "../rest/feemarket";
7
7
  import type { TxOptions, TxResult } from "./types";
8
8
  /**
9
9
  * Internal exec options: the public {@link TxOptions} plus the per-call `wait`
10
- * flag. `wait` is not part of `TxOptions` — only the CLOB order methods expose
11
- * it, and they thread it through here.
10
+ * flag (CLOB order methods) and the gas-simulation knobs (staking builders only).
12
11
  */
13
12
  type ExecOptions = TxOptions & {
14
13
  wait?: boolean;
14
+ simulateGas?: boolean;
15
+ simulatedGasMultiplier?: number;
15
16
  };
16
17
  interface TxExecutorOptions {
17
18
  /**