1
- import { makeLogger } from '@chainlink/external-adapter-framework/util'
1
+ import { WebsocketReverseMappingTransport } from '@chainlink/external-adapter-framework/transports'
2
+ import { ProviderResult , makeLogger } from '@chainlink/external-adapter-framework/util'
3
+ import { WsTransportTypes as PriceWsTransportTypes } from './price'
4
+ import { WsTransportTypes as VwapWsTransportTypes } from './vwap'
2
5
3
6
const logger = makeLogger ( 'BlocksizeCapitalTransportUtils' )
4
7
5
8
export interface BaseMessage {
6
9
jsonrpc : string
7
10
id ?: string | number | null
8
- method : string
11
+ method ?: string
12
+ }
13
+
14
+ export type VwapUpdate = {
15
+ ticker : string
16
+ price ?: number
17
+ size ?: number
18
+ volume ?: number
19
+ ts : number
20
+ }
21
+
22
+ export type ProviderParams = {
23
+ tickers ?: string [ ]
24
+ api_key ?: string
25
+ }
26
+
27
+ const buildBlocksizeWebsocketMessage = ( method : string , params : ProviderParams ) : unknown => {
28
+ return {
29
+ jsonrpc : '2.0' ,
30
+ method : method ,
31
+ params : params ,
32
+ }
33
+ }
34
+
35
+ export const buildBlocksizeWebsocketAuthMessage = ( apiKey : string ) =>
36
+ buildBlocksizeWebsocketMessage ( 'authentication_logon' , { api_key : apiKey } )
37
+ export const buildBlocksizeWebsocketTickersMessage = ( method : string , pair : string ) =>
38
+ buildBlocksizeWebsocketMessage ( method , { tickers : [ pair ] } )
39
+
40
+ export const blocksizeDefaultUnsubscribeMessageBuilder = (
41
+ base : string ,
42
+ quote : string ,
43
+ method : string ,
44
+ ) : unknown => {
45
+ const pair = `${ base } ${ quote } ` . toUpperCase ( )
46
+ return buildBlocksizeWebsocketTickersMessage ( method , pair )
9
47
}
10
48
11
49
// use as open handler for standard WS connections
@@ -20,14 +58,48 @@ export const blocksizeDefaultWebsocketOpenHandler = (
20
58
logger . debug ( 'Got logged in response, connection is ready' )
21
59
resolve ( )
22
60
} else {
23
- reject ( new Error ( ' Failed to make WS connection' ) )
61
+ reject ( new Error ( ` Failed to make WS connection: ${ JSON . stringify ( parsed ) } ` ) )
24
62
}
25
63
} )
26
- const options = {
27
- jsonrpc : '2.0' ,
28
- method : 'authentication_logon' ,
29
- params : { api_key : apiKey } ,
30
- }
31
- connection . send ( JSON . stringify ( options ) )
64
+ const message = buildBlocksizeWebsocketAuthMessage ( apiKey )
65
+ connection . send ( JSON . stringify ( message ) )
32
66
} )
33
67
}
68
+
69
+ export const handlePriceUpdates = (
70
+ updates : VwapUpdate [ ] ,
71
+ transport : WebsocketReverseMappingTransport < any , any > ,
72
+ ) : ProviderResult < PriceWsTransportTypes | VwapWsTransportTypes > [ ] | undefined => {
73
+ const results = [ ]
74
+ for ( const update of updates ) {
75
+ const params = transport . getReverseMapping ( update . ticker )
76
+ if ( ! params ) {
77
+ continue
78
+ }
79
+ if ( ! update . price ) {
80
+ const errorMessage = `The data provider didn't return any value for ${ params . base } /${ params . quote } `
81
+ logger . info ( errorMessage )
82
+ results . push ( {
83
+ params,
84
+ response : {
85
+ statusCode : 502 ,
86
+ errorMessage,
87
+ } ,
88
+ } )
89
+ } else {
90
+ results . push ( {
91
+ params,
92
+ response : {
93
+ result : update . price ,
94
+ data : {
95
+ result : update . price ,
96
+ } ,
97
+ timestamps : {
98
+ providerIndicatedTimeUnixMs : update . ts ,
99
+ } ,
100
+ } ,
101
+ } )
102
+ }
103
+ }
104
+ return results
105
+ }
0 commit comments