[Ethereum] What are best practices for submitting multiple sequenced transactions with web3

dapp-developmentmetamaskweb3js

I have a dapp that, as part of one logical user operation, must issue an approve() call on a token contract followed by a call to another contract that will use the funds. Since these each require user-authorization I have to issue them at about the same time. Some options that I can see include:

  1. Submitting both sequentially but as fast as possible (the nonce will keep them ordered).
  2. Chaining (waiting) on the approve tx until I get its transaction hash and then submitting the transfer tx.
  3. Chaining as in #2 but introducing a short artificial delay between submissions.
  4. Using the batch API?

I am currently doing #2 (chaining). The effect that I see is that normally the user gets two popups to confirm and it is fine. (I have seen Metamask combine them into one dialog sometimes but I don’t know what triggered this – timing related?). The problem is that occasionally this just seems to fail and one of the transactions is never presented to the user. I’ve had reports of this happening on multiple platforms and in multiple wallets.

Is this what #4 (the batch API) is for? I did some experiments with it but it appeared to me that no one was using this API and it was a bit dated. I was concerned about how well supported it was in various wallets and I wasn’t sure it actually helped the issue.

EDIT: Another issue that may rule out the batch API is that it does not appear to provide a way to get the transaction hashes for the submitted transactions, making it impossible to monitor them properly.

Thanks!

Best Answer

I'm not sure it's a best practice, but I think I would incline to #2 in order to simplify exception handling. It's important to back out the approve appropriately in the case that doesn't mine, or the next transaction doesn't mine.

You can reduce the pop-ups with EIP-1102 which is definitely recommended. In summary, the dApp is approved instead of discrete transactions and that is more intuitive for users.

There is a how-to document for EIP-1102 over here: https://status.im/developer_tools/run_on_status/eip-1102.html

Hope it helps.

Related Topic