From a1477067bff479059828c2502d321a1f8d4ece2d Mon Sep 17 00:00:00 2001 From: lambda-0x <0xlambda@protonmail.com> Date: Sun, 6 Aug 2023 17:01:40 +0530 Subject: [PATCH] feat(sozo): output block number after successful migration --- crates/dojo-world/src/migration/mod.rs | 22 +++++++++++++++--- crates/sozo/src/ops/migration/mod.rs | 31 +++++++++++++++++++++----- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/crates/dojo-world/src/migration/mod.rs b/crates/dojo-world/src/migration/mod.rs index 2dfc660e6a..4ef039b41a 100644 --- a/crates/dojo-world/src/migration/mod.rs +++ b/crates/dojo-world/src/migration/mod.rs @@ -10,7 +10,7 @@ use starknet::accounts::{Account, AccountError, Call, ConnectedAccount, SingleOw use starknet::core::types::contract::{CompiledClass, SierraClass}; use starknet::core::types::{ BlockId, BlockTag, DeclareTransactionResult, FieldElement, FlattenedSierraClass, - InvokeTransactionResult, StarknetError, + InvokeTransactionResult, StarknetError, TransactionReceipt, }; use starknet::core::utils::{ get_contract_address, get_selector_from_name, CairoShortStringToFeltError, @@ -33,6 +33,7 @@ pub struct DeployOutput { pub transaction_hash: FieldElement, pub contract_address: FieldElement, pub declare: Option, + pub block_number: u64, } #[derive(Debug)] @@ -181,11 +182,16 @@ pub trait Deployable: Declarable + Sync { .await .map_err(MigrationError::Migrator)?; - let _ = TransactionWaiter::new(transaction_hash, account.provider()) + let txn = TransactionWaiter::new(transaction_hash, account.provider()) .await .map_err(MigrationError::WaitingError)?; - Ok(DeployOutput { transaction_hash, contract_address, declare }) + Ok(DeployOutput { + transaction_hash, + contract_address, + declare, + block_number: get_block_number(&txn), + }) } fn salt(&self) -> FieldElement; @@ -216,3 +222,13 @@ fn get_compiled_class_hash(artifact_path: &PathBuf) -> Result { let compiled_class: CompiledClass = serde_json::from_str(&res)?; Ok(compiled_class.class_hash()?) } + +fn get_block_number(tx: &TransactionReceipt) -> u64 { + match tx { + TransactionReceipt::Invoke(tx) => tx.block_number, + TransactionReceipt::L1Handler(tx) => tx.block_number, + TransactionReceipt::Declare(tx) => tx.block_number, + TransactionReceipt::Deploy(tx) => tx.block_number, + TransactionReceipt::DeployAccount(tx) => tx.block_number, + } +} diff --git a/crates/sozo/src/ops/migration/mod.rs b/crates/sozo/src/ops/migration/mod.rs index ba4522fa41..40e273d35d 100644 --- a/crates/sozo/src/ops/migration/mod.rs +++ b/crates/sozo/src/ops/migration/mod.rs @@ -10,7 +10,7 @@ use dojo_world::utils::TransactionWaiter; use scarb::core::Config; use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount}; use starknet::core::types::{ - BlockId, BlockTag, FieldElement, InvokeTransactionResult, StarknetError, + BlockId, BlockTag, FieldElement, InvokeTransactionResult, StarknetError, TransactionReceipt, }; use starknet::core::utils::cairo_short_string_to_felt; use starknet::providers::jsonrpc::HttpTransport; @@ -68,13 +68,14 @@ where println!(" "); - execute_strategy(&strategy, &account, config) + let block_height = execute_strategy(&strategy, &account, config) .await .map_err(|e| anyhow!(e)) .with_context(|| "Problem trying to migrate.")?; config.ui().print(format!( - "\nšŸŽ‰ Successfully migrated World at address {}", + "\nšŸŽ‰ Successfully migrated World on block #{} at address {}", + block_height.expect("because world address always exists there is always a deployment so this cannot be none"), bold_message(format!( "{:#x}", strategy.world_address().expect("world address must exist") @@ -196,15 +197,17 @@ where Ok(migration) } +// returns the block number at which new/updated world contract if deployed async fn execute_strategy( strategy: &MigrationStrategy, migrator: &SingleOwnerAccount, ws_config: &Config, -) -> Result<()> +) -> Result> where P: Provider + Sync + Send + 'static, S: Signer + Sync + Send + 'static, { + let mut block_height_world = None; match &strategy.executor { Some(executor) => { ws_config.ui().print_header("# Executor"); @@ -236,10 +239,14 @@ where .set_executor(executor.contract_address) .await?; - let _ = TransactionWaiter::new(transaction_hash, migrator.provider()) + let txn = TransactionWaiter::new(transaction_hash, migrator.provider()) .await .map_err(MigrationError::::Error>::WaitingError); + if let Ok(txn) = txn { + block_height_world = Some(get_block_number(&txn)); + } + ws_config.ui().print_hidden_sub(format!("Updated at: {transaction_hash:#x}")); } @@ -272,6 +279,8 @@ where "Deploy transaction: {:#x}", val.transaction_hash )); + + block_height_world = Some(val.block_number); Ok(()) } @@ -291,7 +300,7 @@ where register_components(strategy, migrator, ws_config).await?; register_systems(strategy, migrator, ws_config).await?; - Ok(()) + Ok(block_height_world) } async fn register_components( @@ -413,3 +422,13 @@ where Ok(Some(RegisterOutput { transaction_hash, declare_output })) } + +fn get_block_number(tx: &TransactionReceipt) -> u64 { + match tx { + TransactionReceipt::Invoke(tx) => tx.block_number, + TransactionReceipt::L1Handler(tx) => tx.block_number, + TransactionReceipt::Declare(tx) => tx.block_number, + TransactionReceipt::Deploy(tx) => tx.block_number, + TransactionReceipt::DeployAccount(tx) => tx.block_number, + } +}