Interacting With Contracts

After initializing the SDK on a given chain, you can obtain instances of a target contract by their address, read state from it and execute transactions on it on behalf of the connected wallet.

Getting a Contract Instance

To get a contract instance, you only need the contract's address. The contract ABI will be automatically resolved from the onchain contract metadata.

const sdk = new ThirdwebSDK("sepolia");
const contract = await sdk.getContract("0x...");

You can still optionally specify the ABI as a second argument to skip the automatic resolution.

const ABI = [...];
const contract = await sdk.getContract("0x...", ABI);

This will return an instance of SmartContract which you can use to interact with the contract.

Reading State

Calling generic view functions on your contract can be done via the call function. The return type will match the return type of the contract function.

const result = await"myViewFunction", [arg1, arg2]);

Executing Transactions

Executing transactions on your contract can also be done via the call function. The return type will always be a TransactionResult containing the transaction receipt.

By default, this will wait till the transaction is fully mined onchain.

const result = await"myTransactionFunction", [
console.log("Executed transaction:", result.receipt.transationHash);

You can also optionally specify transaction overrides as the third argument.

const result = await"myTransactionFunction", [arg1, arg2], {
value: toWei(0.1),
gasLimit: 1000000,
nonce: 0,

Preparing Transactions

You can also prepare a transaction without executing it. This will return a Transaction object that you can use to estimate, simulate, sign, send and execute the transaction yourself.

const tx = await contract.prepare("myTransactionFunction", [
const encoded = await tx.encode(); // Encode the transaction
const gasCost = await tx.estimateGasCost(); // Estimate the gas cost
const simulatedTx = await tx.simulate(); // Simulate the transaction
const signedTx = await tx.sign(); // Sign the transaction for later use
// Submit the transaction, but don't wait for confirmations
const sentTx = await tx.send();
console.log("Submitted transaction:", sentTx.hash);

Listening to Events

You can retrieve and listen to events emitted by your contract by using the namespace.

Here's how to get past events emitted by your contract. Note that passing a block range is highly recommended.

// Optionally pass in filters to limit the blocks from which events are retrieved
const filters = {
fromBlock: -2000,
toBlock: "latest",
// get All events
const events = await;
// or get a particular event type
const events = await"Transfer", filters);

And how to listen to new events emitted by your contract in real time: => {
console.log(event.eventName) // the name of the emitted event
console.log( // event payload
// or listen to a particular event type"Transfer", (event) => {