Skip to content

Commit

Permalink
feat: propose multicall format for auth writer
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Feb 19, 2024
1 parent 504b83e commit 1ffcc16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
13 changes: 7 additions & 6 deletions bin/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String>,

Check warning on line 28 in bin/sozo/src/commands/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/auth.rs#L28

Added line #L28 was not covered by tests

#[command(flatten)]
world: WorldOptions,
Expand Down
30 changes: 27 additions & 3 deletions bin/sozo/src/ops/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Environment>) -> Result<()> {
match command {
AuthCommand::Writer { model, contract, world, starknet, account, transaction } => {
AuthCommand::Writer { models_contracts, world, starknet, account, transaction } => {

Check warning on line 13 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L13

Added line #L13 was not covered by tests
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![];

Check warning on line 20 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L20

Added line #L20 was not covered by tests

for mc in models_contracts {
let parts: Vec<&str> = mc.split(',').collect();

Check warning on line 23 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L22-L23

Added lines #L22 - L23 were not covered by tests

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`"
),

Check warning on line 30 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L25-L30

Added lines #L25 - L30 were not covered by tests
};

let contract = FieldElement::from_hex_be(contract_part)
.map_err(|_| anyhow::anyhow!("Invalid contract address: {}", contract_part))?;

Check warning on line 34 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L33-L34

Added lines #L33 - L34 were not covered by tests

calls.push(
world
.grant_writer_getcall(&cairo_utils::str_to_felt(&model)?, &contract.into()),

Check warning on line 38 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L36-L38

Added lines #L36 - L38 were not covered by tests
);
}

let res = account
.execute(calls)

Check warning on line 43 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
.send()
.await
.with_context(|| "Failed to send transaction")?;
Expand Down

0 comments on commit 1ffcc16

Please sign in to comment.