Example with the Execution API

This is a hands-on tutorial that walks through building a simple refuel automation using a JS script. Perfect for getting familiar with core API types and execution flows.

While the Execution Layer provides the foundational security and access control for automations, the Orchestration Layer enhances it with scheduling capabilities. For complex automation scenarios that go beyond the standard scheduling API, developers can directly utilize the core execution endpoints. Let's explore this flexibility through an example.

A Simple Cross Chain Refuel BOT

An automation built using Console Automation APIs that helps keep ETH balances topped up across different chains.

Users can subscribe by providing:

  • A refuelAddress to monitor

  • minAmount of ETH to maintain

  • List of chains to watch

The automation checks if the refuelAddress drops below minAmount of ETH on any of the specified chains. If it does, it automatically bridges funds to keep the balances above the minimum threshold

Creating the Automation

To create an automation, the executor must first configure the automation parameters.

// Define the EIP-712 message parameters
const msgParams = {
  domain: {
    chainId: 42161
  },
  message: {
    timestamp: 1,
    executor: "0xAE75B29ADe678372D77A8B41225654138a7E6ff1",
    inputTokens: ["0x0000000000000000000000000000000000000000"],
    hopAddresses: ["0x3a23F943181408EAC424116Af7b7790c94Cb97a5"],
    feeInBPS: 0,
    feeToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
    feeReceiver: "0xAE75B29ADe678372D77A8B41225654138a7E6ff1",
    limitPerExecution: true,
    clientId: "auto-refuel"
  },
  primaryType: "RegisterExecutor",
  types: {
    EIP712Domain: [
      { name: "chainId", type: "uint256" },
      { name: "name", type: "string" },
      { name: "version", type: "string" },
      { name: "verifyingContract", type: "address" }
    ],
    RegisterExecutor: [
      { name: "timestamp", type: "uint256" },
      { name: "executor", type: "address" },
      { name: "inputTokens", type: "address[]" },
      { name: "hopAddresses", type: "address[]" },
      { name: "feeInBPS", type: "uint256" },
      { name: "feeToken", type: "address" },
      { name: "feeReceiver", type: "address" },
      { name: "limitPerExecution", type: "bool" },
      { name: "clientId", type: "string" }
    ]
  }
};

Once we have the automation parameters defined, we can create a helper to sign the parameters, as this signature is required for automation creation.

Here, RPC_URL and EXECUTOR_PRIVATE_KEY must be configured with appropriate values.

Once the signature is ready, we can simply call the POST /executor endpoint to create the automation for the executor.

Fetching subscribers of automation

Once the automation is created, Console users will be able to subscribe to the automation from console's Automation Marketplace. The automation executors can fetch a list of all subscribers and their metadata from the API, to perform further operations.

First, the executor's registryID must be fetched in order to fetch its subscribers.

Post fetching, we can also perform further cleanup to filter out the active subscriptions.

Checking if the automation should be executed for a subscriber

The automation only has to be executed, if the refuelAddress has a balance lower than the minAmount threshold on any given chain ID from chains. We define a function to perform a check to see if a target address has a low balance-

Here, RPC_URL needs to be defined

Now we can go through all the subscribers and check if the automation is to be performed.

Here, EXECUTOR_ADDRESS and CHAIN_ID must be defined.

Executing calldata to bridge funds to refuelAddress on required chains from subscriber

Before proceeding with the actual execution, we first need to define some helpers.

  • Signing execution digest

    Here, EXECUTOR_PLUGIN_ADDRESS and EXECUTOR_PLUGIN_ABI must be assigned to the appropriate values.

  • Helpers to get bridge routes from socket

    Here, API_KEY must be assigned to the executor's socket API key.

  • Helper to use the /execute endpoint to execute a task on subscriber

Now that we have all the required helpers, we can combine them for the complete automation execution logic.

Here, EXECUTOR_ADDRESS, CHAIN_ID and RPC_URL must be configured with appropriate values.

Automation Demo

Wrapping up

  • Reference for the APIs used in this example can be found here.

  • The complete script for this example automation can be found here.

Last updated