Skip to content

Commit

Permalink
feat(sozo): add Cartridge's Controller-based account (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Jun 22, 2024
1 parent 0d3c0b0 commit 8a5902b
Show file tree
Hide file tree
Showing 11 changed files with 711 additions and 48 deletions.
350 changes: 330 additions & 20 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ alloy-sol-types = { version = "0.7.2", default-features = false }

criterion = "0.5.1"

# Controller integration
account_sdk = { git = "https://github.com/cartridge-gg/controller", rev = "e8276cfffe48b5d09f0598026d13635374204b72" }
slot = { git = "https://github.com/cartridge-gg/slot", rev = "1d0c9f7" }

[patch.crates-io]
cairo-felt = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }
cairo-vm = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }
Expand Down
9 changes: 8 additions & 1 deletion bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
account_sdk = { workspace = true, optional = true }
slot = { workspace = true, optional = true }

anyhow.workspace = true
async-trait.workspace = true
bigdecimal = "0.4.1"
Expand All @@ -25,11 +28,11 @@ clap-verbosity-flag.workspace = true
clap.workspace = true
clap_complete.workspace = true
console.workspace = true
derive_more.workspace = true
dojo-bindgen.workspace = true
dojo-lang.workspace = true
dojo-types.workspace = true
dojo-world = { workspace = true, features = [ "contracts", "metadata", "migration" ] }
derive_more.workspace = true
futures.workspace = true
hex = "0.4.3"
hex-literal = "0.4.1"
Expand Down Expand Up @@ -63,3 +66,7 @@ assert_fs.workspace = true
dojo-test-utils = { workspace = true, features = [ "build-examples" ] }
katana-runner.workspace = true
snapbox = "0.4.6"

[features]
controller = [ "dep:account_sdk", "dep:slot" ]
default = [ "controller" ]
11 changes: 9 additions & 2 deletions bin/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl AuthArgs {
env_metadata,
kind,
transaction,
config,
))
}
AuthCommand::Revoke { kind, world, starknet, account, transaction } => {
Expand All @@ -84,6 +85,7 @@ impl AuthArgs {
env_metadata,
kind,
transaction,
config,
))
}
}
Expand Down Expand Up @@ -115,6 +117,7 @@ pub enum AuthKind {
},
}

#[allow(clippy::too_many_arguments)]
pub async fn grant(
ui: &Ui,
world: WorldOptions,
Expand All @@ -123,10 +126,11 @@ pub async fn grant(
env_metadata: Option<Environment>,
kind: AuthKind,
transaction: TransactionOptions,
config: &Config,
) -> Result<()> {
trace!(?kind, ?world, ?starknet, ?account, ?transaction, "Executing Grant command.");
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();
utils::world_from_env_metadata(world, account, starknet, &env_metadata, config).await?;

match kind {
AuthKind::Writer { models_contracts } => {
Expand All @@ -146,6 +150,7 @@ pub async fn grant(
}
}

#[allow(clippy::too_many_arguments)]
pub async fn revoke(
ui: &Ui,
world: WorldOptions,
Expand All @@ -154,10 +159,12 @@ pub async fn revoke(
env_metadata: Option<Environment>,
kind: AuthKind,
transaction: TransactionOptions,
config: &Config,
) -> Result<()> {
trace!(?kind, ?world, ?starknet, ?account, ?transaction, "Executing Revoke command.");
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();
utils::world_from_env_metadata(world, account, starknet, &env_metadata, config).await?;

match kind {
AuthKind::Writer { models_contracts } => {
trace!(
Expand Down
5 changes: 3 additions & 2 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ impl ExecuteArgs {
self.account,
self.starknet,
&env_metadata,
config,
)
.await
.unwrap();
.await?;

let tx_config = self.transaction.into();

trace!(
Expand Down
22 changes: 12 additions & 10 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use dojo_world::migration::TxnConfig;
use katana_rpc_api::starknet::RPC_SPEC_VERSION;
use scarb::core::{Config, Workspace};
use sozo_ops::migration;
use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount};
use starknet::accounts::{Account, ConnectedAccount};
use starknet::core::types::{BlockId, BlockTag, FieldElement, StarknetError};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider, ProviderError};
use starknet::signers::LocalWallet;
use tracing::trace;

use super::options::account::AccountOptions;
use super::options::account::{AccountOptions, SozoAccount};
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use super::options::world::WorldOptions;
use crate::commands::options::account::WorldAddressOrName;

#[derive(Debug, Args)]
pub struct MigrateArgs {
Expand Down Expand Up @@ -156,11 +156,7 @@ pub async fn setup_env<'a>(
world: WorldOptions,
name: &str,
env: Option<&'a Environment>,
) -> Result<(
Option<FieldElement>,
SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>,
String,
)> {
) -> Result<(Option<FieldElement>, SozoAccount<JsonRpcClient<HttpTransport>>, String)> {
trace!("Setting up environment.");
let ui = ws.config().ui();

Expand Down Expand Up @@ -190,8 +186,14 @@ pub async fn setup_env<'a>(
.with_context(|| "Cannot parse chain_id as string")?;
trace!(chain_id);

let mut account = account.account(provider, env).await?;
account.set_block_id(BlockId::Tag(BlockTag::Pending));
let account = {
// This is mainly for controller account for creating policies.
let world_address_or_name = world_address
.map(WorldAddressOrName::Address)
.unwrap_or(WorldAddressOrName::Name(name.to_string()));

account.account(provider, world_address_or_name, &starknet, env, ws.config()).await?
};

let address = account.address();

Expand Down
Loading

0 comments on commit 8a5902b

Please sign in to comment.