Gelato Network
Search…
⌃K

Gelato Automate SDK

TODO: Can I change any mention of ops in NPM packages?
Create automated task executions using our typescript SDK:

NPM Package

How to use

Install ops-sdk:
yarn add @gelatonetwork/ops-sdk
Import GelatoOpsSDK:
import { GelatoOpsSDK } from "@gelatonetwork/ops-sdk";
Instantiate the SDK with your signer:
const gelatoOps = new GelatoOpsSDK(chainId, signer);
The instructions below are for the latest version of the Automate SDK. If you are using an older version (prior to 2.0), the dedicatedMsgSender option is not available.
Use createTask to automate your function calls:
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 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 };
const { taskId, tx }: TaskTransaction = await gelatoOps.createTask(params);

Examples

Deploy a contract & automate your function call:

// Deploying Counter contract
const counterFactory = await hre.ethers.getContractFactory("Counter");
const counter = await counterFactory.deploy(GELATO_ADDRESSES[chainId].ops);
await counter.deployed();
// Call Counter.increaseCount(42) every 10 minutes
const { taskId, tx }: TaskTransaction = await gelatoOps.createTask({
execAddress: counter.address,
execSelector: counter.interface.getSighash("increaseCount(uint256)"),
execData: counter.interface.encodeFunctionData("increaseCount", [42]),
execAbi: counter.interface.format("json") as string,
interval: 10 * 60, // execute every 10 minutes
name: "Automated counter every 10min",
dedicatedMsgSender: true
});

Use a resolver contract to automate your function call:

If you need more configurable execution condition and/or dynamic input data, you can create a task using a resolver function (learn how to write a resolver).
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const resolver = new Contract(COUNTER_RESOLVER_ADDRESSES, counterResolverAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = resolver.interface.getSighash("checker()");
// Create task
const { taskId, tx }: TaskTransaction = await gelatoOps.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: resolver.address,
resolverData: resolverData,
name: "Automated counter using resolver",
dedicatedMsgSender: true
});

Enable dedicated msg.sender:

To have a custom msg.sender that you can whitelist on your contract, you can enable the dedicatedMsgSender flag.
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const resolver = new Contract(COUNTER_RESOLVER_ADDRESSES, counterResolverAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = resolver.interface.getSighash("checker()");
// Create task
const { taskId, tx }: TaskTransaction = await gelatoOps.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: resolver.address,
resolverData: resolverData,
dedicatedMsgSender: true,
name: "Automated counter using resolver",
});
// Get dedicated proxy address to whitelist
const { address, isDeployed } = await gelatoOps.getDedicatedMsgSender()

More examples in our Hello World repository: