@@ -25,8 +25,10 @@ export function getSwapAdapterConfig(chainId: string) {
25
25
26
26
export class SwapBeacon {
27
27
private client : Client ;
28
- private adapters : Adapter [ ] ;
29
- private chainIds : string [ ] ;
28
+ private pingTimeout : NodeJS . Timeout | undefined ;
29
+ private readonly adapters : Adapter [ ] ;
30
+ private readonly chainIds : string [ ] ;
31
+ private readonly PING_INTERVAL = 30000 ;
30
32
31
33
constructor ( endpoint : string , _chainIds : string [ ] ) {
32
34
this . client = new Client (
@@ -213,6 +215,7 @@ export class SwapBeacon {
213
215
}
214
216
215
217
async opportunityHandler ( opportunity : Opportunity ) {
218
+ console . log ( "Received opportunity:" , opportunity . opportunityId ) ;
216
219
const swapAdapterConfig = getSwapAdapterConfig ( opportunity . chainId ) ;
217
220
218
221
if (
@@ -235,6 +238,10 @@ export class SwapBeacon {
235
238
return ;
236
239
}
237
240
await this . client . submitOpportunity ( convertedOpportunity ) ;
241
+ console . log (
242
+ "Submitted converted opportunity. Original id:" ,
243
+ opportunity . opportunityId
244
+ ) ;
238
245
} catch ( error ) {
239
246
console . error (
240
247
`Failed to convert and submit opportunity ${ JSON . stringify (
@@ -246,12 +253,25 @@ export class SwapBeacon {
246
253
) ;
247
254
}
248
255
256
+ heartbeat ( ) {
257
+ if ( this . pingTimeout !== undefined ) clearTimeout ( this . pingTimeout ) ;
258
+
259
+ this . pingTimeout = setTimeout ( ( ) => {
260
+ console . error ( "Received no ping. Terminating connection." ) ;
261
+ this . client . websocket . terminate ( ) ;
262
+ } , this . PING_INTERVAL + 2000 ) ; // 2 seconds for latency
263
+ }
264
+
249
265
async start ( ) {
250
266
try {
251
267
await this . client . subscribeChains ( this . chainIds ) ;
252
268
console . log (
253
269
`Subscribed to chain ${ this . chainIds } . Waiting for opportunities...`
254
270
) ;
271
+ this . heartbeat ( ) ;
272
+ this . client . websocket . on ( "ping" , ( ) => {
273
+ this . heartbeat ( ) ;
274
+ } ) ;
255
275
} catch ( error ) {
256
276
console . error ( error ) ;
257
277
this . client . websocket ?. close ( ) ;
0 commit comments