@@ -5,15 +5,21 @@ import { getPXEConfig, type PXEConfig } from '@aztec/pxe/config';
55import { createPXE , PXE } from '@aztec/pxe/client/lazy' ;
66import { AztecAddress } from '@aztec/stdlib/aztec-address' ;
77import { getContractInstanceFromInstantiationParams } from '@aztec/stdlib/contract' ;
8- import { mergeExecutionPayloads , type ExecutionPayload , type TxSimulationResult } from '@aztec/stdlib/tx' ;
8+ import { BlockHeader , mergeExecutionPayloads , type ExecutionPayload , type TxSimulationResult } from '@aztec/stdlib/tx' ;
99import type { DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account' ;
1010import { deriveSigningKey } from '@aztec/stdlib/keys' ;
1111import { SignerlessAccount , type Account , type AccountContract } from '@aztec/aztec.js/account' ;
12- import { AccountManager } from '@aztec/aztec.js/wallet' ;
12+ import { AccountManager , type SimulateOptions } from '@aztec/aztec.js/wallet' ;
1313import type { AztecNode } from '@aztec/aztec.js/node' ;
1414import type { SimulateInteractionOptions } from '@aztec/aztec.js/contracts' ;
1515import { Fr , GrumpkinScalar } from '@aztec/aztec.js/fields' ;
16- import { BaseWallet } from '@aztec/wallet-sdk/base-wallet' ;
16+ import {
17+ BaseWallet ,
18+ buildMergedSimulationResult ,
19+ extractOptimizablePublicStaticCalls ,
20+ simulateViaNode ,
21+ type FeeOptions ,
22+ } from '@aztec/wallet-sdk/base-wallet' ;
1723
1824/**
1925 * Data for generating an account.
@@ -120,13 +126,58 @@ export class EmbeddedWallet extends BaseWallet {
120126 } ;
121127 }
122128
123- override async simulateTx (
129+ override async simulateTx ( executionPayload : ExecutionPayload , opts : SimulateOptions ) : Promise < TxSimulationResult > {
130+ const feeOptions = opts . fee ?. estimateGas
131+ ? await this . completeFeeOptionsForEstimation ( opts . from , executionPayload . feePayer , opts . fee ?. gasSettings )
132+ : await this . completeFeeOptions ( opts . from , executionPayload . feePayer , opts . fee ?. gasSettings ) ;
133+ const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls ( executionPayload ) ;
134+ const remainingPayload = { ...executionPayload , calls : remainingCalls } ;
135+
136+ const chainInfo = await this . getChainInfo ( ) ;
137+ let blockHeader : BlockHeader ;
138+ // PXE might not be synced yet, so we pull the latest header from the node
139+ // To keep things consistent, we'll always try with PXE first
140+ try {
141+ blockHeader = await this . pxe . getSyncedBlockHeader ( ) ;
142+ } catch {
143+ blockHeader = ( await this . aztecNode . getBlockHeader ( ) ) ! ;
144+ }
145+
146+ const [ optimizedResults , normalResult ] = await Promise . all ( [
147+ optimizableCalls . length > 0
148+ ? simulateViaNode (
149+ this . aztecNode ,
150+ optimizableCalls ,
151+ opts . from ,
152+ chainInfo ,
153+ feeOptions . gasSettings ,
154+ blockHeader ,
155+ opts . skipFeeEnforcement ?? true ,
156+ )
157+ : Promise . resolve ( [ ] ) ,
158+ remainingCalls . length > 0
159+ ? this . simulateViaEntrypoint (
160+ remainingPayload ,
161+ opts . from ,
162+ feeOptions ,
163+ opts . skipTxValidation ,
164+ opts . skipFeeEnforcement ?? true ,
165+ )
166+ : Promise . resolve ( null ) ,
167+ ] ) ;
168+
169+ return buildMergedSimulationResult ( optimizedResults , normalResult ) ;
170+ }
171+
172+ protected override async simulateViaEntrypoint (
124173 executionPayload : ExecutionPayload ,
125- opts : SimulateInteractionOptions ,
174+ from : AztecAddress ,
175+ feeOptions : FeeOptions ,
176+ _skipTxValidation ?: boolean ,
177+ _skipFeeEnforcement ?: boolean ,
126178 ) : Promise < TxSimulationResult > {
127- const feeOptions = opts . fee ?. estimateGas
128- ? await this . completeFeeOptionsForEstimation ( opts . from , executionPayload . feePayer , opts . fee . gasSettings )
129- : await this . completeFeeOptions ( opts . from , executionPayload . feePayer , opts . fee . gasSettings ) ;
179+ const { account : fromAccount , instance, artifact } = await this . getFakeAccountDataFor ( from ) ;
180+
130181 const feeExecutionPayload = await feeOptions . walletFeePaymentMethod ?. getExecutionPayload ( ) ;
131182 const executionOptions : DefaultAccountEntrypointOptions = {
132183 txNonce : Fr . random ( ) ,
@@ -136,19 +187,15 @@ export class EmbeddedWallet extends BaseWallet {
136187 const finalExecutionPayload = feeExecutionPayload
137188 ? mergeExecutionPayloads ( [ feeExecutionPayload , executionPayload ] )
138189 : executionPayload ;
139- const { account : fromAccount , instance, artifact } = await this . getFakeAccountDataFor ( opts . from ) ;
140190 const chainInfo = await this . getChainInfo ( ) ;
141191 const txRequest = await fromAccount . createTxExecutionRequest (
142192 finalExecutionPayload ,
143193 feeOptions . gasSettings ,
144194 chainInfo ,
145195 executionOptions ,
146196 ) ;
147- const contractOverrides = {
148- [ opts . from . toString ( ) ] : { instance, artifact } ,
149- } ;
150- return this . pxe . simulateTx ( txRequest , true /* simulatePublic */ , true , true , {
151- contracts : contractOverrides ,
197+ return this . pxe . simulateTx ( txRequest , true , true , true , {
198+ contracts : { [ from . toString ( ) ] : { instance, artifact } } ,
152199 } ) ;
153200 }
154201}
0 commit comments