|
| 1 | +import { HttpTransport } from '@chainlink/external-adapter-framework/transports' |
| 2 | +import { BaseEndpointTypes } from '../endpoint/balance' |
| 3 | + |
| 4 | +export interface ResponseSchema { |
| 5 | + accountName: string |
| 6 | + totalReserve: number |
| 7 | + totalToken: number |
| 8 | + timestamp: string |
| 9 | + ripcord: boolean |
| 10 | + ripcordDetails: { |
| 11 | + insufficient_balance: boolean |
| 12 | + source_failure: boolean |
| 13 | + external_intervention: boolean |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export type HttpTransportTypes = BaseEndpointTypes & { |
| 18 | + Provider: { |
| 19 | + RequestBody: never |
| 20 | + ResponseBody: ResponseSchema |
| 21 | + } |
| 22 | +} |
| 23 | +export const httpTransport = new HttpTransport<HttpTransportTypes>({ |
| 24 | + prepareRequests: (params, config) => { |
| 25 | + return params.map((param) => { |
| 26 | + return { |
| 27 | + params: [param], |
| 28 | + request: { |
| 29 | + baseURL: config.API_ENDPOINT, |
| 30 | + url: '/balances', |
| 31 | + headers: { |
| 32 | + 'x-functions-key': config.API_KEY, |
| 33 | + }, |
| 34 | + params: { |
| 35 | + client_name: param.clientName, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + }) |
| 40 | + }, |
| 41 | + parseResponse: (params, response) => { |
| 42 | + // Return error if ripcord indicator true |
| 43 | + if (response.data.ripcord) { |
| 44 | + const message = `Ripcord indicator true. Details: ${JSON.stringify( |
| 45 | + response.data.ripcordDetails, |
| 46 | + )}` |
| 47 | + return [ |
| 48 | + { |
| 49 | + params: params[0], |
| 50 | + response: { |
| 51 | + errorMessage: message, |
| 52 | + statusCode: 502, |
| 53 | + timestamps: { |
| 54 | + providerIndicatedTimeUnixMs: new Date(response.data.timestamp).getTime(), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + const result = response.data.totalReserve |
| 62 | + |
| 63 | + if (typeof result === 'undefined') { |
| 64 | + return [ |
| 65 | + { |
| 66 | + params: params[0], |
| 67 | + response: { |
| 68 | + errorMessage: `Response missing totalReserve`, |
| 69 | + statusCode: 502, |
| 70 | + timestamps: { |
| 71 | + providerIndicatedTimeUnixMs: new Date(response.data.timestamp).getTime(), |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + ] |
| 76 | + } |
| 77 | + |
| 78 | + return [ |
| 79 | + { |
| 80 | + params: params[0], |
| 81 | + response: { |
| 82 | + result, |
| 83 | + data: { |
| 84 | + result, |
| 85 | + }, |
| 86 | + timestamps: { |
| 87 | + providerIndicatedTimeUnixMs: new Date(response.data.timestamp).getTime(), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + ] |
| 92 | + }, |
| 93 | +}) |
0 commit comments