Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sozo): output block number after successful migration #717

Merged
merged 5 commits into from
Aug 11, 2023
Merged
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
12 changes: 9 additions & 3 deletions crates/dojo-world/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet::providers::{Provider, ProviderError};
use starknet::signers::Signer;
use thiserror::Error;

use crate::utils::{TransactionWaiter, TransactionWaitingError};
use crate::utils::{block_number_from_receipt, TransactionWaiter, TransactionWaitingError};

pub mod class;
pub mod contract;
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: block_number_from_receipt(&txn),
})
}

fn salt(&self) -> FieldElement;
Expand Down
12 changes: 11 additions & 1 deletion crates/dojo-world/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ where
}
}

fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionStatus {
pub fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionStatus {
match receipt {
TransactionReceipt::Invoke(receipt) => receipt.status,
TransactionReceipt::Deploy(receipt) => receipt.status,
Expand All @@ -163,6 +163,16 @@ fn transaction_status_from_receipt(receipt: &TransactionReceipt) -> TransactionS
}
}

pub fn block_number_from_receipt(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,
}
}

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
Expand Down
66 changes: 47 additions & 19 deletions crates/sozo/src/ops/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,29 @@ 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 {}",
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
if let Some(block_height) = block_height {
config.ui().print(format!(
"\n🎉 Successfully migrated World on block #{} at address {}",
block_height,
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
} else {
config.ui().print(format!(
"\n🎉 Successfully migrated World at address {}",
bold_message(format!(
"{:#x}",
strategy.world_address().expect("world address must exist")
))
));
}
}

Ok(())
Expand Down Expand Up @@ -196,15 +207,18 @@ where
Ok(migration)
}

// returns the Some(block number) at which migration world is deployed, returns none if world was
// not redeployed
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 = None;
match &strategy.executor {
Some(executor) => {
ws_config.ui().print_header("# Executor");
Expand Down Expand Up @@ -236,9 +250,13 @@ where
.set_executor(executor.contract_address)
.await?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
let _ =
TransactionWaiter::new(transaction_hash, migrator.provider()).await.map_err(
MigrationError::<
<SingleOwnerAccount<P, S> as Account>::SignError,
<P as Provider>::Error,
>::WaitingError,
)?;

ws_config.ui().print_hidden_sub(format!("Updated at: {transaction_hash:#x}"));
}
Expand Down Expand Up @@ -273,6 +291,8 @@ where
val.transaction_hash
));

block_height = Some(val.block_number);

Ok(())
}
Err(MigrationError::ContractAlreadyDeployed) => Err(anyhow!(
Expand All @@ -291,7 +311,7 @@ where
register_components(strategy, migrator, ws_config).await?;
register_systems(strategy, migrator, ws_config).await?;

Ok(())
Ok(block_height)
}

async fn register_components<P, S>(
Expand Down Expand Up @@ -345,9 +365,13 @@ where
.await
.map_err(|e| anyhow!("Failed to register components to World: {e}"))?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
let _ =
TransactionWaiter::new(transaction_hash, migrator.provider()).await.map_err(
MigrationError::<
<SingleOwnerAccount<P, S> as Account>::SignError,
<P as Provider>::Error,
>::WaitingError,
)?;

ws_config.ui().print_hidden_sub(format!("registered at: {transaction_hash:#x}"));

Expand Down Expand Up @@ -405,9 +429,13 @@ where
.await
.map_err(|e| anyhow!("Failed to register systems to World: {e}"))?;

let _ = TransactionWaiter::new(transaction_hash, migrator.provider())
.await
.map_err(MigrationError::<S, <P as Provider>::Error>::WaitingError);
let _ =
TransactionWaiter::new(transaction_hash, migrator.provider()).await.map_err(
MigrationError::<
<SingleOwnerAccount<P, S> as Account>::SignError,
<P as Provider>::Error,
>::WaitingError,
)?;

ws_config.ui().print_hidden_sub(format!("registered at: {transaction_hash:#x}"));

Expand Down