Skip to main content

estimateTransactionGas

Estimates the total gas cost for a transaction in wei. This provides the unsponsored gas estimate, useful for showing users the potential cost before sponsorship is applied.

Usage

import { estimateTransactionGas } from "@dynamic-labs-sdk/zerodev";
import { isEvmWalletAccount } from "@dynamic-labs-sdk/evm";
import { getPrimaryWalletAccount } from "@dynamic-labs-sdk/client";
import { parseEther, formatEther } from "viem";

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isEvmWalletAccount(walletAccount)) {
  const gasEstimate = await estimateTransactionGas({
    walletAccount,
    transaction: {
      to: recipientAddress,
      value: parseEther("0.01"),
      data: "0x",
    },
  });

  if (gasEstimate) {
    console.log("Estimated gas cost:", formatEther(gasEstimate), "ETH");
  }
}

Parameters

ParameterTypeDescription
transaction.toHexThe recipient address
transaction.valuebigintThe value to send in wei
transaction.dataHex (optional)The transaction data
walletAccountEvmWalletAccountThe wallet account that will send the transaction

Returns

Promise<bigint | null> - The estimated gas cost in wei, or null if estimation fails.