prepareContractCall

Prepares a contract call by resolving the ABI function, parameters and encoded data. Optionally specify other properties such as value or gas price.

Example

Usage with a human-readable method signature:

import { prepareContractCall } from "thirdweb";
const transaction = prepareContractCall({
contract,
method: "function transfer(address to, uint256 value)",
params: [to, value],
});

Usage with explicit gas price and/or value:

import { prepareContractCall } from "thirdweb";
import { toWei } from "thirdweb/utils";
const transaction = prepareContractCall({
contract,
method: "function transfer(address to, uint256 value)",
params: [to, value],
maxFeePerGas: 30n,
maxPriorityFeePerGas: 1n,
value: toWei("0.01"),
});

Usage with a JSON ABI function object:

import { prepareContractCall } from "thirdweb";
const transaction = prepareContractCall({
contract,
method: {
name: "transfer",
type: "function",
inputs: [
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
],
outputs: [],
stateMutability: "payable",
},
params: [to, value],
});

Usage with a the ABI defined on the contract:

import { getContract, prepareContractCall } from "thirdweb";
const contract = getContract({
..., // chain, address, client
abi: [...] // ABI with a "transfer" method
});
const transaction = prepareContractCall({
contract,
method: "transfer", // <- this gets inferred from the contract
params: [to, value],
});

Passing extra call data to the transaction

import { getContract, prepareContractCall } from "thirdweb";
const contract = getContract({
..., // chain, address, client
});
const transaction = prepareContractCall({
contract,
method: "function transfer(address to, uint256 value)",
params: [...],
// The extra call data MUST be encoded to hex before passing
extraCallData: "0x......."
});

Parameters

Returns

A promise that resolves to the prepared transaction.