βœ…Create a batch transaction compatible with Squid router

How to generate a proper encoded batch call made of several encoded subcalls

To create a compatible Batch inside Squid Hook, we first need to create a the full encoded data for the batch preCompile, then encode this transaction data before integrating it into Squid. This works both on the source and the destination chain.

Each of the calls to be included in the batch precompile need to be encoded using encodeFunctionData which is available on your contract instance.

      let iface = new ethers.utils.Interface(contractABI);
      let encodedTx = iface.encodeFunctionData("functionName", [
       arg1,
       arg2
      ]);

You need to repeat this operation for each of the encoded calls (subcalls) that you'd like to integrate in the batch precompile. For instance, a simple use-case would be to send an increaseAllowance then an ERC20 transfer easily by encoding 2 transactions.

When you've got all your encoded transactions data, you need to encoded the full batch transaction.

For instance, it can looks like this:

      let iface = new ethers.utils.Interface(batchABI);
      let encodedTx = iface.encodeFunctionData("batchAll", [
          [destinationAddress1, destinationAddress2],
          ["0", "0"], // native balance
          [encodedTx1, encodedTx2],
          [],
      ]);       

You can refer to the previous page "Understand the batch precompile" to better grasp how to generate a valid transaction using the batch Precompile.

Remember, there are 3 different batch functions:

  • batchSome will continue to execute the following subcallls even is one is failing

  • batchSomeUntilFailure will abort on failure as its name suggests, avoiding failure

  • batchAll will make all subcall revert if one is failing

Last updated