Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save melanke/eadb664976fa2dd77956d4518bd3b856 to your computer and use it in GitHub Desktop.
Save melanke/eadb664976fa2dd77956d4518bd3b856 to your computer and use it in GitHub Desktop.
Ethereum: How to get a parsed event after writting on a contract (Using Scaffold-eth and Viem)
import React from "react";
import { Abi, parseEventLogs } from "viem";
import { useDeployedContractInfo, useScaffoldWriteContract } from "~~/hooks/scaffold-eth";
export const MyComponent: React.FC = () => {
const { data: contractInfo } = useDeployedContractInfo("MyContract");
const { writeContractAsync } = useScaffoldWriteContract("MyContract");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
await writeContractAsync(
{
functionName: "myFunction",
},
{
onBlockConfirmation: receipt => {
const [myEvent] = parseEventLogs({
abi: contractInfo?.abi as Abi,
eventName: "MyEvent",
logs: receipt.logs,
});
if (myEvent && "myField" in myEvent.args) {
const myField = myEvent.args.myField as bigint;
console.log(myField);
}
},
},
);
};
return (
<form onSubmit={handleSubmit}>
<div>
<button type="submit">
Execute
</button>
</div>
</form>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment