sendTransaction
Creates, signs, and sends a new transaction to the network
- For JSON-RPC accounts (or when no local signer is available), sends via
eth_sendTransaction. - For local accounts (implementing TransactionSignableAccount), prepares, signs locally, and sends via
eth_sendRawTransaction.
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() }()
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})if err != nil { log.Fatal(err)}log.Printf("Transaction hash: %s", hash)Account Hoisting
If you do not wish to pass an account to every sendTransaction, you can also hoist the Account on the Wallet Client.
import ( "github.com/ChefBingbong/viem-go/accounts" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
account, err := accounts.PrivateKeyToAccount("0x...")if err != nil { log.Fatal(err)}
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{ Account: account, Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000), // Account is automatically used from the client})Returns
string
The transaction hash as a hex string.
Parameters
Configuration options accepted by this action.
Account
-
Type:
Account -
Optional
The account to send from. If nil, uses the client's account.
Accepts a JSON-RPC Account or Local Account.
import ( "github.com/ChefBingbong/viem-go/accounts" "github.com/ChefBingbong/viem-go/actions/wallet")
account, err := accounts.PrivateKeyToAccount("0x...")if err != nil { log.Fatal(err)}
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Account: account, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})To
Specify the recipient or contract address for the transaction.
-
Type:
string -
Optional
The transaction recipient or contract address.
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})Value
Set the amount of ETH (in wei) to send with the transaction.
-
Type:
*big.Int -
Optional
Value in wei sent with this transaction.
import "math/big"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000),})Data
Provide calldata for contract method invocations.
-
Type:
string -
Optional
A contract hashed method call with encoded args.
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Data: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Gas
Control the gas limit used for this transaction.
-
Type:
*big.Int -
Optional
The gas limit of the transaction. If missing, it will be estimated.
import "math/big"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Gas: big.NewInt(21000), To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})GasPrice
Configure the gas price for legacy transactions.
- Type:
*big.Int
The price (in wei) to pay per gas. Only applies to Legacy Transactions.
import "math/big"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ GasPrice: big.NewInt(20000000000), To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})MaxFeePerGas
-
Type:
*big.Int -
Optional
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions.
import "math/big"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ MaxFeePerGas: big.NewInt(20000000000), To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})MaxPriorityFeePerGas
Configure the priority fee per gas for EIP-1559 transactions.
-
Type:
*big.Int -
Optional
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions.
import "math/big"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ MaxFeePerGas: big.NewInt(20000000000), MaxPriorityFeePerGas: big.NewInt(1000000000), To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Nonce
Override the nonce used for this transaction.
-
Type:
*int -
Optional
Unique number identifying this transaction.
nonce := 69hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Nonce: &nonce, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Type
Select the transaction type to use.
-
Type:
formatters.TransactionType -
Optional
The transaction type. Can be "legacy", "eip2930", "eip1559", "eip4844", or "eip7702".
import "github.com/ChefBingbong/viem-go/utils/formatters"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Type: formatters.TransactionTypeEIP1559, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})AccessList
-
Type:
[]formatters.AccessListItem -
Optional
The access list for EIP-2930 transactions.
import "github.com/ChefBingbong/viem-go/utils/formatters"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ AccessList: []formatters.AccessListItem{ { Address: "0x1", StorageKeys: []string{"0x1"}, }, }, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})AuthorizationList
Attach an authorization list for EIP-7702 transactions.
-
Type:
[]transaction.SignedAuthorization -
Optional
Signed EIP-7702 Authorization list.
import "github.com/ChefBingbong/viem-go/utils/transaction"
authorization, err := wallet.SignAuthorization(ctx, walletClient, wallet.SignAuthorizationParameters{ ContractAddress: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",})
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ AuthorizationList: []transaction.SignedAuthorization{authorization}, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})Blobs
-
Type:
[]string -
Optional
Blobs for Blob Transactions (EIP-4844).
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Blobs: []string{"0x..."}, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})BlobVersionedHashes
Provide blob versioned hashes for EIP-4844 transactions.
-
Type:
[]string -
Optional
Blob versioned hashes for Blob Transactions (EIP-4844).
MaxFeePerBlobGas
Configure the max fee per blob gas for EIP-4844 transactions.
- Type:
*big.Int
Max fee per blob gas for Blob Transactions (EIP-4844).
- Optional
Chain
- Type:
*chain.Chain
The target chain. If there is a mismatch between the wallet's current chain & the target chain, an error will be thrown.
import "github.com/ChefBingbong/viem-go/chain/definitions"
hash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ Chain: definitions.Optimism, To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",})AssertChainID
-
Type:
*bool -
Default:
true -
Optional
When true, asserts the chain ID matches. Default: true.
DataSuffix
Append additional data to the end of the calldata.
- Type:
string - Optional
Data to append to the end of the calldata. Takes precedence over client.DataSuffix().
JSON-RPC Methods
-
The underlying JSON-RPC methods invoked by this action.
-
JSON-RPC Accounts:
eth_sendTransaction(withwallet_sendTransactionfallback) -
Local Accounts:
eth_sendRawTransaction