Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SDR adapter for AlphaVantage #35

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions alphavantage-sdr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Chainlink External Adapter for AlphaVantage (SDR)

## 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

```json
{
"jobRunID":"1",
"data":{
"data":{
"from_symbol":"ETH",
"last_refreshed":"2020-05-29 08:45:03 +0000 UTC",
"rate":301.3654364,
"time_zone":"UTC",
"to_symbol":"SDR"
},
"jobRunID":"",
"result":301.3654364,
"status":"200"
},
"result":301.3654364,
"statusCode":200
}
```
41 changes: 41 additions & 0 deletions alphavantage-sdr/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { Requester, Validator } = require('@chainlink/external-adapter')

const customError = (data) => {
if (data.status !== '200') return true
return false
}

const customParams = {
base: ['base', 'from', 'coin'],
quote: ['quote', 'to', 'market']
}

const createRequest = (input, callback) => {
const validator = new Validator(callback, input, customParams)
const jobRunID = validator.validated.id
const url = 'https://us-central1-alpha-chain-api.cloudfunctions.net/test-api'
const base = validator.validated.data.base.toUpperCase()
const quote = validator.validated.data.quote.toUpperCase()

const params = {
from_symbol: base,
to_symbol: quote,
chainlink_node: true
}

const config = {
url,
params
}

Requester.request(config, customError)
.then(response => {
response.data.result = Requester.validateResultNumber(response.data, ['result'])
callback(response.status, Requester.success(jobRunID, response))
})
.catch(error => {
callback(500, Requester.errored(jobRunID, error))
})
}

module.exports.createRequest = createRequest
10 changes: 10 additions & 0 deletions alphavantage-sdr/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const bootstrap = require('@chainlink/ea-bootstrap')
const { createRequest } = require('./adapter')

module.exports = {
server: bootstrap.server.init(createRequest),

gcpservice: bootstrap.serverless.initGcpService(createRequest),
handler: bootstrap.serverless.initHandler(createRequest),
handlerv2: bootstrap.serverless.initHandlerV2(createRequest)
}
14 changes: 14 additions & 0 deletions alphavantage-sdr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "alphavantage-sdr",
"version": "0.1.0",
"license": "MIT",
"main": "index.js",
"scripts": {
"server": "node -e 'require(\"./index.js\").server()'",
"test": "yarn _mocha --timeout 0"
},
"dependencies": {
"@chainlink/ea-bootstrap": "0.0.1",
"@chainlink/external-adapter": "^0.2.3"
}
}
51 changes: 51 additions & 0 deletions alphavantage-sdr/test/adapter_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const assert = require('chai').assert
const createRequest = require('../adapter').createRequest

describe('createRequest', () => {
const jobID = '1'

context('successful calls', () => {
const requests = [
{ name: 'id not supplied', testData: { data: { base: 'ETH', quote: 'SDR' } } },
{ name: 'base/quote', testData: { id: jobID, data: { base: 'ETH', quote: 'SDR' } } },
{ name: 'from/to', testData: { id: jobID, data: { from: 'ETH', to: 'SDR' } } },
{ name: 'coin/market', testData: { id: jobID, data: { coin: 'ETH', market: 'SDR' } } }
]

requests.forEach(req => {
it(`${req.name}`, (done) => {
createRequest(req.testData, (statusCode, data) => {
assert.equal(statusCode, 200)
assert.equal(data.jobRunID, jobID)
assert.isNotEmpty(data.data)
assert.isAbove(data.result, 0)
assert.isAbove(data.data.result, 0)
done()
})
})
})
})

context('error calls', () => {
const requests = [
{ name: 'empty body', testData: {} },
{ name: 'empty data', testData: { data: {} } },
{ name: 'base not supplied', testData: { id: jobID, data: { quote: 'SDR' } } },
{ name: 'quote not supplied', testData: { id: jobID, data: { base: 'ETH' } } },
{ name: 'unknown base', testData: { id: jobID, data: { base: 'not_real', quote: 'SDR' } } },
{ name: 'unknown quote', testData: { id: jobID, data: { base: 'ETH', quote: 'not_real' } } }
]

requests.forEach(req => {
it(`${req.name}`, (done) => {
createRequest(req.testData, (statusCode, data) => {
assert.equal(statusCode, 500)
assert.equal(data.jobRunID, jobID)
assert.equal(data.status, 'errored')
assert.isNotEmpty(data.error)
done()
})
})
})
})
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "MIT",
"private": true,
"workspaces": [
"alphavantage-sdr",
"nomics",
"external-adapter",
"example",
Expand Down