Skip to content

Commit 5ea3fa5

Browse files
committed
Allow disabling printing in wait_for_tx
commit-id:94945a36
1 parent 713e286 commit 5ea3fa5

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
@@ -136,6 +136,7 @@ pub struct AccountData {
136136
pub struct WaitForTx {
137137
pub wait: bool,
138138
pub wait_params: ValidatedWaitParams,
139+
pub show_ui_outputs: bool,
139140
}
140141

141142
#[derive(Deserialize, Serialize, Clone, Debug, Copy, PartialEq)]
@@ -625,9 +626,9 @@ pub async fn wait_for_tx(
625626
provider: &JsonRpcClient<HttpTransport>,
626627
tx_hash: Felt,
627628
wait_params: ValidatedWaitParams,
628-
ui: &UI,
629+
ui: Option<&UI>,
629630
) -> Result<String, WaitForTransactionError> {
630-
ui.println(&format!("Transaction hash: {tx_hash:#x}"));
631+
ui.inspect(|ui| ui.println(&format!("Transaction hash: {tx_hash:#x}")));
631632

632633
let retries = wait_params.get_retries();
633634
for i in (1..retries).rev() {
@@ -659,24 +660,32 @@ pub async fn wait_for_tx(
659660
Ok(starknet::core::types::TransactionStatus::PreConfirmed(
660661
ExecutionResult::Succeeded,
661662
)) => {
662-
let remaining_time = wait_params.remaining_time(i);
663-
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
664-
ui.println(&format!(
665-
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
666-
));
663+
ui.inspect(|ui| {
664+
let remaining_time = wait_params.remaining_time(i);
665+
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
666+
ui.println(&format!(
667+
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
668+
));
669+
});
667670
}
668671
Ok(
669672
starknet::core::types::TransactionStatus::Received
670673
| starknet::core::types::TransactionStatus::Candidate,
671674
)
672675
| Err(StarknetError(TransactionHashNotFound)) => {
673-
let remaining_time = wait_params.remaining_time(i);
674-
ui.println(&format!(
675-
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
676-
));
676+
ui.inspect(|ui| {
677+
let remaining_time = wait_params.remaining_time(i);
678+
ui.println(&format!(
679+
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
680+
));
681+
});
677682
}
678683
Err(ProviderError::RateLimited) => {
679-
ui.println(&"Request rate limited while waiting for transaction to be accepted");
684+
ui.inspect(|ui| {
685+
ui.println(
686+
&"Request rate limited while waiting for transaction to be accepted",
687+
);
688+
});
680689
sleep(Duration::from_secs(wait_params.get_retry_interval().into()));
681690
}
682691
Err(err) => return Err(WaitForTransactionError::ProviderError(err.into())),
@@ -713,7 +722,18 @@ pub async fn handle_wait_for_tx<T>(
713722
ui: &UI,
714723
) -> Result<T, WaitForTransactionError> {
715724
if wait_config.wait {
716-
return match wait_for_tx(provider, transaction_hash, wait_config.wait_params, ui).await {
725+
return match wait_for_tx(
726+
provider,
727+
transaction_hash,
728+
wait_config.wait_params,
729+
if wait_config.show_ui_outputs {
730+
Some(ui)
731+
} else {
732+
None
733+
},
734+
)
735+
.await
736+
{
717737
Ok(_) => Ok(return_value),
718738
Err(error) => Err(error),
719739
};

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
@@ -26,7 +26,7 @@ async fn test_happy_path() {
2626
&provider,
2727
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
2828
ValidatedWaitParams::default(),
29-
&ui,
29+
Some(&ui),
3030
)
3131
.await;
3232

@@ -109,7 +109,7 @@ async fn test_wait_for_reverted_transaction() {
109109
&provider,
110110
transaction_hash,
111111
ValidatedWaitParams::new(1, 3),
112-
&ui,
112+
Some(&ui),
113113
)
114114
.await
115115
.map_err(anyhow::Error::from)
@@ -125,7 +125,7 @@ async fn test_wait_for_nonexistent_tx() {
125125
&provider,
126126
"0x123456789".parse().expect("Could not parse a number"),
127127
ValidatedWaitParams::new(1, 3),
128-
&ui,
128+
Some(&ui),
129129
)
130130
.await
131131
.map_err(anyhow::Error::from)
@@ -143,6 +143,7 @@ async fn test_happy_path_handle_wait_for_tx() {
143143
WaitForTx {
144144
wait: true,
145145
wait_params: ValidatedWaitParams::new(5, 63),
146+
show_ui_outputs: true,
146147
},
147148
&ui,
148149
)
@@ -160,7 +161,7 @@ async fn test_wait_for_wrong_retry_values() {
160161
&provider,
161162
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
162163
ValidatedWaitParams::new(2, 1),
163-
&ui,
164+
Some(&ui),
164165
)
165166
.await
166167
.unwrap();
@@ -175,7 +176,7 @@ async fn test_wait_for_wrong_retry_values_timeout_zero() {
175176
&provider,
176177
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
177178
ValidatedWaitParams::new(2, 0),
178-
&ui,
179+
Some(&ui),
179180
)
180181
.await
181182
.unwrap();
@@ -190,7 +191,7 @@ async fn test_wait_for_wrong_retry_values_interval_zero() {
190191
&provider,
191192
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
192193
ValidatedWaitParams::new(0, 1),
193-
&ui,
194+
Some(&ui),
194195
)
195196
.await
196197
.unwrap();

0 commit comments

Comments
 (0)