Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions crates/sncast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct AccountData {
pub struct WaitForTx {
pub wait: bool,
pub wait_params: ValidatedWaitParams,
pub show_ui_outputs: bool,
}

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

let retries = wait_params.get_retries();
for i in (1..retries).rev() {
Expand Down Expand Up @@ -659,24 +660,32 @@ pub async fn wait_for_tx(
Ok(starknet::core::types::TransactionStatus::PreConfirmed(
ExecutionResult::Succeeded,
)) => {
let remaining_time = wait_params.remaining_time(i);
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
ui.println(&format!(
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
));
ui.inspect(|ui| {
let remaining_time = wait_params.remaining_time(i);
ui.println(&"Transaction status: PRE_CONFIRMED".to_string());
ui.println(&format!(
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
));
});
}
Ok(
starknet::core::types::TransactionStatus::Received
| starknet::core::types::TransactionStatus::Candidate,
)
| Err(StarknetError(TransactionHashNotFound)) => {
let remaining_time = wait_params.remaining_time(i);
ui.println(&format!(
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
));
ui.inspect(|ui| {
let remaining_time = wait_params.remaining_time(i);
ui.println(&format!(
"Waiting for transaction to be accepted ({i} retries / {remaining_time}s left until timeout)"
));
});
}
Err(ProviderError::RateLimited) => {
ui.println(&"Request rate limited while waiting for transaction to be accepted");
ui.inspect(|ui| {
ui.println(
&"Request rate limited while waiting for transaction to be accepted",
);
});
sleep(Duration::from_secs(wait_params.get_retry_interval().into()));
}
Err(err) => return Err(WaitForTransactionError::ProviderError(err.into())),
Expand Down Expand Up @@ -713,7 +722,18 @@ pub async fn handle_wait_for_tx<T>(
ui: &UI,
) -> Result<T, WaitForTransactionError> {
if wait_config.wait {
return match wait_for_tx(provider, transaction_hash, wait_config.wait_params, ui).await {
return match wait_for_tx(
provider,
transaction_hash,
wait_config.wait_params,
if wait_config.show_ui_outputs {
Some(ui)
} else {
None
},
)
.await
{
Ok(_) => Ok(return_value),
Err(error) => Err(error),
};
Expand Down
1 change: 1 addition & 0 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<()>
let wait_config = WaitForTx {
wait: cli.wait,
wait_params: config.wait_params,
show_ui_outputs: true,
};

match cli.command {
Expand Down
3 changes: 3 additions & 0 deletions crates/sncast/src/starknet_commands/script/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
WaitForTx {
wait: true,
wait_params: self.config.wait_params,
show_ui_outputs: true,
},
true,
self.ui,
Expand Down Expand Up @@ -183,6 +184,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
WaitForTx {
wait: true,
wait_params: self.config.wait_params,
show_ui_outputs: true,
},
self.ui,
));
Expand Down Expand Up @@ -221,6 +223,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
WaitForTx {
wait: true,
wait_params: self.config.wait_params,
show_ui_outputs: true,
},
self.ui,
));
Expand Down
13 changes: 7 additions & 6 deletions crates/sncast/tests/integration/wait_for_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_happy_path() {
&provider,
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
ValidatedWaitParams::default(),
&ui,
Some(&ui),
)
.await;

Expand Down Expand Up @@ -109,7 +109,7 @@ async fn test_wait_for_reverted_transaction() {
&provider,
transaction_hash,
ValidatedWaitParams::new(1, 3),
&ui,
Some(&ui),
)
.await
.map_err(anyhow::Error::from)
Expand All @@ -125,7 +125,7 @@ async fn test_wait_for_nonexistent_tx() {
&provider,
"0x123456789".parse().expect("Could not parse a number"),
ValidatedWaitParams::new(1, 3),
&ui,
Some(&ui),
)
.await
.map_err(anyhow::Error::from)
Expand All @@ -143,6 +143,7 @@ async fn test_happy_path_handle_wait_for_tx() {
WaitForTx {
wait: true,
wait_params: ValidatedWaitParams::new(5, 63),
show_ui_outputs: true,
},
&ui,
)
Expand All @@ -160,7 +161,7 @@ async fn test_wait_for_wrong_retry_values() {
&provider,
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
ValidatedWaitParams::new(2, 1),
&ui,
Some(&ui),
)
.await
.unwrap();
Expand All @@ -175,7 +176,7 @@ async fn test_wait_for_wrong_retry_values_timeout_zero() {
&provider,
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
ValidatedWaitParams::new(2, 0),
&ui,
Some(&ui),
)
.await
.unwrap();
Expand All @@ -190,7 +191,7 @@ async fn test_wait_for_wrong_retry_values_interval_zero() {
&provider,
MAP_CONTRACT_DECLARE_TX_HASH_SEPOLIA.parse().unwrap(),
ValidatedWaitParams::new(0, 1),
&ui,
Some(&ui),
)
.await
.unwrap();
Expand Down
Loading