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:
- The returned value when it is a read-only call to the blockchain
- 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.
Agreed, an āonConfirmedā hook would be useful