Skip to content

Commit

Permalink
TS EAs #4 batch 0 of 3 (#332)
Browse files Browse the repository at this point in the history
* Add couple more best practices into example adapter

* TrueUSD adapter to TS

* Reverts change to TrueUSD param name

* Rename path param --> field
  • Loading branch information
justinkaseman authored Mar 5, 2021
1 parent df09d69 commit 99a1264
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 98 deletions.
10 changes: 6 additions & 4 deletions example/src/endpoint/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const customError = (data: any) => data.Response === 'Error'
const customParams = {
base: ['base', 'from', 'coin'],
quote: ['quote', 'to', 'market'],
field: false,
}

export const execute: ExecuteWithConfig<Config> = async (request, config) => {
Expand All @@ -17,20 +18,21 @@ export const execute: ExecuteWithConfig<Config> = async (request, config) => {
const jobRunID = validator.validated.id
const base = validator.validated.data.base
const quote = validator.validated.data.quote
const field = validator.validated.data.field || 'price'
const url = `price`

const params = {
base,
quote,
}

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

const response = await Requester.request(reqConfig, customError)
const result = Requester.validateResultNumber(response.data, ['price'])
const response = await Requester.request(options, customError)
const result = Requester.validateResultNumber(response.data, [field])

return Requester.success(jobRunID, {
data: { result },
data: config.verbose ? { ...response.data, result } : { result },
result,
status: 200,
})
Expand Down
3 changes: 3 additions & 0 deletions trueusd/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('../.eslintrc.ts.js'),
}
29 changes: 25 additions & 4 deletions trueusd/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
# Chainlink External Adapter for TrueUSD

## Total Supply API
### Input Parameters

| Required? | Name | Description | Options | Defaults to |
| :-------: | :------: | :-----------------: | :--------------------------: | :---------: |
| | endpoint | The endpoint to use | [trueusd](#TrueUSD-Endpoint) | `trueusd` |

---

## TrueUSD Endpoint

https://core-api.real-time-attest.trustexplorer.io/trusttoken/TrueUSD

## Input Params
### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :-----: | :-------------------------------------------------: | :------------------------: | :----------: |
| | `field` | The data point to return from the API response data | `totalTrust`, `totalToken` | `totalTrust` |

### Sample Input

- `field`: The data field to return. (defaults to `totalTrust`, one of `totalToken`, `totalTrust`)
```json
{
"id": "1",
"data": {
"field": "totalToken"
}
}
```

## Output
### Output

```json
{
Expand Down
28 changes: 0 additions & 28 deletions trueusd/adapter.js

This file was deleted.

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

This file was deleted.

40 changes: 35 additions & 5 deletions trueusd/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
{
"name": "@chainlink/trueusd-adapter",
"version": "0.0.3",
"description": "Chainlink TrueUSD adapter.",
"keywords": [
"Chainlink",
"LINK",
"blockchain",
"oracle"
],
"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 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": {}
}
35 changes: 35 additions & 0 deletions trueusd/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 { trueusd } 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 trueusd.NAME: {
return await trueusd.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())
}
11 changes: 11 additions & 0 deletions trueusd/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'

export const DEFAULT_ENDPOINT = 'trueusd'
export const DEFAULT_BASE_URL = 'https://core-api.real-time-attest.trustexplorer.io'

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

export const NAME = 'trueusd'

const customError = (data: any) => data.Response === 'Error'

const customParams = {
field: 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 field = validator.validated.data.field || 'totalTrust'
const url = '/trusttoken/TrueUSD'

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

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

return Requester.success(jobRunID, {
data: config.verbose ? { ...response.data, result } : { result },
result,
status: 200,
})
}
7 changes: 7 additions & 0 deletions trueusd/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expose } from '@chainlink/ea-bootstrap'
import { makeExecute } from './adapter'
import { makeConfig } from './config'

const NAME = 'TRUEUSD'

export = { NAME, makeExecute, makeConfig, ...expose(makeExecute()) }
53 changes: 0 additions & 53 deletions trueusd/test/adapter_test.js

This file was deleted.

56 changes: 56 additions & 0 deletions trueusd/test/trueusd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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: {} },
},
{
name: 'id is supplied',
testData: { id: jobID, data: { field: 'totalTrust' } },
},
{
name: 'trust supply',
testData: { id: jobID, data: { field: 'totalToken' } },
},
]

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('error calls @integration', () => {
const requests = [
{
name: 'unknown field',
testData: { id: jobID, data: { field: '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)
}
})
})
})
})
10 changes: 10 additions & 0 deletions trueusd/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"]
}

0 comments on commit 99a1264

Please sign in to comment.