useWeb3Transfer() - How to wait success status

Hello, I am new to Moralis and I can’t seem to find a way on how to check whether the transaction is Success or Completed on Blockchain.

I have this code snippet below on what I want to achieve

     const { fetch, error, isFetching } = useWeb3Transfer({
        amount: Moralis.Units.Token(0.1, 18),
        receiver: '0x7A16424328fDA0ecEA1DE692164F7D2995354d9c',
        type: 'erc20',
        contractAddress: '0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e',
    })

    const transfer = async () => {
        fetch().then(() => {
            // Do something - Save data on DB
            // I need to check first if the transaction is successful and not on pending status.
        })
    }

Good question. The onSuccess event won’t work for this because it happens well before the transaction is actually confirmed on-chain.

I agree. I tried both onComplete and onSuccess. Do you know any method to solve this problem boss?

Use the wait() method.

E.g.

const transaction = await fetch({... 

await transaction.wait();

// logic
1 Like

It worked. I also find another way to do it.

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

Hello @alex,

i tried to use this solution but got an error:

await (await DoTransaction.fetch()).wait();

// logic

Do you a way to correct it? I would like not to use “OnSuccess” paramaters, (i have multiple following functions to execute and so i don’t want to have a cascade of executions)

You do not want to wait for the transactions to finish? Or do you want to execute them simultaneously?

For normal wait usage, use:

const transaction = await fetch({... // or DoTransaction.fetch()

await transaction.wait();

You can post more of your code.

Hello @alex,

I would like to wait until the transaction is finished before executing other instructions.

The thing is, with

const transaction = await DoTransaction.fetch()
await transaction.wait();

or

await (await DoTransaction.fetch()).wait();

i got the following error message Object is of type 'unknown' in VScode

image

I’m pretty sure it’s related to the type Promise<unknown> in the fetch method, but i didn’t find a fix.

At the end:
-> when running npm run dev, it works, it waits until the transaction is completed.
-> nevertheless, it’s impossible to build with npm run build because of this error.

Can you try this workaround.

For your example:

const tx = await doBorrow.fetch() as unknown as TransactionResponse;

Or:

const tx = await doBorrow.fetch() as TransactionResponse;