Skip to content
Open

R25 EA #4346

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions packages/sources/r25/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Chainlink R25 External Adapter

This adapter fetches the latest NAV (Net Asset Value) via R25's REST API and returns a single numeric result with timestamps for both Data Feeds and Data Streams.

## Configuration

The adapter takes the following environment variables:

| Required? | Name | Description | Type | Options | Default |
| :-------: | :------------: | :-----------------------------------------: | :----: | :-----: | :-------------------: |
|| `API_KEY` | An API key for R25 | string | | |
|| `API_SECRET` | An API secret for R25 used to sign requests | string | | |
| | `API_ENDPOINT` | An API endpoint for R25 | string | | `https://app.r25.xyz` |

## Input Parameters

### `nav` endpoint

Supported names for this endpoint are: `nav`, `price`.

#### Input Params

| Required? | Name | Description | Type | Options | Default |
| :-------: | :---------: | :---------------------------------: | :----: | :-----: | :-----: |
|| `chainType` | The chain type (e.g., polygon, sui) | string | | |
|| `tokenName` | The token name (e.g., rcusdp) | string | | |

### Example

Request:

```json
{
"data": {
"endpoint": "nav",
"chainType": "polygon",
"tokenName": "rcusdp"
}
}
```

Response:

```json
{
"data": {
"result": 1.020408163265306
},
"result": 1.020408163265306,
"timestamps": {
"providerIndicatedTimeUnixMs": 1731344153448
},
"statusCode": 200
}
```

## Rate Limiting

The adapter implements rate limiting of 5 requests/second per IP as specified in the R25 API documentation.

## Authentication

The adapter automatically generates the following headers for authentication:

- `x-api-key`: API key
- `x-utc-timestamp`: Current timestamp in milliseconds
- `x-signature`: HMAC-SHA256 signature

### Signature Algorithm

The signature is generated using the HMAC-SHA256 algorithm. The signature string is constructed as follows:

```
{method}\n{path}\n{sorted_params}\n{timestamp}\n{api_key}
```

Where:

- `method`: HTTP method in lowercase (e.g., "get")
- `path`: Request path (e.g., "/api/public/current/nav")
- `sorted_params`: Query parameters sorted by key in lexicographical order, formatted as key=value, joined with &
- `timestamp`: Current UTC timestamp in milliseconds
- `api_key`: API key

The HMAC-SHA256 hash is computed using the `API_SECRET` as the secret key, and the result is hex-encoded.

## API Response Mapping

### Data Feeds mapping

- `answer` = `currentNav` (from API response)

### Data Streams mapping

- `navPerShare` = `currentNav`
- `aum` = `totalAsset`
- `navDate` = `lastUpdate`
42 changes: 42 additions & 0 deletions packages/sources/r25/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@chainlink/r25-adapter",
"version": "1.0.0",
"description": "Chainlink r25 adapter.",
"keywords": [
"Chainlink",
"LINK",
"blockchain",
"oracle",
"r25"
],
"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"
},
"dependencies": {
"@chainlink/external-adapter-framework": "2.8.0",
"crypto-js": "4.2.0",
"tslib": "2.4.1"
},
"devDependencies": {
"@types/crypto-js": "4.2.2",
"@types/jest": "^29.5.14",
"@types/node": "22.14.1",
"nock": "13.5.6",
"typescript": "5.8.3"
}
}
21 changes: 21 additions & 0 deletions packages/sources/r25/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AdapterConfig } from '@chainlink/external-adapter-framework/config'

export const config = new AdapterConfig({
API_KEY: {
description: 'An API key for R25',
type: 'string',
required: true,
sensitive: true,
},
API_SECRET: {
description: 'An API secret for R25 used to sign requests',
type: 'string',
required: true,
sensitive: true,
},
API_ENDPOINT: {
description: 'An API endpoint for R25',
type: 'string',
default: 'https://app.r25.xyz',
},
})
1 change: 1 addition & 0 deletions packages/sources/r25/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as nav from './nav'
39 changes: 39 additions & 0 deletions packages/sources/r25/src/endpoint/nav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
import { SingleNumberResultResponse } from '@chainlink/external-adapter-framework/util'
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
import { config } from '../config'
import { httpTransport } from '../transport/nav'

export const inputParameters = new InputParameters(
{
chainType: {
type: 'string',
description: 'The chain type (e.g., polygon, sui)',
required: true,
},
tokenName: {
type: 'string',
description: 'The token name (e.g., rcusdp)',
required: true,
},
},
[
{
chainType: 'polygon',
tokenName: 'rcusdp',
},
],
)

export type BaseEndpointTypes = {
Parameters: typeof inputParameters.definition
Response: SingleNumberResultResponse
Settings: typeof config.settings
}

export const endpoint = new AdapterEndpoint({
name: 'nav',
aliases: ['price'],
transport: httpTransport,
inputParameters,
})
20 changes: 20 additions & 0 deletions packages/sources/r25/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
import { config } from './config'
import { endpoint as navEndpoint } from './endpoint/nav'

export const adapter = new Adapter({
defaultEndpoint: navEndpoint.name,
name: 'R25',
config,
endpoints: [navEndpoint],
rateLimiting: {
tiers: {
default: {
rateLimit1s: 5, //5 requests per second
},
},
},
})

export const server = (): Promise<ServerInstance | undefined> => expose(adapter)
52 changes: 52 additions & 0 deletions packages/sources/r25/src/transport/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import CryptoJS from 'crypto-js'

/**
* Generate the HMAC-SHA256 signature for R25 API requests.
*
* The signature string is constructed as:
* {method}\n{path}\n{sorted_params}\n{timestamp}\n{api_key}
*
* Where:
* - method: HTTP method in lowercase (e.g., "get")
* - path: Request path (e.g., "/api/public/current/nav")
* - sorted_params: Query parameters sorted by key, formatted as key=value, joined with &
* - timestamp: Current UTC timestamp in milliseconds
* - api_key: API key
*/
export const getRequestHeaders = ({
method,
path,
params,
apiKey,
secret,
timestamp,
}: {
method: string
path: string
params: Record<string, string>
apiKey: string
secret: string
timestamp: number
}): Record<string, string> => {
// Sort parameters by key in lexicographical order and format as key=value
const sortedParams = Object.keys(params)
.sort()
.map((key) => `${key}=${params[key]}`)
.join('&')

const stringToSign = [
method.toLowerCase(),
path,
sortedParams,
timestamp.toString(),
apiKey,
].join('\n')

const signature = CryptoJS.HmacSHA256(stringToSign, secret).toString(CryptoJS.enc.Hex)

return {
'x-api-key': apiKey,
'x-utc-timestamp': timestamp.toString(),
'x-signature': signature,
}
}
87 changes: 87 additions & 0 deletions packages/sources/r25/src/transport/nav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { HttpTransport } from '@chainlink/external-adapter-framework/transports'
import { BaseEndpointTypes } from '../endpoint/nav'
import { getRequestHeaders } from './authentication'

export interface ResponseSchema {
code: string
success: boolean
message: string
data: {
lastUpdate: string
tokenName: string
chainType: string
totalSupply: number
totalAsset: number
currentNav: string
}
}

export type HttpTransportTypes = BaseEndpointTypes & {
Provider: {
RequestBody: never
ResponseBody: ResponseSchema
}
}

export const httpTransport = new HttpTransport<HttpTransportTypes>({
prepareRequests: (params, config) => {
return params.map((param) => {
const method = 'GET'
const path = '/api/public/current/nav'
const timestamp = Date.now()

const queryParams = {
chainType: param.chainType,
tokenName: param.tokenName,
}

const headers = getRequestHeaders({
method,
path,
params: queryParams,
apiKey: config.API_KEY,
secret: config.API_SECRET,
timestamp,
})

return {
params: [param],
request: {
baseURL: config.API_ENDPOINT,
url: path,
params: queryParams,
headers,
},
}
})
},
parseResponse: (params, response) => {
return params.map((param) => {
if (!response.data.success) {
return {
params: param,
response: {
errorMessage: response.data.message || 'Request failed',
statusCode: 502,
},
}
}

const currentNav = Number(response.data.data.currentNav)
const lastUpdate = response.data.data.lastUpdate

return {
params: param,
response: {
result: currentNav,
data: {
result: currentNav,
},
timestamps: {
providerIndicatedTimeUnixMs: new Date(lastUpdate).getTime(),
},
},
}
})
},
})
Loading
Loading