Example task automation using Gelato Automate SDK:
- Deploy a contract & automate your function call
- Configure your task execution:
msg.sender
of task executions- Manage your tasks
- Check automate-sdk npm package page to know more about how to use the Gelato Automate SDK
- Install project dependencies:
yarn install
- Create a
.env
file with your private config:
PRIVATE_KEY=
ALCHEMY_ID= <- required for goerli
- Use
automate.createTask
and specify your contract call withexecAddress
,execSelector
&execData
:
// Deploying Counter contract
const counterFactory = await hre.ethers.getContractFactory("Counter");
const counter = await counterFactory.deploy(GELATO_ADDRESSES[chainId].automate);
await counter.deployed();
// Call Counter.increaseCount(42) every 10 minutes
const { taskId, tx }: TaskTransaction = await automate.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
});
- Check the example source code
examples/deploy-create-task.ts
and try it yourself using:
yarn run deploy-create-task --network goerli
- Use
automate.createTask
and specify your contract call withexecAddress
,execSelector
&execData
:
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const data = counter.interface.encodeFunctionData("increaseCount", [42]);
// Create task
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
execData: data,
name: "Automated Counter with pre-defined input",
dedicatedMsgSender: true
});
- Check the example source code
examples/create-task-predefined-input.ts
and try it yourself using:
yarn run create-task-predefined-input --network goerli
- Use
automate.createTask
and specify your resolver function withresolverAddress
&resolverData
:
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = counter.interface.getSighash("checker()");
// Create task
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: counter.address,
resolverData: resolverData,
name: "Automated counter using resolver",
dedicatedMsgSender: true
});
- Check the example source code
examples/create-task-with-resolver.ts
and try it yourself using:
yarn run create-task-with-resolver --network goerli
- Use
automate.createTask
with your executioninterval
& set your optionalstartTime
:
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const execData = counter.interface.encodeFunctionData("increaseCount", [42]);
const startTime = Math.floor(Date.now() / 1000) + 60; // start in 1 minute
const interval = 5 * 60; // exec every 5 minutes
// Create task
console.log("Creating Timed Task...");
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
execData,
startTime, // starting timestamp in seconds
interval, // execution interval in seconds
name: "Automated counter every 5min",
dedicatedMsgSender: true
});
- Check the example source code
examples/create-timed-task.ts
and try it yourself using:
yarn run create-timed-task --network goerli
- Use
automate.createTask
with your executioninterval
& set your resolver function withresolverAddress
&resolverData
:
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = counter.interface.getSighash("checker()");
const interval = 5 * 60; // exec every 5 minutes
// Create task
console.log("Creating Timed Task...");
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: counter.address,
resolverData: resolverData,
interval, // execution interval in seconds
name: "Automated counter with resolver every 5min",
dedicatedMsgSender: true
});
- Check the example source code
examples/create-timed-task-with-resolver.ts
and try it yourself using:
yarn run create-timed-task-with-resolver --network goerli
- Use
automate.createTask
and setuseTreasury: false
to let the task pay for itself:
// Prepare Task data to automate
const counter = new Contract(COUNTER_WITHOUT_TREASURY_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = counter.interface.getSighash("checker()");
// Create task
console.log("Creating Task...");
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: counter.address,
resolverData: resolverData,
useTreasury: false,
name: "Automated Counter without treasury",
dedicatedMsgSender: true
});
- Check the example source code
examples/create-self-paying-task.ts
and try it yourself using:
yarn run create-self-paying-task --network opgoerli
- Use
automate.createTask
and setsingleExec: true
for tasks that only need to be executed once. The task is automatically cancelled on the first execution.
// Prepare Task data to automate
const counter = new Contract(COUNTER_ADDRESSES, counterAbi, signer);
const selector = counter.interface.getSighash("increaseCount(uint256)");
const resolverData = counter.interface.getSighash("checker()");
// Create task
const { taskId, tx }: TaskTransaction = await automate.createTask({
execAddress: counter.address,
execSelector: selector,
resolverAddress: counter.address,
resolverData: resolverData,
dedicatedMsgSender: true,
name: "Automated counter using resolver",
dedicatedMsgSender: true,
singleExec: true
});
If you set dedicatedMsgSender: true
, your task will be called via a dedicated msg.sender
which you can whitelist on your smart contract for extra security.
To get your dedicated msg.sender
:
// Get dedicated msg.sender to whitelist
const { address, isDeployed } = await automate.getDedicatedMsgSender()
If dedicatedMsgSender: false
, the msg.sender
of the task will be Automate contract.
- Use
automate.getActiveTasks
to retrieve all active that you created:
const activeTasks = await automate.getActiveTasks();
activeTasks.forEach((task: Task) => {
console.log(`- ${task.name} (${task.taskId})`);
});
- Use
automate.renameTask
to rename one of your task:
const task: Task = activeTasks[0];
await automate.renameTask(task.taskId, `[RENAMED] ${task.name}`);
- Use
automate.cancelTask
to cancel one of your task:
const task: Task = activeTasks[0];
await automate.cancelTask(task.taskId);
- Check the example source code
examples/manage-tasks.ts
and try it yourself using:
yarn run manage-tasks --network goerli