1

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?

2

As of December 2019 at least, it doesn't seem possible to do this. Read this discussion on Twitter for more details.

The best option is to chain the promises, as Ismael suggested in the comment above.

createTokenApprovalTx().then(() => createFooTx());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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