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

coinmarketcap typescript refactor #218

Merged
merged 12 commits into from
Feb 4, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- `polygon`
- `nomics`
- `openexchangerates`
- `coinmarketcap`

### Removed

Expand Down
8 changes: 8 additions & 0 deletions bootstrap/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export const toObjectWithNumbers = (obj: any) => {
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, toNumber(v)]))
}

// pick a random string from env var after splitting with the delimiter ("a&b&c" "&" -> choice(["a","b","c"]))
export const getRandomEnv = (name: string, delimiter = ',', prefix = '') => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I left a comment in the batch PR about this -> #226 (comment)

What do you think?

const val = getEnv(name, prefix)
if (!val) return val
const items = val.split(delimiter)
return items[Math.floor(Math.random() * items.length)]
}

// pick a random string from env var after splitting with the delimiter ("a&b&c" "&" -> choice(["a","b","c"]))
export const getRandomRequiredEnv = (name: string, delimiter = ',', prefix = '') => {
const val = getRequiredEnv(name, prefix)
Expand Down
4 changes: 3 additions & 1 deletion coinmarketcap/.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'),
}
184 changes: 0 additions & 184 deletions coinmarketcap/adapter.js

This file was deleted.

4 changes: 0 additions & 4 deletions coinmarketcap/index.js

This file was deleted.

41 changes: 36 additions & 5 deletions coinmarketcap/package.json
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": {}
}
41 changes: 41 additions & 0 deletions coinmarketcap/src/adapter.ts
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())
}
15 changes: 15 additions & 0 deletions coinmarketcap/src/config.ts
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
}
33 changes: 33 additions & 0 deletions coinmarketcap/src/endpoint/dominance.ts
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,
})
}
3 changes: 3 additions & 0 deletions coinmarketcap/src/endpoint/index.ts
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'
Loading