Automate smart contract transaction for hourly activity

Defender 2.0 allows you to automate smart contract operational tasks with easy integration with the rest of Defender 2.0. This tutorial shows how to build an action that sends an on-chain transaction every hour that adds an object to a box and increases the number of objects inside.

Pre-requisites

  • OpenZeppelin Defender 2.0 account. You can sign up for early access to Defender 2.0 here.

Learn to deploy and monitor contracts through Defender 2.0!

1. Set up action

You will configure an action that sends an hourly transaction with the addObject() function to the 0x7A99C479775C945C2Ea4CF6986425de1d816DaE4 contract in Goerli, which is an example of a Box contract deployed in the Deploy tutorial. To do so, follow these steps:

  1. Run the following command in your terminal:

  2. Open Defender 2.0 Manage in a web browser.

  3. Click on Create Relayer with the name Actions Relayer and Goerli network.

  4. Fund it with some Goerli ETH. This relayer will send and pay for the automated transactions.

  5. Open Defender 2.0 Actions.

  6. Click on Create Action.

  7. Name the action Hourly object add.

  8. Select Schedule as trigger, and a Timespan of 1 hour as schedule.

  9. Select the Actions Relayer as the connected relayer.

  10. Paste the following code in the Code field:

    const { Defender } = require('@openzeppelin/defender-sdk');
    
    exports.handler = async function(credentials) {
      const client = new Defender(credentials);
    
      const txRes = await client.relaySigner.sendTransaction({
        to: '0x7A99C479775C945C2Ea4CF6986425de1d816DaE4',
        speed: 'fast',
        data: '0x62029d2a',
        gasLimit: '80000',
      });
    
      console.log(txRes);
      return txRes.hash;
    }

    The contract address is the target, which is 0x7A99C479775C945C2Ea4CF6986425de1d816DaE4, and data is 0x62029d2a, the encoded version of the addObject() function of the contract.

  11. Click on Save Action.

After saving, the Actions page should look like this:

Actions action card

Your action will now be running every hour! You can check the Defender 2.0 Logs for more detailed information about the activity.

2. Verify activity

After the action has run for a while, you can verify that the transaction is being sent every hour. To do so, open the Etherscan contract page and look for transactions from the configured Relayer. An alternative is to search your Relayer in Etherscan and look for the transactions sent to the contract.

You will also receive alerts in case the action fails (for example, if the Relayer runs out of gas). They look like this:

Actions alert

Next steps

Congratulations! You can modify the action to automate other contracts and build more complex transactions. In case you are interested in advanced use cases, we are working on actions-related guides.

Alongisde actions, we recommend using Access Control to manage a contract’s permissions through Defender 2.0. Learn how to use Access Control with its tutorial here.