Plugin,a Decentralized Oracle built on XDC Network
  • Migrating from Plugin Node V2 to V2.4 Without Changing the Node Address
    • 1 - Backup on Plugin Node V2.0
    • 2 - Installation and Configuration of Plugin Node V2.4
    • 3 - Importing PluginV2 Node Address into PluginV2.4
    • 4. Backup After Plugin V2.4 Upgrade
  • Plugin 2.0 - Node Operators
    • Introduction
    • Unstake PLI Tokens from Plugin 1.0 Node
    • Plugin 2.0 Set Up Requirements
    • Node Installation
      • Node Fulfillment
      • Job Setup
        • Steps to Setup Direct Request Job
      • Process of Approval
    • How to Update Your Plugin Node for the Latest XDC Gas Fee
  • PLUGIN 2.0 - Validators
    • Introduction
    • Set-up Requirements
    • Job Setup
      • Steps to Setup Direct Request Job
      • Flux Monitor Job
        • Idle Timer
        • Drum Beat
        • Poll Timer
        • POLL TIMER + IDLE TIMER (Recommended)
      • Process Of Approval
      • Rewards Information
    • OCR Set-up
    • How to Update Your Plugin Node for the Latest XDC Gas Fee
  • WALLET
    • XDCPay - Apothem
    • XDCPay - Mainnet
  • PLUGIN VRF SUBSCRIPTION
    • Introduction
    • Guidance on Utilizing Random Values
    • Creation and Deployment of VRF-Consumer Contract
    • Requesting Random Values
  • Multichain
    • PLISwap - How to instructions
  • Plugin Data Feeds platform
    • Introduction
    • End data consumers - Mainnet
    • End data consumers - Apothem
    • Benefits
  • Restaking Model
    • Introduction
      • Scenarios in Re-staking Model
      • Benefits To Node Members
      • Steps for Restaking
  • Tips and tricks
    • Known-Issues
    • Open-Issues
  • Plugin 1.0 Installations - Deprecated
    • How to install Plugin 1.0 Node
      • Modular Method Deployment (Recommended Approach)
      • Script Method (Legacy)
        • Script - Phase 1
        • Script - Phase 2
      • Docker Method
        • Docker - Phase 1
        • Docker - Phase 2
    • Core Adapters
    • Fund your Node
  • External Initiators - Deprecated
    • Introduction
    • Installation
    • Setup & Build
  • Oracle Plugin 1.0 - Deprecated
    • Deployment
    • Fulfillment Request
    • Job-Setup
    • Testing
  • Job-Tasks Plugin 1.0 Deprecated
    • Sleep
    • Get > Bytes32
    • HttpGet
    • CRON
    • Web
  • External Adapters Plugin 1.0 - Deprecated
    • Introduction
    • Implement External -Adapters
    • Define Bridge
    • Add Bridge to Job Spec
  • Deprecated
    • How to use
  • Use Cases
    • Plugin WFN Use Case
      • Plugin WFN Data Review
      • Plugin WFN Use Case - FAQ
      • Plugin WFN - Ambient Weather Unit Onboarding Instructions
      • Plugin WFN - Acurite Weather Unit Onboarding Instructions - LEGACY
      • Unlock WFN Node
    • Crypto Compare - Pricing Index
  • Node Operators
    • How to Register / Sign-up
    • How to enable 2FA
    • How to update profile
    • How to add XDC Wallet Address
    • How to Navigate Dashboards
    • How to submit Node Details
    • How to stake PLI token for Plugin Node
    • How to add Job to your node
    • View the node details
    • De-Activate / Re-activate my nodes
    • Withdraw staked PLI
    • Withdraw PLI from Plugin Node
    • Withdraw PLI from Oracle contract
    • How to add more stake in Node?
    • How to Migrate my Plugin Node to New Server?
    • How to Un-Stake the node.
    • How a Reputation is calculated?
    • Node Maintenance Instructions for node operators
  • FEATURES
    • PLI Yield Farming
      • Steps for Staking in Plugin Yield Farming (PLIYF)
      • PLIYF - FAQ
  • Support
    • FAQ
  • RESOURCES
    • Node Setup Video Tutorials
    • Community Supports
    • How to Submit your node details - Video Tutorial
  • CHANGELOG
    • v1.0.1
    • V1.0.2
    • V1.0.3
    • V1.0.4
    • V1.05
  • Terms and Conditions
Powered by GitBook
On this page
  1. External Adapters Plugin 1.0 - Deprecated

Implement External -Adapters

For the demo purpose, we will setup our external adapter to pull the data from weather API. Follow along the following tutorial to understand how it works.

Setting up external initiator requires the following steps to be performed

  • Git clone the repository

  • cd & npm install

  • Include the API endpoint & Parse the payload

  • Run the server

Step 1 -

git clone https://github.com/GoPlugin/external-adapter-template.git

Step 2 -

cd weather_adapter

Step 3 -

npm install

Step 4 -

map the api endpoint

Requesting Data

When an external adapter receives a request from the Plugin node, the JSON payload will include the following objects:

  • data (Guaranteed to be present but may be empty)

  • meta (Optional, depends on job type)

  • responseURL (Optional, will be supplied if job supports asynchronous callbacks)

  • id (Optional, can be nice to use for EA logging to help debug job runs)

Returning Data

When the external adapter wants to return data immediately, it must include data in the returned JSON.

An example of the response data can look like this:

{
  "data": {
    "result": "20.04"
  }
}

Note - The external adapter that you are running, should be the same URL endpoint mapped in Bridge.

const { Requester } = require('@goplugin/external-adapter')
require("dotenv").config();

const customError = (data) => {
  if (data.Response === 'Error') return true
  return false
}

const customParams = {
  endpoint: ['endpoint']
}

const createRequest = (input, callback) => {

  const url = `http://<host>/api/${input.data.endpoint}`

  const config = {
    url
  }

  if (process.env.API_KEY) {
    config.headers = {
      Authorization: process.env.API_KEY
    }
  }
  Requester.request(config, customError)
    .then(response => {

      if (input.data.envCheck == "WindDirection") {
        var resultData = response.data[0]['windDirection'];
      } else if (input.data.envCheck == "Temperature") {
        var resultData = response.data[0]['tempC'];
      } else if (input.data.envCheck == "WindChill") {
        var resultData = response.data[0]['windChillC'];
      }
      response.data.result = resultData.toString();
      
      const res = {
        data: {
          "result": response.data.result.toString()
        }
      }
      callback(response.status, Requester.success(input.id, res));
    })
    .catch(error => {
      callback(500, Requester.errored(input.id, error))
    })
}

module.exports.createRequest = createRequest

Let's see what's in "index.js"

  • We are importing 'external-adapter' from goplugin npm using importing methods such as Requester

  • Importing "dotenv" module to access the .env file for sensitive info (API_KEY).

  • "customError" is a function to return either true or false if an error occurs.

  • "customParams" is a variable to declare parameters to pass on in URL - this can be customized accordingly.

  • "createRequest" is the method actually doing the heavy lifting.

    • URL is getting defined since for the weather node the endpoint is getting passed via smart contract the "endpoint" is accessed via "input.data.endpoint".

    • <host> to be replaced with a respective domain name or IP address where we are fetching the data

    • api -> router name. Again for our weather node, we have API as rrouter name for your case it can be a different

    • config -> variable name to have url parm

    • Next, we are checking APIKEY if exists in env parm, else it will not be applied. For authenticated API, you have to pass APIKEY

    • Requester.request will scan for the payload from the API endpoint and customize the output .. In this example, we are checking "input.data.envCheck".

      • Note that, envCheck is a parameter we are passing via smart contract(Check weather node)

      • Based on envCheck, the loop iterates and scans the value from the output payload

      • Finally we are constructing the response and return

When you run PM2 Start server.js, the server will by default run in port 5001 and just make sure the bridge is correctly pointed out to this port number. For instance, this should be http://localhost:5001/

PreviousIntroductionNextDefine Bridge

Last updated 2 years ago