In a previous tutorial, we saw how to receive an email every time a transfer on a specific ERC-20 was triggered.

In that tutorial we used the MESG Engine's API to do it. The Engine's API provides all information needed do do this, but doesn't let you run the application completely autonomously, and you need to make sure your application is always running.

In this tutorial we will show you how to recreate the same application, but let the Engine run and manage it for you. By allowing the Engine to run the application, it will also be able to be decentralized without any additional steps.

Let's get started.

What do we want to build?

We want to listen to all transfer events from a specific smart contract and send an email for each one of them.

In order to do that, we will split this tutorial into two parts to create our process.

  • The triggering of the process (the ERC-20 event we want to listen to)
  • The task to execute (the email we want to send)

How do we build this?

We will use the MESG Orchestrator and will create a process file.

ERC-20 Transfer trigger

Let's create our process file.

key: erc20-notifications
steps:
  - type: trigger
    instance:
      src: https://github.com/mesg-foundation/service-ethereum-erc20
      env:
        - PROVIDER_ENDPOINT=$(env:PROVIDER_ENDPOINT)
        - CONTRACT_ADDRESS=0xdac17f958d2ee523a2206206994597c13d831ec7
    eventKey: transfer

Here, we create our first step of our process that is a trigger. We listen for all the events transfer of the service https://github.com/mesg-foundation/service-ethereum-erc20 that is running with the environment variables:

Send the email

Let's add the next task that will send an email each time our event is triggered. We will use the SendGrid service https://github.com/mesg-foundation/service-email-sendgrid to send an email.

key: erc20-notifications
steps:
  ...
  - type: task
    instance:
      src: https://github.com/mesg-foundation/service-email-sendgrid
      env:
        - SENDGRID_API_KEY=$(env:SENDGRID_API_KEY)
    taskKey: send
    inputs:
      from: "test@erc20notification.com"
      to: "__YOUR_EMAIL_HERE__"
      subject: "New ERC20 transfer"
      text:
        key: transactionHash

We just added another step of type task that will use the service https://github.com/mesg-foundation/service-email-sendgrid configured with the environmental variable SENDGRID_API_KEY that will be set during the deployment.

From this service, the task send will be called with a specific set of inputs:

  • from: A constant value for the sender of the email
  • to: The email you want to use to receive your notification
  • subject: A subject for your email
  • text: The text of your email.

The text input is an actual reference to the value transactionHash from our event. Every time a email will be sent, the value of the text will actually be the value of the transactionHash from the event.

The full process should look like this:

key: erc20-notifications
steps:
  - type: trigger
    instance:
      src: https://github.com/mesg-foundation/service-ethereum-erc20
      env:
        - PROVIDER_ENDPOINT=$(env:PROVIDER_ENDPOINT)
        - CONTRACT_ADDRESS=0xdac17f958d2ee523a2206206994597c13d831ec7
    eventKey: transfer
  - type: task
    instance:
      src: https://github.com/mesg-foundation/service-email-sendgrid
      env:
        - SENDGRID_API_KEY=$(env:SENDGRID_API_KEY)
    taskKey: send
    inputs:
      from: "test@erc20notification.com"
      to: "__YOUR_EMAIL_HERE__"
      subject: "New ERC20 transfer"
      text:
        key: transactionHash

Yes that's right, with just these 20 lines of "code" you will be able to receive an email notification for each transfer on a specific ERC-20.

Compile and run the process

Now that our process file is ready, we only need to do one thing: deploy it on the MESG Engine to let MESG run this process for you.

mesg-cli process:dev erc20-notifications.yml --env PROVIDER_ENDPOINT=https://mainnet.infura.io/v3/d75ab9cb284f4536b1da2ce9f8201bdb --env SENDGRID_API_KEY=__SENDGRID_API_KEY__

Don't forget to change the __SENDGRID_API_KEY__ to one that you can create here.

Now you should start to receive emails every time an ERC-20 transfer occurs on the smart contract: 0xdac17f958d2ee523a2206206994597c13d831ec7.

If you want to stop the process, just exit it by pressing CTRL+C or CMD+C. And if you want to run it fully, you can do so with the following command:

mesg-cli process:create "$(mesg-cli process:compile erc20-notifications.yml --env PROVIDER_ENDPOINT=https://mainnet.infura.io/v3/d75ab9cb284f4536b1da2ce9f8201bdb --env SENDGRID_API_KEY=__SENDGRID_API_KEY__ --dev)"

This will compile and deploy it in your Engine and your process will run inside your Engine so you won't need to worry about anything anymore. Also, by deploying your process, your application is ready to be decentralized without changing anything and every execution that your process does will be distributed on the network.