readContract

Reads state from a deployed smart contract.

You can use raw read calls or read extensions to read from a contract.

Example

Using a contract read extension

import { getContract } from "thirdweb";
import { sepolia } from "thirdweb/chains";
import { useReadContract } from "thirdweb/react";
import { getOwnedNFTs } form "thirdweb/extensions/erc721";
const contract = getContract({
client,
address: "0x...",
chain: sepolia,
});
const { data, isLoading } = useReadContract(getOwnedNFTs, { contract, owner: address });

Using a raw contract 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],
});

Using resolveMethod

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.