@@ -6,8 +6,10 @@ import {
6
6
getContract ,
7
7
readContract ,
8
8
toSerializableTransaction ,
9
+ toTokens ,
9
10
type Hex ,
10
11
} from "thirdweb" ;
12
+ import { getChainMetadata } from "thirdweb/chains" ;
11
13
import { stringify } from "thirdweb/utils" ;
12
14
import type { Account } from "thirdweb/wallets" ;
13
15
import {
@@ -32,10 +34,10 @@ import { getChain } from "../../utils/chain";
32
34
import { msSince } from "../../utils/date" ;
33
35
import { env } from "../../utils/env" ;
34
36
import {
37
+ isInsufficientFundsError ,
35
38
isNonceAlreadyUsedError ,
36
39
isReplacementGasFeeTooLow ,
37
- prettifyError ,
38
- prettifyTransactionError ,
40
+ wrapError ,
39
41
} from "../../utils/error" ;
40
42
import { getChecksumAddress } from "../../utils/primitiveTypes" ;
41
43
import { recordMetrics } from "../../utils/prometheus" ;
@@ -243,16 +245,12 @@ const _sendUserOp = async (
243
245
// we don't want this behavior in the engine context
244
246
waitForDeployment : false ,
245
247
} ) ) as UserOperation ; // TODO support entrypoint v0.7 accounts
246
- } catch ( e ) {
247
- const erroredTransaction : ErroredTransaction = {
248
+ } catch ( error ) {
249
+ return {
248
250
...queuedTransaction ,
249
251
status : "errored" ,
250
- errorMessage : prettifyError ( e ) ,
251
- } ;
252
- job . log (
253
- `Failed to populate transaction: ${ erroredTransaction . errorMessage } ` ,
254
- ) ;
255
- return erroredTransaction ;
252
+ errorMessage : wrapError ( error , "Bundler" ) . message ,
253
+ } satisfies ErroredTransaction ;
256
254
}
257
255
258
256
job . log ( `Populated userOp: ${ stringify ( signedUserOp ) } ` ) ;
@@ -325,15 +323,11 @@ const _sendTransaction = async (
325
323
} ,
326
324
} ) ;
327
325
} catch ( e : unknown ) {
328
- const erroredTransaction : ErroredTransaction = {
326
+ return {
329
327
...queuedTransaction ,
330
328
status : "errored" ,
331
- errorMessage : prettifyError ( e ) ,
332
- } ;
333
- job . log (
334
- `Failed to populate transaction: ${ erroredTransaction . errorMessage } ` ,
335
- ) ;
336
- return erroredTransaction ;
329
+ errorMessage : wrapError ( e , "RPC" ) . message ,
330
+ } satisfies ErroredTransaction ;
337
331
}
338
332
339
333
// Handle if `maxFeePerGas` is overridden.
@@ -380,7 +374,28 @@ const _sendTransaction = async (
380
374
job . log ( `Recycling nonce: ${ nonce } ` ) ;
381
375
await recycleNonce ( chainId , from , nonce ) ;
382
376
}
383
- throw error ;
377
+
378
+ // Do not retry errors that are expected to be rejected by RPC again.
379
+ if ( isInsufficientFundsError ( error ) ) {
380
+ const { name, nativeCurrency } = await getChainMetadata ( chain ) ;
381
+ const { gas, value = 0n } = populatedTransaction ;
382
+ const gasPrice =
383
+ populatedTransaction . gasPrice ?? populatedTransaction . maxFeePerGas ;
384
+
385
+ const minGasTokens = gasPrice
386
+ ? toTokens ( gas * gasPrice + value , 18 )
387
+ : null ;
388
+ const errorMessage = minGasTokens
389
+ ? `Insufficient funds in ${ account . address } on ${ name } . Transaction requires > ${ minGasTokens } ${ nativeCurrency . symbol } .`
390
+ : `Insufficient funds in ${ account . address } on ${ name } . Transaction requires more ${ nativeCurrency . symbol } .` ;
391
+ return {
392
+ ...queuedTransaction ,
393
+ status : "errored" ,
394
+ errorMessage,
395
+ } satisfies ErroredTransaction ;
396
+ }
397
+
398
+ throw wrapError ( error , "RPC" ) ;
384
399
}
385
400
386
401
await addSentNonce ( chainId , from , nonce ) ;
@@ -466,7 +481,7 @@ const _resendTransaction = async (
466
481
job . log ( "A pending transaction exists with >= gas fees. Do not resend." ) ;
467
482
return null ;
468
483
}
469
- throw error ;
484
+ throw wrapError ( error , "RPC" ) ;
470
485
}
471
486
472
487
return {
@@ -572,7 +587,7 @@ export const initSendTransactionWorker = () => {
572
587
const erroredTransaction : ErroredTransaction = {
573
588
...transaction ,
574
589
status : "errored" ,
575
- errorMessage : await prettifyTransactionError ( transaction , error ) ,
590
+ errorMessage : error . message ,
576
591
} ;
577
592
job . log ( `Transaction errored: ${ stringify ( erroredTransaction ) } ` ) ;
578
593
0 commit comments