|
| 1 | +import { EndpointContext } from '@chainlink/external-adapter-framework/adapter' |
| 2 | +import { |
| 3 | + WebSocketTransport, |
| 4 | + WebSocketTransportConfig, |
| 5 | +} from '@chainlink/external-adapter-framework/transports' |
| 6 | +import { BaseEndpointTypes } from '../endpoint/price' |
| 7 | + |
| 8 | +export interface PriceMessage { |
| 9 | + last_updated: string |
| 10 | + price: number |
| 11 | + symbol: string |
| 12 | +} |
| 13 | + |
| 14 | +export interface WSResponse { |
| 15 | + success: boolean |
| 16 | + data: Array<PriceMessage> |
| 17 | + topic: string |
| 18 | +} |
| 19 | + |
| 20 | +export type WsTransportTypes = BaseEndpointTypes & { |
| 21 | + Provider: { |
| 22 | + WsMessage: WSResponse |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +export const options: WebSocketTransportConfig<WsTransportTypes> = { |
| 27 | + url: (context: EndpointContext<WsTransportTypes>) => context.adapterSettings.WS_API_ENDPOINT, |
| 28 | + options: async (context: EndpointContext<WsTransportTypes>) => ({ |
| 29 | + headers: { |
| 30 | + 'X-GMCI-API-KEY': context.adapterSettings.API_KEY, |
| 31 | + }, |
| 32 | + }), |
| 33 | + |
| 34 | + handlers: { |
| 35 | + message(message) { |
| 36 | + if (message.success === false) { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + const results = [] |
| 41 | + |
| 42 | + if (message.topic === 'price') { |
| 43 | + for (const item of message.data) { |
| 44 | + results.push({ |
| 45 | + params: { symbol: item.symbol }, |
| 46 | + response: { |
| 47 | + result: item.price, |
| 48 | + data: { |
| 49 | + result: item.price, |
| 50 | + symbol: item.symbol, |
| 51 | + }, |
| 52 | + timestamps: { |
| 53 | + providerIndicatedTimeUnixMs: Date.parse(item.last_updated), |
| 54 | + }, |
| 55 | + }, |
| 56 | + }) |
| 57 | + } |
| 58 | + } |
| 59 | + return results |
| 60 | + }, |
| 61 | + }, |
| 62 | + |
| 63 | + builders: { |
| 64 | + subscribeMessage: (params) => { |
| 65 | + return { |
| 66 | + op: 'subscribe', |
| 67 | + args: [`price.${params.symbol}`.toLowerCase()], |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + unsubscribeMessage: (params) => { |
| 72 | + return { |
| 73 | + op: 'unsubscribe', |
| 74 | + args: [`price.${params.symbol}`.toLowerCase()], |
| 75 | + } |
| 76 | + }, |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +export const transport = new WebSocketTransport(options) |
0 commit comments