LogoLogo
MidpointContact UsThe Docs are Wrong!
  • What is Midpoint
  • What actually is a midpoint?
  • Midpoint FAQs
  • Get Started
    • Sample Midpoints
      • Filecoin Filrep Oracle: Obtain miner reputations on-chain
      • Oracle: Call any API from a smart contract
      • Listener: Subscribe an internal service to any event
      • Data Feed: Stream a data feed on-chain at regular intervals
      • Data Bridge: Pass arbitrary messages between chains
      • Automation: Ensure a function is called at regular intervals
    • Videos
  • Using Midpoint
    • API Key
    • Midpoint CLI
    • GraphQL API
    • Dashboard
  • Core Concepts
    • Midpoints
      • Midpoint ID
      • Sources
      • Tasks
      • Source and Task Definitions
        • Secrets
        • Variables
        • Exclude Logs
        • Extracts
    • Requests
      • Request ID
      • Events
    • Credits and Payment
  • Sources
    • Startpoint Called
    • EVM Event Emitted
    • Cron Job Triggered
  • Tasks
    • Make HTTP Request
      • OAuth and Multi-Step Authentication
    • Call EVM Function
    • Transact to EVM Function
      • Security
    • Shift Decimal
  • More Reading
    • Chain IDs
    • Startpoints
    • Midpoint EOAs
  • Get in Touch
    • Contact Links
Powered by GitBook
On this page
  • Overview
  • Midpoint

Was this helpful?

  1. Get Started
  2. Sample Midpoints

Listener: Subscribe an internal service to any event

Overview

A Listener is a midpoint that listen for an event from a contract and makes an HTTP request based on the values from the event values.

As an example of a Listener, we will define a midpoint that sends Slack notification when a user borrows a token on Aave V2.

EVMEventEmittedSource:

  1. Listen for the deposit event to be emitted

  2. Extract the sender, recipient address and amount to be send to the MakeHttpRequestTask

MakeHttpRequestTask:

  1. Notify the user via Slack using a Slack webhook

Midpoint

$ npm install -g midpoint-cli

$ midpoint init blank my-listener

1 . EVMEventEmittedSource

$ midpoint add-source evmEventEmittedSource listener-source

We define the EVM Event Emitted source so that Midpoint can listen for the borrow event.

{
    "evmEventEmittedSource": {
      "chainId": "1",
      "contractAddress": "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9",
      "eventName": "Borrow",
      "arguments": [
        {
          "name": "reserve",
          "datatype": "address",
          "indexed": true
        },
        {
          "name": "user",
          "datatype": "address",
          "indexed": true
        },
        {
          "name": "onBehalfOf",
          "datatype": "address",
          "indexed": false
        },
        {
          "name": "amount",
          "datatype": "uint256",
          "indexed": false
        },
        {
          "name": "borrowRateMode",
          "datatype": "uint256",
          "indexed": false
        },
        {
          "name": "borrowRate",
          "datatype": "uint256",
          "indexed": false
        },
        {
          "name": "referral",
          "datatype": "uint16",
          "indexed": true
        }
      ]
    }
}

2. CallEvmFunction Task

$ midpoint add-task callEvmFunction get-erc20-name

We define the Call EVM Function task to retrieve the ERC20 name of the token that was borrowed.

{
  "callEvmFunction": {
      "chainId": "1",
      "contractAddress": "{{reserve}}",
      "functionName": "name",
      "arguments": [],
      "returnValues": [
        {
          "name": "erc20-name",
          "datatype": "string"
        }
      ]
    }
}

3. MakeHttpRequest Task

$ midpoint add-task makeHttpRequest send-to-slack

We define the Make HTTP Request task to notify users via Slack message using the Slack Webhook.

$ midpoint add-secret <slack webhook url> WEB_HOOK_URL

{
    "makeHttpRequest": {
        "urlRaw": "https://hooks.slack.com/services/[[WEB_HOOK_URL]]",
        "method": "POST",
        "urlType": "raw",
        "bodyType": "json",
        "body": "{\"text\": \"The deposit function was called by {{user}} for {{amount}} on the Apecoin contract\"}"
    }
}

4. Define Path

We define our path file.

listener-source => get-erc20-name
get-erc20-name => send-to-slack

5. Publish Midpoint

$ midpoint publish

PreviousOracle: Call any API from a smart contractNextData Feed: Stream a data feed on-chain at regular intervals

Last updated 2 years ago

Was this helpful?

To add WEB_HOOK_URL as a :

secret