Docs

Transactions

Transactions are the primary way to read and write to the blockchain.

Reading contract state

The recommended way to read contract state is to use the readContract function and pass the Solidity method signature and the params. This is type safe based on the Solidity method signature you define. You can get your desired contract method signature from the solidity code directly.

import { readContract } from "thirdweb";
const balance = await readContract({
contract: contract,
method: "function balanceOf(address) view returns (uint256)",
params: ["0x123..."],
});

This will execute the read immediately and return the result from the blockchain.

Preparing a contract call

The recommended way to prepare a contract call is to pass the Solidity method signature and the params. This is type safe based on the Solidity method signature you define. You can get your desired contract method signature from the solidity code directly.

import { prepareContractCall, toWei } from "thirdweb";
const tx = prepareContractCall({
contract,
// pass the method signature that you want to call
method: "function mintTo(address to, uint256 amount)",
// and the params for that method
// their types are automatically inferred based on the method signature
params: ["0x123...", toWei("100")],
});

This will return a prepared transaction object that is ready to be sent to the blockchain and can be acted on in various ways.

Preparing a raw transaction

You can also create a raw transaction directly. This is useful when you want to send ether to an address for example, or when you already have encoded data.

import { prepareTransaction, toWei } from "thirdweb";
const transaction = prepareTransaction({
// the account that will be the receiver
to: "0x456...",
// the value is the amount of ether you want to send with the transaction
value: toWei("1"),
});

Acting on a prepared transaction

Transactions have a variety of actions that can be called on them, in all cases this is done by passing the transaction to the various action functions.

Send the prepared transaction to the blockchain. Sending a transaction requires a wallet.

See Wallets for more information on how to create a wallet.

import { sendTransaction } from "thirdweb";
import { metamaskWallet } from "thirdweb/wallets";
const wallet = metamaskWallet();
await wallet.connect();
const transactionResult = await sendTransaction({
transaction,
wallet,
});

Other ways to prepare a transaction

There are 3 other ways to prepare a contract call, all of these return the same transaction object.