Skip to content

writeContract

Action for writing data to a Starknet contract.

Import

import { writeContract } from "starkweb/core";

Usage

index.ts
import { writeContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20"; 
import { config } from "./config";
 
const hash = await writeContract(config, {
  address: "0x123...", 
  abi: erc20Abi, 
  functionName: "transfer", 
  args: ["0x456...", "1000"], 
});

Parameters

import { type WriteContractParameters } from "starkweb/core";

address

Address

The address of the contract.

abi

Abi

The ABI of the contract.

functionName

string

The name of the function to call.

args (optional)

readonly unknown[]

The arguments to pass to the function.

index.ts
import { writeContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20"; 
import { config } from "./config";
 
const hash = await writeContract(config, {
  address: "0x123...",
  abi: erc20Abi, 
  functionName: "transfer",
  args: ["0x456...", "1000"], 
});

chainId (optional)

Hex | undefined

The chain ID to send the transaction to.

index.ts
import { writeContract } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
const hash = await writeContract(config, {
  address: "0x123...",
  abi: erc20Abi,
  functionName: "transfer",
  chainId: mainnet.chain_id, 
});

connector (optional)

Connector | undefined

The connector to use for the transaction. If not provided, uses the current connector.

Return Type

import { type WriteContractReturnType } from "starkweb/core";

Hex

The transaction hash.

Example

example.ts
import { writeContract } from "starkweb/core";
import { erc20Abi } from "./abis/erc20"; 
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
// Transfer tokens
const hash = await writeContract(config, {
  address: "0x123...",
  abi: erc20Abi, 
  functionName: "transfer",
  args: ["0x456...", "1000"], 
});
console.log("Transaction hash:", hash);
 
// Write to specific chain
const approveHash = await writeContract(config, {
  address: "0x123...",
  abi: erc20Abi,
  functionName: "approve",
  args: ["0x789...", "1000"],
  chainId: mainnet.chain_id, 
});

Error

import { type WriteContractErrorType } from "starkweb/core";

TanStack Query

import {
  type WriteContractData,
  type WriteContractVariables,
  type WriteContractMutate,
  type WriteContractMutateAsync,
  writeContractMutationOptions,
} from "starkweb/query";