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

EA migration - batch #1 #226

Merged
merged 28 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a9c9ff9
1forge to TS
justinkaseman Jan 11, 2021
7ae2bfb
Add Config type for recent change to generic types
justinkaseman Jan 13, 2021
d00485c
Utilize ExecuteWithConfig in 1forge
justinkaseman Jan 14, 2021
88f9f3d
Adds example README template & remove 'server' script
justinkaseman Jan 14, 2021
aff743c
Alphavantage adapter to TS
justinkaseman Jan 14, 2021
b67f5cd
Remove alphavantage-sdr folder
justinkaseman Jan 14, 2021
af98026
Amberdata-gasprice to TS
justinkaseman Jan 15, 2021
3741cc1
Anyblock-gasprice to TS
justinkaseman Jan 15, 2021
507e414
Binance-DEX to TS
justinkaseman Jan 15, 2021
d88758f
Add note to binance-dex's differing use of API_ENDPOINT env var
justinkaseman Jan 15, 2021
f6a2ded
Brave New Coin to TS
justinkaseman Jan 16, 2021
9844af4
Bring Brave New Coin VWAP into Brave New Coin EA
justinkaseman Jan 16, 2021
d687942
Coinapi to TS
justinkaseman Jan 19, 2021
d3473cc
Coinbase to TS
justinkaseman Jan 19, 2021
a006e1a
Coingecko to TS
justinkaseman Jan 20, 2021
d5174da
Move BNC helpers into BNC adapter
justinkaseman Jan 20, 2021
e2ba252
Increase alphavantage test timeout
justinkaseman Jan 20, 2021
3769c70
Moves alphavantage APIkey param into endpoint - issue open for improv…
justinkaseman Jan 21, 2021
73fd1ab
Clean up leftover bravenewcoin-vwap traces
justinkaseman Jan 21, 2021
b959ba6
Remove 1forge's conflicting and not useful endpoint param
justinkaseman Jan 21, 2021
b5d6268
Requested changes: 1forge balance -> price, alphavantage example output
justinkaseman Jan 27, 2021
7a2e510
Re-add server script & spread response data into endpoint responses
justinkaseman Feb 3, 2021
5fe263c
Uses verbose env var to determine response data
justinkaseman Feb 4, 2021
2eb9dc7
Gets API_KEY from makeConfig
justinkaseman Feb 4, 2021
9a12f44
Add test dummy API_KEY
justinkaseman Feb 4, 2021
674b974
Merge branch 'develop' into feature/EA-TS-1
justinkaseman Feb 4, 2021
268d78f
Merge branch 'develop' into feature/EA-TS-1
justinkaseman Feb 4, 2021
e4d1a19
Re-add server script to example
justinkaseman Feb 4, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/strategy/adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"blockcypher",
"bootstrap",
"bravenewcoin",
"bravenewcoin-vwap",
"coinapi",
"coinbase",
"coincodex",
Expand Down
4 changes: 3 additions & 1 deletion 1forge/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = require('../.eslintrc.js')
module.exports = {
...require('../.eslintrc.ts.js'),
}
24 changes: 13 additions & 11 deletions 1forge/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# Chainlink 1Forge External Adapter

## Input Params
## Price Endpoint

### Input Params

- `base` or `to`: The target currency to query (required)
- `quote` or `from`: The currency to convert to (required)
- `endpoint`: The endpoint to call (optional)

## Output
### Output

```json
{
"jobRunID": "1",
"data": {
"value": 1.22687,
"text": "1.0 GBP is worth 1.22687 USD",
"timestamp": 1587489920,
"result": 1.22687
},
"result": 1.22687,
"statusCode": 200
"jobRunID": "1",
"data": {
"value": 1.22687,
"text": "1.0 GBP is worth 1.22687 USD",
"timestamp": 1587489920,
"result": 1.22687
},
"result": 1.22687,
"statusCode": 200
}
```
43 changes: 0 additions & 43 deletions 1forge/adapter.js

This file was deleted.

4 changes: 0 additions & 4 deletions 1forge/index.js

This file was deleted.

29 changes: 24 additions & 5 deletions 1forge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
"name": "@chainlink/1forge-adapter",
"version": "0.0.2",
"license": "MIT",
"main": "index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"server": "node -e 'require(\"./index.js\").server()'",
"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": "yarn _mocha --timeout 0",
"test:unit": "yarn _mocha --grep @integration --invert --timeout 0",
"test:integration": "yarn _mocha --grep @integration --timeout 0"
"test": "mocha --exit --timeout 15000 -r ts-node/register 'test/**/*.test.ts'",
justinkaseman marked this conversation as resolved.
Show resolved Hide resolved
"test:unit": "mocha --exit --grep @integration --invert -r ts-node/register 'test/**/*.test.ts'",
"test:integration": "mocha --exit --timeout 15000 --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": {}
}
35 changes: 35 additions & 0 deletions 1forge/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter'
import { Config, ExecuteWithConfig, ExecuteFactory } 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)
}
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())
}
17 changes: 17 additions & 0 deletions 1forge/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'

export const DEFAULT_ENDPOINT = 'price'
export const DEFAULT_API_ENDPOINT = 'https://api.1forge.com/'

export const makeConfig = (prefix?: string): Config => {
const config = Requester.getDefaultConfig(prefix, true)
config.api = {
...config.api,
baseURL: config.api.baseUrl || DEFAULT_API_ENDPOINT,
params: {
api_key: config.apiKey,
},
}
return config
}
1 change: 1 addition & 0 deletions 1forge/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as price from './price'
43 changes: 43 additions & 0 deletions 1forge/src/endpoint/price.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Requester, Validator } from '@chainlink/external-adapter'
import { ExecuteWithConfig, Config } from '@chainlink/types'

export const NAME = 'price'

const customParams = {
base: ['base', 'from'],
quote: ['quote', 'to'],
quantity: false,
}

export const execute: ExecuteWithConfig<Config> = async (request, config) => {
const validator = new Validator(request, customParams)
if (validator.error) throw validator.error

const jobRunID = validator.validated.id
const url = `/convert`
const from = validator.validated.data.base.toUpperCase()
const to = validator.validated.data.quote.toUpperCase()
const quantity = validator.validated.data.quantity || 1

const params = {
...config.api.params,
from,
to,
quantity,
}

const options = {
...config.api,
url,
params,
}

const response = await Requester.request(options)
const result = Requester.validateResultNumber(response.data, ['value'])

return Requester.success(jobRunID, {
data: config.verbose ? { ...response.data, result } : { result },
result,
status: 200,
})
}
7 changes: 7 additions & 0 deletions 1forge/src/index.ts
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 = '1FORGE'

export = { NAME, makeExecute, makeConfig, ...expose(util.wrapExecute(makeExecute())) }
46 changes: 26 additions & 20 deletions 1forge/test/adapter_test.js → 1forge/test/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const { assert } = require('chai')
const { assertSuccess, assertError } = require('@chainlink/adapter-test-helpers')
const { execute } = require('../adapter')
import { assert } from 'chai'
import { Requester } from '@chainlink/external-adapter'
import { assertSuccess, assertError } from '@chainlink/adapter-test-helpers'
import { makeExecute } from '../src/adapter'
import { AdapterRequest } from '@chainlink/types'

describe('execute', () => {
const jobID = '1'
const execute = makeExecute()
process.env.API_KEY = process.env.API_KEY ?? 'test_api_key'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this a lot, setting directly the process.env field, and with a value that is not providing any actual functionality...

But we could tackle this separately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This would be out of scope of this TS migration though.

There is an issue tracking it @ #245.


context('successful calls @integration', () => {
const requests = [
Expand All @@ -22,13 +26,11 @@ describe('execute', () => {
]

requests.forEach((req) => {
it(`${req.name}`, (done) => {
execute(req.testData, (statusCode, data) => {
assertSuccess({ expected: 200, actual: statusCode }, data, jobID)
assert.isAbove(Number(data.result), 0)
assert.isAbove(Number(data.data.result), 0)
done()
})
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)
})
})
})
Expand All @@ -48,11 +50,13 @@ describe('execute', () => {
]

requests.forEach((req) => {
it(`${req.name}`, (done) => {
execute(req.testData, (statusCode, data) => {
assertError({ expected: 400, actual: statusCode }, data, jobID)
done()
})
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)
}
})
})
})
Expand All @@ -70,11 +74,13 @@ describe('execute', () => {
]

requests.forEach((req) => {
it(`${req.name}`, (done) => {
execute(req.testData, (statusCode, data) => {
assertError({ expected: 500, actual: statusCode }, data, jobID)
done()
})
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)
}
})
})
})
Expand Down
10 changes: 10 additions & 0 deletions 1forge/tsconfig.json
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"]
}
1 change: 0 additions & 1 deletion alphavantage-sdr/.eslintrc.js

This file was deleted.

Loading