readContract

Reads state from a deployed smart contract.

Use this for raw read calls from a contract, but you can also use read extensions for predefined methods for common standards.

Example

Raw contract call (recommended)

You can read from any contract by using the solidity signature of the function you want to call.

import { getContract } from "thirdweb";
import { sepolia } from "thirdweb/chains";
import { useReadContract } from "thirdweb/react";
const contract = getContract({
client,
address: "0x...",
chain: sepolia,
});
const { data, isLoading } = useReadContract({
contract,
method: "function tokenURI(uint256 tokenId) returns (string)",
params: [1n],
});

Note that this is type safe, the params types will be enforced based on the signature.

Raw contract call with resolveMethod

If you don't have the solidity signature of the function you want to call, you can use the resolveMethod helper to resolve the method from any deployed contract.

Note that this is not type safe, and will also have a 1 time overhead of resolving the contract ABI.

import { getContract, resolveMethod } from "thirdweb";
import { sepolia } from "thirdweb/chains";
import { useReadContract } from "thirdweb/react";
const contract = getContract({
client,
address: "0x...",
chain: sepolia,
});
const { data, isLoading } = useReadContract({
contract,
method: resolveMethod("tokenURI"),
params: [1n],
});
function readContract(
options: ReadContractOptions<TAbi, TMethod, TPreparedMethod>,
): Promise<ReadContractResult<TPreparedMethod[2]>>;

Parameters

The transaction options.

Type

let options: ReadContractOptions<TAbi, TMethod, TPreparedMethod>;

Returns

let returnType: outputs extends { length: 0 }
? never
: outputs extends { length: 1 }
? AbiParametersToPrimitiveTypes<outputs>[0]
: AbiParametersToPrimitiveTypes<outputs>;

A promise that resolves with the result of the read call.