9

I need to send 0.1 ETH to 10 unique addresses from the same address at the same time. I am manually incrementing each transaction's nonce, and the transactions are sent successfully.

However, each transaction is entered into a new block. For example, transaction A in block 1, B in 2, C in 3, and so on. The problem is that this isn't scalable across n transactions, because I'd have to wait for n blocks to confirm.

Is there a proper way to batch-send ETH so that the collective of transactions confirms as quickly as possible?

Edit: This has only been tested on Kovan. Even with Kovan's 4 second block time, shouldn't transactions sent at the exact same time be included in the same block?

5
  • Yes, don't await for the transactions to complete. Apr 14, 2020 at 4:45
  • I'm not. They're sent asynchronously at the exact same time.
    – slider
    Apr 14, 2020 at 6:04
  • 1
    You might wanna share how exactly you submit those transactions (that is, if you're hoping to get an actual suggestion on how to change it to fit your goals). Apr 14, 2020 at 6:14
  • 1
    It is literally called BatchRequest: web3js.readthedocs.io/en/v1.2.6/web3-eth.html#batchrequest. And you can never 100% guarantee to have all transactions in one block. It's up to the miner to decide which transactions to include. The only thing you can guarantee is the order by setting the nonce. Apr 14, 2020 at 8:33
  • @soliditydeveloper.com yeah, BatchRequest does the trick. Although, I can't get a transaction's confNumber from a web3.eth.sendSignedTransaction.request(), only the txHash from the callback.
    – slider
    Apr 14, 2020 at 19:00

3 Answers 3

2

You can write a solidity contract with one method that receives the addresses and the amounts to transfer, and performs the corresponding transfers.

Then you achieve your objective in a single transaction.

For security reasons I recommend to extend the Ownable contract and use the onlyOwner() modifier:

https://docs.openzeppelin.com/contracts/4.x/api/access#Ownable

:)

1
  • Hey bro I have been wanted to do that using a contract. Can you send me examples please? If you want you can DM me user3638836
    – hugocabral
    Jan 2, 2022 at 20:17
1

I have been playing around with a similar thing however, on the ropsten network and i get multiple transactions in the same block when testing with sending 100 transactions. I have written the following function that allows me to send an arbitrary amount of transactions to my smart contract at the same time. Perhaps this could be re-purposed for your needs?

async function multipleTx(n) {
  accounts = await web3.eth.getAccounts()
  for (let i = 0; i < n; i++) {
    instance.methods
      .addSubmission(i, i, i, i)
      .send({
        from: accounts[0],
        gasPrice: 10000000000,
      })
      .then((receipt) => {
        console.log(receipt)
      })
  }
}
0

All web3.eth.send() calls automatically increment nonce by your wallet (what wallet provider you are using for this?).

You can simply fire out as many web3.eth.send() calls or contract calls as possible and then await them as Promise.all() to resolve. Miners will fill your transactions in block and try to fit as many transactions, yours or otheres, in a block as possible.

11
  • Thanks. But this would only block my process from continuing until every single transaction has resolved. My question relates to sending the transactions, not receiving confirmations.
    – slider
    Apr 14, 2020 at 17:16
  • web3.eth.send() returns something called PromiEvent. You can listen to event.on("txhash") to immediately go forward as soon as wallet has a txhash. Depending on the wallet logic this can be before of after the transaction has been broadcasted to the network. web3js.readthedocs.io/en/v1.2.0/… Apr 14, 2020 at 18:16
  • I'm doing that. But I need to wait for x confirmations of each transaction, which doesn't help if they're sent in different blocks.
    – slider
    Apr 14, 2020 at 18:56
  • Your last comment does not make sense. Each transaction has its own confirmation cycle. Just wait that all transactions have current block - mined block > needed confirmations. Apr 14, 2020 at 23:17
  • Transactions that are in the same block are on the same confirmation cycle. This solution doesn't put transactions in the same block like BatchRequest does. It merely sends them out one at a time and waits for the very last tx to confirm before resolving the promise.
    – slider
    Apr 14, 2020 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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