Skip to content

Commit b71bd55

Browse files
Feat/OPDATA-5851 truflation (#4749)
* OPDATA-5851 truflation EA initial commit * OPDATA-5851 truflation EA initial commit - 2 * remove unused generated file * add changeset * add test and update error message --------- Co-authored-by: app-token-issuer-data-feeds[bot] <134377064+app-token-issuer-data-feeds[bot]@users.noreply.github.com>
1 parent ec12ee7 commit b71bd55

19 files changed

Lines changed: 552 additions & 0 deletions

File tree

.changeset/chubby-aliens-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/truflation-adapter': major
3+
---
4+
5+
Truflation initial release

.pnp.cjs

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sources/truflation/CHANGELOG.md

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Chainlink External Adapter for truflation
2+
3+
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 truflation`.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@chainlink/truflation-adapter",
3+
"version": "0.0.0",
4+
"description": "Chainlink truflation adapter.",
5+
"keywords": [
6+
"Chainlink",
7+
"LINK",
8+
"blockchain",
9+
"oracle",
10+
"truflation"
11+
],
12+
"main": "dist/index.js",
13+
"types": "dist/index.d.ts",
14+
"files": [
15+
"dist"
16+
],
17+
"repository": {
18+
"url": "https://github.com/smartcontractkit/external-adapters-js",
19+
"type": "git"
20+
},
21+
"license": "MIT",
22+
"scripts": {
23+
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo",
24+
"prepack": "yarn build",
25+
"build": "tsc -b",
26+
"server": "node -e 'require(\"./index.js\").server()'",
27+
"server:dist": "node -e 'require(\"./dist/index.js\").server()'",
28+
"start": "yarn server:dist"
29+
},
30+
"devDependencies": {
31+
"@types/jest": "^29.5.14",
32+
"@types/node": "22.14.1",
33+
"nock": "13.5.6",
34+
"typescript": "5.8.3"
35+
},
36+
"dependencies": {
37+
"@chainlink/external-adapter-framework": "2.13.1",
38+
"tslib": "2.4.1"
39+
}
40+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { AdapterConfig } from '@chainlink/external-adapter-framework/config'
2+
3+
export const config = new AdapterConfig({
4+
API_KEY: {
5+
description: 'An API key for Data Provider',
6+
type: 'string',
7+
required: true,
8+
sensitive: true,
9+
},
10+
API_ENDPOINT: {
11+
description: 'An API endpoint for Data Provider',
12+
type: 'string',
13+
default:
14+
'https://api.truflation.com/api/v1/feed/truflation/macro-data-us/truflation_us_cpi_frozen_index',
15+
},
16+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
2+
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
3+
import { config } from '../config'
4+
import { httpTransport } from '../transport/cpi'
5+
6+
export const inputParameters = new InputParameters({})
7+
8+
export type BaseEndpointTypes = {
9+
Parameters: typeof inputParameters.definition
10+
Response: {
11+
Result: number
12+
Data: {
13+
date: string
14+
cpi: number
15+
}
16+
}
17+
Settings: typeof config.settings
18+
}
19+
20+
export const endpoint = new AdapterEndpoint({
21+
name: 'cpi',
22+
transport: httpTransport,
23+
inputParameters,
24+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { endpoint as cpi } from './cpi'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
2+
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
3+
import { config } from './config'
4+
import { cpi } from './endpoint'
5+
6+
export const adapter = new Adapter({
7+
defaultEndpoint: cpi.name,
8+
name: 'TRUFLATION',
9+
config,
10+
endpoints: [cpi],
11+
rateLimiting: {
12+
tiers: {
13+
default: {
14+
rateLimit1m: 6,
15+
},
16+
},
17+
},
18+
})
19+
20+
export const server = (): Promise<ServerInstance | undefined> => expose(adapter)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
HttpTransport,
3+
HttpTransportConfig,
4+
} from '@chainlink/external-adapter-framework/transports'
5+
import { BaseEndpointTypes } from '../endpoint/cpi'
6+
7+
export interface ResponseSchema {
8+
index: string[] // dates as string
9+
truflation_us_cpi_frozen_index: number[]
10+
start_date: string
11+
}
12+
13+
export type HttpTransportTypes = BaseEndpointTypes & {
14+
Provider: {
15+
RequestBody: never
16+
ResponseBody: ResponseSchema
17+
}
18+
}
19+
20+
const transportConfig: HttpTransportConfig<HttpTransportTypes> = {
21+
prepareRequests: (params, config) => {
22+
return params.map((param) => {
23+
return {
24+
params: [param],
25+
request: {
26+
baseURL: config.API_ENDPOINT,
27+
headers: {
28+
Authorization: config.API_KEY,
29+
},
30+
},
31+
}
32+
})
33+
},
34+
parseResponse: (params, response) => {
35+
const data = response.data
36+
if (
37+
!data?.index?.length ||
38+
!data?.truflation_us_cpi_frozen_index?.length ||
39+
data.index.length !== data.truflation_us_cpi_frozen_index.length
40+
) {
41+
return params.map((param) => {
42+
return {
43+
params: param,
44+
response: {
45+
errorMessage: `Invalid data provider response: index and truflation_us_cpi_frozen_index arrays must contain data and have equal length`,
46+
statusCode: 502,
47+
},
48+
}
49+
})
50+
}
51+
52+
const originalIndexArrayCopy = Array.from(data.index)
53+
// filter response, .sort() sorts ascending by default
54+
const sortedIndexArray = originalIndexArrayCopy.sort()
55+
const latestDate = sortedIndexArray[sortedIndexArray.length - 1]
56+
const mappedIndex = data.index.indexOf(latestDate)
57+
const result = data.truflation_us_cpi_frozen_index[mappedIndex]
58+
59+
return params.map((param) => {
60+
return {
61+
params: param,
62+
response: {
63+
result,
64+
data: {
65+
date: latestDate,
66+
cpi: result,
67+
},
68+
},
69+
}
70+
})
71+
},
72+
}
73+
74+
export class CpiTransport extends HttpTransport<HttpTransportTypes> {
75+
constructor() {
76+
super(transportConfig)
77+
}
78+
}
79+
80+
export const httpTransport = new CpiTransport()

0 commit comments

Comments
 (0)