[SOLVED] Reading Events from useWeb3Contract

Hi,

I’m using useWeb3Contract() to call a function in a contract that emits an event called CoinFlipped. I am trying to get that event back to make some state changes on my front end. I’ve read that it should be possible to get it back in the transaction result in onSuccess.


    const { runContractFunction: flipCoin, isFetching } = useWeb3Contract({
        contractAddress: contractAddress,
        abi: abi,
        functionName: "flipCoin",
        params: { playerCoinFaceSelection: coinFaceSelection },
        msgValue: ethers.utils.parseEther(wager).toString(),
    })

    const handleFlipCoinSuccess = async (tx: ContractTransaction) => {
        await tx.wait(1)
        console.log(tx)
    }

     onClick={async function () {
         await flipCoin({
              onSuccess: (tx) => handleFlipCoinSuccess(tx as ContractTransaction),
          })
       }}

The call works - the solidity function is called and the following is logged as the tx in handleFlipCoinSuccess:

{
    "hash": "0x276086328572a2d24d4aeb70be4345ba7d9cd1575b3f58985b3fca377f19557a",
    "type": 2,
    "accessList": null,
    "blockHash": null,
    "blockNumber": null,
    "transactionIndex": null,
    "confirmations": 0,
    "from": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    "gasPrice": {
        "type": "BigNumber",
        "hex": "0x90a0d517"
    },
    "maxPriorityFeePerGas": {
        "type": "BigNumber",
        "hex": "0x59682f00"
    },
    "maxFeePerGas": {
        "type": "BigNumber",
        "hex": "0x90a0d517"
    },
    "gasLimit": {
        "type": "BigNumber",
        "hex": "0x01bad4d8"
    },
    "to": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
    "value": {
        "type": "BigNumber",
        "hex": "0x0de0b6b3a7640000"
    },
    "nonce": 2,
    "data": "0x6afa82ea0000000000000000000000000000000000000000000000000000000000000000",
    "r": "0xd974198d24feaace44b2facb13aebad6393fdf3be57707b84d2d992d56cf9f03",
    "s": "0x1eec8963cd760ff72a98233b53806dad323476e0d3b52c68f8172dffc9863602",
    "v": 1,
    "creates": null,
    "chainId": 0
}

This does not seem to return events.
Am I missing something?
Thanks!

this looks like the info associated with the sent transaction, maybe you have to use wait to get the receipt for that transaction

Thanks for your help! The following worked:

    const handleFlipCoinSuccess = async (tx: ContractTransaction) => {
        await tx.wait(1).then((result) => {
            console.log(result)
        })
    }

1 Like