Skip to main content

useScaffoldEventSubscriber

Use this hook to subscribe to events emitted by your smart contract, and receive real-time updates when these events are emitted.

useScaffoldEventSubscriber({
contractName: "YourContract",
eventName: "GreetingChange",
// The listener function is called whenever a GreetingChange event is emitted by the contract.
// Parameters emitted by the event can be destructed using the below example
// for this example: event GreetingChange(address greetingSetter, string newGreeting, bool premium, uint256 value);
listener: logs => {
logs.map(log => {
const { greetingSetter, value, premium, newGreeting } = log.args;
console.log("๐Ÿ“ก GreetingChange event", greetingSetter, value, premium, newGreeting);
});
},
});

This example subscribes to the GreetingChange event emitted by the YourContract smart contract and logs the parameters from the event to the console when it's emitted.

This hook is a wrapper around wagmi's useContractEvent.

Configurationโ€‹

ParameterTypeDescription
contractNamestringName of the contract to read from.
eventNamestringName of the event to read.
listenerfunctionCallback function to execute when the event is emitted. Accepts an array of logs that occurred during the pollingInterval set at scaffold.config.ts. Each array item contains an args property, which can be destructured to get the parameters emitted by the event. This function can customized according to your needs.
note

It is recommended to setState using updater function in the listener function to avoid problems due to caching.