diff --git a/.changeset/orange-cats-rhyme.md b/.changeset/orange-cats-rhyme.md new file mode 100644 index 0000000000..443c41174c --- /dev/null +++ b/.changeset/orange-cats-rhyme.md @@ -0,0 +1,5 @@ +--- +'@chainlink/accountable-adapter': major +--- + +Accountable EA diff --git a/.pnp.cjs b/.pnp.cjs index 978292df7b..c54d2d9de5 100644 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -190,6 +190,10 @@ const RAW_RUNTIME_STATE = "name": "@chainlink/1forge-adapter",\ "reference": "workspace:packages/sources/1forge"\ },\ + {\ + "name": "@chainlink/accountable-adapter",\ + "reference": "workspace:packages/sources/accountable"\ + },\ {\ "name": "@chainlink/ada-balance-adapter",\ "reference": "workspace:packages/sources/ada-balance"\ @@ -959,6 +963,7 @@ const RAW_RUNTIME_STATE = "ignorePatternData": "(^(?:\\\\.yarn\\\\/sdks(?:\\\\/(?!\\\\.{1,2}(?:\\\\/|$))(?:(?:(?!(?:^|\\\\/)\\\\.{1,2}(?:\\\\/|$)).)*?)|$))$)",\ "fallbackExclusionList": [\ ["@chainlink/1forge-adapter", ["workspace:packages/sources/1forge"]],\ + ["@chainlink/accountable-adapter", ["workspace:packages/sources/accountable"]],\ ["@chainlink/ada-balance-adapter", ["workspace:packages/sources/ada-balance"]],\ ["@chainlink/aleno-adapter", ["workspace:packages/sources/aleno"]],\ ["@chainlink/allium-state-adapter", ["workspace:packages/sources/allium-state"]],\ @@ -5125,6 +5130,21 @@ const RAW_RUNTIME_STATE = "linkType": "SOFT"\ }]\ ]],\ + ["@chainlink/accountable-adapter", [\ + ["workspace:packages/sources/accountable", {\ + "packageLocation": "./packages/sources/accountable/",\ + "packageDependencies": [\ + ["@chainlink/accountable-adapter", "workspace:packages/sources/accountable"],\ + ["@chainlink/external-adapter-framework", "npm:2.11.0"],\ + ["@types/jest", "npm:29.5.14"],\ + ["@types/node", "npm:22.14.1"],\ + ["nock", "npm:13.5.6"],\ + ["tslib", "npm:2.4.1"],\ + ["typescript", "patch:typescript@npm%3A5.8.3#optional!builtin::version=5.8.3&hash=5786d5"]\ + ],\ + "linkType": "SOFT"\ + }]\ + ]],\ ["@chainlink/ada-balance-adapter", [\ ["workspace:packages/sources/ada-balance", {\ "packageLocation": "./packages/sources/ada-balance/",\ diff --git a/packages/sources/accountable/CHANGELOG.md b/packages/sources/accountable/CHANGELOG.md new file mode 100644 index 0000000000..7057bc3882 --- /dev/null +++ b/packages/sources/accountable/CHANGELOG.md @@ -0,0 +1 @@ +# @chainlink/accountable-adapter diff --git a/packages/sources/accountable/README.md b/packages/sources/accountable/README.md new file mode 100644 index 0000000000..1eecb2eedd --- /dev/null +++ b/packages/sources/accountable/README.md @@ -0,0 +1,54 @@ +# ACCOUNTABLE + +![1.5.3](https://img.shields.io/github/package-json/v/smartcontractkit/external-adapters-js?filename=packages/sources/accountable/package.json) ![v3](https://img.shields.io/badge/framework%20version-v3-blueviolet) + +This document was generated automatically. Please see [README Generator](../../scripts#readme-generator) for more info. + +## Environment Variables + +| Required? | Name | Description | Type | Options | Default | +| :-------: | :----------: | :-----------------: | :----: | :-----: | :-----: | +| ✅ | API_ENDPOINT | API Endpoint to use | string | | | + +--- + +## Data Provider Rate Limits + +| Name | Requests/credits per second | Requests/credits per minute | Requests/credits per hour | Note | +| :-----: | :-------------------------: | :-------------------------: | :-----------------------: | :--: | +| default | | 30 | | | + +--- + +## Input Parameters + +| Required? | Name | Description | Type | Options | Default | +| :-------: | :------: | :-----------------: | :----: | :--------------------------: | :-------: | +| | endpoint | The endpoint to use | string | [reserve](#reserve-endpoint) | `reserve` | + +## Reserve Endpoint + +`reserve` is the only supported name for this endpoint. + +### Input Params + +| Required? | Name | Aliases | Description | Type | Options | Default | Depends On | Not Valid With | +| :-------: | :----: | :-----: | :------------------------------------------------: | :----: | :-----: | :-----: | :--------: | :------------: | +| ✅ | client | | The name of the Accountable client to consume from | string | | | | | + +### Example + +Request: + +```json +{ + "data": { + "endpoint": "reserve", + "client": "axis" + } +} +``` + +--- + +MIT License diff --git a/packages/sources/accountable/package.json b/packages/sources/accountable/package.json new file mode 100644 index 0000000000..18e0190853 --- /dev/null +++ b/packages/sources/accountable/package.json @@ -0,0 +1,40 @@ +{ + "name": "@chainlink/accountable-adapter", + "version": "1.0.0", + "description": "Chainlink's Accountable adapter.", + "keywords": [ + "Chainlink", + "LINK", + "blockchain", + "oracle", + "accountable" + ], + "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": "^29.5.14", + "@types/node": "22.14.1", + "nock": "13.5.6", + "typescript": "5.8.3" + }, + "dependencies": { + "@chainlink/external-adapter-framework": "2.11.0", + "tslib": "2.4.1" + } +} diff --git a/packages/sources/accountable/src/config/index.ts b/packages/sources/accountable/src/config/index.ts new file mode 100644 index 0000000000..bd163df1dd --- /dev/null +++ b/packages/sources/accountable/src/config/index.ts @@ -0,0 +1,9 @@ +import { AdapterConfig } from '@chainlink/external-adapter-framework/config' + +export const config = new AdapterConfig({ + API_ENDPOINT: { + description: 'API Endpoint to use', + type: 'string', + required: true, + }, +}) diff --git a/packages/sources/accountable/src/endpoint/index.ts b/packages/sources/accountable/src/endpoint/index.ts new file mode 100644 index 0000000000..d2d7aef563 --- /dev/null +++ b/packages/sources/accountable/src/endpoint/index.ts @@ -0,0 +1 @@ +export { endpoint as reserve } from './reserve' diff --git a/packages/sources/accountable/src/endpoint/reserve.ts b/packages/sources/accountable/src/endpoint/reserve.ts new file mode 100644 index 0000000000..07de38c0ab --- /dev/null +++ b/packages/sources/accountable/src/endpoint/reserve.ts @@ -0,0 +1,42 @@ +import { + PoRProviderEndpoint, + PoRProviderResponse, +} from '@chainlink/external-adapter-framework/adapter/por' +import { InputParameters } from '@chainlink/external-adapter-framework/validation' +import { AdapterError } from '@chainlink/external-adapter-framework/validation/error' +import { config } from '../config' +import { getApiKey, httpTransport } from '../transport/reserve' + +export const inputParameters = new InputParameters( + { + client: { + type: 'string', + required: true, + description: + 'The name of the Accountable client to consume from. Used to match {client}_API_KEY env variable', + }, + }, + [ + { + client: 'axis', + }, + ], +) + +export type BaseEndpointTypes = { + Parameters: typeof inputParameters.definition + Response: PoRProviderResponse + Settings: typeof config.settings +} + +export const endpoint = new PoRProviderEndpoint({ + name: 'reserve', + transport: httpTransport, + inputParameters, + customInputValidation: (request): AdapterError | undefined => { + if (request.requestContext.data.client) { + getApiKey(request.requestContext.data.client) + } + return + }, +}) diff --git a/packages/sources/accountable/src/index.ts b/packages/sources/accountable/src/index.ts new file mode 100644 index 0000000000..4d00ce1c21 --- /dev/null +++ b/packages/sources/accountable/src/index.ts @@ -0,0 +1,20 @@ +import { expose, ServerInstance } from '@chainlink/external-adapter-framework' +import { PoRAdapter } from '@chainlink/external-adapter-framework/adapter/por' +import { config } from './config' +import { reserve } from './endpoint' + +export const adapter = new PoRAdapter({ + defaultEndpoint: reserve.name, + name: 'ACCOUNTABLE', + config, + endpoints: [reserve], + rateLimiting: { + tiers: { + default: { + rateLimit1m: 30, + }, + }, + }, +}) + +export const server = (): Promise => expose(adapter) diff --git a/packages/sources/accountable/src/transport/reserve.ts b/packages/sources/accountable/src/transport/reserve.ts new file mode 100644 index 0000000000..a4a284efd3 --- /dev/null +++ b/packages/sources/accountable/src/transport/reserve.ts @@ -0,0 +1,73 @@ +import { HttpTransport } from '@chainlink/external-adapter-framework/transports' +import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error' +import { BaseEndpointTypes } from '../endpoint/reserve' + +export interface ResponseSchema { + client: string + totalReserve: number + totalSupply: number + underlyingAssets: { + name: string + value: number + }[] + collateralization: number +} + +export type HttpTransportTypes = BaseEndpointTypes & { + Provider: { + RequestBody: never + ResponseBody: ResponseSchema + } +} + +export const getApiKey = (client: string) => { + const apiKeyName = `${client.replace(/-/g, '_').toUpperCase()}_API_KEY` + const apiKeyValue = process.env[apiKeyName] + + if (!apiKeyValue) { + throw new AdapterInputError({ + statusCode: 400, + message: `Missing '${apiKeyName}' environment variable.`, + }) + } + + return apiKeyValue +} + +export const httpTransport = new HttpTransport({ + prepareRequests: (params, config) => { + return params.map((param) => { + const client = param.client + return { + params: [param], + request: { + baseURL: config.API_ENDPOINT, + params: { + client: client, + }, + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getApiKey(client)}`, + }, + }, + } + }) + }, + parseResponse: (params, response) => { + return params.map((param) => { + const result = response.data.totalReserve + const totalReserve = Number(result) + return { + params: param, + response: { + result, + data: { + result, + totalReserve, + ripcord: false, + }, + }, + } + }) + }, +}) diff --git a/packages/sources/accountable/test/integration/__snapshots__/reserve.test.ts.snap b/packages/sources/accountable/test/integration/__snapshots__/reserve.test.ts.snap new file mode 100644 index 0000000000..8e8d961649 --- /dev/null +++ b/packages/sources/accountable/test/integration/__snapshots__/reserve.test.ts.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`execute reserve endpoint should fail if client name is not present or wrong 1`] = ` +{ + "error": { + "message": "[Param: client] param is required but no value was provided", + "name": "AdapterError", + }, + "status": "errored", + "statusCode": 400, +} +`; + +exports[`execute reserve endpoint should return success for axis 1`] = ` +{ + "data": { + "result": "40438382.35", + "ripcord": false, + "totalReserve": 40438382.35, + }, + "result": "40438382.35", + "statusCode": 200, + "timestamps": { + "providerDataReceivedUnixMs": 978347471111, + "providerDataRequestedUnixMs": 978347471111, + }, +} +`; + +exports[`execute reserve endpoint should return success for unitas 1`] = ` +{ + "data": { + "result": "25559832.85", + "ripcord": false, + "totalReserve": 25559832.85, + }, + "result": "25559832.85", + "statusCode": 200, + "timestamps": { + "providerDataReceivedUnixMs": 978347471111, + "providerDataRequestedUnixMs": 978347471111, + }, +} +`; diff --git a/packages/sources/accountable/test/integration/fixtures.ts b/packages/sources/accountable/test/integration/fixtures.ts new file mode 100644 index 0000000000..3128f7b7fe --- /dev/null +++ b/packages/sources/accountable/test/integration/fixtures.ts @@ -0,0 +1,41 @@ +import nock from 'nock' + +export const mockReserveAxisResponseSuccess = (): nock.Scope => + nock('http://test-endpoint-new?client=axis', { + encodedQueryParams: true, + }) + .get('/') + .query({ client: 'axis' }) + .reply(200, { + client: 'axis', + totalReserve: '40438382.35', + totalSupply: '40355393.07', + underlyingAssets: [ + { name: 'Copper', value: 20000000 }, + { name: 'Fireblocks', value: 20438382.35 }, + { name: 'Insurance Fund', value: 630502.74 }, + { name: 'Ethereum Chain', value: 76414.71688973988 }, + { name: 'Binance', value: 3095.5639951004273 }, + { name: 'BNB Smart Chain', value: 7.3854242495045 }, + ], + collateralization: 1.002056, + }) + .persist() + +export const mockReserveUnitasResponseSuccess = (): nock.Scope => + nock('http://test-endpoint-new?client=unitas', { + encodedQueryParams: true, + }) + .get('/') + .query({ client: 'unitas' }) + .reply(200, { + client: 'unitas', + totalReserve: '25559832.85', + totalSupply: '25559832.85', + underlyingAssets: [ + { name: 'Solana', value: 18440848.73 }, + { name: 'Binance', value: 7118984.11 }, + ], + collateralization: 1.00416, + }) + .persist() diff --git a/packages/sources/accountable/test/integration/reserve.test.ts b/packages/sources/accountable/test/integration/reserve.test.ts new file mode 100644 index 0000000000..87c24a5aab --- /dev/null +++ b/packages/sources/accountable/test/integration/reserve.test.ts @@ -0,0 +1,74 @@ +import { + setEnvVariables, + TestAdapter, +} from '@chainlink/external-adapter-framework/util/testing-utils' +import * as nock from 'nock' +import { mockReserveAxisResponseSuccess, mockReserveUnitasResponseSuccess } from './fixtures' + +describe('execute', () => { + let spy: jest.SpyInstance + let testAdapter: TestAdapter + let oldEnv: NodeJS.ProcessEnv + + beforeAll(async () => { + oldEnv = JSON.parse(JSON.stringify(process.env)) + const mockDate = new Date('2001-01-01T11:11:11.111Z') + spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime()) + + process.env.API_ENDPOINT = 'http://test-endpoint-new' + process.env.AXIS_API_KEY = 'axis-api-key' + process.env.UNITAS_API_KEY = 'unitas-api-key' + + const adapter = (await import('../../src')).adapter + adapter.rateLimiting = undefined + testAdapter = await TestAdapter.startWithMockedCache(adapter, { + testAdapter: {} as TestAdapter, + }) + }) + + afterAll(async () => { + setEnvVariables(oldEnv) + await testAdapter.api.close() + nock.restore() + nock.cleanAll() + spy.mockRestore() + }) + + describe('reserve endpoint', () => { + it('should return success for axis', async () => { + const data = { + endpoint: 'reserve', + client: 'axis', + } + + mockReserveAxisResponseSuccess() + + const response = await testAdapter.request(data) + expect(response.statusCode).toBe(200) + expect(response.json()).toMatchSnapshot() + }) + + it('should return success for unitas', async () => { + const data = { + endpoint: 'reserve', + client: 'unitas', + } + + mockReserveUnitasResponseSuccess() + + const response = await testAdapter.request(data) + expect(response.statusCode).toBe(200) + expect(response.json()).toMatchSnapshot() + }) + + it('should fail if client name is not present or wrong', async () => { + const data = { + endpoint: 'reserve', + } + + const response = await testAdapter.request(data) + expect(response.statusCode).toBe(400) + expect(response.json()).toMatchSnapshot() + }) + }) +}) diff --git a/packages/sources/accountable/tsconfig.json b/packages/sources/accountable/tsconfig.json new file mode 100644 index 0000000000..f59363fd76 --- /dev/null +++ b/packages/sources/accountable/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "include": ["src/**/*", "src/**/*.json"], + "exclude": ["dist", "**/*.spec.ts", "**/*.test.ts"] +} diff --git a/packages/sources/accountable/tsconfig.test.json b/packages/sources/accountable/tsconfig.test.json new file mode 100644 index 0000000000..e3de28cb5c --- /dev/null +++ b/packages/sources/accountable/tsconfig.test.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["src/**/*", "**/test", "src/**/*.json"], + "compilerOptions": { + "noEmit": true + } +} diff --git a/packages/tsconfig.json b/packages/tsconfig.json index 1c9d3cccc6..83b4c9d0e8 100644 --- a/packages/tsconfig.json +++ b/packages/tsconfig.json @@ -125,6 +125,9 @@ { "path": "./sources/1forge" }, + { + "path": "./sources/accountable" + }, { "path": "./sources/ada-balance" }, diff --git a/packages/tsconfig.test.json b/packages/tsconfig.test.json index 5b4eac1545..055f4a79cd 100644 --- a/packages/tsconfig.test.json +++ b/packages/tsconfig.test.json @@ -125,6 +125,9 @@ { "path": "./sources/1forge/tsconfig.test.json" }, + { + "path": "./sources/accountable/tsconfig.test.json" + }, { "path": "./sources/ada-balance/tsconfig.test.json" }, diff --git a/yarn.lock b/yarn.lock index 1df5d3b3d8..b32213ef94 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2442,6 +2442,19 @@ __metadata: languageName: unknown linkType: soft +"@chainlink/accountable-adapter@workspace:packages/sources/accountable": + version: 0.0.0-use.local + resolution: "@chainlink/accountable-adapter@workspace:packages/sources/accountable" + dependencies: + "@chainlink/external-adapter-framework": "npm:2.11.0" + "@types/jest": "npm:^29.5.14" + "@types/node": "npm:22.14.1" + nock: "npm:13.5.6" + tslib: "npm:2.4.1" + typescript: "npm:5.8.3" + languageName: unknown + linkType: soft + "@chainlink/ada-balance-adapter@workspace:*, @chainlink/ada-balance-adapter@workspace:packages/sources/ada-balance": version: 0.0.0-use.local resolution: "@chainlink/ada-balance-adapter@workspace:packages/sources/ada-balance"