-
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 #218 from smartcontractkit/emmick/coinmarketcap-re…
…factor coinmarketcap typescript refactor
- Loading branch information
Showing
18 changed files
with
450 additions
and
232 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
module.exports = require('../.eslintrc.js') | ||
module.exports = { | ||
...require('../.eslintrc.ts.js'), | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,15 +1,46 @@ | ||
{ | ||
"name": "@chainlink/coinmarketcap-adapter", | ||
"version": "0.0.3", | ||
"description": "Coinmarketcap adapter.", | ||
"keywords": [ | ||
"Chainlink", | ||
"LINK", | ||
"blockchain", | ||
"oracle", | ||
"coinmarketcap" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"url": "https://github.com/smartcontractkit/external-adapters-js", | ||
"type": "git" | ||
}, | ||
"license": "MIT", | ||
"main": "index.js", | ||
"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 4000 -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 4000 --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": {} | ||
} |
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,41 @@ | ||
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter' | ||
import { ExecuteWithConfig, ExecuteFactory, Config } from '@chainlink/types' | ||
import { makeConfig, DEFAULT_ENDPOINT } from './config' | ||
import { dominance, marketcap, 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) | ||
} | ||
case dominance.NAME: { | ||
return await dominance.execute(request, config) | ||
} | ||
case marketcap.NAME: { | ||
return await marketcap.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()) | ||
} |
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,15 @@ | ||
import { Requester } from '@chainlink/external-adapter' | ||
import { Config } from '@chainlink/types' | ||
|
||
export const DEFAULT_ENDPOINT = 'price' | ||
|
||
const DEFAULT_API_ENDPOINT = 'https://pro-api.coinmarketcap.com/v1/' | ||
|
||
export const makeConfig = (prefix?: string): Config => { | ||
const config = Requester.getDefaultConfig(prefix, true) | ||
config.api.baseURL = config.api.baseUrl || DEFAULT_API_ENDPOINT | ||
config.api.headers = { | ||
'X-CMC_PRO_API_KEY': config.apiKey, | ||
} | ||
return config | ||
} |
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,33 @@ | ||
import { Requester, Validator } from '@chainlink/external-adapter' | ||
import { ExecuteWithConfig, Config } from '@chainlink/types' | ||
|
||
export const NAME = 'dominance' | ||
|
||
const dominanceParams = { | ||
market: ['market', 'to', 'quote'], | ||
} | ||
|
||
export const execute: ExecuteWithConfig<Config> = async (request, config) => { | ||
const validator = new Validator(request, dominanceParams) | ||
if (validator.error) throw validator.error | ||
|
||
const jobRunID = validator.validated.id | ||
|
||
const url = 'global-metrics/quotes/latest' | ||
|
||
const options = { | ||
...config.api, | ||
url, | ||
} | ||
|
||
const symbol = validator.validated.data.market.toLowerCase() | ||
const dataKey = `${symbol}_dominance` | ||
|
||
const response = await Requester.request(options) | ||
const result = Requester.validateResultNumber(response.data, ['data', dataKey]) | ||
return Requester.success(jobRunID, { | ||
data: config.verbose ? { ...response.data, result } : { result }, | ||
result, | ||
status: 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,3 @@ | ||
export * as dominance from './dominance' | ||
export * as price from './price' | ||
export * as marketcap from './marketcap' |
Oops, something went wrong.