-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from smartcontractkit/feature/dxfeed-secondary
dxfeed-secondary adapter
- Loading branch information
Showing
26 changed files
with
516 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
...require('../.eslintrc.ts.js'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Chainlink External Adapter for dxFeed | ||
|
||
An adapter to add secondary mapping for symbols: | ||
|
||
```bash | ||
TSLA ➡️ 'TSLA.US:TEI' | ||
``` | ||
|
||
## Configuration | ||
|
||
This adapter supports the following environment variables: | ||
|
||
- `API_USERNAME`: Your API username | ||
- `API_PASSWORD`: Your API password | ||
- `API_ENDPOINT`: The endpoint for your dxFeed. Defaults to the demo endpoint (`https://tools.dxfeed.com/webservice/rest`) | ||
|
||
## Input Params | ||
|
||
- `base`, `from`, or `asset`: The symbol of the asset to query | ||
- `endpoint`: Optional endpoint param | ||
|
||
## Output | ||
|
||
```json | ||
{ | ||
"jobRunID": "1", | ||
"data": { | ||
"status": "OK", | ||
"Trade": { | ||
"UKX:FTSE": { | ||
"eventSymbol": "UKX:FTSE", | ||
"eventTime": 0, | ||
"time": 1593001772000, | ||
"timeNanoPart": 0, | ||
"sequence": 115972, | ||
"exchangeCode": "", | ||
"price": 6194.63, | ||
"size": 0, | ||
"dayVolume": 0, | ||
"dayTurnover": "NaN", | ||
"tickDirection": "ZERO_UP", | ||
"extendedTradingHours": false | ||
} | ||
}, | ||
"result": 6194.63 | ||
}, | ||
"result": 6194.63, | ||
"statusCode": 200 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@chainlink/dxfeed-secondary-adapter", | ||
"version": "0.0.1", | ||
"description": "Chainlink dxfeed secondary adapter.", | ||
"keywords": [ | ||
"Chainlink", | ||
"LINK", | ||
"blockchain", | ||
"oracle", | ||
"dxfeed" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"url": "https://github.com/smartcontractkit/external-adapters-js", | ||
"type": "git" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"prepublishOnly": "yarn build && yarn test:unit", | ||
"setup": "yarn build", | ||
"build": "tsc -b", | ||
"lint": "eslint --ignore-path ../.eslintignore . --ext .js,.jsx,.ts,.tsx", | ||
"lint:fix": "eslint --ignore-path ../.eslintignore . --ext .js,.jsx,.ts,.tsx --fix", | ||
"test": "mocha --exit --timeout 3000 -r ts-node/register 'test/**/*.test.ts'", | ||
"test:unit": "mocha --exit --grep @integration --invert -r ts-node/register 'test/**/*.test.ts'", | ||
"test:integration": "mocha --exit --timeout 3000 --grep @integration -r ts-node/register 'test/**/*.test.ts'", | ||
"server": "node -e 'require(\"./index.js\").server()'", | ||
"server:dist": "node -e 'require(\"./dist/index.js\").server()'", | ||
"start": "yarn server:dist" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.11", | ||
"@types/express": "^4.17.6", | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^14.0.13", | ||
"@typescript-eslint/eslint-plugin": "^3.9.0", | ||
"@typescript-eslint/parser": "^3.9.0", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.7" | ||
}, | ||
"dependencies": { | ||
"@chainlink/dxfeed-adapter": "0.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter' | ||
import { ExecuteWithConfig, ExecuteFactory, Config } from '@chainlink/types' | ||
import { makeConfig, DEFAULT_ENDPOINT } from './config' | ||
import { price } from './endpoint' | ||
|
||
const inputParams = { | ||
endpoint: false, | ||
} | ||
|
||
export const execute: ExecuteWithConfig<Config> = async (request, config) => { | ||
const validator = new Validator(request, inputParams) | ||
if (validator.error) throw validator.error | ||
|
||
Requester.logConfig(config) | ||
|
||
const jobRunID = validator.validated.id | ||
const endpoint = validator.validated.data.endpoint || DEFAULT_ENDPOINT | ||
|
||
switch (endpoint) { | ||
case price.NAME: { | ||
return await price.execute(request, config) | ||
break | ||
} | ||
default: { | ||
throw new AdapterError({ | ||
jobRunID, | ||
message: `Endpoint ${endpoint} not supported.`, | ||
statusCode: 400, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export const makeExecute: ExecuteFactory<Config> = (config) => { | ||
return async (request) => execute(request, config || makeConfig()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Requester } from '@chainlink/external-adapter' | ||
import { Config } from '@chainlink/types' | ||
|
||
export const DEFAULT_ENDPOINT = 'price' | ||
|
||
export const makeConfig = (prefix?: string): Config => Requester.getDefaultConfig(prefix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as price from './price' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Validator } from '@chainlink/external-adapter' | ||
import { ExecuteWithConfig, Config } from '@chainlink/types' | ||
import * as dxfeed from '@chainlink/dxfeed-adapter' | ||
|
||
export const NAME = 'price' | ||
|
||
const customParams = { | ||
base: ['base', 'from', 'coin'], | ||
} | ||
|
||
const commonSymbols: { [key: string]: string } = { | ||
N225: 'NKY.IND:TEI', | ||
FTSE: 'UKX.IND:TEI', | ||
TSLA: 'TSLA.US:TEI', | ||
} | ||
|
||
export const execute: ExecuteWithConfig<Config> = async (request, config) => { | ||
const validator = new Validator(request, customParams) | ||
if (validator.error) throw validator.error | ||
|
||
let symbol = validator.validated.data.base.toUpperCase() | ||
if (symbol in commonSymbols) { | ||
symbol = commonSymbols[symbol] | ||
} | ||
request.data.base = symbol | ||
|
||
const exec = dxfeed.makeExecute(config) | ||
return exec(request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expose, util } from '@chainlink/ea-bootstrap' | ||
import { makeExecute } from './adapter' | ||
import { makeConfig } from './config' | ||
|
||
const NAME = 'DXFEED_SECONDARY' | ||
|
||
export = { NAME, makeExecute, makeConfig, ...expose(util.wrapExecute(makeExecute())) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { assert } from 'chai' | ||
import { Requester } from '@chainlink/external-adapter' | ||
import { assertSuccess, assertError } from '@chainlink/adapter-test-helpers' | ||
import { AdapterRequest } from '@chainlink/types' | ||
import { makeExecute } from '../src/adapter' | ||
|
||
describe('execute', () => { | ||
const jobID = '1' | ||
const execute = makeExecute() | ||
|
||
context('successful calls @integration', () => { | ||
const requests = [ | ||
{ name: 'id not supplied', testData: { data: { base: 'FTSE' } } }, | ||
{ name: 'base', testData: { id: jobID, data: { base: 'FTSE' } } }, | ||
{ name: 'from', testData: { id: jobID, data: { from: 'FTSE' } } }, | ||
] | ||
|
||
requests.forEach((req) => { | ||
it(`${req.name}`, async () => { | ||
const data = await execute(req.testData as AdapterRequest) | ||
assertSuccess({ expected: 200, actual: data.statusCode }, data, jobID) | ||
assert.isAbove(data.result, 0) | ||
assert.isAbove(data.data.result, 0) | ||
}) | ||
}) | ||
}) | ||
|
||
context('validation error', () => { | ||
const requests = [ | ||
{ name: 'empty body', testData: {} }, | ||
{ name: 'empty data', testData: { data: {} } }, | ||
{ name: 'base not supplied', testData: { id: jobID, data: {} } }, | ||
] | ||
|
||
requests.forEach((req) => { | ||
it(`${req.name}`, async () => { | ||
try { | ||
await execute(req.testData as AdapterRequest) | ||
} catch (error) { | ||
const errorResp = Requester.errored(jobID, error) | ||
assertError({ expected: 400, actual: errorResp.statusCode }, errorResp, jobID) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
context('error calls @integration', () => { | ||
const requests = [ | ||
{ | ||
name: 'unknown base', | ||
testData: { id: jobID, data: { base: 'not_real' } }, | ||
}, | ||
] | ||
|
||
requests.forEach((req) => { | ||
it(`${req.name}`, async () => { | ||
try { | ||
await execute(req.testData as AdapterRequest) | ||
} catch (error) { | ||
const errorResp = Requester.errored(jobID, error) | ||
assertError({ expected: 500, actual: errorResp.statusCode }, errorResp, jobID) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"typeRoots": ["../node_modules/@types", "../typings", "./typings"] | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["dist", "**/*.spec.ts", "**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module.exports = require('../.eslintrc.js') | ||
module.exports = { | ||
...require('../.eslintrc.ts.js'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.