5

Say I need a user to approve a contract with an erc20 token balance and then the contract can execute it's function after. Typically, I would have to do an approve transaction with a nonce of 1 and then call the contract after that with a nonce of 2.

This means that we have to wait at least two blocks before the total of the two transactions complete.

How can I send these transactions at the same time, and included in the same block while still ensuring the ordering of their execution?

It might look something like this:

await sendTransactions([
    token.approve(myContract, toWei('1000000')), // Gets executed in the same block before someFunction
    myContract.someFunction() // Gets executed in the same block after approve function
]);
4
  • 1
    This means that we have to wait at least two blocks before the total of the two transactions complete - you're assuming that each transaction gets executed in a new block, which is wrong. Sep 30, 2020 at 10:16
  • In function sendTransactions, just add await at the beginning of each line. Sep 30, 2020 at 10:17
  • Ok. I read somewhere that the wallets will wait for a new block before broadcasting a transaction with a higher nonce, if one with a lower nonce already exists. Sep 30, 2020 at 11:00
  • @DylanKerler That may be true, as I just tried to broadcast a txn with nonce 44 (I'm currently on nonce 42 so my next should be nonce 43) and etherescan said broadcast succeeded but it was no where to be found in any mempool which implies it just gets rejected almost immediately after; so it seems you do have to broadcast the correct lower nonce first, but what goodvibration is saying is that they can both still end up being mined into the same block (and in order if you look at the 'index' property of each txn in that one block). Oct 7, 2020 at 19:08

1 Answer 1

1

Do you know the MultiSender app?

It allows for multiple Ether and ERC 20 token transactions to be bundled in one transaction. It's open-source on Github rstormsf/multisender in case you want to review the logic.

There's also a more detailled write-up on Medium: MultiSender — Send Ether and Ethereum Tokens to Multiple Ethereum Addresses In A Single Transaction.

2
  • 1
    Is this legit and safe?
    – Ingus
    Feb 5 at 16:21
  • I would say it's legit. Roman is very well known and reputable. github.com/rstormsf/multisender - I cannot tell you whether it's save though :)
    – q9f
    Feb 11 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.