I create my transactions with ethers.js and push them into an array like this:
const promises = [];
promises.push(createTokenApprovalTx());
promises.push(createFooTx());
It's important that the token approval transaction is signed and submitted before the foo transaction, because the latter moves tokens around. Therefore, I order them properly and set a higher nonce on the foo transaction.
I then submit them like this:
const txs = await Promise.all(promises);
await Promise.all(
txs.map(tx => {
return tx.wait(1);
}),
);
The catch is that the order is not guaranteed. Sometimes, roughly 3 in 4 times, it is indeed the case that the token approval comes first, but not all the time.
How to accomplish this with MetaMask or, generally, with any JSON-RPC provider?
createTokenApprovalTx().then(() => createFooTx())
. The user is in total control of Metamask and he can reorder them. – Ismael Dec 5 '19 at 23:49createTokenApprovaTx()
andcreateFooTx()
build the tx and set the nonce manually. See on these links how: (For fetching nonce) web3js.readthedocs.io/en/v1.2.0/… (For building tx) web3js.readthedocs.io/en/v1.2.0/web3-eth.html#sendtransaction – Gabriel G. Dec 6 '19 at 15:38