prepareTransactionRequest
Prepares a transaction request for signing
Import
Import the wallet actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/wallet"Usage
See how to construct a wallet client and call this action.
import ( "context" "log" "math/big"
"github.com/ChefBingbong/viem-go/actions/wallet" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
ctx := context.Background()
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = walletClient.Close() }()
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})if err != nil { log.Fatal(err)}log.Printf("Prepared transaction: %+v", prepared)Returns
*PrepareTransactionRequestParameters
The fully-prepared transaction request with all required fields populated (nonce, chainId, gas, fees, type).
Parameters
Configuration options accepted by this action.
Account
-
Type:
Account -
Optional
The account to prepare the transaction for. If nil, uses the client's account.
import "github.com/ChefBingbong/viem-go/accounts"
account, err := accounts.PrivateKeyToAccount("0x...")if err != nil { log.Fatal(err)}
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ Account: account, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Parameters
-
Type:
[]string -
Default:
["blobVersionedHashes", "chainId", "fees", "gas", "nonce", "type"] -
Optional
Parameters to prepare. For instance, if ["gas", "nonce"] is provided, then only the gas and nonce parameters will be prepared.
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ Parameters: []string{"gas", "nonce"}, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})To
-
Type:
string -
Optional
The transaction recipient or contract address.
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Value
-
Type:
*big.Int -
Optional
Value in wei sent with this transaction.
import "math/big"
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})Data
-
Type:
string -
Optional
A contract hashed method call with encoded args.
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ Data: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Gas
-
Type:
*big.Int -
Optional
The gas limit of the transaction. If missing and gas is in Parameters, it will be estimated.
import "math/big"
prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ Gas: big.NewInt(21000), To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})GasPrice
-
Type:
*big.Int -
Optional
The price (in wei) to pay per gas. Only applies to Legacy Transactions.
MaxFeePerGas
-
Type:
*big.Int -
Optional
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions. If missing and fees is in Parameters, it will be estimated.
MaxPriorityFeePerGas
-
Type:
*big.Int -
Optional
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions. If missing and fees is in Parameters, it will be estimated.
Nonce
-
Type:
*int -
Optional
Unique number identifying this transaction. If missing and nonce is in Parameters, it will be fetched from the network.
nonce := 69prepared, err := wallet.PrepareTransactionRequest(ctx, walletClient, wallet.PrepareTransactionRequestParameters{ Nonce: &nonce, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Type
-
Type:
formatters.TransactionType -
Optional
The transaction type. Can be "legacy", "eip2930", "eip1559", "eip4844", or "eip7702". If missing and type is in Parameters, it will be inferred from the network or transaction fields.
ChainID
-
Type:
*int64 -
Optional
The chain ID. If missing and chainId is in Parameters, it will be fetched from the client's chain or via eth_chainId.
Chain
-
Type:
*chain.Chain -
Optional
The target chain. Used to infer chain ID and transaction type.
AccessList
-
Type:
[]formatters.AccessListItem -
Optional
The access list for EIP-2930 transactions.
AuthorizationList
-
Type:
[]transaction.SignedAuthorization -
Optional
Signed EIP-7702 Authorization list.
Blobs
-
Type:
[]string -
Optional
Blobs for Blob Transactions (EIP-4844).
BlobVersionedHashes
-
Type:
[]string -
Optional
Blob versioned hashes for Blob Transactions (EIP-4844).
MaxFeePerBlobGas
-
Type:
*big.Int -
Optional
Max fee per blob gas for Blob Transactions (EIP-4844).