-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathfiller.ts
More file actions
547 lines (476 loc) · 17 KB
/
filler.ts
File metadata and controls
547 lines (476 loc) · 17 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import { EventMonitor } from "./event-monitor"
import { FillerStrategy } from "@/strategies/base"
import {
OrderV2,
FillerConfig,
ChainConfig,
getChainId,
retryPromise,
type HexString,
IntentsCoprocessor,
} from "@hyperbridge/sdk"
import pQueue from "p-queue"
import { privateKeyToAddress } from "viem/accounts"
import {
BidStorageService,
ChainClientManager,
ContractInteractionService,
DelegationService,
RebalancingService,
} from "@/services"
import { FillerConfigService } from "@/services/FillerConfigService"
import { getLogger } from "@/services/Logger"
import { Decimal } from "decimal.js"
export class IntentFiller {
public monitor: EventMonitor
private strategies: FillerStrategy[]
private chainQueues: Map<number, pQueue>
private globalQueue: pQueue
private chainClientManager: ChainClientManager
private contractService: ContractInteractionService
private delegationService?: DelegationService
private rebalancingService?: RebalancingService
private bidStorage?: BidStorageService
private retractionQueue: pQueue
private pendingRetractions = new Set<string>()
private rebalancingInterval?: NodeJS.Timeout
private retractionSweepInterval?: NodeJS.Timeout
private hyperbridge: Promise<IntentsCoprocessor> | undefined = undefined
private config: FillerConfig
private configService: FillerConfigService
private privateKey: HexString
private logger = getLogger("intent-filler")
constructor(
chainConfigs: ChainConfig[],
strategies: FillerStrategy[],
config: FillerConfig,
configService: FillerConfigService,
chainClientManager: ChainClientManager,
contractService: ContractInteractionService,
privateKey: HexString,
rebalancingService?: RebalancingService,
bidStorage?: BidStorageService,
) {
this.configService = configService
this.privateKey = privateKey
this.chainClientManager = chainClientManager
this.contractService = contractService
this.rebalancingService = rebalancingService
this.bidStorage = bidStorage
const fillerAddress = privateKeyToAddress(privateKey) as HexString
this.monitor = new EventMonitor(chainConfigs, configService, this.chainClientManager, fillerAddress)
this.strategies = strategies
this.config = config
this.chainQueues = new Map()
chainConfigs.forEach((chainConfig) => {
// 1 order per chain at a time due to EVM constraints
this.chainQueues.set(chainConfig.chainId, new pQueue({ concurrency: 1 }))
})
this.globalQueue = new pQueue({
concurrency: config.maxConcurrentOrders || 5,
})
this.retractionQueue = new pQueue({ concurrency: 1 })
const hyperbridgeWsUrl = configService.getHyperbridgeWsUrl()
const substrateKey = configService.getSubstratePrivateKey()
if (hyperbridgeWsUrl && substrateKey) {
this.hyperbridge = IntentsCoprocessor.connect(hyperbridgeWsUrl, substrateKey)
}
// Set up event handlers
this.monitor.on("newOrder", ({ order }) => {
this.handleNewOrder(order)
})
this.monitor.on("orderFilledOnChain", ({ commitment, filler, chainId }) => {
this.handleOrderFilledOnChain(commitment as HexString, filler, chainId)
})
}
/**
* Initializes the filler, including setting up EIP-7702 delegation if solver selection is active on any chain.
* This should be called before start().
*/
public async initialize(): Promise<void> {
// Check which chains have solver selection active
const chainIds = this.configService.getConfiguredChainIds()
const chainsWithSolverSelection: string[] = []
for (const chainId of chainIds) {
const chain = `EVM-${chainId}`
const isActive = await this.contractService.isSolverSelectionActive(chain)
if (isActive) {
chainsWithSolverSelection.push(chain)
this.logger.info({ chain }, "Solver selection is active on chain")
}
}
// Set up delegation service on chains where solver selection is active
if (chainsWithSolverSelection.length > 0 && this.hyperbridge) {
this.delegationService = new DelegationService(this.chainClientManager, this.configService, this.privateKey)
this.logger.info(
{ chains: chainsWithSolverSelection },
"Setting up EIP-7702 delegation on chains with solver selection",
)
const result = await this.delegationService.setupDelegationOnChains(chainsWithSolverSelection)
if (!result.success) {
this.logger.warn({ results: result.results }, "Some chains failed EIP-7702 delegation setup")
}
}
}
public start(): void {
this.monitor.startListening()
// Start periodic rebalancing if service is configured
if (this.rebalancingService) {
this.startRebalancing()
}
if (this.bidStorage && this.hyperbridge) {
this.startRetractionSweep()
}
}
/**
* Start periodic rebalancing checks.
* Checks every 5 minutes for triggers and executes rebalancing if needed.
*/
private startRebalancing(): void {
// Run initial check after 30 seconds (to let the filler start up)
setTimeout(() => {
this.checkAndRebalance().catch((error) => {
this.logger.error({ error }, "Error in initial rebalancing check")
})
}, 30_000)
// Then check every 5 minutes
this.rebalancingInterval = setInterval(
() => {
this.checkAndRebalance().catch((error) => {
this.logger.error({ error }, "Error in periodic rebalancing check")
})
},
5 * 60 * 1000,
) // 5 minutes
this.logger.info("Periodic rebalancing checks started (every 5 minutes)")
}
/**
* Check for rebalancing triggers and execute if needed.
*/
private async checkAndRebalance(): Promise<void> {
if (!this.rebalancingService) {
return
}
try {
const result = await this.rebalancingService.rebalancePortfolio()
if (result.success && result.transfers.length > 0) {
this.logger.info(
{
transferCount: result.transfers.length,
executedCount: result.executedTransfers.length,
},
"Portfolio rebalancing completed",
)
} else if (result.transfers.length === 0) {
this.logger.debug("No rebalancing needed")
}
} catch (error) {
this.logger.error({ error }, "Portfolio rebalancing failed")
}
}
private startRetractionSweep(): void {
const BID_TTL_MS = 60 * 60 * 1000 // 1 hour
const SWEEP_INTERVAL_MS = 5 * 60 * 1000 // 5 minutes
this.retractionSweepInterval = setInterval(() => {
this.sweepExpiredBids(BID_TTL_MS).catch((error) => {
this.logger.error({ error }, "Error in retraction sweep")
})
}, SWEEP_INTERVAL_MS)
this.logger.info("Periodic retraction sweep started (every 5 minutes, 1h TTL)")
}
private async sweepExpiredBids(maxAgeMs: number): Promise<void> {
if (!this.bidStorage || !this.hyperbridge) {
return
}
const expired = this.bidStorage.getExpiredUnretractedBids(maxAgeMs)
if (expired.length === 0) {
return
}
this.logger.info({ count: expired.length }, "Sweeping expired unretracted bids")
for (const bid of expired) {
this.enqueueRetraction(bid.commitment)
}
}
public async stop(): Promise<void> {
this.monitor.stopListening()
// Stop rebalancing interval
if (this.rebalancingInterval) {
clearInterval(this.rebalancingInterval)
this.rebalancingInterval = undefined
this.logger.info("Periodic rebalancing checks stopped")
}
if (this.retractionSweepInterval) {
clearInterval(this.retractionSweepInterval)
this.retractionSweepInterval = undefined
this.logger.info("Periodic retraction sweep stopped")
}
// Wait for all queues to complete
const promises: Promise<void>[] = []
this.chainQueues.forEach((queue) => {
promises.push(queue.onIdle())
})
promises.push(this.globalQueue.onIdle())
promises.push(this.retractionQueue.onIdle())
await Promise.all(promises)
// Withdraw any remaining EntryPoint deposits back to the solver EOA
await this.contractService.withdrawAllEntryPointDeposits()
// Disconnect shared Hyperbridge connection
if (this.hyperbridge) {
const service = await this.hyperbridge.catch(() => null)
await service?.disconnect()
}
this.logger.info("All orders processed, filler stopped")
}
// Operations
private handleNewOrder(order: OrderV2): void {
// Use the global queue for the initial analysis
// This can happen in parallel for PublicClient orders
this.globalQueue.add(async () => {
this.logger.info({ orderId: order.id }, "New order detected")
try {
// Early check: if solver selection is active, ensure hyperbridge is configured
const solverSelectionActive = this.contractService.getCache().getSolverSelection(order.destination)
if (solverSelectionActive == null) {
this.logger.error({ orderId: order.id }, "Shared cache is not initialized")
return
}
if (solverSelectionActive && !this.hyperbridge) {
this.logger.error(
{ orderId: order.id },
"Solver selection is active but Hyperbridge is not configured. Skipping order.",
)
return
}
const sourceClient = this.chainClientManager.getPublicClient(order.source)
// Base layer: stable-only USD value from ContractInteractionService
const baseInputUsd = await this.contractService.getInputUsdValue(order)
const canFillCache = new Map<FillerStrategy, boolean>()
for (const strategy of this.strategies) {
try {
canFillCache.set(strategy, await strategy.canFill(order))
} catch (err) {
this.logger.error({ orderId: order.id, strategy: strategy.name, err }, "Error checking canFill")
canFillCache.set(strategy, false)
}
}
let inputUsdValue = baseInputUsd
for (const [strategy, canFill] of canFillCache) {
if (!canFill || typeof strategy.getOrderUsdValue !== "function") continue
try {
const stratValue = await strategy.getOrderUsdValue(order)
if (stratValue != null) {
inputUsdValue = Decimal.max(baseInputUsd, stratValue.inputUsd)
break
}
} catch (err) {
this.logger.error(
{ orderId: order.id, strategy: strategy.name, err },
"Error getting strategy-specific inputUsdValue",
)
}
}
const isCrossChain = order.source !== order.destination
let requiredConfirmations = 0
if (isCrossChain) {
for (const [strategy, canFill] of canFillCache) {
if (!canFill || !strategy.confirmationPolicy) continue
requiredConfirmations = Math.max(
requiredConfirmations,
strategy.confirmationPolicy.getConfirmationBlocks(
getChainId(order.source)!,
inputUsdValue.toNumber(),
),
)
}
}
// Run confirmation waiting and evaluation in parallel.
// The AbortController lets evaluateOrder cancel the confirmation
// loop early when the order turns out to be unprofitable.
const abortController = new AbortController()
const waitForConfirmations = async (): Promise<void> => {
let currentConfirmations = await retryPromise(
() =>
sourceClient.getTransactionConfirmations({
hash: order.transactionHash!,
}),
{
maxRetries: 3,
backoffMs: 250,
logMessage: "Failed to get initial transaction confirmations",
},
)
this.logger.info(
{ orderId: order.id, requiredConfirmations, currentConfirmations },
"Order confirmation requirements",
)
while (currentConfirmations < requiredConfirmations) {
if (abortController.signal.aborted) return
await new Promise((resolve) => setTimeout(resolve, 300)) // Wait 300ms
if (abortController.signal.aborted) return
currentConfirmations = await retryPromise(
() =>
sourceClient.getTransactionConfirmations({
hash: order.transactionHash!,
}),
{
maxRetries: 3,
backoffMs: 250,
logMessage: "Failed to get transaction confirmations",
},
)
this.logger.debug({ orderId: order.id, currentConfirmations }, "Order confirmation progress")
}
this.logger.info({ orderId: order.id, currentConfirmations }, "Order confirmed on source chain")
}
// Run confirmation and evaluation in parallel
const [, evaluationResult] = await Promise.all([
waitForConfirmations(),
this.evaluateOrder(order, canFillCache).then((result) => {
if (!result) abortController.abort()
return result
}),
])
// Execute immediately
if (evaluationResult) {
this.executeOrder(order, evaluationResult.strategy, solverSelectionActive)
}
} catch (error) {
this.logger.error({ orderId: order.id, err: error }, "Error processing order")
}
})
}
private async evaluateOrder(
order: OrderV2,
canFillCache: Map<FillerStrategy, boolean>,
): Promise<{ strategy: FillerStrategy; profitability: number } | null> {
// Check if watch-only mode is enabled for the destination chain
const destChainId = getChainId(order.destination)
const isWatchOnly =
destChainId !== undefined &&
this.config.watchOnly !== undefined &&
typeof this.config.watchOnly === "object" &&
this.config.watchOnly[destChainId] === true
if (isWatchOnly) {
this.logger.info(
{
orderId: order.id,
sourceChain: order.source,
destChain: order.destination,
destChainId,
user: order.user,
inputs: order.inputs,
outputs: order.output.assets,
watchOnly: true,
},
"Order detected in watch-only mode (execution skipped)",
)
this.monitor.emit("orderDetected", { orderId: order.id, order, watchOnly: true })
return null
}
const eligibleStrategies = await Promise.all(
this.strategies.map(async (strategy) => {
if (!canFillCache.get(strategy)) return null
const profitability = await strategy.calculateProfitability(order)
return { strategy, profitability }
}),
)
const validStrategies = eligibleStrategies
.filter((s): s is NonNullable<typeof s> => s !== null && s.profitability > 0)
.sort((a, b) => b.profitability - a.profitability)
if (validStrategies.length === 0) {
this.logger.warn({ orderId: order.id }, "No profitable strategy found for order")
return null
}
this.logger.info(
{
orderId: order.id,
strategy: validStrategies[0].strategy.name,
profitability: validStrategies[0].profitability.toString(),
},
"Order evaluation complete - profitable strategy found",
)
return validStrategies[0]
}
private executeOrder(order: OrderV2, bestStrategy: FillerStrategy, solverSelectionActive: boolean): void {
// Get the chain-specific queue
const chainQueue = this.chainQueues.get(getChainId(order.destination)!)
if (!chainQueue) {
this.logger.error({ chain: order.destination }, "No queue configured for chain")
return
}
// Execute with the most profitable strategy using the chain-specific queue
// This ensures transactions for the same chain are processed sequentially
chainQueue.add(async () => {
this.logger.info(
{ orderId: order.id, strategy: bestStrategy.name, chain: order.destination },
"Executing order",
)
try {
if (solverSelectionActive) {
this.contractService.ensureEntryPointDeposit(order).catch((err) => {
this.logger.error({ orderId: order.id, err }, "Background EntryPoint deposit top-up failed")
})
}
const hyperbridgeService = solverSelectionActive ? await this.hyperbridge : undefined
const result = await bestStrategy.executeOrder(order, hyperbridgeService)
this.logger.info({ orderId: order.id, result }, "Order execution completed")
if (result.success) {
this.monitor.emit("orderFilled", { orderId: order.id, hash: result.txHash })
}
if (result.commitment) {
const commitment = result.commitment as HexString
this.bidStorage?.storeBid({
commitment,
extrinsicHash: (result.txHash as HexString) || undefined,
success: result.success,
error: result.error,
})
if (this.pendingRetractions.delete(commitment)) {
this.logger.info({ commitment }, "OrderFilled arrived before bid was stored, retracting now")
this.enqueueRetraction(commitment)
}
}
return result
} catch (error) {
this.logger.error({ orderId: order.id, err: error }, "Order execution failed")
throw error
}
})
}
private handleOrderFilledOnChain(commitment: HexString, filler: string, chainId: number): void {
if (!this.bidStorage || !this.hyperbridge) {
return
}
const bid = this.bidStorage.getBidByCommitment(commitment)
if (!bid) {
this.pendingRetractions.add(commitment)
this.logger.debug(
{ commitment, filler, chainId },
"OrderFilled received before bid stored, deferring retraction",
)
return
}
if (bid.retracted) {
this.logger.debug({ commitment }, "Bid already retracted, skipping")
return
}
this.enqueueRetraction(commitment)
}
private enqueueRetraction(commitment: HexString): void {
this.retractionQueue.add(async () => {
try {
this.logger.info({ commitment }, "Retracting bid after on-chain OrderFilled")
const coprocessor = await this.hyperbridge!
const result = await coprocessor.retractBid(commitment)
if (result.success) {
this.bidStorage!.markBidAsRetracted(commitment, result.extrinsicHash as HexString)
this.logger.info({ commitment, retractHash: result.extrinsicHash }, "Bid retracted successfully")
} else {
this.logger.error({ commitment, error: result.error }, "Failed to retract bid")
}
} catch (error) {
this.logger.error({ commitment, err: error }, "Error retracting bid")
}
})
}
}