Skip to content

Commit

Permalink
feat(sozo): output block number after successful migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x committed Aug 7, 2023
1 parent 44024c2 commit a147706
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
22 changes: 19 additions & 3 deletions crates/dojo-world/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +33,7 @@ pub struct DeployOutput {
pub transaction_hash: FieldElement,
pub contract_address: FieldElement,
pub declare: Option<DeclareOutput>,
pub block_number: u64,
}

#[derive(Debug)]
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -216,3 +222,13 @@ fn get_compiled_class_hash(artifact_path: &PathBuf) -> Result<FieldElement> {
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,
}
}
31 changes: 25 additions & 6 deletions crates/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -196,15 +197,17 @@ where
Ok(migration)
}

// returns the block number at which new/updated world contract if deployed
async fn execute_strategy<P, S>(
strategy: &MigrationStrategy,
migrator: &SingleOwnerAccount<P, S>,
ws_config: &Config,
) -> Result<()>
) -> Result<Option<u64>>
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");
Expand Down Expand Up @@ -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::<S, <P as Provider>::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}"));
}

Expand Down Expand Up @@ -272,6 +279,8 @@ where
"Deploy transaction: {:#x}",
val.transaction_hash
));

block_height_world = Some(val.block_number);

Ok(())
}
Expand All @@ -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<P, S>(
Expand Down Expand Up @@ -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,
}
}

0 comments on commit a147706

Please sign in to comment.