Orbs Network
  • Overview
    • What is Orbs?
    • Layer 3 Protocol
    • Running on EVM and TON
    • Enhanced Execution
    • L3 for Advanced Trading
    • Network Diagram
    • White Papers and Spec
  • Orbs Lambda
    • What is Orbs Lambda?
    • Network Diagram
    • Example Use Cases
    • Step by Step Overview
      • Development Requirements
      • Select Unique ID
      • Project Template
      • index.js Implementation
        • Execution Environment
        • Allowed Packages
        • Supported Networks
        • Execution Triggers
        • Sending Transactions
      • Testing Locally
      • Deploying to Production
      • Analyzing Execution
  • Orbs VM
    • What is Orbs VM?
    • Network Diagram
    • Example Use Cases
    • Step by Step Overview
      • Development Requirements
      • Select Unique ID
      • Docker Image Implementation
        • Execution Environment
        • Working Directory
        • Entry Point
        • Health Check
        • status.json
      • Testing Locally
      • Deploying to Production
      • Analyzing Execution
  • Powered by Orbs
    • Liquidity Hub
      • Integration Spec
    • Perpetual Hub
    • dTWAP Protocol
      • Integration Spec
      • Network Diagram
      • dTWAP Tutorial
      • FAQ
    • dLIMIT Protocol
      • Integration Spec
      • dLIMIT Tutorial
      • FAQ
  • Community Projects
    • Notification Protocol
      • Integrate a New Project
      • Network Diagram
    • TON Access
      • Network Diagram
    • TON Vote
Powered by GitBook
On this page
  • Mandatory exports
  • Importing dependencies
  • Async await
  • Complete example
  1. Orbs Lambda
  2. Step by Step Overview

index.js Implementation

PreviousProject TemplateNextExecution Environment

Last updated 4 months ago

The file index.js that is found in the top-level directory of your unique ID is the main entry point of your lambda function. This file should contain the majority of your JavaScript logic.

Mandatory exports

The file must export in the function register which registers the that the lambda function is listening on.

module.exports.register = function(engine) {
  // register all triggers here
}

Importing dependencies

The file may import additional .js and .json dependencies that must be found in the same directory as the file index.js itself (or under a subdirectory next to it).

Importing must use the require() syntax.

NPM packages may also be imported, subject to being in the .

const { ContractAbi } = require("./abi.js");

const Config = require("./config.json");

const BigNumber = require("bignumber.js");

Async await

Asynchronous code can and should use the modern async await syntax directly.

async function doSomething(web3) {
  const contract = new web3.eth.Contract(abi, address);
  const res = await contract.methods.readState();
}

Complete example

const BigNumber = require("bignumber.js");

// config
const aaveAbi = [{/* ... */}];
const closingAbi = [{/* ... */}];
const positionAddress = "0xe93C0f419AF19297D320563214Adbb2f28EF4C0c";

// globals
let aaveContract; // the standard Aave contract
let closingContract; // contract for closing Aave positions, deployed for this lambda

async function checkAavePosition(args) {
  if (!aaveContract) aaveContract = new args.web3.eth.Contract(aaveAbi, "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9");
  if (!closingContract) closingContract = new args.web3.eth.Contract(aaveAbi, "0xBB51bD166d68c3DC5860190844Dfe1056Cea3122");
  const position = await aaveContract.methods.getPositionData(positionAddress).call();
  if (new BigNumber(position.healthFactor).dividedBy("1e18").toNumber() < 1.05) {
    await closingContract.methods.closePosition(positionAddress).send();
  }
}

module.exports.register = function(engine) {
  engine.onInterval(checkAavePosition, {
    interval: "10m", 
    network: "ethereum"
  });
}

The following example monitors an borrowing position every 10 minutes and if the position is close to liquidation, closes it using a contract that was deployed in advance for this lambda.

CommonJs format
execution triggers
CommonJs
allowed list
Aave