Reading contract state

The recommended way to read the 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.

Reading contract events

The recommended way to read the contract events is to use the getContractEvents function and passing a prepared event with the Solidity event signature and the params. This is type-safe based on the Solidity event signature you define. You can get your desired contract event signature from the solidity code directly.

import { getContractEvents, prepareEvent } from "thirdweb";
const myEvent = prepareEvent({
signature:
"event Transfer(address indexed from, address indexed to, uint256 value)",
});
const events = await getContractEvents({
contract: myContract,
events: [myEvent],
blockRange: 1000,
});

Generating all read functions and events for a deployed contract

Using the CLI, you can generate optimized functions for all the possible calls to a contract. This saves you time and precomputes all the necessary encoding.

npx thirdweb generate <contractId>/<contractAddress>

Read more on how to generate extension functions using the CLI.