Listen Contract Events
Subscribe to any contract event with auto-polling hooks and type-safe event handlers. Supports all common standards out of the box
Code
Preview
Subscribe to any contract event with auto-polling hooks and type-safe event handlers. Supports all common standards out of the box
import { useContractEvents } from "thirdweb/react";
import { getContract } from "thirdweb";
import { base } from "thirdweb/chains";
import { transferEvent } from "thirdweb/extensions/erc20";
const usdcContractOnBase = getContract({
address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
chain: base,
client,
});
function App() {
// Listen to USDC transfers on Base
const contractEvents = useContractEvents({
contract: usdcContractOnBase,
events: [transferEvent()],
blockRange: 100,
});
(contractEvents.data || []).forEach((item) => {
const { from, to, value } = item.args;
console.log("{from}...{value} USDC...{to}");
});
}