diff --git a/crates/sozo/src/args.rs b/crates/sozo/src/args.rs index dacb0b6007..f5b81ab298 100644 --- a/crates/sozo/src/args.rs +++ b/crates/sozo/src/args.rs @@ -17,7 +17,6 @@ use crate::commands::execute::ExecuteArgs; use crate::commands::init::InitArgs; use crate::commands::migrate::MigrateArgs; use crate::commands::register::RegisterArgs; -use crate::commands::system::SystemArgs; use crate::commands::test::TestArgs; #[derive(Parser)] @@ -66,8 +65,6 @@ pub enum Commands { Execute(ExecuteArgs), #[command(about = "Interact with a worlds components")] Component(ComponentArgs), - #[command(about = "Interact with a worlds systems")] - System(SystemArgs), #[command(about = "Register new systems and components")] Register(RegisterArgs), #[command(about = "Queries world events")] diff --git a/crates/sozo/src/commands/execute.rs b/crates/sozo/src/commands/execute.rs index d0a463bb15..5a2d3f4a93 100644 --- a/crates/sozo/src/commands/execute.rs +++ b/crates/sozo/src/commands/execute.rs @@ -6,14 +6,16 @@ use starknet::core::types::FieldElement; use super::options::account::AccountOptions; use super::options::starknet::StarknetOptions; -use super::options::world::WorldOptions; use crate::ops::execute; #[derive(Debug, Args)] #[command(about = "Execute a system with the given calldata.")] pub struct ExecuteArgs { - #[arg(help = "The name of the system to be executed.")] - pub system: String, + #[arg(help = "The name of the contract to be executed.")] + pub contract: FieldElement, + + #[arg(help = "The name of the entrypoint to be executed.")] + pub entrypoint: String, #[arg(short, long)] #[arg(value_delimiter = ',')] @@ -21,9 +23,6 @@ pub struct ExecuteArgs { 0x12345,0x69420.")] pub calldata: Vec, - #[command(flatten)] - pub world: WorldOptions, - #[command(flatten)] pub starknet: StarknetOptions, diff --git a/crates/sozo/src/commands/mod.rs b/crates/sozo/src/commands/mod.rs index 9d3ce0dbd7..7ce24a257e 100644 --- a/crates/sozo/src/commands/mod.rs +++ b/crates/sozo/src/commands/mod.rs @@ -14,7 +14,6 @@ pub(crate) mod init; pub(crate) mod migrate; pub(crate) mod options; pub(crate) mod register; -pub(crate) mod system; pub(crate) mod test; // copy of non pub functions from scarb @@ -30,7 +29,6 @@ pub fn run(command: Commands, config: &Config) -> Result<()> { Commands::Auth(args) => args.run(config), Commands::Execute(args) => args.run(config), Commands::Component(args) => args.run(config), - Commands::System(args) => args.run(config), Commands::Register(args) => args.run(config), Commands::Events(args) => args.run(config), Commands::Completions(args) => args.run(), diff --git a/crates/sozo/src/commands/system.rs b/crates/sozo/src/commands/system.rs deleted file mode 100644 index 74a7c484db..0000000000 --- a/crates/sozo/src/commands/system.rs +++ /dev/null @@ -1,61 +0,0 @@ -use anyhow::Result; -use clap::{Args, Subcommand}; -use dojo_world::metadata::dojo_metadata_from_workspace; -use scarb::core::Config; - -use super::options::starknet::StarknetOptions; -use super::options::world::WorldOptions; -use crate::ops::system; - -#[derive(Debug, Args)] -pub struct SystemArgs { - #[command(subcommand)] - command: SystemCommands, -} - -#[derive(Debug, Subcommand)] -pub enum SystemCommands { - #[command(about = "Get the class hash of a system.")] - Get { - #[arg(help = "The name of the system.")] - name: String, - - #[command(flatten)] - world: WorldOptions, - - #[command(flatten)] - starknet: StarknetOptions, - }, - - #[command(alias = "dep")] - #[command(about = "Retrieve the component dependencies of a system.")] - Dependency { - #[arg(help = "The name of the system.")] - name: String, - - #[arg(short = 'j', long = "json")] - #[arg(help_heading = "Display options")] - to_json: bool, - - #[command(flatten)] - world: WorldOptions, - - #[command(flatten)] - starknet: StarknetOptions, - }, -} - -impl SystemArgs { - pub fn run(self, config: &Config) -> Result<()> { - let env_metadata = if config.manifest_path().exists() { - let ws = scarb::ops::read_workspace(config.manifest_path(), config)?; - - // TODO: Check the updated scarb way to read profile specific values - dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env().cloned()) - } else { - None - }; - - config.tokio_handle().block_on(system::execute(self.command, env_metadata)) - } -} diff --git a/crates/sozo/src/ops/execute.rs b/crates/sozo/src/ops/execute.rs index 8dbda3b1e4..3d8fe31a64 100644 --- a/crates/sozo/src/ops/execute.rs +++ b/crates/sozo/src/ops/execute.rs @@ -1,20 +1,26 @@ use anyhow::{Context, Result}; use dojo_world::metadata::Environment; -use torii_client::contract::world::WorldContract; +use starknet::accounts::{Account, Call}; +use starknet::core::utils::get_selector_from_name; use crate::commands::execute::ExecuteArgs; pub async fn execute(args: ExecuteArgs, env_metadata: Option) -> Result<()> { - let ExecuteArgs { system, calldata, world, starknet, account } = args; + let ExecuteArgs { contract, entrypoint, calldata, starknet, account } = args; - 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 world = WorldContract::new(world_address, &account); - let res = - world.execute(&system, calldata).await.with_context(|| "Failed to send transaction")?; + let res = account + .execute(vec![Call { + calldata, + to: contract, + selector: get_selector_from_name(&entrypoint).unwrap(), + }]) + .send() + .await + .with_context(|| "Failed to send transaction")?; println!("Transaction: {:#x}", res.transaction_hash); diff --git a/crates/sozo/src/ops/mod.rs b/crates/sozo/src/ops/mod.rs index c1e215ddfb..02302ca2f2 100644 --- a/crates/sozo/src/ops/mod.rs +++ b/crates/sozo/src/ops/mod.rs @@ -4,4 +4,3 @@ pub mod events; pub mod execute; pub mod migration; pub mod register; -pub mod system; diff --git a/crates/sozo/src/ops/system.rs b/crates/sozo/src/ops/system.rs deleted file mode 100644 index 8b2612bbc3..0000000000 --- a/crates/sozo/src/ops/system.rs +++ /dev/null @@ -1,66 +0,0 @@ -use anyhow::Result; -use console::Style; -use dojo_world::metadata::Environment; -use starknet::core::types::{BlockId, BlockTag}; -use torii_client::contract::world::WorldContractReader; - -use crate::commands::system::SystemCommands; - -pub async fn execute(command: SystemCommands, env_metadata: Option) -> Result<()> { - match command { - SystemCommands::Get { name, world, starknet } => { - let world_address = world.address(env_metadata.as_ref())?; - let provider = starknet.provider(env_metadata.as_ref())?; - - let world = WorldContractReader::new(world_address, &provider); - let system = world.system(&name, BlockId::Tag(BlockTag::Pending)).await?; - - println!("{:#x}", system.class_hash()) - } - - SystemCommands::Dependency { name, to_json, world, starknet } => { - let world_address = world.address(env_metadata.as_ref())?; - let provider = starknet.provider(env_metadata.as_ref())?; - - let world = WorldContractReader::new(world_address, &provider); - let system = world.system(&name, BlockId::Tag(BlockTag::Pending)).await?; - - let deps = system.dependencies(BlockId::Tag(BlockTag::Pending)).await?; - - if to_json { - println!("{}", serde_json::to_string_pretty(&deps)?); - } else { - let read = deps - .iter() - .enumerate() - .filter_map(|(i, d)| { - if d.read { Some(format!("{}.{}", i + 1, d.name.clone())) } else { None } - }) - .collect::>(); - - let write = deps - .iter() - .enumerate() - .filter_map(|(i, d)| { - if d.write { Some(format!("{}. {}", i + 1, d.name.clone())) } else { None } - }) - .collect::>(); - - let output = format!( - r"{} -{} -{} -{}", - Style::from_dotted_str("bold.underlined").apply_to("Read:"), - read.join("\n"), - Style::from_dotted_str("bold.underlined").apply_to("Write:"), - write.join("\n"), - ); - - println!("{output}") - } - } - } - - Ok(()) -} diff --git a/crates/torii/client/src/contract/mod.rs b/crates/torii/client/src/contract/mod.rs index 0131b090a4..94b8f34a34 100644 --- a/crates/torii/client/src/contract/mod.rs +++ b/crates/torii/client/src/contract/mod.rs @@ -1,3 +1,2 @@ pub mod component; -pub mod system; pub mod world; diff --git a/crates/torii/client/src/contract/system.rs b/crates/torii/client/src/contract/system.rs deleted file mode 100644 index 9717beb8a5..0000000000 --- a/crates/torii/client/src/contract/system.rs +++ /dev/null @@ -1,179 +0,0 @@ -use dojo_types::system::Dependency; -use starknet::accounts::ConnectedAccount; -use starknet::core::types::{BlockId, FieldElement, FunctionCall, InvokeTransactionResult}; -use starknet::core::utils::{ - cairo_short_string_to_felt, get_selector_from_name, parse_cairo_short_string, - CairoShortStringToFeltError, ParseCairoShortStringError, -}; -use starknet::providers::{Provider, ProviderError}; - -use crate::contract::world::{ - ContractReaderError, WorldContract, WorldContractError, WorldContractReader, -}; - -#[cfg(test)] -#[path = "system_test.rs"] -mod test; - -#[derive(Debug, thiserror::Error)] -pub enum SystemError { - #[error(transparent)] - WorldError(WorldContractError), - #[error(transparent)] - ProviderError(ProviderError

), - #[error(transparent)] - ParseCairoShortStringError(ParseCairoShortStringError), - #[error(transparent)] - CairoShortStringToFeltError(CairoShortStringToFeltError), - #[error(transparent)] - ContractReaderError(ContractReaderError

), - #[error(transparent)] - ReaderError(SystemReaderError

), -} - -pub struct System<'a, A: ConnectedAccount + Sync> { - world: &'a WorldContract<'a, A>, - reader: SystemReader<'a, A::Provider>, - name: String, -} - -impl<'a, A: ConnectedAccount + Sync> System<'a, A> { - pub async fn new( - world: &'a WorldContract<'a, A>, - name: String, - block_id: BlockId, - ) -> Result, SystemError::Error>> { - Ok(Self { - name: name.clone(), - world, - reader: SystemReader::new(&world.reader, name, block_id) - .await - .map_err(SystemError::ReaderError)?, - }) - } - - pub fn class_hash(&self) -> FieldElement { - self.reader.class_hash - } - - pub async fn call( - &self, - calldata: Vec, - block_id: BlockId, - ) -> Result, SystemError::Error>> - { - self.reader.call(calldata, block_id).await.map_err(SystemError::ReaderError) - } - - pub async fn execute( - &self, - calldata: Vec, - ) -> Result::Error>> - { - let res = - self.world.execute(&self.name, calldata).await.map_err(SystemError::WorldError)?; - - Ok(res) - } -} - -#[derive(Debug, thiserror::Error)] -pub enum SystemReaderError

{ - #[error(transparent)] - ProviderError(ProviderError

), - #[error(transparent)] - ParseCairoShortStringError(ParseCairoShortStringError), - #[error(transparent)] - CairoShortStringToFeltError(CairoShortStringToFeltError), - #[error(transparent)] - ContractReaderError(ContractReaderError

), - #[error("Invalid dependency length")] - InvalidDependencyLength, -} - -pub struct SystemReader<'a, P: Provider + Sync> { - world: &'a WorldContractReader<'a, P>, - name: String, - class_hash: FieldElement, -} - -impl<'a, P: Provider + Sync> SystemReader<'a, P> { - pub async fn new( - world: &'a WorldContractReader<'a, P>, - name: String, - block_id: BlockId, - ) -> Result, SystemReaderError> { - let res = world - .provider - .call( - FunctionCall { - contract_address: world.address, - calldata: vec![ - cairo_short_string_to_felt(&name) - .map_err(SystemReaderError::CairoShortStringToFeltError)?, - ], - entry_point_selector: get_selector_from_name("system").unwrap(), - }, - block_id, - ) - .await - .map_err(SystemReaderError::ProviderError)?; - - Ok(Self { name, world, class_hash: res[0] }) - } - - pub fn class_hash(&self) -> FieldElement { - self.class_hash - } - - pub async fn call( - &self, - mut calldata: Vec, - block_id: BlockId, - ) -> Result, SystemReaderError> { - calldata.insert(0, (calldata.len() as u64).into()); - - let res = self - .world - .call(&self.name, calldata, block_id) - .await - .map_err(SystemReaderError::ContractReaderError)?; - - Ok(res) - } - - pub async fn dependencies( - &self, - block_id: BlockId, - ) -> Result, SystemReaderError> { - let entrypoint = get_selector_from_name("dependencies").unwrap(); - - let res = self - .world - .call( - "library_call", - vec![FieldElement::THREE, self.class_hash, entrypoint, FieldElement::ZERO], - block_id, - ) - .await - .map_err(SystemReaderError::ContractReaderError)?; - - let mut dependencies = vec![]; - for chunk in res[3..].chunks(2) { - if chunk.len() != 2 { - return Err(SystemReaderError::InvalidDependencyLength); - } - - let is_write: bool = chunk[1] == FieldElement::ONE; - - dependencies.push(Dependency { - name: parse_cairo_short_string(&chunk[0]) - .map_err(SystemReaderError::ParseCairoShortStringError)?, - read: !is_write, - write: is_write, - }); - } - - Ok(dependencies) - } -} diff --git a/crates/torii/client/src/contract/system_test.rs b/crates/torii/client/src/contract/system_test.rs deleted file mode 100644 index 93ec05a141..0000000000 --- a/crates/torii/client/src/contract/system_test.rs +++ /dev/null @@ -1,58 +0,0 @@ -// use std::time::Duration; - -// use camino::Utf8PathBuf; -// use dojo_test_utils::sequencer::{ -// get_default_test_starknet_config, SequencerConfig, TestSequencer, -// }; -// use starknet::accounts::Account; -// use starknet::core::types::{BlockId, BlockTag}; -// use starknet_crypto::FieldElement; - -// use crate::contract::world::test::deploy_world; -// use crate::contract::world::WorldContract; - -// #[tokio::test(flavor = "multi_thread")] -// async fn test_system() { -// let sequencer = -// TestSequencer::start(SequencerConfig::default(), -// get_default_test_starknet_config()).await; let account = sequencer.account(); -// let (world_address, _) = deploy_world( -// &sequencer, -// Utf8PathBuf::from_path_buf("../../../examples/ecs/target/dev".into()).unwrap(), -// ) -// .await; - -// let block_id: BlockId = BlockId::Tag(BlockTag::Latest); -// let world = WorldContract::new(world_address, &account); -// let spawn = world.system("spawn", block_id).await.unwrap(); - -// let _ = spawn.execute(vec![]).await.unwrap(); - -// // wait for the tx to be mined -// tokio::time::sleep(Duration::from_millis(250)).await; - -// let component = world.component("Moves", block_id).await.unwrap(); -// let moves = component.entity(vec![account.address()], block_id).await.unwrap(); - -// assert_eq!(moves, vec![10_u8.into(), FieldElement::ZERO]); - -// let move_system = world.system("move", block_id).await.unwrap(); - -// let _ = move_system.execute(vec![FieldElement::ONE]).await.unwrap(); -// // wait for the tx to be mined -// tokio::time::sleep(Duration::from_millis(250)).await; - -// let _ = move_system.execute(vec![FieldElement::THREE]).await.unwrap(); -// // wait for the tx to be mined -// tokio::time::sleep(Duration::from_millis(250)).await; - -// let moves = component.entity(vec![account.address()], block_id).await.unwrap(); - -// assert_eq!(moves, vec![8_u8.into(), FieldElement::THREE]); - -// let position_component = world.component("Position", block_id).await.unwrap(); - -// let position = position_component.entity(vec![account.address()], block_id).await.unwrap(); - -// assert_eq!(position, vec![9_u8.into(), 9_u8.into()]); -// } diff --git a/crates/torii/client/src/contract/world.rs b/crates/torii/client/src/contract/world.rs index 7c48e3919e..a1a68ee1f5 100644 --- a/crates/torii/client/src/contract/world.rs +++ b/crates/torii/client/src/contract/world.rs @@ -8,7 +8,6 @@ use starknet::core::utils::{ use starknet::providers::{Provider, ProviderError}; use crate::contract::component::{ComponentError, ComponentReader}; -use crate::contract::system::{System, SystemError, SystemReader, SystemReaderError}; #[cfg(test)] #[path = "world_test.rs"] @@ -100,31 +99,6 @@ impl<'a, A: ConnectedAccount + Sync> WorldContract<'a, A> { self.account.execute(calls).send().await } - pub async fn execute( - &self, - name: &str, - mut calldata: Vec, - ) -> Result< - InvokeTransactionResult, - WorldContractError::Error>, - > { - calldata.insert(0, (calldata.len() as u64).into()); - calldata.insert( - 0, - cairo_short_string_to_felt(name) - .map_err(WorldContractError::CairoShortStringToFeltError)?, - ); - self.account - .execute(vec![Call { - calldata, - to: self.address, - selector: get_selector_from_name("execute").unwrap(), - }]) - .send() - .await - .map_err(WorldContractError::AccountError) - } - pub async fn executor( &self, block_id: BlockId, @@ -149,14 +123,6 @@ impl<'a, A: ConnectedAccount + Sync> WorldContract<'a, A> { { self.reader.component(name, block_id).await } - - pub async fn system( - &'a self, - name: &str, - block_id: BlockId, - ) -> Result, SystemError::Error>> { - System::new(self, name.to_string(), block_id).await - } } #[derive(Debug, thiserror::Error)] @@ -300,12 +266,4 @@ impl<'a, P: Provider + Sync> WorldContractReader<'a, P> { ) -> Result, ComponentError> { ComponentReader::new(self, name.to_string(), block_id).await } - - pub async fn system( - &'a self, - name: &str, - block_id: BlockId, - ) -> Result, SystemReaderError> { - SystemReader::new(self, name.to_string(), block_id).await - } } diff --git a/crates/torii/client/src/provider/jsonrpc.rs b/crates/torii/client/src/provider/jsonrpc.rs index 67ccdab552..e71290d633 100644 --- a/crates/torii/client/src/provider/jsonrpc.rs +++ b/crates/torii/client/src/provider/jsonrpc.rs @@ -6,13 +6,10 @@ use starknet_crypto::FieldElement; use super::Provider; use crate::contract::component::ComponentError; -use crate::contract::system::SystemReaderError; use crate::contract::world::WorldContractReader; #[derive(Debug, thiserror::Error)] pub enum JsonRpcProviderError

{ - #[error(transparent)] - SystemReader(SystemReaderError

), #[error(transparent)] ComponetReader(ComponentError

), } @@ -48,16 +45,6 @@ where { type Error = JsonRpcProviderError>; - async fn system(&self, name: &str) -> Result { - let world = self.world(); - let class_hash = world - .system(name, self.block_id) - .await - .map_err(JsonRpcProviderError::SystemReader)? - .class_hash(); - Ok(class_hash) - } - async fn component(&self, name: &str) -> Result { let world = self.world(); let class_hash = world diff --git a/crates/torii/client/src/provider/mod.rs b/crates/torii/client/src/provider/mod.rs index 26fd8eab12..e7ffa683d6 100644 --- a/crates/torii/client/src/provider/mod.rs +++ b/crates/torii/client/src/provider/mod.rs @@ -13,9 +13,6 @@ pub trait Provider { /// Get the class hash of a component. async fn component(&self, name: &str) -> Result; - /// Get the class hash of a system. - async fn system(&self, name: &str) -> Result; - /// Get the component values of an entity. async fn entity( &self,