[Ethereum] Batching Web3 Transactions

web3js

I want to make sure a number of transactions execute in a specific order while sending them all at once (preferably within a single block). Here's what the transactions look like:

  1. Fund ETH
  2. Interact With Contract
  3. Interact With Contract

2 is dependent on 1 running first and 3 is dependent on 2 running first.

It looks like I can guarantee this order using web3's batching. Can I or does the web3 batching just guarantee that this node will propagate the messages to the next node together only?

Best Answer

No, if they're separate transactions, it's up to the mining node's transaction ordering rules to determine if they all happen in the same block.

If you want atomic transactions, I'd recommend using a disposable, self-destructing contract to do the work for you.

Something like this:

contract AtomicInteraction {
  function AtomicInteraction() payable {
    // Fund ETH
    // Interact with Contract
    // Interact with Contract
    selfdestruct(msg.sender);
  }
}

You could probably make it even more generic, and have it accept an array of addresses and an array of data for each message, but that's a pretty complex solution for something that's probably going to require some application-specific logic anyway.

Related Topic