Automations
Triggers
Event based

How to use the event-based trigger

The event-based trigger is a trigger that is executed when a specific event occurs on the blockchain. This trigger is useful when you want to automate a specific action based on a specific event, such as ERC-20 transfers.

Create the automations client

First, create an instance of the Automations client:

import { createAutomationClient } from "@rhinestone/automations-sdk";
 
const automationClient = createAutomationClient({
  account: "0x...",
  accountType: 'SAFE',  // 'SAFE', 'KERNEL',
  apiKey: "YOUR_API_KEY",
  accountInitCode: "0x",
  network: 11155111,
  validator: "0x...",
});

Create the automation details

Next, create the automation details:

const actions = [
  {
    type: "static",
    target: "0x503b54Ed1E62365F0c9e4caF1479623b08acbe77",
    value: 100,
    callData: "0x",
  },
];
 
const maxNumberOfExecutions = 10;

Here, the actions are an array of actions to make on behalf of the account. They can either be static, in which case all the calldata is pre-defined, or dynamic, in which case the calldata is generated by a call to a calldata builder service. The maxNumberOfExecutions is the number of times the automation will be executed.

Create the event based trigger details

Next, create the event based trigger details:

triggerData: {
  query: '{\n  block {\n  transactions (filter:{addresses:{to:"0xc2b17e73603dccc195118a36f3203134fd7985f5"}}) {\n      value,\n      hash,\n      from {\n        address\n      }\n    }\n  }\n}';
}

Here, the query is a GraphQL query that defines the filter for the events. Check out the GraphQL API (opens in a new tab) to get the required query.

Create the automation

Finally, create the automation:

const automation = await automationClient.createAutomation({
  type: "event-based",
  data: {
    trigger: {
      triggerData,
    },
    actions,
    maxNumberOfExecutions,
  },
});