Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
droconnel22 authored Dec 3, 2024
2 parents 732fd7f + 8c5177d commit 481efb8
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-penguins-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/bx-digital-adapter': major
---

Init
20 changes: 20 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
3 changes: 3 additions & 0 deletions packages/sources/bx-digital/README.md
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`.
40 changes: 40 additions & 0 deletions packages/sources/bx-digital/package.json
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"
}
}
15 changes: 15 additions & 0 deletions packages/sources/bx-digital/src/config/index.ts
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',
},
})
1 change: 1 addition & 0 deletions packages/sources/bx-digital/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { endpoint as price } from './price'
33 changes: 33 additions & 0 deletions packages/sources/bx-digital/src/endpoint/price.ts
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,
})
21 changes: 21 additions & 0 deletions packages/sources/bx-digital/src/index.ts
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)
69 changes: 69 additions & 0 deletions packages/sources/bx-digital/src/transport/price.ts
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,
},
}
}
})
},
})
5 changes: 5 additions & 0 deletions packages/sources/bx-digital/test-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"requests": [{
"securityId": "CH0012032048"
}]
}
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 packages/sources/bx-digital/test/integration/adapter.test.ts
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()
})
})
})
26 changes: 26 additions & 0 deletions packages/sources/bx-digital/test/integration/fixtures.ts
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()
9 changes: 9 additions & 0 deletions packages/sources/bx-digital/tsconfig.json
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"]
}
7 changes: 7 additions & 0 deletions packages/sources/bx-digital/tsconfig.test.json
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
}
}
Loading

0 comments on commit 481efb8

Please sign in to comment.