A Wallet Action is an action that maps one-to-one with a "wallet" or "signable" Ethereum RPC method (eth_requestAccounts, eth_sendTransaction, personal_sign, etc). They are used with a Wallet Client.
Wallet Actions require special permissions and provide signing capabilities to the user. Examples of Wallet Actions include sending a transaction, signing a message, writing to a contract, and retrieving account addresses.
Wallet Actions provide a secure and flexible way to access the user's accounts and perform actions on the Ethereum network. They are commonly used by dapps and other applications that need to execute transactions, interact with smart contracts, or sign messages.
Wallet Actions work with two types of accounts:
Wallet Actions in viem-go are organized into logical groups:
All Wallet Actions follow a consistent pattern:
context.Context as the first parameter for cancellation and timeoutsClient as the second parameterParameters struct containing all options (including optional Account)import ( "context" "log" "math/big" "time"
"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, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()
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() }()
// Example: Send a transactionhash, 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)If you do not wish to pass an account to every Wallet Action, you can hoist the Account on the Wallet Client. This is especially useful when using a single account throughout your application.
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"),})
// Now you can call actions without passing accounthash, err := wallet.SendTransaction(ctx, walletClient, wallet.SendTransactionParameters{ To: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", Value: big.NewInt(1000000000000000000), // Account is automatically used from the client})All Wallet Actions return errors that should be checked. Common error patterns include:
Always handle errors appropriately in your application, especially user rejection errors which are common when interacting with external wallets.