-
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.
* 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
1 parent
df09d69
commit 99a1264
Showing
14 changed files
with
219 additions
and
98 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
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
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,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": {} | ||
} |
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,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()) | ||
} |
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,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 | ||
} |
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 trueusd from './trueusd' |
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,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, | ||
}) | ||
} |
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 } from '@chainlink/ea-bootstrap' | ||
import { makeExecute } from './adapter' | ||
import { makeConfig } from './config' | ||
|
||
const NAME = 'TRUEUSD' | ||
|
||
export = { NAME, makeExecute, makeConfig, ...expose(makeExecute()) } |
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 |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
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"] | ||
} |