Skip to content

Commit

Permalink
Use TransactionWaiter
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyFate committed Mar 21, 2024
1 parent 43a1f6e commit fc1cd0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/dojo-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum TransactionWaitingError {
/// let provider = JsonRpcClient::new(HttpTransport::new(Url::parse("http://localhost:5000").unwrap()));
///
/// let tx_hash = FieldElement::from(0xbadbeefu64);
/// let receipt = TransactionWaiter::new(tx_hash, &provider).with_finality(TransactionFinalityStatus::ACCEPTED_ON_L2).await.unwrap();
/// let receipt = TransactionWaiter::new(tx_hash, &provider).with_tx_status(TransactionFinalityStatus::AcceptedOnL2).await.unwrap();
/// ```
#[must_use = "TransactionWaiter does nothing unless polled"]
pub struct TransactionWaiter<'a, P: Provider> {
Expand Down
46 changes: 10 additions & 36 deletions crates/sozo/ops/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::io::Write;
use std::path::PathBuf;
use std::time::Duration;

use anyhow::Result;
use colored::Colorize;
use colored_json::{ColorMode, Output};
use dojo_utils::TransactionWaiter;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use starknet::accounts::{AccountFactory, AccountFactoryError, OpenZeppelinAccountFactory};
use starknet::core::serde::unsigned_field_element::UfeHex;
use starknet::core::types::{BlockId, BlockTag, ExecutionResult, StarknetError};
use starknet::core::types::{BlockId, BlockTag, StarknetError, TransactionFinalityStatus};
use starknet::core::utils::get_contract_address;
use starknet::macros::felt;
use starknet::providers::jsonrpc::HttpTransport;
Expand Down Expand Up @@ -302,7 +302,14 @@ pub async fn deploy(
format!("{:#064x}", account_deployment_tx).bright_yellow(),
"sozo account fetch".bright_yellow(),
);
watch_tx(&provider, account_deployment_tx, Duration::from_millis(poll_interval)).await?;
TransactionWaiter::new(account_deployment_tx, &provider)
.with_tx_status(TransactionFinalityStatus::AcceptedOnL2)
.with_interval(poll_interval)
.await?;
eprintln!(
"Transaction {} confirmed",
format!("{:#064x}", account_deployment_tx).bright_yellow()
);

account.deployment = DeploymentStatus::Deployed(DeployedStatus {
class_hash: undeployed_status.class_hash,
Expand Down Expand Up @@ -352,39 +359,6 @@ fn map_starknet_error(err: StarknetError) -> anyhow::Error {
}
}

Check warning on line 360 in crates/sozo/ops/src/account.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/account.rs#L360

Added line #L360 was not covered by tests

pub async fn watch_tx<P>(
provider: P,
transaction_hash: FieldElement,
poll_interval: Duration,
) -> Result<()>
where
P: Provider,
{
loop {
match provider.get_transaction_receipt(transaction_hash).await {
Ok(receipt) => match receipt.execution_result() {
ExecutionResult::Succeeded => {
eprintln!(
"Transaction {} confirmed",
format!("{:#064x}", transaction_hash).bright_yellow()
);

return Ok(());
}
ExecutionResult::Reverted { reason } => {
return Err(anyhow::anyhow!("transaction reverted: {}", reason));
}
},
Err(ProviderError::StarknetError(StarknetError::TransactionHashNotFound)) => {
eprintln!("Transaction not confirmed yet...");
}
Err(err) => return Err(err.into()),
}

tokio::time::sleep(poll_interval).await;
}
}

#[derive(Debug)]

Check warning on line 362 in crates/sozo/ops/src/account.rs

View check run for this annotation

Codecov / codecov/patch

crates/sozo/ops/src/account.rs#L362

Added line #L362 was not covered by tests
pub enum FeeSetting {
Manual(FieldElement),
Expand Down

0 comments on commit fc1cd0f

Please sign in to comment.