fillTransaction
Fills a transaction request with the necessary fields to be signed over
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 the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"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)*FillTransactionReturnType
An object containing:
Raw []byte - The RLP-encoded transaction ready to be signedTransaction *FilledTransaction - The filled transaction with all fields populatedConfiguration options accepted by this action.
*common.AddressThe 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),})*common.AddressThe recipient address.
to := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &to, Value: big.NewInt(1000000000000000000),})[]byteTransaction data.
result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Data: []byte{0xc0, 0x2a, 0xaa, 0x39},})*big.IntThe 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})*uint64The 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,})*uint64The gas limit. If nil, will be estimated.
gas := uint64(21000)result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, Gas: &gas,})*big.IntThe 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,})*big.IntThe 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,})*big.IntThe 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,})*float641.2 (20% buffer)The multiplier for the base fee.
multiplier := 1.5result, err := public.FillTransaction(ctx, publicClient, public.FillTransactionParameters{ Account: &senderAddr, To: &recipientAddr, BaseFeeMultiplier: &multiplier,})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 using getTransactionCount, estimateGas, and getGasPrice/getMaxPriorityFeePerGas.