From 716520e953fc1e44fa22bfea19a73fd8b2fb5ccc Mon Sep 17 00:00:00 2001 From: glihm Date: Mon, 19 Feb 2024 19:55:55 -0600 Subject: [PATCH] feat(sozo): propose multicall format for `sozo auth writer` (#1555) feat: propose multicall format for auth writer --- bin/sozo/src/commands/auth.rs | 13 +++++++------ bin/sozo/src/ops/auth.rs | 30 +++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/bin/sozo/src/commands/auth.rs b/bin/sozo/src/commands/auth.rs index 76b45137c7..98b9587401 100644 --- a/bin/sozo/src/commands/auth.rs +++ b/bin/sozo/src/commands/auth.rs @@ -2,7 +2,6 @@ use anyhow::Result; use clap::{Args, Subcommand}; use dojo_world::metadata::dojo_metadata_from_workspace; use scarb::core::Config; -use starknet::core::types::FieldElement; use super::options::account::AccountOptions; use super::options::starknet::StarknetOptions; @@ -20,11 +19,13 @@ pub struct AuthArgs { pub enum AuthCommand { #[command(about = "Auth a system with the given calldata.")] Writer { - #[arg(help = "Name of the model to grant write access to.")] - model: String, - - #[arg(help = "Address of the contract to grant writer access to.")] - contract: FieldElement, + #[arg(num_args = 1..)] + #[arg(required = true)] + #[arg(value_name = "model,contract_address")] + #[arg(help = "A list of models and contract address to grant write access to. Comma \ + separated values to indicate model name and contract address e.g. \ + model_name,0x1234 model_name,0x1111 ")] + models_contracts: Vec, #[command(flatten)] world: WorldOptions, diff --git a/bin/sozo/src/ops/auth.rs b/bin/sozo/src/ops/auth.rs index 369313ee30..222f96a7ce 100644 --- a/bin/sozo/src/ops/auth.rs +++ b/bin/sozo/src/ops/auth.rs @@ -3,20 +3,44 @@ use dojo_world::contracts::cairo_utils; use dojo_world::contracts::world::WorldContract; use dojo_world::metadata::Environment; use dojo_world::utils::TransactionWaiter; +use starknet::accounts::Account; +use starknet::core::types::FieldElement; use crate::commands::auth::AuthCommand; pub async fn execute(command: AuthCommand, env_metadata: Option) -> Result<()> { match command { - AuthCommand::Writer { model, contract, world, starknet, account, transaction } => { + AuthCommand::Writer { models_contracts, world, starknet, account, transaction } => { 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 - .grant_writer(&cairo_utils::str_to_felt(&model)?, &contract.into()) + let mut calls = vec![]; + + for mc in models_contracts { + let parts: Vec<&str> = mc.split(',').collect(); + + let (model, contract_part) = match parts.as_slice() { + [model, contract] => (model.to_string(), *contract), + _ => anyhow::bail!( + "Model and contract address are expected to be comma separated: `sozo \ + auth writer model_name,0x1234`" + ), + }; + + let contract = FieldElement::from_hex_be(contract_part) + .map_err(|_| anyhow::anyhow!("Invalid contract address: {}", contract_part))?; + + calls.push( + world + .grant_writer_getcall(&cairo_utils::str_to_felt(&model)?, &contract.into()), + ); + } + + let res = account + .execute(calls) .send() .await .with_context(|| "Failed to send transaction")?;