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 ERC20 value:

For transactions that transfer ERC20 tokens, you can specify the value as the amount of tokens to transfer.

You can use this in conjuction with the getApprovalForTransaction function to easily create approval transactions for ERC20 tokens.

This value will also be read by the react hooks and UI components to present to total cost to the user.

import { prepareContractCall } from "thirdweb";
import { toWei } from "thirdweb/utils";
const transaction = prepareContractCall({
contract,
method: "function payWithCoin()",
params: [],
erc20Value: {
tokenAddress: "0x...", // the address of the ERC20 token
amountWei: toWei("0.1"), // the amount of tokens to transfer in wei
},
});

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 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......."
});
function prepareContractCall(
options: PrepareContractCallOptions<TAbi, TMethod, TPreparedMethod>,
TAbi,
ParseMethod<TAbi, TMethod>,
>;

Parameters

The options for preparing the contract call.

Type

TAbi,
TMethod,
TPreparedMethod
>;

Returns

let returnType: PreparedTransaction<
TAbi,
ParseMethod<TAbi, TMethod>,
>;

A promise that resolves to the prepared transaction.