viem-goviem-go

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.

Internally, this uses the EstimateGas action with ABI-encoded calldata.

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

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 ABI
erc20ABI, 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)

Returns

uint64

The estimated gas amount in units of gas.

Parameters

Configuration options accepted by this action.

Address

  • Type: common.Address
  • Required

The 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

  • Type: *abi.ABI
  • Required

The 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)},
})

FunctionName

  • Type: string
  • Required

The 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)},
})

Args

  • Type: []any
  • Optional

The 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)},
})

Account

  • Type: *common.Address
  • Optional

The 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)},
})

Value

  • Type: *big.Int
  • Optional

The 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
})

BlockNumber

  • Type: *uint64
  • Optional

The 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,
})

BlockTag

  • Type: BlockTag
  • Optional

The 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,
})

StateOverride

  • Type: types.StateOverride
  • Optional

State overrides for the estimation.

DataSuffix

  • Type: []byte
  • Optional

Optional data to append to the end of the calldata. Useful for adding a "domain" tag.

JSON-RPC Method

Underlying JSON-RPC method used by this action.

Uses eth_estimateGas with ABI-encoded data.