Giter Site home page Giter Site logo

cl-ea-nodejs-template's Introduction

Chainlink NodeJS External Adapter Template

This template provides a basic framework for developing Chainlink external adapters in NodeJS. Comments are included to assist with development and testing of the external adapter. Once the API-specific values (like query parameters and API key authentication) have been added to the adapter, it is very easy to add some tests to verify that the data will be correctly formatted when returned to the Chainlink node. There is no need to use any additional frameworks or to run a Chainlink node in order to test the adapter.

Creating your own adapter from this template

Clone this repo and change "ExternalAdapterProject" below to the name of your project

git clone https://github.com/thodges-gh/CL-EA-NodeJS-Template.git ExternalAdapterProject

Enter into the newly-created directory

cd ExternalAdapterProject

You can remove the existing git history by running:

rm -rf .git

See Install Locally for a quickstart

Input Params

  • base, from, or coin: The symbol of the currency to query
  • quote, to, or market: The symbol of the currency to convert to

Output

{
 "jobRunID": "278c97ffadb54a5bbb93cfec5f7b5503",
 "data": {
  "USD": 164.02,
  "result": 164.02
 },
 "statusCode": 200
}

Install Locally

Install dependencies:

yarn

Test

Run the local tests:

yarn test

Natively run the application (defaults to port 8080):

Run

yarn start

Call the external adapter/API server

curl -X POST -H "content-type:application/json" "http://localhost:8080/" --data '{ "id": 0, "data": { "from": "ETH", "to": "USD" } }'

Docker

If you wish to use Docker to run the adapter, you can build the image by running the following command:

docker build . -t external-adapter

Then run it with:

docker run -p 8080:8080 -it external-adapter:latest

Serverless hosts

After installing locally:

Create the zip

zip -r external-adapter.zip .

Install to AWS Lambda

  • In Lambda Functions, create function
  • On the Create function page:
    • Give the function a name
    • Use Node.js 12.x for the runtime
    • Choose an existing role or create a new one
    • Click Create Function
  • Under Function code, select "Upload a .zip file" from the Code entry type drop-down
  • Click Upload and select the external-adapter.zip file
  • Handler:
    • index.handler for REST API Gateways
    • index.handlerv2 for HTTP API Gateways
  • Add the environment variable (repeat for all environment variables):
    • Key: API_KEY
    • Value: Your_API_key
  • Save

To Set Up an API Gateway (HTTP API)

If using a HTTP API Gateway, Lambda's built-in Test will fail, but you will be able to externally call the function successfully.

  • Click Add Trigger
  • Select API Gateway in Trigger configuration
  • Under API, click Create an API
  • Choose HTTP API
  • Select the security for the API
  • Click Add

To Set Up an API Gateway (REST API)

If using a REST API Gateway, you will need to disable the Lambda proxy integration for Lambda-based adapter to function.

  • Click Add Trigger
  • Select API Gateway in Trigger configuration
  • Under API, click Create an API
  • Choose REST API
  • Select the security for the API
  • Click Add
  • Click the API Gateway trigger
  • Click the name of the trigger (this is a link, a new window opens)
  • Click Integration Request
  • Uncheck Use Lamba Proxy integration
  • Click OK on the two dialogs
  • Return to your function
  • Remove the API Gateway and Save
  • Click Add Trigger and use the same API Gateway
  • Select the deployment stage and security
  • Click Add

Install to GCP

  • In Functions, create a new function, choose to ZIP upload
  • Click Browse and select the external-adapter.zip file
  • Select a Storage Bucket to keep the zip in
  • Function to execute: gcpservice
  • Click More, Add variable (repeat for all environment variables)
    • NAME: API_KEY
    • VALUE: Your_API_key

cl-ea-nodejs-template's People

Contributors

patrickalphac avatar thodges-gh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cl-ea-nodejs-template's Issues

Issue requesting external adaptor from on-cahin smart contract

Hi Team, I am new to external adaptor. I appoligise if I am asking a dumb question. I created an external adaptor (by the nodejs template) --> created bridge on chianlink node --> created a new job to query the oracle which correspond with my chainlink node. But the error msg shows on job run tab saying "error making http request: Post "http://localhost:8080": dial tcp 127.0.0.1:8080: connect: connection refused". I appricate if somebody can give me a hint which part should I do the debug (there are too many elements are involved)? I also attached the related screenshot below.
My adaptor is listening on localhost:8080 port in the same server as my chainlink node.
Screen Shot 2021-10-30 at 20 09 56

I query the external adaptor locally and it successfully returns the result. like the attached screenshot. But it failed with the above error when I call it through the oracle contract. Hope somebody could give me a hint
Screen Shot 2021-10-30 at 22 21 44

new Validator (callback, input, customParams) is out of date with @chainlink\external-adapter Validator class

Currently, a validator object is created in createRequest in line 22 with

const validator = new Validator(callback, input, customParams)

However, the validator.js class constructor shows as

constructor(input = {}, customParams = {}) {
    this.input = input
    this.customParams = customParams
    this.validated = { data: {} }
    this.validateInput()
  }

So the external adapter template needs to be updated to

const validator = new Validator(input, customParams)

Otherwise the tests/calls will always fail with the error:

"{"error":{"jobRunID":"1","status":"errored","statusCode":400,"name":"AdapterError","cause":{}},"level":"error","message":"Error validating input.","instanceId":"fc8f1c9b-bcb4-4a2f-ade5-f8d85a116582","timestamp":"2021-06-10T01:48:11.433Z"}"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.