How to guarantee the order of transactions?
See original GitHub issueI read #435, but following the instructions don’t guarantee that the transactions sent to an end-user wallet, such as MetaMask, will prompt the user to sign them in the same order.
Here’s what I do:
const promises = [];
promises.push(createTokenApprovalTx());
promises.push(createMyTx());
await Promise.all(promises);
The 2nd transaction depends on the first one, that is, the token approval must be sufficient for the contract to be able to move tokens around. In the transaction creation functions, I call getTransactionCount
to get the latest nonce and increment it for the 2nd transaction.
But the order is not enforced in the MetaMask popups. I couldn’t identify a pattern for when it works and when it doesn’t - it seems to be a stochastic process. I tried swapping the order of adding the transactions in the promises
array, but that didn’t move the needle either.
I find this a basic example and arguably the safer alternative to approve(MAX_UINT256). What’s the best way to do this with ethers?
As you can see in the screenshots, the token approval comes 2nd, even if it should come 1st.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
I think this is resolved, so I’m going to close it now. If not, please re-open.
Thanks! 😃
The
1
means wait for 1 block confirmation. The default is one, so it is not necessary, but if you pass in0
, then wait will return null if the transaction is not mined, and for Example,2
would indicate to wait until there were two confirmations.