Skip to content

Commit 0b18bd5

Browse files
committed
Allow disabling printing in wait_for_tx
commit-id:94945a36
1 parent 40ad69c commit 0b18bd5

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

crates/sncast/src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub struct AccountData {
132132
pub struct WaitForTx {
133133
pub wait: bool,
134134
pub wait_params: ValidatedWaitParams,
135+
pub show_ui_outputs: bool,
135136
}
136137

137138
#[derive(Deserialize, Serialize, Clone, Debug, Copy, PartialEq)]
@@ -576,9 +577,9 @@ pub async fn wait_for_tx(
576577
provider: &JsonRpcClient<HttpTransport>,
577578
tx_hash: Felt,
578579
wait_params: ValidatedWaitParams,
579-
ui: &UI,
580+
ui: Option<&UI>,
580581
) -> Result<String, WaitForTransactionError> {
581-
ui.println(&format!("Transaction hash: {tx_hash:#x}"));
582+
ui.inspect(|ui| ui.println(&format!("Transaction hash: {tx_hash:#x}")));
582583

583584
let retries = wait_params.get_retries();
584585
for i in (1..retries).rev() {
@@ -610,24 +611,32 @@ pub async fn wait_for_tx(
610611
Ok(starknet::core::types::TransactionStatus::PreConfirmed(
611612
ExecutionResult::Succeeded,
612613
)) => {
613-
let remaining_time = wait_params.remaining_time(i);
614-
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
615-
ui.println(&format!(
616-
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
617-
));
614+
ui.inspect(|ui| {
615+
let remaining_time = wait_params.remaining_time(i);
616+
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
617+
ui.println(&format!(
618+
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
619+
));
620+
});
618621
}
619622
Ok(
620623
starknet::core::types::TransactionStatus::Received
621624
| starknet::core::types::TransactionStatus::Candidate,
622625
)
623626
| Err(StarknetError(TransactionHashNotFound)) => {
624-
let remaining_time = wait_params.remaining_time(i);
625-
ui.println(&format!(
626-
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
627-
));
627+
ui.inspect(|ui| {
628+
let remaining_time = wait_params.remaining_time(i);
629+
ui.println(&format!(
630+
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
631+
));
632+
});
628633
}
629634
Err(ProviderError::RateLimited) => {
630-
ui.println(&"Request rate limited while waiting for transaction to be accepted");
635+
ui.inspect(|ui| {
636+
ui.println(
637+
&"Request rate limited while waiting for transaction to be accepted",
638+
);
639+
});
631640
sleep(Duration::from_secs(wait_params.get_retry_interval().into()));
632641
}
633642
Err(err) => return Err(WaitForTransactionError::ProviderError(err.into())),
@@ -664,7 +673,18 @@ pub async fn handle_wait_for_tx<T>(
664673
ui: &UI,
665674
) -> Result<T, WaitForTransactionError> {
666675
if wait_config.wait {
667-
return match wait_for_tx(provider, transaction_hash, wait_config.wait_params, ui).await {
676+
return match wait_for_tx(
677+
provider,
678+
transaction_hash,
679+
wait_config.wait_params,
680+
if wait_config.show_ui_outputs {
681+
Some(ui)
682+
} else {
683+
None
684+
},
685+
)
686+
.await
687+
{
668688
Ok(_) => Ok(return_value),
669689
Err(error) => Err(error),
670690
};

crates/sncast/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<()>
260260
let wait_config = WaitForTx {
261261
wait: cli.wait,
262262
wait_params: config.wait_params,
263+
show_ui_outputs: true,
263264
};
264265

265266
match cli.command {

crates/sncast/src/starknet_commands/script/run.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
143143
WaitForTx {
144144
wait: true,
145145
wait_params: self.config.wait_params,
146+
show_ui_outputs: true,
146147
},
147148
true,
148149
self.ui,
@@ -183,6 +184,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
183184
WaitForTx {
184185
wait: true,
185186
wait_params: self.config.wait_params,
187+
show_ui_outputs: true,
186188
},
187189
self.ui,
188190
));
@@ -221,6 +223,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
221223
WaitForTx {
222224
wait: true,
223225
wait_params: self.config.wait_params,
226+
show_ui_outputs: true,
224227
},
225228
self.ui,
226229
));

crates/sncast/tests/integration/wait_for_tx.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn test_happy_path() {
2424
&provider,
2525
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
2626
ValidatedWaitParams::default(),
27-
&ui,
27+
Some(&ui),
2828
)
2929
.await;
3030

@@ -103,7 +103,7 @@ async fn test_wait_for_reverted_transaction() {
103103
&provider,
104104
transaction_hash,
105105
ValidatedWaitParams::new(1, 3),
106-
&ui,
106+
Some(&ui),
107107
)
108108
.await
109109
.map_err(anyhow::Error::from)
@@ -119,7 +119,7 @@ async fn test_wait_for_nonexistent_tx() {
119119
&provider,
120120
"0x123456789".parse().expect("Could not parse a number"),
121121
ValidatedWaitParams::new(1, 3),
122-
&ui,
122+
Some(&ui),
123123
)
124124
.await
125125
.map_err(anyhow::Error::from)
@@ -137,6 +137,7 @@ async fn test_happy_path_handle_wait_for_tx() {
137137
WaitForTx {
138138
wait: true,
139139
wait_params: ValidatedWaitParams::new(5, 63),
140+
show_ui_outputs: true,
140141
},
141142
&ui,
142143
)
@@ -154,7 +155,7 @@ async fn test_wait_for_wrong_retry_values() {
154155
&provider,
155156
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
156157
ValidatedWaitParams::new(2, 1),
157-
&ui,
158+
Some(&ui),
158159
)
159160
.await
160161
.unwrap();
@@ -169,7 +170,7 @@ async fn test_wait_for_wrong_retry_values_timeout_zero() {
169170
&provider,
170171
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
171172
ValidatedWaitParams::new(2, 0),
172-
&ui,
173+
Some(&ui),
173174
)
174175
.await
175176
.unwrap();
@@ -184,7 +185,7 @@ async fn test_wait_for_wrong_retry_values_interval_zero() {
184185
&provider,
185186
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
186187
ValidatedWaitParams::new(0, 1),
187-
&ui,
188+
Some(&ui),
188189
)
189190
.await
190191
.unwrap();

0 commit comments

Comments
 (0)