@@ -282,75 +282,97 @@ export class ContractInteractionService {
282282 }
283283
284284 /**
285- * Ensures the solver's EntryPoint deposit has enough native token to cover
286- * the estimated gas cost for a given order.
285+ * Tops up the solver's EntryPoint deposit so it covers at least
286+ * `targetGasUnits` at the current gas price. Skips if the wallet
287+ * balance cannot afford at least 1M gas units (not enough to send txs).
287288 *
288- * Uses cached gas estimates (from estimateGasFillPost) and, if the current
289- * deposit is insufficient, tops up by depositing 10% of the solver's EOA
290- * native balance on the destination chain.
289+ * @param chain - The chain identifier
290+ * @param targetGasUnits - Gas units the deposit should cover (default 3M)
291+ * @param thresholdGasUnits - Only top up if deposit is below this many gas units (defaults to targetGasUnits)
291292 */
292- async ensureEntryPointDeposit ( order : Order ) : Promise < void > {
293- if ( ! order . id ) {
294- this . logger . warn ( { destination : order . destination } , "Order has no ID, skipping EntryPoint deposit check" )
293+ async topUpEntryPointDeposit ( chain : string , targetGasUnits : bigint = 3_000_000n , thresholdGasUnits ?: bigint ) : Promise < void > {
294+ const effectiveThreshold = thresholdGasUnits ?? targetGasUnits
295+ const entryPointAddress = this . configService . getEntryPointAddress ( chain )
296+ if ( ! entryPointAddress ) {
295297 return
296298 }
297299
298- const gasEstimate = this . cacheService . getGasEstimate ( order . id )
299- if ( ! gasEstimate ) {
300- this . logger . warn (
301- { orderId : order . id , destination : order . destination } ,
302- "No cached gas estimate found, skipping EntryPoint deposit check" ,
303- )
300+ const publicClient = this . clientManager . getPublicClient ( chain )
301+ const [ currentDeposit , solverBalance , gasPrice ] = await Promise . all ( [
302+ this . getSolverEntryPointBalance ( chain ) ,
303+ publicClient . getBalance ( { address : this . solverAccountAddress } ) ,
304+ publicClient . getGasPrice ( ) ,
305+ ] )
306+
307+ if ( gasPrice === 0n ) {
308+ this . logger . warn ( { chain } , "Gas price is zero, skipping EntryPoint top-up" )
304309 return
305310 }
306311
307- const requiredNative = 3n * gasEstimate . totalGasCostWei
312+ // Skip if wallet can't afford at least 1M gas units
313+ const walletGasUnits = solverBalance / gasPrice
314+ const minWalletGasUnits = 1_000_000n
308315
309- const currentDeposit = await this . getSolverEntryPointBalance ( order . destination )
316+ if ( walletGasUnits < minWalletGasUnits ) {
317+ this . logger . warn (
318+ {
319+ chain,
320+ walletBalance : formatEther ( solverBalance ) ,
321+ walletGasUnits : walletGasUnits . toString ( ) ,
322+ gasPrice : gasPrice . toString ( ) ,
323+ } ,
324+ "Wallet balance too low to afford minimum gas, skipping EntryPoint top-up" ,
325+ )
326+ return
327+ }
310328
311- this . logger . debug (
312- {
313- orderId : order . id ,
314- destination : order . destination ,
315- currentDeposit : formatEther ( currentDeposit ) ,
316- requiredNative : formatEther ( requiredNative ) ,
317- } ,
318- "EntryPoint deposit gas coverage check" ,
319- )
329+ const targetDeposit = targetGasUnits * gasPrice
330+ const thresholdDeposit = effectiveThreshold * gasPrice
331+ const depositGasUnits = currentDeposit / gasPrice
320332
321- if ( currentDeposit >= requiredNative ) {
333+ if ( currentDeposit >= thresholdDeposit ) {
334+ this . logger . info (
335+ {
336+ chain,
337+ currentDeposit : formatEther ( currentDeposit ) ,
338+ depositGasUnits : depositGasUnits . toString ( ) ,
339+ targetGasUnits : targetGasUnits . toString ( ) ,
340+ walletBalance : formatEther ( solverBalance ) ,
341+ } ,
342+ "EntryPoint deposit covers target gas units, no top-up needed" ,
343+ )
322344 return
323345 }
324346
325- const publicClient = this . clientManager . getPublicClient ( order . destination )
326- const solverBalance = await publicClient . getBalance ( { address : this . solverAccountAddress } )
327- const depositAmount = solverBalance / 10n
347+ const deficit = targetDeposit - currentDeposit
328348
329- if ( depositAmount === 0n ) {
349+ if ( solverBalance < deficit ) {
330350 this . logger . warn (
331351 {
332- orderId : order . id ,
333- destination : order . destination ,
352+ chain ,
353+ deficit : formatEther ( deficit ) ,
334354 solverBalance : formatEther ( solverBalance ) ,
355+ depositGasUnits : depositGasUnits . toString ( ) ,
356+ targetGasUnits : targetGasUnits . toString ( ) ,
335357 } ,
336- "Solver EOA balance too low to top up EntryPoint deposit" ,
358+ "Solver EOA balance insufficient to reach target deposit, depositing available balance " ,
337359 )
360+ await this . depositToEntryPoint ( chain , solverBalance )
338361 return
339362 }
340363
341364 this . logger . info (
342365 {
343- orderId : order . id ,
344- destination : order . destination ,
345- requiredNative : formatEther ( requiredNative ) ,
366+ chain,
346367 currentDeposit : formatEther ( currentDeposit ) ,
347- solverBalance : formatEther ( solverBalance ) ,
348- depositAmount : formatEther ( depositAmount ) ,
368+ depositGasUnits : depositGasUnits . toString ( ) ,
369+ targetGasUnits : targetGasUnits . toString ( ) ,
370+ topUpAmount : formatEther ( deficit ) ,
349371 } ,
350- "Top up EntryPoint deposit by 10% of solver EOA balance " ,
372+ "Topping up EntryPoint deposit to cover target gas units " ,
351373 )
352374
353- await this . depositToEntryPoint ( order . destination , depositAmount )
375+ await this . depositToEntryPoint ( chain , deficit )
354376 }
355377
356378 /**
0 commit comments