Giter VIP home page Giter VIP logo

external-adapters-js-19's Introduction

Chainlink External Adapters (JavaScript)

This repository contains the source for Chainlink external adapters. Each adapter must document its own required parameters and output format.

Requirements

  • Yarn

Install

yarn

Installs packages for all workspaces.

Setup

yarn setup

Runs the setup step for all adapters. Typically this step just compiles TypeScript, but may involve other tasks.

Test

In order to test adapters locally, you may need to set an $API_KEY environment variable for the given API.

cd $adapter
yarn test

Create a new adapter

Run the command below to have the example directory cloned using the name you provide for $adapter:

make new adapter=my-adapter-name

If on a Mac, this requires gnu-sed to be installed and set as the default for the command sed.

Input

When flux monitor or OCR jobs post to external adapters, the request body looks as follows:

{
  "id": "2cae6a10e5184aa685c3428964b02418",
  "data": { "from": "ETH", "to": "USD" },
  "meta": {
    "latestAnswer": 39307000000,
    "updatedAt": 1616448197,
  }
}

The updatedAt field is a unix timestamp representing when the latestAnswer was computed.

Optionally data parameters can also be passed via a query string like: {ENDPOINT}?from=ETH&to=USD

Docker

To build a Docker container for a specific $adapter, use the following example:

make docker adapter=bravenewcoin

The naming convention for Docker containers will be $adapter-adapter.

Then run it with:

docker run -p 8080:8080 -e API_KEY='YOUR_API_KEY' -it bravenewcoin-adapter:latest

Serverless

Create the zip:

make zip adapter=bravenewcoin

The zip will be created as ./$adapter/dist/$adapter-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 $adapter-adapter.zip file
  • Handler:
    • index.handler (same as index.awsHandlerREST) for REST API Gateways (AWS Lambda default)
    • index.awsHandlerREST for REST API Gateways
    • index.awsHandlerHTTP for HTTP API Gateways
  • Add the environment variable (repeat for all environment variables):
    • Key: API_KEY
    • Value: Your_API_key
  • Save

By default, Lambda functions time out after 3 seconds. You may want to change that to 60s in case an API takes longer than expected to respond.

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
  • Click the Mapping Templates dropdown
  • Check "When there are no templates defined (recommended)"
  • Add new Content-Type application/json
  • Use Mapping Template:
#set($input.path("$").queryStringParameters = $input.params().querystring)
$input.json('$')
  • Click Save
  • 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
  • Select Node.js 12 for the Runtime
  • Click Browse and select the $adapter-adapter.zip file
  • Select a Storage Bucket to keep the zip in
  • Function to execute: gcpHandler
  • Click More, Add variable (repeat for all environment variables)
    • NAME: API_KEY
    • VALUE: Your_API_key

Multiple API Key Support

In order to use multiple API keys for an adapter, simply comma delimit the keys where you define the environment variable. This will work for an arbitrary number of keys.

API_KEY=myapikey1,myapikey2,myapikey3

The external adapter will then randomly rotate the keys. Over time this should balance out the number of requests between each of the API keys.

external-adapters-js-19's People

Contributors

boxhock avatar justinkaseman avatar krebernisak avatar thodges-gh avatar rodrigoad avatar ebarakos avatar henrynguyen5 avatar emmick4 avatar emedvedev avatar yonine avatar nzleet avatar tyrion70 avatar dependabot[bot] avatar saschagoebel avatar skubakdj avatar rupurt avatar se3000 avatar vietthang207 avatar aalu1418 avatar micdorn avatar deividask avatar momentmaker avatar gupadhyaya avatar johnnymugs avatar rstormsf avatar vladi-coti avatar

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.