forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.test.ts
299 lines (261 loc) · 8.39 KB
/
price.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import nock from 'nock'
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter'
import { metrics } from '@chainlink/external-adapter-framework/metrics'
import { SubscriptionDeltas } from '@chainlink/external-adapter-framework/transports/abstract/streaming'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports/websocket'
import { LoggerFactoryProvider, sleep } from '@chainlink/external-adapter-framework/util'
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
import * as price from '../../src/transport/price'
import { RequestParams } from '../../src/endpoint/price'
//Since the test is directly using transport functions, we need to initialize the logger here
LoggerFactoryProvider.set()
const desiredSubs = [
{
base: 'ETH',
quote: 'USD',
},
]
export const mockTokenResponse = (): nock.Scope =>
nock('https://dar-test.com', {
encodedQueryParams: true,
})
.post('/token-auth')
.reply(
200,
() => ({
access_token: '111111111111111111111111111',
expires_in: 12345,
token_type: 'test_key',
}),
[
'Content-Type',
'application/json',
'Connection',
'close',
'Vary',
'Accept-Encoding',
'Vary',
'Origin',
],
)
.persist()
describe('Config', () => {
const context = {
adapterSettings: {
WS_API_ENDPOINT: 'wss://dar--ws-test.com',
},
endpointName: 'price',
inputParameters: new InputParameters({}),
} as EndpointContext<price.WsTransportTypes>
describe('url', () => {
it('returns the endpoint URL from the config', () => {
expect(price.config.url(context, desiredSubs)).toEqual(
context.adapterSettings.WS_API_ENDPOINT,
)
})
})
describe('message', () => {
it('returns a result for each symbol', () => {
const message = {
priceIdentifier: 'eth-USD-10-1694168714.4',
methodologyCode: 'DARSTD400msVW',
darAssetID: 'DASK8KY',
darAssetTicker: 'eth',
quoteCurrency: 'USD',
price: 1621.891790669695,
priceTier: 2,
volumes: { verifiable: 7599.211796003789, estimatedReal: '' },
windowStart: '2023-09-08T10:25:14+00:00',
indowEnd: '2023-09-08T10:25:14+00:00',
holdover: true,
principalMarketPrice: null,
errors: '',
publishedAt: '2023-09-08T10:26:07.915375Z',
receivedAt: '2023-09-08T10:26:07+00:00',
effectiveTime: 1694168714.4,
}
expect(price.config.handlers.message(message, context)).toEqual([
{
params: { base: 'eth', quote: 'USD' },
response: {
result: 1621.891790669695,
data: {
result: 1621.891790669695,
},
timestamps: {
providerIndicatedTimeUnixMs: 1694168714400,
},
},
},
])
})
})
})
describe('DarWebsocketTransport', () => {
const context = {
adapterSettings: {
API_ENDPOINT: 'https://dar-test.com',
WS_API_ENDPOINT: 'wss://dar-ws-test.com',
WS_API_KEY: 'test_key',
},
endpointName: 'price',
inputParameters: new InputParameters({}),
} as EndpointContext<price.WsTransportTypes>
let transport: price.DarWebsocketTransport
let subscriptions: SubscriptionDeltas<RequestParams>
let connClosed: boolean
class MockWebSocket {
onclose?: () => void
readyState?: number
close() {
connClosed = true
this.readyState = 3 // CLOSED
if (this.onclose) {
this.onclose()
}
}
addEventListener(event: string, listener: any) {
if (event === 'open') {
listener({ type: 'mock_open' })
}
}
removeAllListeners() {
return
}
}
beforeAll(() => {
WebSocketClassProvider.set(MockWebSocket)
process.env.WS_API_KEY = 'test_key'
mockTokenResponse()
metrics.initialize()
})
beforeEach(() => {
transport = new price.DarWebsocketTransport(price.config)
connClosed = false
})
describe('test connection', () => {
it('first request, establish connection', async () => {
subscriptions = {
desired: [{ base: 'BTC', quote: 'USD' }],
new: [],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
expect(subscriptions['new']).toEqual([{ base: 'BTC', quote: 'USD' }])
transport.wsConnection.close()
})
it('new subscription with existing base does not close the connection', async () => {
subscriptions = {
desired: [{ base: 'BTC', quote: 'USD' }],
new: [],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
subscriptions = {
desired: [
{ base: 'BTC', quote: 'USD' },
{ base: 'BTC', quote: 'ETH' },
],
new: [{ base: 'BTC', quote: 'ETH' }],
stale: [],
}
await transport.streamHandler(context, subscriptions)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
})
})
describe('test closing of connection', () => {
it('new subscription, closes existing connection and resubscribes', async () => {
subscriptions = {
desired: [{ base: 'BTC', quote: 'USD' }],
new: [{ base: 'BTC', quote: 'USD' }],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
subscriptions = {
desired: [
{ base: 'BTC', quote: 'USD' },
{ base: 'ETH', quote: 'USD' },
],
new: [{ base: 'ETH', quote: 'USD' }],
stale: [],
}
await transport.streamHandler(context, subscriptions)
// we close the connection however since we have a new unique base ('ETH') we will resubscribe again
expect(connClosed).toEqual(true)
expect(transport.connectionClosed()).toEqual(false)
})
it('stale connection, closes existing connection', async () => {
subscriptions = {
desired: [{ base: 'BTC', quote: 'USD' }],
new: [],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
subscriptions = {
desired: [],
new: [],
stale: [{ base: 'BTC', quote: 'USD' }],
}
await transport.streamHandler(context, subscriptions)
expect(connClosed).toEqual(true)
expect(transport.connectionClosed()).toEqual(true)
})
})
describe('test opening of connection', () => {
it('new subscription opens a new connection', async () => {
subscriptions = {
desired: [{ base: 'BTC', quote: 'USD' }],
new: [],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
subscriptions['desired'].push({ base: 'ETH', quote: 'USD' })
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(subscriptions['new']).toEqual([
{ base: 'BTC', quote: 'USD' },
{ base: 'ETH', quote: 'USD' },
])
expect(transport.connectionClosed()).toEqual(false)
})
it('stale connection, opens a new connection', async () => {
subscriptions = {
desired: [
{ base: 'BTC', quote: 'USD' },
{ base: 'ETH', quote: 'USD' },
],
new: [],
stale: [],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(connClosed).toEqual(false)
expect(transport.connectionClosed()).toEqual(false)
subscriptions = {
desired: [{ base: 'ETH', quote: 'USD' }],
new: [],
stale: [{ base: 'BTC', quote: 'USD' }],
}
await transport.streamHandler(context, subscriptions)
await sleep(100)
expect(subscriptions['new']).toEqual([{ base: 'ETH', quote: 'USD' }])
expect(transport.connectionClosed()).toEqual(false)
})
})
})