writeContract
Executes a write function on a contract
Executes a write function on a contract
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, encodes the function call using the ABI and delegates to sendTransaction with the ABI-encoded data.
:::warning This internally sends a transaction – it does not validate if the contract write will succeed. It is highly recommended to simulate the contract write first. :::
Import the wallet actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/wallet"See how to construct a wallet client and call this action.
import ( "context" "log"
"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.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ Address: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2", ABI: erc20ABI, FunctionName: "transfer", Args: []any{toAddress, amount},})if err != nil { log.Fatal(err)}log.Printf("Transaction hash: %s", hash)string
The transaction hash as a hex string.
Configuration options accepted by this action.
Control which account is used to send the contract write.
Type: Account
Optional
The account to send from. If nil, uses the client's account.
Specify the contract address that will receive the write.
Type: string
Required
The contract address to call.
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ Address: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2", ABI: erc20ABI, FunctionName: "transfer", Args: []any{toAddress, amount},})Provide the ABI used to encode the contract call.
Type: any ([]byte, string, or *abi.ABI)
Required
The contract ABI as JSON bytes, string, or a pre-parsed *abi.ABI.
import "github.com/ChefBingbong/viem-go/abi"
// Option 1: Pre-parsed ABIparsedABI, _ := abi.Parse([]byte(`[{"type":"function",...}]`))hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ ABI: parsedABI, // ...})
// Option 2: JSON stringhash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ ABI: `[{"type":"function",...}]`, // ...})
// Option 3: JSON byteshash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ ABI: []byte(`[{"type":"function",...}]`), // ...})Choose which function from the ABI to call.
Type: string
Required
The name of the function to call.
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ Address: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2", ABI: erc20ABI, FunctionName: "transfer", Args: []any{toAddress, amount},})Pass the arguments for the selected function.
Type: []any
Optional
The function arguments.
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ Address: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2", ABI: erc20ABI, FunctionName: "transfer", Args: []any{toAddress, amount},})Optionally send ETH alongside the contract write.
Type: *big.Int
Optional
The amount of ETH to send with the transaction.
import "math/big"
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{ Address: "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2", ABI: contractABI, FunctionName: "deposit", Value: big.NewInt(1000000000000000000),})All transaction fields from sendTransaction are supported:
Chain - The target chainAssertChainID - Whether to assert chain ID matchesDataSuffix - Data to append to the end of the calldataGas - Gas limitGasPrice - Gas price (Legacy transactions)MaxFeePerGas - Max fee per gas (EIP-1559)MaxPriorityFeePerGas - Max priority fee per gas (EIP-1559)Nonce - Transaction nonceType - Transaction typeAccessList - Access list (EIP-2930)AuthorizationList - Authorization list (EIP-7702)Blobs - Blobs (EIP-4844)BlobVersionedHashes - Blob versioned hashes (EIP-4844)MaxFeePerBlobGas - Max fee per blob gas (EIP-4844)eth_sendTransaction (with wallet_sendTransaction fallback)eth_sendRawTransaction