Skip to main content

useScaffoldContract

Use this hook to get your contract instance by providing the contract name. It enables you interact with your contract methods. For reading data or sending transactions, it's recommended to use useScaffoldContractRead and useScaffoldContractWrite.

const { data: yourContract } = useScaffoldContract({
contractName: "YourContract",
});
// Returns the greeting and can be called in any function, unlike useScaffoldContractRead
await yourContract?.read.greeting();

// Used to write to a contract and can be called in any function
import { useWalletClient } from "wagmi";

const { data: walletClient } = useWalletClient();
const { data: yourContract } = useScaffoldContract({
contractName: "YourContract",
walletClient,
});
const setGreeting = async () => {
// Call the method in any function
await yourContract?.write.setGreeting(["the greeting here"]);
};

This example uses the useScaffoldContract hook to obtain a contract instance for the YourContract smart contract.

Configuration

ParameterTypeDescription
contractNamestringName of the contract.
walletClient (optional)WalletClientWallet client must be passed in order to call write methods of the contract

Return Value

  • data : Object representing viem's contract instance. Which can be used to call read and write of the contract.