Router

Generated transactions are executed using the Router smart contract.

Introduction

Requests made to the Jam API will be tailed to be executed on the Router contract. It is what enables execution of complex, multi-step DeFi transactions in a single call.

This documentation focuses on the runSteps function and its data structures.

Functions

function runSteps(Step[] calldata steps, uint256[] memory stores) external

Executes a sequence of steps, each comprising a call to another contract and potentially modifying an array of "stores."

Parameters

  • steps: An array of Step structs that represent the steps in the multi-step transaction.

  • stores: An array of uint256 numbers that serve as a store for values that can be used in the transaction steps.

Conditions

  • Must be invoked using DELEGATECALL.

  • The steps array must not be empty.

Behavior

  • Executes the steps in sequence, modifying the calldata and using the stores as needed.

  • Reverts the transaction if any of the steps fail.

Data structures

Step

struct Step {
    address stepAddress;
    bytes stepEncodedCall;
    StoreOperation[] storeOperations;
}

Struct for representing a transaction step.

Fields

  • stepAddress: The target contract address for this step.

  • stepEncodedCall: ABI-encoded calldata to be passed to the target contract.

  • storeOperations: An array of StoreOperation structs detailing any store-related modifications required for this step.

StoreOperation

struct StoreOperation {
    StoreOpType storeOpType;
    uint256 storeNumber;
    uint256 offset;
    uint256 fraction;
}

Struct that describes how to operate on stores or results of steps.

  • storeOpType: A numerical code representing the type of store operation, determined by the following enum:

enum StoreOpType {
    RetrieveStoreAssignValue,
    RetrieveStoreAssignCall,
    RetrieveResultAssignStore,
    RetrieveResultSubtractStore,
    RetrieveStoreAssignValueSubtract,
    RetrieveStoreAssignCallSubtract,
    SubtractStoreFromStore
}
  • Type RetrieveStoreAssignValue:

    • Retrieve store value at storeNumber, multiply by fraction and set value

  • Type RetrieveStoreAssignCall:

    • Retrieve store value at storeNumber, multiply by fraction and set offset at stepEncodedCall

  • Type RetrieveResultAssignStore:

    • Retrieve result value at offset of function's result and add it to store at storeNumber

  • Type RetrieveResultSubtractStore:

    • Retrieve result value at offset of function's result and subtracts it from store at storeNumber

  • Type RetrieveStoreAssignValueSubtract:

    • Retrieve store value at storeNumber, multiply by fraction and set value.Subtracts calculated value from store.

  • Type RetrieveStoreAssignCallSubtract:

    • Retrieve store value at storeNumber, multiply by fraction and set offset at stepEncodedCall. Subtracts calculated value from store.

  • Type SubtractStoreFromStore:

    • Subtracts store value at store storeNumber from store at offset

Examples

Examples will be added here.

// Prepare a sequence of steps and an initial stores array.
Step[] memory steps = // ...
uint256[] memory stores = // ...

// Execute the steps via the Router contract.
routerContract.runSteps(steps, stores);

Source code

Router.sol

Last updated