|
| 1 | +import { ethers } from 'ethers'; |
| 2 | +import { BaseExecutor } from './strategies/BaseExecutor'; |
| 3 | +import { |
| 4 | + ExecutorConfig, |
| 5 | + QueuedTransaction, |
| 6 | + QueueStats, |
| 7 | + TransactionReceipt, |
| 8 | +} from './interfaces/types'; |
| 9 | +import { DEFAULT_EXECUTOR_CONFIG } from './constants'; |
| 10 | +import { ProfitabilityCheck } from '@/profitability/interfaces/types'; |
| 11 | + |
| 12 | +export class ExecutorWrapper { |
| 13 | + private executor: BaseExecutor; |
| 14 | + |
| 15 | + constructor( |
| 16 | + stakerContract: ethers.Contract, |
| 17 | + provider: ethers.Provider, |
| 18 | + config: Partial<ExecutorConfig> = {}, |
| 19 | + ) { |
| 20 | + // Merge provided config with defaults |
| 21 | + const fullConfig: ExecutorConfig = { |
| 22 | + ...DEFAULT_EXECUTOR_CONFIG, |
| 23 | + ...config, |
| 24 | + wallet: { |
| 25 | + ...DEFAULT_EXECUTOR_CONFIG.wallet, |
| 26 | + ...config.wallet, |
| 27 | + }, |
| 28 | + }; |
| 29 | + |
| 30 | + this.executor = new BaseExecutor(stakerContract, provider, fullConfig); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Start the executor |
| 35 | + */ |
| 36 | + async start(): Promise<void> { |
| 37 | + await this.executor.start(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Stop the executor |
| 42 | + */ |
| 43 | + async stop(): Promise<void> { |
| 44 | + await this.executor.stop(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the current status of the executor |
| 49 | + */ |
| 50 | + async getStatus(): Promise<{ |
| 51 | + isRunning: boolean; |
| 52 | + walletBalance: bigint; |
| 53 | + pendingTransactions: number; |
| 54 | + queueSize: number; |
| 55 | + }> { |
| 56 | + return this.executor.getStatus(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Queue a transaction for execution |
| 61 | + */ |
| 62 | + async queueTransaction( |
| 63 | + depositId: bigint, |
| 64 | + profitability: ProfitabilityCheck, |
| 65 | + ): Promise<QueuedTransaction> { |
| 66 | + return this.executor.queueTransaction(depositId, profitability); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get statistics about the transaction queue |
| 71 | + */ |
| 72 | + async getQueueStats(): Promise<QueueStats> { |
| 73 | + return this.executor.getQueueStats(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get a specific transaction by ID |
| 78 | + */ |
| 79 | + async getTransaction(id: string): Promise<QueuedTransaction | null> { |
| 80 | + return this.executor.getTransaction(id); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get transaction receipt |
| 85 | + */ |
| 86 | + async getTransactionReceipt( |
| 87 | + hash: string, |
| 88 | + ): Promise<TransactionReceipt | null> { |
| 89 | + return this.executor.getTransactionReceipt(hash); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Transfer accumulated tips to the configured receiver |
| 94 | + */ |
| 95 | + async transferOutTips(): Promise<TransactionReceipt | null> { |
| 96 | + return this.executor.transferOutTips(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Clear the transaction queue |
| 101 | + */ |
| 102 | + async clearQueue(): Promise<void> { |
| 103 | + await this.executor.clearQueue(); |
| 104 | + } |
| 105 | +} |
0 commit comments