Skip to content

Commit

Permalink
Merge branch 'main' into katana/commitments
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Nov 1, 2024
2 parents 8acd21c + b93f761 commit 8e6a1fa
Show file tree
Hide file tree
Showing 16 changed files with 2,352 additions and 86 deletions.
4 changes: 4 additions & 0 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use dojo_types::naming;
use dojo_utils::Invoker;
use dojo_world::contracts::naming::ensure_namespace;
use scarb::core::Config;
use sozo_ops::migration_ui::MigrationUi;
use sozo_scarbext::WorkspaceExt;
use sozo_walnut::WalnutDebugger;
use starknet::core::types::{Call, Felt};
Expand Down Expand Up @@ -79,11 +80,14 @@ impl ExecuteArgs {
);

config.tokio_handle().block_on(async {
let mut spinner = MigrationUi::new("").with_silent();

let (world_diff, account, _) = utils::get_world_diff_and_account(
self.account,
self.starknet.clone(),
self.world,
&ws,
&mut spinner,
)
.await?;

Expand Down
18 changes: 8 additions & 10 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use colored::Colorize;
use dojo_utils::{self, TxnConfig};
use dojo_world::contracts::WorldContract;
use scarb::core::{Config, Workspace};
use sozo_ops::migrate::{Migration, MigrationResult, MigrationUi};
use sozo_ops::migrate::{Migration, MigrationResult};
use sozo_ops::migration_ui::MigrationUi;
use sozo_scarbext::WorkspaceExt;
use spinoff::{spinner, spinners, Spinner};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::Provider;
use tabled::settings::Style;
Expand Down Expand Up @@ -45,20 +45,17 @@ impl MigrateArgs {

let MigrateArgs { world, starknet, account, .. } = self;

let frames = spinner!(["⛩️ ", "🎃", "👻", "🧟", "💀"], 500);
// let frames = spinner!(["⛩️ ", "🥷 ", "🗡️ "], 500);

config.tokio_handle().block_on(async {
print_banner(&ws, &starknet).await?;

let mut spinner =
MigrationUi::Spinner(Spinner::new(frames, "Evaluating world diff...", None));
let mut spinner = MigrationUi::new("Evaluating world diff...");

let mut txn_config: TxnConfig = self.transaction.into();
txn_config.wait = true;

let (world_diff, account, rpc_url) =
utils::get_world_diff_and_account(account, starknet, world, &ws).await?;
utils::get_world_diff_and_account(account, starknet, world, &ws, &mut spinner)
.await?;

let world_address = world_diff.world_info.address;

Expand All @@ -84,7 +81,7 @@ impl MigrateArgs {
("🎃", format!("No changes for world at address {:#066x}", world_address))
};

spinner.stop_and_persist(symbol, Box::leak(end_text.into_boxed_str()));
spinner.stop_and_persist_boxed(symbol, end_text);

Ok(())
})
Expand All @@ -100,7 +97,8 @@ pub struct Banner {

/// Prints the migration banner.
async fn print_banner(ws: &Workspace<'_>, starknet: &StarknetOptions) -> Result<()> {
let (provider, rpc_url) = starknet.provider(None)?;
let profile_config = ws.load_profile_config()?;
let (provider, rpc_url) = starknet.provider(profile_config.env.as_ref())?;

let chain_id = provider.chain_id().await?;
let chain_id = parse_cairo_short_string(&chain_id)
Expand Down
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/options/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl AccountOptions {
let account_address = self.account_address(env_metadata)?;

let signer = self.signer.signer(env_metadata, false)?;
trace!(?signer, "Signer obtained.");

trace!("Fetching chain id...");
let chain_id = provider.chain_id().await?;
trace!(?chain_id);

Expand Down
7 changes: 7 additions & 0 deletions bin/sozo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use dojo_world::local::WorldLocal;
use katana_rpc_api::starknet::RPC_SPEC_VERSION;
use scarb::core::{TomlManifest, Workspace};
use semver::Version;
use sozo_ops::migration_ui::MigrationUi;
use sozo_scarbext::WorkspaceExt;
use starknet::accounts::{Account, ConnectedAccount};
use starknet::core::types::Felt;
Expand Down Expand Up @@ -144,19 +145,25 @@ pub async fn get_world_diff_and_account(
starknet: StarknetOptions,
world: WorldOptions,
ws: &Workspace<'_>,
ui: &mut MigrationUi,
) -> Result<(WorldDiff, SozoAccount<JsonRpcClient<HttpTransport>>, String)> {
let profile_config = ws.load_profile_config()?;
let env = profile_config.env.as_ref();

let (world_diff, provider, rpc_url) =
get_world_diff_and_provider(starknet.clone(), world, ws).await?;

// Ensures we don't interfere with the spinner if a password must be prompted.
ui.stop();

let account = {
account
.account(provider, world_diff.world_info.address, &starknet, env, &world_diff)
.await?
};

ui.restart("Verifying account...");

if !dojo_utils::is_deployed(account.address(), &account.provider()).await? {
return Err(anyhow!("Account with address {:#x} doesn't exist.", account.address()));
}
Expand Down
89 changes: 88 additions & 1 deletion crates/dojo/bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,79 @@ impl Buffer {
self.0.push(s.clone());
}

/// Inserts string after the first occurrence of the separator.
///
/// * `s` - The string to insert.
/// * `pos` - The string inside inner vec to search position for.
/// * `sep` - The separator to search for.
/// * `idx` - The index of the separator to insert after.
pub fn insert_after(&mut self, s: String, pos: &str, sep: &str, idx: usize) {
let pos = self.0.iter().position(|b| b.contains(pos)).unwrap();
let pos = self.pos(pos).unwrap();
if let Some(st) = self.0.get_mut(pos) {
let indices = st.match_indices(sep).map(|(i, _)| i).collect::<Vec<usize>>();
let append_after = indices[indices.len() - idx] + 1;
st.insert_str(append_after, &s);
}
}

/// Inserts string at the specified position.
///
/// * `s` - The string to insert.
/// * `pos` - The position to insert the string at.
/// * `idx` - The index of the string to insert at.
pub fn insert_at(&mut self, s: String, pos: usize, idx: usize) {
if let Some(st) = self.0.get_mut(idx) {
st.insert_str(pos + 1, &s);
}
}

/// Finds position of the given string in the inner vec.
///
/// * `pos` - The string to search for.
pub fn pos(&self, pos: &str) -> Option<usize> {
self.0.iter().position(|b| b.contains(pos))
}

pub fn join(&mut self, sep: &str) -> String {
self.0.join(sep)
}

/// At given index, finds the first occurrence of the needle string after the search string.
///
/// * `needle` - The string to search for.
/// * `search` - The string to search after.
/// * `idx` - The index to search at.
pub fn get_first_after(&self, needle: &str, search: &str, idx: usize) -> Option<usize> {
if let Some(st) = self.0.get(idx) {
let indices = st.match_indices(needle).map(|(i, _)| i).collect::<Vec<usize>>();
if indices.is_empty() {
return None;
}

let start = indices[indices.len() - 1] + 1;
let search_indices = st.match_indices(search).map(|(i, _)| i).collect::<Vec<usize>>();
return search_indices.iter().filter(|&&i| i > start).min().copied();
}
None
}

/// At given index, finds the first occurrence of the needle string before the position in
/// string
///
/// * `search` - The token to search for.
/// * `pos` - Starting position of the search.
/// * `idx` - The index to search at.
pub fn get_first_before_pos(&self, search: &str, pos: usize, idx: usize) -> Option<usize> {
if let Some(st) = self.0.get(idx) {
let indices = st.match_indices(search).map(|(i, _)| i).collect::<Vec<usize>>();
if indices.is_empty() {
return None;
}

return indices.iter().filter(|&&i| i < pos).max().copied();
}
None
}
}

impl Deref for Buffer {
Expand Down Expand Up @@ -112,3 +174,28 @@ pub trait BindgenContractGenerator: Sync {
buffer: &mut Buffer,
) -> BindgenResult<String>;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_buffer_get_first_after() {
let mut buff = Buffer::new();
buff.push("import { DojoProvider } from \"@dojoengine/core\";".to_owned());
buff.push("return { actions: { changeTheme, increaseGlobalCounter, } };".to_owned());
let pos = buff.get_first_after("actions: {", "}", 1);

assert_eq!(pos, Some(56));
}

#[test]
fn test_buffer_get_first_before() {
let mut buff = Buffer::new();
buff.push("import { DojoProvider } from \"@dojoengine/core\";".to_owned());
buff.push("return { actions: { changeTheme, increaseGlobalCounter, } };".to_owned());
let pos = buff.get_first_before_pos(",", 56, 1);

assert_eq!(pos, Some(54));
}
}
Loading

0 comments on commit 8e6a1fa

Please sign in to comment.