|
| 1 | +import { |
| 2 | + HttpTransport, |
| 3 | + HttpTransportConfig, |
| 4 | +} from '@chainlink/external-adapter-framework/transports' |
| 5 | +import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error' |
| 6 | +import { BaseEndpointTypes } from '../endpoint/nav' |
| 7 | +import { validateResponseSignature } from './sigutils' |
| 8 | + |
| 9 | +export interface SingleResponseSchema { |
| 10 | + assetId: string |
| 11 | + name?: string |
| 12 | + nav: number |
| 13 | + seqNum: number |
| 14 | + yieldOneDay?: string |
| 15 | + yieldSevenDay?: string |
| 16 | + recordDate: string |
| 17 | + staleness?: number |
| 18 | + signedMessage: { |
| 19 | + signature: string |
| 20 | + content: string |
| 21 | + hash: string |
| 22 | + prevHash: string | null |
| 23 | + prevSig: string | null |
| 24 | + prevContent: string | null |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export interface ResponseSchema { |
| 29 | + docs: SingleResponseSchema[] |
| 30 | +} |
| 31 | + |
| 32 | +export type HttpTransportTypes = BaseEndpointTypes & { |
| 33 | + Provider: { |
| 34 | + RequestBody: never |
| 35 | + ResponseBody: ResponseSchema |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function getPubKeys(assetEnvVarPrefix: string): string[] { |
| 40 | + const envVarName = `${assetEnvVarPrefix.toUpperCase()}_PUBKEYS` |
| 41 | + const pubkeys = process.env[envVarName] |
| 42 | + if (!pubkeys) { |
| 43 | + throw new AdapterInputError({ |
| 44 | + message: `Missing env var ${envVarName}`, |
| 45 | + statusCode: 400, |
| 46 | + }) |
| 47 | + } |
| 48 | + return pubkeys.split(',').map((s) => s.trim()) ?? [] |
| 49 | +} |
| 50 | + |
| 51 | +const transportConfig: HttpTransportConfig<HttpTransportTypes> = { |
| 52 | + prepareRequests: (params, config) => { |
| 53 | + return params.map((param) => { |
| 54 | + return { |
| 55 | + params: [param], |
| 56 | + request: { |
| 57 | + baseURL: config.API_ENDPOINT, |
| 58 | + headers: { |
| 59 | + apikey: config.API_KEY, |
| 60 | + }, |
| 61 | + params: { |
| 62 | + assetId: param.assetId, |
| 63 | + sortBy: 'recordDate', |
| 64 | + sortOrder: 'DESC', |
| 65 | + page: 1, |
| 66 | + limit: 1, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + }) |
| 71 | + }, |
| 72 | + parseResponse: (params, response) => { |
| 73 | + const asset = response?.data?.docs?.[0] |
| 74 | + return params.map((param) => { |
| 75 | + if (!asset) { |
| 76 | + return { |
| 77 | + params: param, |
| 78 | + response: { |
| 79 | + errorMessage: `The data provider didn't return any value for ${param.assetId}`, |
| 80 | + statusCode: 502, |
| 81 | + }, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const timestamps = { |
| 86 | + providerIndicatedTimeUnixMs: new Date(asset.recordDate).getTime(), |
| 87 | + } |
| 88 | + |
| 89 | + const pubkeys = getPubKeys(param.envVarPrefix) |
| 90 | + validateResponseSignature(asset, pubkeys) |
| 91 | + |
| 92 | + if (asset.assetId !== param.assetId) { |
| 93 | + return { |
| 94 | + params: param, |
| 95 | + response: { |
| 96 | + errorMessage: `Could not find ${param.assetId} in the response`, |
| 97 | + statusCode: 502, |
| 98 | + }, |
| 99 | + timestamps, |
| 100 | + } |
| 101 | + } |
| 102 | + const result = asset.nav |
| 103 | + return { |
| 104 | + params: param, |
| 105 | + response: { |
| 106 | + result, |
| 107 | + data: { |
| 108 | + result, |
| 109 | + }, |
| 110 | + timestamps, |
| 111 | + }, |
| 112 | + } |
| 113 | + }) |
| 114 | + }, |
| 115 | +} |
| 116 | + |
| 117 | +// Exported for testing |
| 118 | +export class NavTransport extends HttpTransport<HttpTransportTypes> { |
| 119 | + constructor() { |
| 120 | + super(transportConfig) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export const httpTransport = new NavTransport() |
0 commit comments