@@ -12,6 +12,7 @@ use soroban_spec::read::FromWasmError;
1212
1313use super :: super :: events;
1414use super :: arg_parsing;
15+ use crate :: assembled:: Assembled ;
1516use crate :: {
1617 assembled:: simulate_and_assemble_transaction,
1718 commands:: {
@@ -179,25 +180,23 @@ impl Cmd {
179180 }
180181
181182 // uses a default account to check if the tx should be sent after the simulation
182- async fn should_send_after_sim (
183+ async fn simulate (
183184 & self ,
184- host_function_params : InvokeContractArgs ,
185- rpc_client : Client ,
186- ) -> Result < ShouldSend , Error > {
187- let account_details = default_account_entry ( ) ;
188- let sequence: i64 = account_details. seq_num . into ( ) ;
189- let AccountId ( PublicKey :: PublicKeyTypeEd25519 ( account_id) ) = account_details. account_id ;
185+ host_function_params : & InvokeContractArgs ,
186+ account_details : & AccountEntry ,
187+ rpc_client : & Client ,
188+ ) -> Result < Assembled , Error > {
189+ let sequence: i64 = account_details. seq_num . 0 ;
190+ let AccountId ( PublicKey :: PublicKeyTypeEd25519 ( account_id) ) =
191+ account_details. account_id . clone ( ) ;
190192
191193 let tx = build_invoke_contract_tx (
192194 host_function_params. clone ( ) ,
193195 sequence + 1 ,
194196 self . fee . fee ,
195197 account_id,
196198 ) ?;
197- let txn = simulate_and_assemble_transaction ( & rpc_client, & tx) . await ?;
198- let txn = self . fee . apply_to_assembled_txn ( txn) ; // do we need this part?
199- let sim_res = txn. sim_response ( ) ;
200- self . should_send_tx ( sim_res)
199+ Ok ( simulate_and_assemble_transaction ( rpc_client, & tx) . await ?)
201200 }
202201}
203202
@@ -212,6 +211,7 @@ impl NetworkRunnable for Cmd {
212211 config : Option < & config:: Args > ,
213212 ) -> Result < TxnResult < String > , Error > {
214213 let config = config. unwrap_or ( & self . config ) ;
214+ let print = print:: Print :: new ( global_args. map_or ( false , |g| g. quiet ) ) ;
215215 let network = config. get_network ( ) ?;
216216 tracing:: trace!( ?network) ;
217217 let contract_id = self
@@ -238,11 +238,12 @@ impl NetworkRunnable for Cmd {
238238 let ( function, spec, host_function_params, signers) =
239239 build_host_function_parameters ( & contract_id, & self . slop , & spec_entries, config) ?;
240240
241- let should_send_tx = self
242- . should_send_after_sim ( host_function_params. clone ( ) , client. clone ( ) )
241+ let assembled = self
242+ . simulate ( & host_function_params, & default_account_entry ( ) , & client)
243243 . await ?;
244+ let should_send = self . should_send_tx ( & assembled. sim_res ) ?;
244245
245- let account_details = if should_send_tx == ShouldSend :: Yes {
246+ let account_details = if should_send == ShouldSend :: Yes {
246247 client
247248 . verify_network_passphrase ( Some ( & network. network_passphrase ) )
248249 . await ?;
@@ -251,7 +252,16 @@ impl NetworkRunnable for Cmd {
251252 . get_account ( & config. source_account ( ) ?. to_string ( ) )
252253 . await ?
253254 } else {
254- default_account_entry ( )
255+ if should_send == ShouldSend :: DefaultNo {
256+ print. infoln (
257+ "Simulation identified as read-only. Send by rerunning with `--send=yes`." ,
258+ ) ;
259+ }
260+ let sim_res = assembled. sim_response ( ) ;
261+ let ( return_value, events) = ( sim_res. results ( ) ?, sim_res. events ( ) ?) ;
262+ crate :: log:: event:: all ( & events) ;
263+ crate :: log:: event:: contract ( & events, & print) ;
264+ return Ok ( output_to_string ( & spec, & return_value[ 0 ] . xdr , & function) ?) ;
255265 } ;
256266 let sequence: i64 = account_details. seq_num . into ( ) ;
257267 let AccountId ( PublicKey :: PublicKeyTypeEd25519 ( account_id) ) = account_details. account_id ;
@@ -275,69 +285,32 @@ impl NetworkRunnable for Cmd {
275285 if global_args. map_or ( true , |a| !a. no_cache ) {
276286 data:: write ( sim_res. clone ( ) . into ( ) , & network. rpc_uri ( ) ?) ?;
277287 }
278- let should_send = self . should_send_tx ( sim_res) ?;
279- let ( return_value, events) = match should_send {
280- ShouldSend :: Yes => {
281- let global:: Args { no_cache, .. } = global_args. cloned ( ) . unwrap_or_default ( ) ;
282- // Need to sign all auth entries
283- if let Some ( tx) = config. sign_soroban_authorizations ( & txn, & signers) . await ? {
284- txn = Box :: new ( tx) ;
285- }
286- let res = client
287- . send_transaction_polling ( & config. sign_with_local_key ( * txn) . await ?)
288- . await ?;
289- if !no_cache {
290- data:: write ( res. clone ( ) . try_into ( ) ?, & network. rpc_uri ( ) ?) ?;
291- }
292- let events = res
293- . result_meta
294- . as_ref ( )
295- . map ( crate :: log:: extract_events)
296- . unwrap_or_default ( ) ;
297- ( res. return_value ( ) ?, events)
298- }
299- ShouldSend :: No => ( sim_res. results ( ) ?[ 0 ] . xdr . clone ( ) , sim_res. events ( ) ?) ,
300- ShouldSend :: DefaultNo => {
301- let print = print:: Print :: new ( global_args. map_or ( false , |g| g. quiet ) ) ;
302- print. infoln ( "Send skipped because simulation identified as read-only. Send by rerunning with `--send=yes`." ) ;
303- ( sim_res. results ( ) ?[ 0 ] . xdr . clone ( ) , sim_res. events ( ) ?)
304- }
305- } ;
306- crate :: log:: events ( & events) ;
288+ let global:: Args { no_cache, .. } = global_args. cloned ( ) . unwrap_or_default ( ) ;
289+ // Need to sign all auth entries
290+ if let Some ( tx) = config. sign_soroban_authorizations ( & txn, & signers) . await ? {
291+ txn = Box :: new ( tx) ;
292+ }
293+ let res = client
294+ . send_transaction_polling ( & config. sign_with_local_key ( * txn) . await ?)
295+ . await ?;
296+ if !no_cache {
297+ data:: write ( res. clone ( ) . try_into ( ) ?, & network. rpc_uri ( ) ?) ?;
298+ }
299+ let events = res
300+ . result_meta
301+ . as_ref ( )
302+ . map ( crate :: log:: extract_events)
303+ . unwrap_or_default ( ) ;
304+ let return_value = res. return_value ( ) ?;
305+
306+ crate :: log:: event:: all ( & events) ;
307+ crate :: log:: event:: contract ( & events, & print) ;
307308 Ok ( output_to_string ( & spec, & return_value, & function) ?)
308309 }
309310}
310311
311312const DEFAULT_ACCOUNT_ID : AccountId = AccountId ( PublicKey :: PublicKeyTypeEd25519 ( Uint256 ( [ 0 ; 32 ] ) ) ) ;
312313
313- // fn log_auth_cost_and_footprint(resources: Option<&SorobanResources>) {
314- // if let Some(resources) = resources {
315- // crate::log::footprint(&resources.footprint);
316- // crate::log::cost(resources);
317- // }
318- // }
319-
320- // fn resources(tx: &Transaction) -> Option<&SorobanResources> {
321- // let TransactionExt::V1(SorobanTransactionData { resources, .. }) = &tx.ext else {
322- // return None;
323- // };
324- // Some(resources)
325- // }
326-
327- // fn auth_entries(tx: &Transaction) -> VecM<SorobanAuthorizationEntry> {
328- // tx.operations
329- // .first()
330- // .and_then(|op| match op.body {
331- // OperationBody::InvokeHostFunction(ref body) => (matches!(
332- // body.auth.first().map(|x| &x.root_invocation.function),
333- // Some(&SorobanAuthorizedFunction::ContractFn(_))
334- // ))
335- // .then_some(body.auth.clone()),
336- // _ => None,
337- // })
338- // .unwrap_or_default()
339- // }
340-
341314fn default_account_entry ( ) -> AccountEntry {
342315 AccountEntry {
343316 account_id : DEFAULT_ACCOUNT_ID ,
0 commit comments