Chaining Operations

What is input/output chaining?

Guardians can use arbitrary calldata in operations, however, not all callback data is known when the submit transaction is created. A common example is chaining operations together, for example, when swapping WETH to USDC and then depositing all of the USDC into a lending protocol. The amount of USDC obtained can vary depending on available liquidity and can often not be predicted when the original operation is created. For this reason Aera supports input/output chaining for guardian operations.

Static functions are an important addition to input/output chaining. Aera can be instructed to perform a static call to a contract in an operation which would then make the return value available as an input for future operations.

Why They Matter

Chaining inputs and outputs is crucial to avoid inefficiency or unnecessary transactions. When an operation cannot predict the exact amount to use, it will likely lean on being conservative and leaving dust behind in the contract.

Solvers have been using sophisticated routing contracts to facilitate chaining (sometimes through opcode VMs) and some protocols (e.g., Uniswap) now have built in chaining, however, universal cross-protocol operation chaining has not been available for guardians.

With input/output chaining, static calls allow additional efficiency and safety ensuring that calls used only for providing a return value are not stateful.

How to use input/output chaining

Every operation passed to submit may have a clipboard. The clipboard argument has a length (1 byte field) and a series of 4-byte entries:

  • resultIndex (1 byte): which prior operation to take the return value from;

  • copyWord (1 byte): which 32-byte word in the return value to copy;

  • pasteOffset (2 bytes): which offset in the current calldata to override with the word that is being copied.

As Aera executes operations as part of a submit call, it will go through the following flow:

  • Load the initial calldata as specified in the operation;

  • Check if a clipboard is specified for the given operation;

  • Iterate over each clipboard element. For each element, extract the given word from the result of a prior operation and override the calldata at the paste offset;

  • Proceed as normal with the final modified calldata (check pre-hooks, run operation, check post-hooks).

To use static calls, you just need to set the isStaticCall flag in submit. Return values from static calls can be read with the clipboard in the same way.

Caveats

Guardians have to ensure that copy word offsets are within bounds of return data to enjoy predictable results.

Last updated