Best way to listen for events in react-moralis

Both are basically the same. UseWeb3ExecuteFunction is working well for me. It’s just changing/deactivating the button after confirmation of txns that is the problem. And keeping it deactivated on a page refresh. Difference between `useWeb3ExecuteFunction`, `useApiContract`, `executeFunction`, `useWeb3Contract`, and `Moralis.executeFunction`

1 Like

Ok, I got it so far to run funcitons depending onSuccess, onComplete or onError, but the values
of given data like error are undefined. I guess you did the trick with te tx.wait but I cant get it working to assing the useWeb3Contract with a tx and therefore wait.
Could you please tell me how you defined your tx in this example?
I mean with this code the handleError is called, but the if (error) statement results always in the else because error is undefined

const { runContractFunction: enterRaffle, data: enterTxResponse, error, isLoading, isFetching } = useWeb3Contract({

        abi: abi,

        contractAddress: contractAddress,

        functionName: "addAddress",

        params: {

            _addressToBeAdded: account

        },

    }

    );

    const InteractContract = async () => {

        await enterRaffle(

            {

                onSuccess: handleSuccess,

                onComplete: handleComplete,

                //onError: () => { handleError(JSON.stringify(error)) },

                onError: handleError

            }

        )

        if (error) {

            //error occured

            alert(error?.message.substring(error?.message.search('message') + 10, error?.message.search('data') - 3))

        } else {

            alert("success")

        }

    }
1 Like

Hello michimich^^. Just kidding, will answer my own question, because I just figured it out.

for listening on the tx and therefure execute a wait it could be done like this:

const tx = await enterRaffle(
            {
                onSuccess: (tx) => tx.wait(1).then(handleSuccess),
                onComplete: handleComplete,
                //             //onError: () => { handleError(JSON.stringify(error)) },
                onError: handleError,
            }
        )

so then the handleSuccess is only called if the transaction was successfull. This works fine for me, will ad the same on the other handles as well.

Also I figured out, that a require in my contract can lead to a instant response with the error message, will make that clear with some screenshots if I have them.
Hope this could help others

So onSuccess is when the transaction goes through, which is nice, what about when the transaction gets at least 1 block confirmation? Does the API work for that too?

Added a new thread here: Best way to wait for a transaction to finish

Oh I’m idiot, you do await tx.wait(1).

Exactly what I said above XD

FYI for anyone it might be helpful to (as things are often use case dependent) - you can also just chain the event

eg.

runContractFunction(params)
  .then(data => {
      //do something with data
  }).catch(err =>{ 
      //handle error
  })
1 Like

hi, please the “enterTxResponse” in your code references to what if i was to use usdt has my contract?

Hello there,

I think I just linked a variable on my side to work with the the given data then (man this code doesn´t look pretty good).
In general (Patrick mentioned this at the beginning of this thread):

data: The response of running the contract function

I can´t remember exactly, because I changed it to be more clear. After the mint() function ran I go on and handle the result with either an error message or a success messag. This is what I came up with (if this helps you):

const { runContractFunction: mint } = useWeb3Contract({
        abi: abi,
        contractAddress: process.env.REACT_APP_NFT_CONTRACT_ADDRESS,
        functionName: "mint"
    }
    );

//calling the mint function by using the useWeb3Contract()
await mint({
        params: options,
        onSuccess: (tx) => (handleSuccess(tx)),
        onError: (tx) => (handleError(tx)),
    });

//handle success

const handleSuccess = async (tx) => {
    await tx.wait(1);
    console.log("success entered")
    console.log("tx", tx);
    return ['success', tx];
}


//deal with an occured error
const handleError = async (tx) => {
    var createdErrorMessage;
    console.log("tx from interaction", tx);
    if (tx.error !== undefined) {
        createdErrorMessage = tx.error.message;
        console.log("filtered error message", createdErrorMessage);
    }
    else if (tx.message !== undefined) {
        //tx not fired, could be user cancel transaction
        createdErrorMessage = tx.message;
        console.log("tx message", tx.createdErrorMessage);
    }
    else {
        createdErrorMessage = "undefined error occured";
    }
    console.log('error', createdErrorMessage, "Error");
    return createdErrorMessage;
}

Greetings