estimateContractGas
Estimates the gas required to successfully execute a contract write function call
Estimates the gas required to successfully execute a contract write function call
Estimates the gas required to successfully execute a contract write function call.
Internally, this uses the EstimateGas action with ABI-encoded calldata.
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"An example showing how to estimate gas for a specific contract write function using its ABI.
import ( "context" "log" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public" "github.com/ChefBingbong/viem-go/abi" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
ctx := context.Background()
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = publicClient.Close() }()
// Parse ERC20 ABIerc20ABI, err := abi.ParseABI(`[{"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"type":"function"}]`)if err != nil { log.Fatal(err)}
tokenAddr := common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")senderAddr := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")recipientAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")
gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Account: &senderAddr, Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})if err != nil { log.Fatal(err)}log.Printf("Estimated gas: %d", gas)uint64
The estimated gas amount in units of gas.
Configuration options accepted by this action.
common.AddressThe contract address.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
tokenAddr := common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})*abi.ABIThe contract ABI.
import "github.com/ChefBingbong/viem-go/abi"
erc20ABI, err := abi.ParseABI(`[{"inputs":[...],"name":"transfer",...}]`)gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})stringThe name of the function to call.
gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})[]anyThe function arguments.
import "math/big"
gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})*common.AddressThe account attached to the call (msg.sender).
senderAddr := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Account: &senderAddr, Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)},})*big.IntThe amount of wei to send with the call.
import "math/big"
gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)}, Value: big.NewInt(1000000000000000000), // 1 ETH})*uint64The block number to estimate at.
blockNum := uint64(12345)gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)}, BlockNumber: &blockNum,})BlockTagThe block tag to estimate at (e.g., "latest", "pending").
gas, err := public.EstimateContractGas(ctx, publicClient, public.EstimateContractGasParameters{ Address: tokenAddr, ABI: erc20ABI, FunctionName: "transfer", Args: []any{recipientAddr, big.NewInt(1000000)}, BlockTag: public.BlockTagSafe,})types.StateOverrideState overrides for the estimation.
[]byteOptional data to append to the end of the calldata. Useful for adding a "domain" tag.
Underlying JSON-RPC method used by this action.
Uses eth_estimateGas with ABI-encoded data.