Skip to content

Commit

Permalink
feat(sozo): wait for tx to be accepted (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Dec 28, 2023
1 parent dfc48d0 commit bd1fe3a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 12 deletions.
4 changes: 4 additions & 0 deletions crates/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet::core::types::FieldElement;

use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use super::options::world::WorldOptions;
use crate::ops::auth;

Expand Down Expand Up @@ -33,6 +34,9 @@ pub enum AuthCommand {

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
}

Expand Down
4 changes: 4 additions & 0 deletions crates/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet::core::types::FieldElement;

use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use crate::ops::execute;

#[derive(Debug, Args)]
Expand All @@ -29,6 +30,9 @@ pub struct ExecuteArgs {

#[command(flatten)]
pub account: AccountOptions,

#[command(flatten)]
pub transaction: TransactionOptions,
}

impl ExecuteArgs {
Expand Down
2 changes: 1 addition & 1 deletion crates/sozo/src/commands/options/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl AccountOptions {
env_metadata: Option<&Environment>,
) -> Result<SingleOwnerAccount<P, LocalWallet>>
where
P: Provider + Send + Sync + 'static,
P: Provider + Send + Sync,
{
let account_address = self.account_address(env_metadata)?;
let signer = self.signer(env_metadata)?;
Expand Down
7 changes: 7 additions & 0 deletions crates/sozo/src/commands/options/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ pub struct TransactionOptions {
the estimated fee which will be used as the max fee for the transaction. \
(max_fee = estimated_fee * multiplier)")]
pub fee_estimate_multiplier: Option<f64>,

#[arg(short, long)]
#[arg(help = "Wait until the transaction is accepted by the sequencer, returning the receipt.")]
#[arg(long_help = "Wait until the transaction is accepted by the sequencer, returning the \
receipt. This will poll the transaction status until it gets accepted or \
rejected by the sequencer.")]
pub wait: bool,
}

impl From<TransactionOptions> for TxConfig {
Expand Down
4 changes: 4 additions & 0 deletions crates/sozo/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet::core::types::FieldElement;

use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use super::options::world::WorldOptions;
use crate::ops::register;

Expand Down Expand Up @@ -33,6 +34,9 @@ pub enum RegisterCommand {

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
}

Expand Down
12 changes: 9 additions & 3 deletions crates/sozo/src/ops/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use anyhow::{Context, Result};
use dojo_world::contracts::cairo_utils;
use dojo_world::contracts::world::WorldContract;
use dojo_world::metadata::Environment;
use dojo_world::utils::TransactionWaiter;

use crate::commands::auth::AuthCommand;

pub async fn execute(command: AuthCommand, env_metadata: Option<Environment>) -> Result<()> {
match command {
AuthCommand::Writer { model, contract, world, starknet, account } => {
AuthCommand::Writer { model, contract, world, starknet, account, transaction } => {
let world_address = world.address(env_metadata.as_ref())?;
let provider = starknet.provider(env_metadata.as_ref())?;

let account = account.account(provider, env_metadata.as_ref()).await?;
let account = account.account(&provider, env_metadata.as_ref()).await?;
let world = WorldContract::new(world_address, &account);

let res = world
Expand All @@ -20,7 +21,12 @@ pub async fn execute(command: AuthCommand, env_metadata: Option<Environment>) ->
.await
.with_context(|| "Failed to send transaction")?;

println!("Transaction: {:#x}", res.transaction_hash);
if transaction.wait {
let receipt = TransactionWaiter::new(res.transaction_hash, &provider).await?;
println!("{}", serde_json::to_string_pretty(&receipt)?);
} else {
println!("Transaction hash: {:#x}", res.transaction_hash);
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions crates/sozo/src/ops/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{Context, Result};
use dojo_world::metadata::Environment;
use dojo_world::migration::strategy::generate_salt;
use dojo_world::utils::TransactionWaiter;
use starknet::accounts::{Account, Call};
use starknet::core::types::{BlockId, BlockTag, FieldElement, FunctionCall};
use starknet::core::utils::{get_contract_address, get_selector_from_name};
Expand All @@ -10,7 +11,7 @@ use starknet::providers::Provider;
use crate::commands::execute::ExecuteArgs;

pub async fn execute(args: ExecuteArgs, env_metadata: Option<Environment>) -> Result<()> {
let ExecuteArgs { contract, entrypoint, calldata, starknet, account } = args;
let ExecuteArgs { contract, entrypoint, calldata, starknet, account, transaction } = args;

let provider = starknet.provider(env_metadata.as_ref())?;

Expand Down Expand Up @@ -42,19 +43,24 @@ pub async fn execute(args: ExecuteArgs, env_metadata: Option<Environment>) -> Re
)
};

let account = account.account(provider, env_metadata.as_ref()).await?;
let account = account.account(&provider, env_metadata.as_ref()).await?;

let res = account
.execute(vec![Call {
calldata,
to: contract_address,
selector: get_selector_from_name(&entrypoint).unwrap(),
selector: get_selector_from_name(&entrypoint)?,
}])
.send()
.await
.with_context(|| "Failed to send transaction")?;

println!("Transaction: {:#x}", res.transaction_hash);
if transaction.wait {
let receipt = TransactionWaiter::new(res.transaction_hash, &provider).await?;
println!("{}", serde_json::to_string_pretty(&receipt)?);
} else {
println!("Transaction hash: {:#x}", res.transaction_hash);
}

Ok(())
}
2 changes: 1 addition & 1 deletion crates/sozo/src/ops/migration/migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn migrate_with_small_fee_multiplier_will_fail() {
&ws,
&migration,
&account,
Some(TransactionOptions { fee_estimate_multiplier: Some(0.2f64) }),
Some(TransactionOptions { fee_estimate_multiplier: Some(0.2f64), wait: false }),
)
.await
.is_err()
Expand Down
12 changes: 9 additions & 3 deletions crates/sozo/src/ops/register.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use anyhow::{Context, Result};
use dojo_world::contracts::WorldContract;
use dojo_world::metadata::Environment;
use dojo_world::utils::TransactionWaiter;
use starknet::accounts::Account;

use crate::commands::register::RegisterCommand;

pub async fn execute(command: RegisterCommand, env_metadata: Option<Environment>) -> Result<()> {
match command {
RegisterCommand::Model { models, world, starknet, account } => {
RegisterCommand::Model { models, world, starknet, account, transaction } => {
let world_address = world.address(env_metadata.as_ref())?;
let provider = starknet.provider(env_metadata.as_ref())?;

let account = account.account(provider, env_metadata.as_ref()).await?;
let account = account.account(&provider, env_metadata.as_ref()).await?;
let world = WorldContract::new(world_address, &account);

let calls = models
Expand All @@ -25,7 +26,12 @@ pub async fn execute(command: RegisterCommand, env_metadata: Option<Environment>
.await
.with_context(|| "Failed to send transaction")?;

println!("Models registered at transaction: {:#x}", res.transaction_hash)
if transaction.wait {
let receipt = TransactionWaiter::new(res.transaction_hash, &provider).await?;
println!("{}", serde_json::to_string_pretty(&receipt)?);
} else {
println!("Transaction hash: {:#x}", res.transaction_hash);
}
}
}
Ok(())
Expand Down

0 comments on commit bd1fe3a

Please sign in to comment.