viem-goviem-go

writeContract

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

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"
"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)

Returns

string

The transaction hash as a hex string.

Parameters

Configuration options accepted by this action.

Account

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.

Address

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},
})

ABI

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 ABI
parsedABI, _ := abi.Parse([]byte(`[{"type":"function",...}]`))
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{
ABI: parsedABI,
// ...
})
// Option 2: JSON string
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{
ABI: `[{"type":"function",...}]`,
// ...
})
// Option 3: JSON bytes
hash, err := wallet.WriteContract(ctx, walletClient, wallet.WriteContractParameters{
ABI: []byte(`[{"type":"function",...}]`),
// ...
})

FunctionName

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},
})

Args

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},
})

Value

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),
})

Transaction Fields

All transaction fields from sendTransaction are supported:

  • Chain - The target chain
  • AssertChainID - Whether to assert chain ID matches
  • DataSuffix - Data to append to the end of the calldata
  • Gas - Gas limit
  • GasPrice - Gas price (Legacy transactions)
  • MaxFeePerGas - Max fee per gas (EIP-1559)
  • MaxPriorityFeePerGas - Max priority fee per gas (EIP-1559)
  • Nonce - Transaction nonce
  • Type - Transaction type
  • AccessList - 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)

JSON-RPC Methods