Skip to content
Cive

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, writeContract uses a Wallet Client to call the sendTransaction action with ABI-encoded data.

import { account, publicClient, walletClient } from "./config";
import { Abi } from "./abi";
 
const { request } = await publicClient.simulateContract({
  account,
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
});
await walletClient.writeContract(request);

Passing Arguments

If your function requires argument(s), you can pass them through with the args attribute.

TypeScript types for args will be inferred from the function name & ABI, to guard you from inserting the wrong values.

For example, the mint function name below requires a tokenId argument, and it is typed as [number].

import { account, walletClient } from "./client";
import { Abi } from "./abi";
 
const { request } = await publicClient.simulateContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  account,
});
await walletClient.writeContract(request);

Standalone

If you don't need to perform validation on the contract write, you can also use it by itself:

import { account, walletClient } from "./config";
import { Abi } from "./abi";
 
await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  account,
});

Return Value

Hash

A Transaction Hash.

Unlike readContract, writeContract only returns a Transaction Hash. If you would like to retrieve the return data of a write function, you can use the simulateContract action – this action does not execute a transaction, and does not require gas (it is very similar to readContract).

Parameters

address

The contract address.

await walletClient.writeContract({
  address: "cfx:...", 
  abi: Abi,
  functionName: "mint",
  account,
});

abi

The contract's ABI.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi, 
  functionName: "mint",
  account,
});

functionName

  • Type: string

A function to extract from the ABI.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint", 
  account,
});

account

  • Type: Account | Address

The Account to simulate the contract method from.

Accepts a Local Account (Private Key, etc).

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  account, 
});

accessList (optional)

The access list.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  accessList: [
    {
      address: "cfx:...",
      storageKeys: ["0x1"],
    },
  ],
  account,
});

args (optional)

  • Type: Inferred from ABI.

Arguments to pass to function call.

await walletClient.writeContract({
  address: "cfx:..",
  abi: Abi,
  functionName: "balanceOf",
  args: ["cfx:..."], 
  account,
});

epochNumber (optional)

  • Type: number

The block number to perform the read against.

await walletClient.writeContract({
  address: "cfx",
  abi: Abi,
  functionName: "mint",
  account,
  epochNumber: 15121123n, 
});

epochTag (optional)

  • Type: latest_state | latest_confirmed | latest_checkpoint | earliest

  • Defaut: latest_state

The block number to perform the read against.

 await walletClient.writeContract({
  address: "cfx",
  abi: Abi,
  functionName: "mint",
  account,
  epochTag: "latest_state",, 
});

gas (optional)

  • Type: bigint

The gas limit for the transaction.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  gas: 69420n, 
});

gasPrice (optional)

  • Type: bigint

The price (in drop) to pay per gas. Only applies to Legacy Transactions.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  accoun,
  gasPrice: parseGDrip("20"), 
});

maxFeePerGas (optional)

  • Type: bigint

Total fee per gas (in drop), inclusive of maxPriorityFeePerGas. Only applies to CIP-1559 Transactions

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  account,
  maxFeePerGas: parseGDrip("20"), 
});

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas (in drop). Only applies to EIP-1559 Transactions

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  account,
  maxFeePerGas: parseGDrip("20"),
  maxPriorityFeePerGas: parseGDrip("2"), 
});

nonce (optional)

  • Type: number

Unique number identifying this transaction.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  account,
  nonce: 69, 
});

value (optional)

  • Type: number

Value in wei sent with this transaction.

await walletClient.writeContract({
  address: "cfx:...",
  abi: Abi,
  functionName: "mint",
  args: [69420],
  account,
  value: parseGDrip("1"), 
});