AllTradesz / ops-sdk

Gelato Ops SDK

Home Page:https://www.npmjs.com/package/@gelatonetwork/ops-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gelato Ops SDK

Automate your smart contracts using Gelato Ops SDK

Installation

yarn add @gelatonetwork/ops-sdk

or

npm install @gelatonetwork/ops-sdk

How to use?

  1. Import the Gelato Ops SDK into your project:
import { GelatoOpsSDK } from "@gelatonetwork/ops-sdk";
  1. Check if Gelato Ops is deployed on your network:
  import { isGelatoOpsSupported } from "@gelatonetwork/ops-sdk";

  if (!isGelatoOpsSupported(chainId)) {
    console.log(`Gelato Ops network not supported (${chainId})`);
    return;
  }
  1. Instantiate Gelato Ops using your signer:
const gelatoOps = new GelatoOpsSDK(chainId, signer);
  1. Create an automation task:
interface CreateTaskOptions {
  name: string;             // your task name

  // Function to execute
  execAddress: string;      // address of your target smart contract
  execSelector: string;     // function selector to execute on your target smart contract
  execAbi?: string;         // ABI of your target smart contract
  
  // Proxy caller
  dedicatedMsgSender: boolean;     // task will be called via a dedicated msg.sender which you can whitelist (recommended: true)

  // Optional: Pre-defined / static target smart contract inputs
  execData?: string;        // exec call data 
  
  // Optional: Dynamic target smart contract inputs (using a resolver)
  resolverAddress?: string; // resolver contract address
  resolverData?: string;    // resolver call data (encoded data with function selector)
  resolverAbi?: string;     // your resolver smart contract ABI

  // Optional: Time based task params
  interval?: number;        // execution interval in seconds
  startTime?: number;       // start timestamp in seconds or 0 to start immediately (default: 0)

  // Optional: Single execution task
  singleExec?: boolean;     // task cancels itself after 1 execution if true.

  // Optional: Payment params
  useTreasury?: boolean;    // use false if your task is self-paying (default: true)
}

const params: CreateTaskOptions = { name, execAddress, execSelector, interval, dedicatedMsgSender };
const { taskId, tx }: TaskTransaction = await gelatoOps.createTask(params);
await tx.wait(); // Optionally wait for tx confirmation
console.log(`Task created, taskId: ${taskId} (tx hash: ${tx.hash})`);
  1. Retrieve all your tasks:
  const activeTasks = await gelatoOps.getActiveTasks();
  activeTasks.forEach((task: Task) => {
    console.log(`- ${task.name} (${task.taskId})`);
  });
  1. Rename a task:
await gelatoOps.renameTask(taskId, "Another Gelato name");
  1. Cancel a task:
const { taskId, tx }: TaskTransaction = await gelatoOps.cancelTask(taskId);
await tx.wait(); // Optionally wait for tx confirmation
console.log(`Task canceled, taskId: ${taskId} (tx hash: ${tx.hash})`);
  1. Overriding gas settings:

If you need to override gas settings, you can pass an additional Overrides object to createTask & cancelTask methods:

const params: CreateTaskOptions = { name, execAddress, execSelector, interval, dedicatedMsgSender };
const overrides: Overrides = { gasLimit: 2000000 };
const tx: TaskTransaction = await gelatoOps.createTask(params, overrides);
  1. Whitelisting msg.sender of function:

If you enabled dedicatedMsgSender, your task will be called via a dedicated msg.sender which you can whitelist on your smart contract for extra security.

If dedicatedMsgSender is set to false, msg.sender of the task will be the Ops contract.

To fetch your dedicated msg.sender:

const { address } = await gelatoOps.getDedicatedMsgSender();
console.log("Dedicated msg.sender: ", address);

Examples

Check out our tutorial repository ops-sdk-hello-world for more in-depth examples.

About

Gelato Ops SDK

https://www.npmjs.com/package/@gelatonetwork/ops-sdk


Languages

Language:TypeScript 100.0%