Options
All
  • Public
  • Public/Protected
  • All
Menu

Planner is the main class to use to specify a sequence of operations to execute for a weiroll script.

To use a Planner, construct it and call Planner.add with the function calls you wish to execute. For example:

const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
const planner = new Planner();
planner.add(events.logUint(123));

Hierarchy

  • Planner

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

Readonly state

state: StateValue

Represents the current state of the planner. This value can be passed as an argument to a function that accepts a bytes[]. At runtime it will be replaced with the current state of the weiroll planner.

Methods

add

  • Adds a new function call to the planner. Function calls are executed in the order they are added.

    If the function call has a return value, add returns an object representing that value, which you can pass to subsequent function calls. For example:

    const math = Contract.createLibrary(Math); // Assumes `Math` is an ethers.js contract object
    const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
    const planner = new Planner();
    const sum = planner.add(math.add(21, 21));
    planner.add(events.logUint(sum));
    

    Parameters

    Returns null | ReturnValue

    An object representing the return value of the call, or null if it does not return a value.

addSubplan

  • Adds a call to a subplan. This has the effect of instantiating a nested instance of the weiroll interpreter, and is commonly used for functionality such as flashloans, control flow, or anywhere else you may need to execute logic inside a callback.

    A FunctionCall passed to Planner.addSubplan must take another Planner object as one argument, and a placeholder representing the planner state, accessible as Planner.state, as another. Exactly one of each argument must be provided.

    At runtime, the subplan is replaced by a list of commands for the subplanner (type bytes32[]), and planner.state is replaced by the current state of the parent planner instance (type bytes[]).

    If the call returns a bytes[], this will be used to replace the parent planner's state after the call to the subplanner completes. Return values defined inside a subplan may be used outside that subplan - both in the parent planner and in subsequent subplans - only if the call returns the updated planner state.

    Example usage:

    const exchange = Contract.createLibrary(Exchange); // Assumes `Exchange` is an ethers.js contract
    const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract
    const subplanner = new Planner();
    const outqty = subplanner.add(exchange.swap(tokenb, tokena, qty));
    
    const planner = new Planner();
    planner.addSubplan(exchange.flashswap(tokena, tokenb, qty, subplanner, planner.state));
    planner.add(events.logUint(outqty)); // Only works if `exchange.flashswap` returns updated state
    

    Parameters

    Returns void

plan

  • plan(): { commands: string[]; state: string[] }
  • Builds an execution plan for all the commands added to the planner.

    Returns { commands: string[]; state: string[] }

    commands and state, which can be passed directly to the weiroll executor to execute the plan.

    • commands: string[]
    • state: string[]

replaceState

  • Executes a FunctionCall, and replaces the planner state with the value it returns. This can be used to execute functions that make arbitrary changes to the planner state. Note that the planner library is not aware of these changes - so it may produce invalid plans if you don't know what you're doing.

    Parameters

    Returns void

Generated using TypeDoc