viem-goviem-go

estimateGas

Estimates the gas necessary to complete a transaction without submitting it to the network

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 construct a public client and estimate gas for a transaction.

import (
"context"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ChefBingbong/viem-go/actions/public"
"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() }()
contractAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")
calldata := common.FromHex("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
senderAddr := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
})
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.

Account

  • Type: *common.Address
  • Optional

The account attached to the transaction (msg.sender).

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ChefBingbong/viem-go/actions/public"
)
senderAddr := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
})

To

  • Type: *common.Address
  • Optional

The recipient address. If nil, this is treated as a deployment transaction.

contractAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
})

Data

  • Type: []byte
  • Optional

The calldata to send.

calldata := common.FromHex("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
})

Value

  • Type: *big.Int
  • Optional

The amount of wei to send.

import "math/big"
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
Value: big.NewInt(1000000000000000000), // 1 ETH
})

Gas

  • Type: *uint64
  • Optional

The gas limit for the transaction.

gasLimit := uint64(21000)
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
Gas: &gasLimit,
})

MaxFeePerGas

  • Type: *big.Int
  • Optional

The max fee per gas (EIP-1559).

import "math/big"
maxFeePerGas := big.NewInt(20000000000) // 20 gwei
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
MaxFeePerGas: maxFeePerGas,
})

MaxPriorityFeePerGas

  • Type: *big.Int
  • Optional

The max priority fee per gas (EIP-1559).

import "math/big"
maxPriorityFeePerGas := big.NewInt(2000000000) // 2 gwei
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
MaxPriorityFeePerGas: maxPriorityFeePerGas,
})

BlockNumber

  • Type: *uint64
  • Optional

The block number to estimate at.

blockNum := uint64(12345)
gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
BlockNumber: &blockNum,
})

BlockTag

  • Type: BlockTag
  • Optional

The block tag to estimate at (e.g., "latest", "pending").

gas, err := public.EstimateGas(ctx, publicClient, public.EstimateGasParameters{
Account: &senderAddr,
To: &contractAddr,
Data: calldata,
BlockTag: public.BlockTagSafe,
})

StateOverride

  • Type: types.StateOverride
  • Optional

State overrides for the estimation.

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_estimateGas