Automation: Ensure a function is called at regular intervals

Overview

The CronJobTriggered source allows you to invoke any public function on any Ethereum Virtual Machine (EVM) blockchain network at a regular interval.

Setup

To create an Automation Midpoint, you need to know:

  • the interval you need to make the calls

  • the chain-id of the chain where the contract lives

  • the address of the contract you want to call

  • the signature of the function

Defining the interval

The CronJobTriggered source is based on the Unix utility cron, which is used to schedule commands or scripts to run automatically at specified intervals. It is controlled by a line of text, consisting of five fields, representing what minute, hour, day of the month, month, and day of the week the function should be called. For example, to request that the function be called every hour on the hour, the line is 0 * * * * . If you wanted it called at two o’clock in the morning every Monday, use 0 2 * * 1. To learn more about cron and cron-compatible strings, please refer to the Wikipedia page for cron.

The chain-id

You can look up the chain-id for the chain you want to use here: Chain IDs

The address of the contract

Remix or whatever tool you used to create the contract will give you the address. It’s a 40-digit hex number, like this : 0xc1bebd66dfde46788c78f38b6f402d2ef17a9f9c

The signature

The “signature” (nothing to do with a cryptographic signature) of a function consists of

  • its name

  • the number of arguments

  • the type of each argument

You will also need to select value to give each argument. The same value will be sent each time, unless you want to send the current time (called the “Timestamp”), since no new data is coming from the outside.

Midpoint

1 . Cron Job Triggered

$ midpoint add-source cronJobTriggeredSource cron-job-source

We define the Cron Job Triggered source so that contract can make a midpoint request.

{
  "cronJobTriggeredSource": {
    "crontab": "* 12 * * *"
    "extracts": [
      {
        "name": "currentTime",
        "from": "{{Timestamp}}"
      }
    ]
  }
}

2. Transact To EVM Function

$ midpoint add-task transactToEvmFunctionTestnet post-time-on-blockchain

We define the Transact to EVM Function Task to send the summary back to the contract.

{
  "transactToEvmFunctionTestnet": {
    "chainId": "5",
    "contractAddress": "0x83986ff4fbcaa2acf6092930fcebbe25583be452",
    "functionName": "heartbeat",
    "arguments": [
      {
        "name": "time",
        "datatype": "uint256",
        "value": "{{currentTime}}"
      }
    ]
  }
}

3. Define Path

We define our path file.

cron-job-source => post-time-on-blockchain

4. Publish Midpoint

$ midpoint publish

Last updated