Created
November 14, 2024 19:39
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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