useWeb3ExecuteFunction onsucces not working

hey Moralis forum,
is there anyone who can tell me why this onsucces doesn’t work?. when fired it instantly returns the console.log which it only should do when the contract interaction is finished. I would want to make the onsucces run as soon as the transaction is finished in metamask.

here is my react code

const contractProcessor = useWeb3ExecuteFunction();
  
  async function caMint() {
    let options = {
      contractAddress: "0x19C15f883E9FE7f2168091567425DaD8ABC7b946",
      functionName: "mint",
      abi: abi,
      params: {
        _mintAmount: 1,
        _merkleProof: [],
      },
      msgValue: Moralis.Units.ETH(0.066 * 1),
    };

    await contractProcessor.fetch({
      params: options,

      onSuccess: (data) => {
        console.log(data);
        console.log("mint done");
      },
      onComplete: () => {
        console.log("done");
      },
      onError: (err) => {
        console.log(err);
      },
    });
  }

maybe try using onComplete but it will be called no matter success or error so you need to check that. As far as I know though, the onSuccess callback should be called only when the transaction is successful

I also tried oncomplete but it calls the console.log right after the function gets called and doesn’t wait on the transaction to complete.

So the onsuccess and oncomplete does the exact same things for me

what is the data that you get there? if has the transaction hash?

it is not easy to get response when the the transaction is finished, sometimes you have to check if the transaction made it on chain, and sometimes it can take minutes depending on the network

Yeah the data I get there is the transaction hash a longside a lot of other information about the transaction but it returns it to me instantly instead of waiting until the transaction is done

As @cryptokid has mentioned,

it is not easy to get response when the the transaction is finished, sometimes you have to check if the transaction made it on chain, and sometimes it can take minutes depending on the network.

Hmm but isn’t that a build in feature in the moralis function? Isn’t onsuccess supposed to wait until the transaction is successfully finished?

For transaction to be finished might take longer than expected. Rather you can sync events.

Yeah I know some transactions take time but the problem is that it doesn’t wait on the transactions to finish at all. It calls the onsuccess even tho the transaction isn’t done

It actually calls success when the function is executed which means the function has been executed successfully.

Okay thanks I’ll try listing for events instead then

what version of Moralis SDK were you using?

im using moralis: 1.2.3 and react-moralis: 1.2.1 and i was trying to do the same as moralis does in this video: https://www.youtube.com/watch?v=7TKqLJd_aYI in the last part of the video he shows it for react and he uses the onsuccess thing which for him waits for the transaction to finish but for me it doesnt. i also tried to copy his code exactly as in his video and still doesnt work for me. has somebody mastered the onsuccess?

I’m having exactly same issue with latest version of react and moralis sdk. OnSuccess triggers instantly after the request, it doesn’t wait the metamask confirm the transaction.

It was working perfectly before I upgrade to the latest version. Do we have any news on it?

on what version of the SDK is working?

I’m in the latest version, but I tested all versions of react and sdk since 1.0.0 and none of them worked.

I tried the OnSuccess and OnComplete from fetch() method, they were working normally on version 0.0.184 , but after the upgrade it stopped working

The onSuccess gets triggered as soon as the transaction has been executed correctly.
When you’re writing to the blockchain, you might want to wait for it to be confirmed.

So there is a difference between a successful transaction, and the transaction to be confirmed. So it is expected to get the transaction data only. So when you write to the chain you might want to call response.wait(), this promise resolves after the transaction has been confirmed

For more details you can check this section: https://docs.moralis.io/moralis-server/web3/web3#example-of-calling-a-write-contract-method

But I think we can improve the functions to make this easier for you. I will look into improving this.

Hello Erno, so the fetch() shouldn’t be used anymore? I’m using react moralis and not moralis sdk only…

Is there a way to make it work with react moralis fetch()?

You can still use fetch from the hook

Under the hood it will call Moralis.executeFunction, and resolve the data and errors for you.
So the response you get back should be the same as in Moralis.executeFunction

In both cases you should get:

  1. The returned value when it is a read-only call to the blockchain
  2. A transaction with the wait() function on it, that you can call to get the results after confirmation

But in react-moralis (and maybe even in the sdk), it might make sense to resolve the results from the wait call for you. And/or provide a onConfirmed hook maybe.

I’ll test this solution and back to you asap.

Yes would be very good to have this being given by the sdk, some kind of “onConfirmed” hook
Also have this information updated on the documentation in the react repository.