-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
380 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@chainlink/bx-digital-adapter': major | ||
--- | ||
|
||
Init |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
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 @@ | ||
# Chainlink External Adapter for bx-digital | ||
|
||
This README will be generated automatically when code is merged to `main`. If you would like to generate a preview of the README, please run `yarn generate:readme bx-digital`. |
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,40 @@ | ||
{ | ||
"name": "@chainlink/bx-digital-adapter", | ||
"version": "0.0.0", | ||
"description": "Chainlink bx-digital adapter.", | ||
"keywords": [ | ||
"Chainlink", | ||
"LINK", | ||
"blockchain", | ||
"oracle", | ||
"bx-digital" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"url": "https://github.com/smartcontractkit/external-adapters-js", | ||
"type": "git" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo", | ||
"prepack": "yarn build", | ||
"build": "tsc -b", | ||
"server": "node -e 'require(\"./index.js\").server()'", | ||
"server:dist": "node -e 'require(\"./dist/index.js\").server()'", | ||
"start": "yarn server:dist" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "27.5.2", | ||
"@types/node": "16.18.119", | ||
"nock": "13.5.5", | ||
"typescript": "5.6.3" | ||
}, | ||
"dependencies": { | ||
"@chainlink/external-adapter-framework": "1.7.3", | ||
"tslib": "2.4.1" | ||
} | ||
} |
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 { AdapterConfig } from '@chainlink/external-adapter-framework/config' | ||
|
||
export const config = new AdapterConfig({ | ||
API_KEY: { | ||
description: 'An API key for Data Provider', | ||
type: 'string', | ||
required: true, | ||
sensitive: true, | ||
}, | ||
API_ENDPOINT: { | ||
description: 'An API endpoint for Data Provider', | ||
type: 'string', | ||
default: 'https://dev-cdf-stage-k8s.bxdigital.ch/securities', | ||
}, | ||
}) |
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 { endpoint as price } from './price' |
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 { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter' | ||
import { InputParameters } from '@chainlink/external-adapter-framework/validation' | ||
import { SingleNumberResultResponse } from '@chainlink/external-adapter-framework/util' | ||
import { config } from '../config' | ||
import { httpTransport } from '../transport/price' | ||
|
||
export const inputParameters = new InputParameters( | ||
{ | ||
securityId: { | ||
required: true, | ||
type: 'string', | ||
description: 'ID of the security to report price on', | ||
}, | ||
}, | ||
[ | ||
{ | ||
securityId: 'CH0012032048', | ||
}, | ||
], | ||
) | ||
|
||
export type BaseEndpointTypes = { | ||
Parameters: typeof inputParameters.definition | ||
Response: SingleNumberResultResponse | ||
Settings: typeof config.settings | ||
} | ||
|
||
export const endpoint = new AdapterEndpoint({ | ||
name: 'price', | ||
aliases: [], | ||
transport: httpTransport, | ||
inputParameters, | ||
}) |
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,21 @@ | ||
import { expose, ServerInstance } from '@chainlink/external-adapter-framework' | ||
import { Adapter } from '@chainlink/external-adapter-framework/adapter' | ||
import { config } from './config' | ||
import { price } from './endpoint' | ||
|
||
export const adapter = new Adapter({ | ||
defaultEndpoint: price.name, | ||
name: 'BX_DIGITAL', | ||
config, | ||
endpoints: [price], | ||
rateLimiting: { | ||
tiers: { | ||
default: { | ||
rateLimit1m: 6, | ||
note: 'Reasonable limits', | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export const server = (): Promise<ServerInstance | undefined> => expose(adapter) |
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,69 @@ | ||
import { HttpTransport } from '@chainlink/external-adapter-framework/transports' | ||
import { BaseEndpointTypes } from '../endpoint/price' | ||
|
||
export interface ResponseSchema { | ||
securityId: string | ||
lastModifiedTime: number | ||
closingPrice: string | ||
} | ||
|
||
export type HttpTransportTypes = BaseEndpointTypes & { | ||
Provider: { | ||
RequestBody: never | ||
ResponseBody: ResponseSchema[] | ||
} | ||
} | ||
export const httpTransport = new HttpTransport<HttpTransportTypes>({ | ||
prepareRequests: (params, config) => { | ||
return params.map((param) => { | ||
return { | ||
params: [param], | ||
request: { | ||
baseURL: config.API_ENDPOINT, | ||
headers: { | ||
'API-key': config.API_KEY, | ||
}, | ||
}, | ||
} | ||
}) | ||
}, | ||
parseResponse: (params, response) => { | ||
if (!response.data) { | ||
return params.map((param) => { | ||
return { | ||
params: param, | ||
response: { | ||
errorMessage: `The data provider didn't return any value`, | ||
statusCode: 502, | ||
}, | ||
} | ||
}) | ||
} | ||
|
||
return params.map((param) => { | ||
const security = response.data.find((r) => r.securityId == param.securityId) | ||
if (security && !isNaN(Number(security?.closingPrice))) { | ||
return { | ||
params: param, | ||
response: { | ||
result: Number(security.closingPrice), | ||
data: { | ||
result: Number(security.closingPrice), | ||
}, | ||
timestamps: { | ||
providerIndicatedTimeUnixMs: security.lastModifiedTime * 1000, | ||
}, | ||
}, | ||
} | ||
} else { | ||
return { | ||
params: param, | ||
response: { | ||
errorMessage: `The data provider didn't return any value for ${param.securityId}`, | ||
statusCode: 502, | ||
}, | ||
} | ||
} | ||
}) | ||
}, | ||
}) |
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,5 @@ | ||
{ | ||
"requests": [{ | ||
"securityId": "CH0012032048" | ||
}] | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/sources/bx-digital/test/integration/__snapshots__/adapter.test.ts.snap
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,42 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`execute price endpoint should return failure for Nan 1`] = ` | ||
{ | ||
"errorMessage": "The data provider didn't return any value for 3", | ||
"statusCode": 502, | ||
"timestamps": { | ||
"providerDataReceivedUnixMs": 978347471111, | ||
"providerDataRequestedUnixMs": 978347471111, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`execute price endpoint should return success for security 1 1`] = ` | ||
{ | ||
"data": { | ||
"result": 111.11, | ||
}, | ||
"result": 111.11, | ||
"statusCode": 200, | ||
"timestamps": { | ||
"providerDataReceivedUnixMs": 978347471111, | ||
"providerDataRequestedUnixMs": 978347471111, | ||
"providerIndicatedTimeUnixMs": 1733155814000, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`execute price endpoint should return success for security 2 1`] = ` | ||
{ | ||
"data": { | ||
"result": 222.22, | ||
}, | ||
"result": 222.22, | ||
"statusCode": 200, | ||
"timestamps": { | ||
"providerDataReceivedUnixMs": 978347471111, | ||
"providerDataRequestedUnixMs": 978347471111, | ||
"providerIndicatedTimeUnixMs": 1733153530000, | ||
}, | ||
} | ||
`; |
65 changes: 65 additions & 0 deletions
65
packages/sources/bx-digital/test/integration/adapter.test.ts
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,65 @@ | ||
import { | ||
TestAdapter, | ||
setEnvVariables, | ||
} from '@chainlink/external-adapter-framework/util/testing-utils' | ||
import * as nock from 'nock' | ||
import { mockResponseSuccess } from './fixtures' | ||
|
||
describe('execute', () => { | ||
let spy: jest.SpyInstance | ||
let testAdapter: TestAdapter | ||
let oldEnv: NodeJS.ProcessEnv | ||
|
||
beforeAll(async () => { | ||
oldEnv = JSON.parse(JSON.stringify(process.env)) | ||
process.env.API_ENDPOINT = 'https://fake-api' | ||
process.env.API_KEY = 'fake-api-key' | ||
|
||
const mockDate = new Date('2001-01-01T11:11:11.111Z') | ||
spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime()) | ||
|
||
const adapter = (await import('./../../src')).adapter | ||
adapter.rateLimiting = undefined | ||
testAdapter = await TestAdapter.startWithMockedCache(adapter, { | ||
testAdapter: {} as TestAdapter<never>, | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
setEnvVariables(oldEnv) | ||
await testAdapter.api.close() | ||
nock.restore() | ||
nock.cleanAll() | ||
spy.mockRestore() | ||
}) | ||
|
||
describe('price endpoint', () => { | ||
it('should return success for security 1', async () => { | ||
const data = { | ||
securityId: '1', | ||
} | ||
mockResponseSuccess() | ||
const response = await testAdapter.request(data) | ||
expect(response.statusCode).toBe(200) | ||
expect(response.json()).toMatchSnapshot() | ||
}) | ||
it('should return success for security 2', async () => { | ||
const data = { | ||
securityId: '2', | ||
} | ||
mockResponseSuccess() | ||
const response = await testAdapter.request(data) | ||
expect(response.statusCode).toBe(200) | ||
expect(response.json()).toMatchSnapshot() | ||
}) | ||
it('should return failure for Nan', async () => { | ||
const data = { | ||
securityId: '3', | ||
} | ||
mockResponseSuccess() | ||
const response = await testAdapter.request(data) | ||
expect(response.statusCode).toBe(502) | ||
expect(response.json()).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
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,26 @@ | ||
import nock from 'nock' | ||
|
||
export const mockResponseSuccess = (): nock.Scope => | ||
nock('https://fake-api', { | ||
encodedQueryParams: true, | ||
}) | ||
.get('/') | ||
.reply( | ||
200, | ||
() => [ | ||
{ securityId: '1', lastModifiedTime: 1733155814, closingPrice: '111.11' }, | ||
{ securityId: '2', lastModifiedTime: 1733153530, closingPrice: '222.22' }, | ||
{ securityId: '3', lastModifiedTime: 1733156220, closingPrice: 'lol' }, | ||
], | ||
[ | ||
'Content-Type', | ||
'application/json', | ||
'Connection', | ||
'close', | ||
'Vary', | ||
'Accept-Encoding', | ||
'Vary', | ||
'Origin', | ||
], | ||
) | ||
.persist() |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src" | ||
}, | ||
"include": ["src/**/*", "src/**/*.json"], | ||
"exclude": ["dist", "**/*.spec.ts", "**/*.test.ts"] | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src/**/*", "**/test", "src/**/*.json"], | ||
"compilerOptions": { | ||
"noEmit": true | ||
} | ||
} |
Oops, something went wrong.