fillTransaction
Fills a transaction request with the necessary fields to be signed over
The function fills in missing fields like nonce, gas, gasPrice/maxFeePerGas, and returns both the raw transaction data and the filled transaction object.
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 fill a transaction before signing.
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() }()
senderAddr := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")recipientAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")
result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Value: big.NewInt(1000000000000000000), // 1 ETH})if err != nil { log.Fatal(err)}log.Printf("Nonce: %d, Gas: %d", result.Transaction.Nonce, result.Transaction.Gas)Returns
*FillTransactionReturnType
An object containing:
Raw []byte- The RLP-encoded transaction ready to be signedTransaction *FilledTransaction- The filled transaction with all fields populated
Parameters
Configuration options accepted by this action.
Account
- Type:
*common.Address - Optional
The account to fill the transaction for (msg.sender).
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
account := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &account, To: &recipientAddr, Value: big.NewInt(1000000000000000000),})To
- Type:
*common.Address - Optional
The recipient address.
to := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &to, Value: big.NewInt(1000000000000000000),})Data
- Type:
[]byte - Optional
Transaction data.
result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Data: []byte{0xc0, 0x2a, 0xaa, 0x39},})Value
- Type:
*big.Int - Optional
The amount of wei to send.
import "math/big"
result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Value: big.NewInt(1000000000000000000), // 1 ETH})Nonce
- Type:
*uint64 - Optional
The nonce for the transaction. If nil, will be filled by the RPC.
nonce := uint64(42)result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Nonce: &nonce,})Gas
- Type:
*uint64 - Optional
The gas limit. If nil, will be estimated.
gas := uint64(21000)result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Gas: &gas,})GasPrice
- Type:
*big.Int - Optional
The gas price (legacy transactions). If nil, will be estimated.
import "math/big"
gasPrice := big.NewInt(20000000000) // 20 gweiresult, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, GasPrice: gasPrice,})MaxFeePerGas
- Type:
*big.Int - Optional
The max fee per gas (EIP-1559). If nil, will be estimated.
import "math/big"
maxFeePerGas := big.NewInt(20000000000) // 20 gweiresult, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, MaxFeePerGas: maxFeePerGas,})MaxPriorityFeePerGas
- Type:
*big.Int - Optional
The max priority fee per gas (EIP-1559). If nil, will be estimated.
import "math/big"
maxPriorityFeePerGas := big.NewInt(2000000000) // 2 gweiresult, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, MaxPriorityFeePerGas: maxPriorityFeePerGas,})BaseFeeMultiplier
- Type:
*float64 - Default:
1.2(20% buffer) - Optional
The multiplier for the base fee.
multiplier := 1.5result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, BaseFeeMultiplier: &multiplier,})Notes
- Not all Ethereum nodes support
eth_fillTransaction. This is primarily supported by Geth-based nodes. If your node doesn't support this method, you may need to manually fill the transaction usinggetTransactionCount,estimateGas, andgetGasPrice/getMaxPriorityFeePerGas.