Moralis object extend after a transaction in Metamask is a success

I just want to ask if there is any way to check if a transaction (sending a BNB) is a success or failed before doing a Moralis.Object.extend(“HistoryTransaction”)

Scenario:
1.) Sending a BNB to a specific address
2.) If it went through create a new data under HistroyTransaction
3.) if not it will not create a data

I just want a checker upon using

const { fetch, isFetching, } = useWeb3Transfer({
    type: "native",
    amount: Moralis.Units.ETH(0.1),
    receiver: "0x93dC7618b0ffd14f1ea148478D8Ab3e6A7bf9512",
  });

before doing a save in database. I’m using ReactJS

Thank you!

There are examples of waiting for transaction confirmation in this thread.

hi Glad, I have been following that thread but it have not get any right answers. I followed one replied but all im getting is this errors:

For the first error, await needs to be used in async functions.

For the second error, try the workaround here.

You’re mixing both of the solutions posted in that thread together, try just this one for example:

fetch({
   onSuccess: (tx) => tx.wait().then(() => {
       // Do something
   })
})

now tx is the problem

Try typing fetch similar to this example.

i tried looking for it in moralis doc for TransactionResponse

The import is covered here from that link I posted.

Fixed it without TransactionResponse but the problem is onComplete or onError is processed upon clicking “Rejetct/Confirm” in metamask not if that transaction success/failed. I want to fetch if the transaction in metamask success/failed

  const onFetch =() => fetch({
    onComplete: () => {
      setSuccessResult(true);
    },
    onError: () => {
      setErrorResult(true);
    }
 })

I tried this: but no luck

This is what is wait can be used for - please read the first post.

You can’t type it like that, add the as unknown... code after the fetch that is being used like in the example I linked (not this one you’ve created at the top). Or for now you can just use the any type to get around this.

can you help me, i dont’ know exactly you mean by use the any type can you provide a sample? Thank you!

Are you familiar with TypeScript (your project is using it) ? It is a way to avoid any type issues. You can read this.

both never work or something is wrong

const tx = fetch() as unknown as TransactionResponse;
const tx = onFetch() as unknown as TransactionResponse;

Remove the const tx line - that won’t work since you’re already using another fetch.

The easiest way you can do is just like that example:

const tx = await fetch() as TransactionResponse;
await tx.wait();
// do something