Estimate Gas

estimateGas estimates the gas cost of a raw (unsigned) transaction before execution. The typical flow is:

  1. Call any service method with raw: true to get an unsigned transaction payload.

  2. Pass that payload to estimateGas to get a chain-specific gas estimate.

estimateGas is available as an instance method on three services — all delegate to SpokeService.estimateGas internally:

  • sodax.swaps.estimateGas(params) — for swap / intent transactions

  • sodax.moneyMarket.estimateGas(params) — for money market transactions

  • sodax.spoke.estimateGas(params) — for any raw spoke transaction (approvals, deposits, etc.)


Method signature

All three methods share the same signature:

public async estimateGas<C extends SpokeChainKey>(
  params: EstimateGasParams<C>,
): Promise<Result<GetEstimateGasReturnType<C>>>

EstimateGasParams<C> is defined in packages/sdk/src/shared/types/spoke-types.ts:

export type EstimateGasParams<C extends SpokeChainKey> = {
  tx: TxReturnType<C, true>; // the raw tx payload from a raw: true call
  chainKey: C;               // the chain key the transaction is for
};

Return type: GetEstimateGasReturnType<C>

The return type is conditional on the source chain's chain family. The concrete type per chain family is:

Chain family
Type
Notes

EVM (Ethereum, Arbitrum, Base, BSC, Optimism, Polygon, Avalanche, HyperEVM, Lightlink, Redbelly, Kaia)

bigint (EvmGasEstimate)

Gas units

EVM hub (Sonic)

bigint (EvmGasEstimate)

Gas units

Solana

number | undefined (SolanaGasEstimate)

Compute units; undefined if simulation unavailable

Stellar

bigint (StellarGasEstimate)

Fee in stroops

ICON

bigint (IconGasEstimate)

Step count

Sui

SuiGasEstimate

Object with computationCost, storageCost, storageRebate, nonRefundableStorageFee (all string)

Injective

InjectiveGasEstimate

Object with gasWanted: number and gasUsed: number

Bitcoin

bigint (BitcoinGasEstimate)

Fee in satoshis

NEAR

bigint (NearGasEstimate)

Gas in yoctoNEAR

Stacks

FeeEstimateTransaction

Object with low, medium, high tiers, each { fee: number, fee_rate: number }

The full type definitions are in packages/types/src/common/common.ts.


raw: true / raw: false — the WalletProviderSlot<K, Raw> constraint

estimateGas always takes a raw transaction, so you must first get one by calling a service method with raw: true. The WalletProviderSlot<K, Raw> type in packages/types/src/common/common.ts enforces this at compile time:

  • raw: truewalletProvider is forbidden (typed as never). The method returns an unsigned payload (TxReturnType<K, true>, e.g. EvmRawTransaction).

  • raw: falsewalletProvider is required and chain-narrowed to GetWalletProviderType<K>. The method broadcasts and returns a tx hash.

Wallet providers are obtained from packages/wallet-sdk-core — never constructed manually in caller code.


ChainKeys.* constants

All chain constants live under ChainKeys.* from @sodax/sdk. There are no separate *_CHAIN_ID exports.


Error handling: Result<T>

Every estimateGas call returns Promise<Result<GetEstimateGasReturnType<C>>> where Result<T> is:

Always check result.ok before accessing result.value.


Examples

Example 1: Estimate gas for a swap intent transaction (EVM spoke)

Example 2: Estimate gas for a money market supply transaction (EVM spoke)

Example 3: Estimate gas for an approval transaction

Example 4: Non-EVM chain — Sui

Example 5: Stacks

Last updated