@@ -265,29 +265,14 @@ export class EthereumAccountBase {
265265
266266 const erc20ApprovalTx = erc20ApprovalTxs [ 0 ] ;
267267
268- try {
269- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
270- const keplr = ( await this . getKeplr ( ) ) ! ;
271-
272- const erc20ApprovalGasEstimated = await keplr . ethereum . request <
273- string | undefined
274- > ( {
275- method : "eth_estimateGas" ,
276- params : [
277- {
278- from : sender ,
279- to : erc20ApprovalTx . to ,
280- value : erc20ApprovalTx . value ,
281- data : erc20ApprovalTx . data ,
282- } ,
283- ] ,
284- chainId : this . chainId ,
285- } ) ;
286-
287- if ( ! erc20ApprovalGasEstimated ) {
288- throw new Error ( "Failed to estimate gas for ERC20 approval" ) ;
289- }
268+ // First, estimate gas for the ERC20 approval tx itself.
269+ // This result is reused whether the bundle simulation with state diff succeeds or falls back.
270+ const erc20ApprovalSimResult = await this . simulateGas (
271+ sender ,
272+ erc20ApprovalTx
273+ ) ;
290274
275+ try {
291276 // State diff tracing for the ERC20 approval transaction
292277 const approvalStateDiff = await traceCallWithDiff ( chainInfo . rpc , {
293278 from : sender ,
@@ -321,16 +306,14 @@ export class EthereumAccountBase {
321306 return {
322307 kind : EVMGasSimulateKind . TX_BUNDLE_SIMULATED ,
323308 gasUsed : result . gasUsed ,
324- erc20ApprovalGasUsed : parseInt ( erc20ApprovalGasEstimated ) ,
309+ erc20ApprovalGasUsed : erc20ApprovalSimResult . gasUsed ,
325310 } ;
326311 } catch ( e ) {
327312 console . error ( "Failed to simulate gas with pending ERC20 approval" , e ) ;
328313
329- // fallback to simulate only the erc20 approval tx
330- const result = await this . simulateGas ( sender , erc20ApprovalTx ) ;
331314 return {
332315 kind : EVMGasSimulateKind . APPROVAL_ONLY_SIMULATED ,
333- erc20ApprovalGasUsed : result . gasUsed ,
316+ erc20ApprovalGasUsed : erc20ApprovalSimResult . gasUsed ,
334317 } ;
335318 }
336319 }
@@ -467,6 +450,8 @@ export class EthereumAccountBase {
467450 unsignedTx : UnsignedEVMTransactionWithErc20Approvals ,
468451 options ?: {
469452 nonceMethod ?: "pending" | "latest" ;
453+ // Whether to offset nonce by the number of required ERC20 approvals
454+ considerRequiredErc20ApprovalsForNonce ?: boolean ;
470455 }
471456 ) {
472457 const chainInfo = this . chainGetter . getChain ( this . chainId ) ;
@@ -488,7 +473,9 @@ export class EthereumAccountBase {
488473 ...unsignedTx ,
489474 nonce :
490475 parseInt ( transactionCount ) +
491- ( unsignedTx . requiredErc20Approvals ?. length ?? 0 ) ,
476+ ( options ?. considerRequiredErc20ApprovalsForNonce
477+ ? unsignedTx . requiredErc20Approvals ?. length ?? 0
478+ : 0 ) ,
492479 } ;
493480
494481 const signEthereum = keplr . signEthereum . bind ( keplr ) ;
0 commit comments