ault-sdk-ts 0.0.2-alpha.12 → 0.0.2-alpha.13

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.d.ts CHANGED
@@ -28,6 +28,7 @@ export * from "./rest/authority";
28
28
  export * from "./rest/bank";
29
29
  export * from "./rest/clob";
30
30
  export * from "./rest/context";
31
+ export * from "./rest/feemarket";
31
32
  export * from "./rest/license";
32
33
  export * from "./rest/market";
33
34
  export * from "./rest/miner";
package/dist/index.js CHANGED
@@ -13557,6 +13557,67 @@ function buildEip712TypedData(context, msgs, evmChainId) {
13557
13557
  };
13558
13558
  }
13559
13559
 
13560
+ // src/rest/feemarket.ts
13561
+ async function fetchFeeMarketParams(network, fetchFn, fetchOptions) {
13562
+ const data = await fetchJson(
13563
+ `${network.restUrl}/cosmos/evm/feemarket/v1/params`,
13564
+ {},
13565
+ fetchOptions,
13566
+ fetchFn
13567
+ );
13568
+ const raw = data.params ?? {};
13569
+ return {
13570
+ noBaseFee: raw.noBaseFee ?? raw.no_base_fee ?? false,
13571
+ baseFee: raw.baseFee ?? raw.base_fee ?? "0",
13572
+ minGasPrice: raw.minGasPrice ?? raw.min_gas_price ?? "0"
13573
+ };
13574
+ }
13575
+ var ONE_E18 = 1000000000000000000n;
13576
+ function computeRequiredFee(params, gas, options) {
13577
+ const marginPercent = options?.marginPercent ?? 20;
13578
+ if (marginPercent < 0) {
13579
+ throw new Error(`marginPercent must be >= 0, got ${marginPercent}`);
13580
+ }
13581
+ const minGasPriceI = BigInt(cosmosDecToProtoInt(params.minGasPrice || "0"));
13582
+ const baseFeeI = params.noBaseFee ? 0n : BigInt(cosmosDecToProtoInt(params.baseFee || "0"));
13583
+ const effectiveI = baseFeeI > minGasPriceI ? baseFeeI : minGasPriceI;
13584
+ if (effectiveI <= 0n) return "0";
13585
+ const gasI = BigInt(gas);
13586
+ if (gasI <= 0n) return "0";
13587
+ const margin = BigInt(100 + marginPercent);
13588
+ const numerator = effectiveI * gasI * margin;
13589
+ const denominator = ONE_E18 * 100n;
13590
+ return ((numerator + denominator - 1n) / denominator).toString();
13591
+ }
13592
+ var DEFAULT_FEE_MARKET_TTL_MS = 3e3;
13593
+ function createFeeMarketCache(ttlMs = DEFAULT_FEE_MARKET_TTL_MS) {
13594
+ return { ttlMs, entries: /* @__PURE__ */ new Map() };
13595
+ }
13596
+ async function getCachedFeeMarketParams(cache, network, fetchFn, fetchOptions) {
13597
+ const key = network.chainId;
13598
+ const now = Date.now();
13599
+ if (cache) {
13600
+ const existing = cache.entries.get(key);
13601
+ if (existing && now - existing.fetchedAt < cache.ttlMs) {
13602
+ return existing.params;
13603
+ }
13604
+ }
13605
+ let params;
13606
+ try {
13607
+ params = await fetchFeeMarketParams(network, fetchFn, fetchOptions);
13608
+ } catch (error) {
13609
+ if (!isGatewayUnsupportedError(error)) throw error;
13610
+ params = null;
13611
+ }
13612
+ if (cache) {
13613
+ cache.entries.set(key, { params, fetchedAt: now });
13614
+ }
13615
+ return params;
13616
+ }
13617
+ function isGatewayUnsupportedError(error) {
13618
+ return error instanceof ApiError && (error.status === 404 || error.status === 501);
13619
+ }
13620
+
13560
13621
  // src/eip712/signers.ts
13561
13622
  import { privateKeyToAccount } from "viem/accounts";
13562
13623
 
@@ -20853,6 +20914,18 @@ async function queryAccount(network, address, fetchFn, fetchOptions) {
20853
20914
  const pubkey = base.pub_key?.key ?? null;
20854
20915
  return { accountNumber, sequence, pubkeyBase64: pubkey };
20855
20916
  }
20917
+ async function resolveDynamicFeeAmount(network, gas, feeMarketCache, fetchFn, fetchOptions) {
20918
+ const params = await getCachedFeeMarketParams(feeMarketCache, network, fetchFn, fetchOptions);
20919
+ if (!params) return GAS_CONSTANTS.EIP712_FEE_AMOUNT;
20920
+ let dynamicI;
20921
+ try {
20922
+ dynamicI = BigInt(computeRequiredFee(params, gas));
20923
+ } catch {
20924
+ return GAS_CONSTANTS.EIP712_FEE_AMOUNT;
20925
+ }
20926
+ const staticI = BigInt(GAS_CONSTANTS.EIP712_FEE_AMOUNT);
20927
+ return dynamicI > staticI ? dynamicI.toString() : GAS_CONSTANTS.EIP712_FEE_AMOUNT;
20928
+ }
20856
20929
  async function recoverPubkeyFromTypedDataSignature(typedData, signature) {
20857
20930
  const hash = hashTypedData({
20858
20931
  domain: typedData.domain,
@@ -20979,7 +21052,8 @@ async function signAndBroadcastEip712({
20979
21052
  confirmationTimeoutMs,
20980
21053
  fetchFn,
20981
21054
  fetchOptions,
20982
- cache
21055
+ cache,
21056
+ feeMarketCache
20983
21057
  }) {
20984
21058
  const resolvedSigner = normalizeSigner(signer);
20985
21059
  if (!resolvedSigner) {
@@ -21001,14 +21075,16 @@ async function signAndBroadcastEip712({
21001
21075
  if (!sequence) {
21002
21076
  throw new Error(`Unable to resolve account sequence for signer ${resolvedSignerAddress}.`);
21003
21077
  }
21078
+ const resolvedGas = gasLimit ?? network.eip712GasLimit ?? GAS_CONSTANTS.EIP712_GAS_LIMIT;
21079
+ const resolvedFeeAmount = feeAmount ?? network.eip712FeeAmount ?? await resolveDynamicFeeAmount(network, resolvedGas, feeMarketCache, fetchFn, fetchOptions);
21004
21080
  const context = {
21005
21081
  chainId: network.chainId,
21006
21082
  accountNumber,
21007
21083
  sequence,
21008
21084
  fee: {
21009
- amount: feeAmount ?? network.eip712FeeAmount ?? GAS_CONSTANTS.EIP712_FEE_AMOUNT,
21085
+ amount: resolvedFeeAmount,
21010
21086
  denom: feeDenom ?? network.eip712FeeDenom ?? GAS_CONSTANTS.DENOM,
21011
- gas: gasLimit ?? network.eip712GasLimit ?? GAS_CONSTANTS.EIP712_GAS_LIMIT
21087
+ gas: resolvedGas
21012
21088
  },
21013
21089
  memo
21014
21090
  };
@@ -21106,6 +21182,7 @@ async function signAndBroadcastEip712({
21106
21182
  function createAultClient(options) {
21107
21183
  const context = createRestContext(options.network.restUrl, options.fetchFn, options.fetchOptions);
21108
21184
  const eip712Caches = /* @__PURE__ */ new Map();
21185
+ const feeMarketCache = createFeeMarketCache();
21109
21186
  const dexApiUrl = options.network.dexApiUrl ?? "";
21110
21187
  const dexContext = createRestContext(dexApiUrl, options.fetchFn, options.fetchOptions);
21111
21188
  const wsUrl = options.network.dexWsUrl ?? (dexApiUrl ? dexApiUrl.replace(/^http/, "ws").replace(/\/?$/, "/ws") : "");
@@ -21162,6 +21239,7 @@ function createAultClient(options) {
21162
21239
  signer,
21163
21240
  signerAddress,
21164
21241
  cache,
21242
+ feeMarketCache,
21165
21243
  confirmationTimeoutMs: params.confirmationTimeoutMs ?? options.defaultConfirmationTimeoutMs
21166
21244
  });
21167
21245
  },
@@ -22391,6 +22469,7 @@ function createTxExecutor(options) {
22391
22469
  const defaultMemo = options.defaultMemo ?? "";
22392
22470
  const defaultConfirmationTimeoutMs = options.defaultConfirmationTimeoutMs;
22393
22471
  const cache = createAccountCache();
22472
+ const feeMarketCache = createFeeMarketCache();
22394
22473
  const fetchOptions = {
22395
22474
  timeout: TIMING_CONSTANTS.API_TIMEOUT_MS,
22396
22475
  retries: 3,
@@ -22409,7 +22488,8 @@ function createTxExecutor(options) {
22409
22488
  confirmationTimeoutMs: txOptions?.confirmationTimeoutMs ?? defaultConfirmationTimeoutMs,
22410
22489
  fetchFn: options.fetchFn,
22411
22490
  fetchOptions,
22412
- cache
22491
+ cache,
22492
+ feeMarketCache
22413
22493
  });
22414
22494
  return {
22415
22495
  txHash: result.txHash,
@@ -23374,7 +23454,7 @@ function createDexWs(url, options) {
23374
23454
  }
23375
23455
 
23376
23456
  // src/index.ts
23377
- var SDK_VERSION = "0.0.2-alpha.12";
23457
+ var SDK_VERSION = "0.0.2-alpha.13";
23378
23458
  export {
23379
23459
  AGENT_DEFAULTS,
23380
23460
  AULT_SIGNER_MARKER,
@@ -23467,6 +23547,7 @@ export {
23467
23547
  bytesToBase64,
23468
23548
  computeFeeFreeGas,
23469
23549
  computeFeeFreeGasCompound,
23550
+ computeRequiredFee,
23470
23551
  cosmosDecToDecimalString,
23471
23552
  cosmosDecToProtoInt,
23472
23553
  createAccountCache,
@@ -23482,6 +23563,7 @@ export {
23482
23563
  createDexWs,
23483
23564
  createEip1193Signer,
23484
23565
  createEthersSigner,
23566
+ createFeeMarketCache,
23485
23567
  createLicenseApi,
23486
23568
  createMarketApi,
23487
23569
  createMinerApi,
@@ -23508,12 +23590,14 @@ export {
23508
23590
  encodeTxRaw,
23509
23591
  evmToAult,
23510
23592
  fetchAllPaginatedProto,
23593
+ fetchFeeMarketParams,
23511
23594
  fetchJson,
23512
23595
  fetchProtoJson,
23513
23596
  fetchWithRetry,
23514
23597
  getBalanceResponseFreeRegExpOne,
23515
23598
  getBalanceResponseTotalRegExpOne,
23516
23599
  getBalanceResponseUsedRegExpOne,
23600
+ getCachedFeeMarketParams,
23517
23601
  getNetworkConfig,
23518
23602
  getOHLCVQueryLimitDefault,
23519
23603
  getOHLCVQueryLimitMax,