Skip to content

Commit 3533756

Browse files
committed
feat(sozo): add invoke command that doesn't require the world context
1 parent 9b36053 commit 3533756

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

bin/sozo/src/commands/execute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::options::world::WorldOptions;
1919
use crate::utils::{self, CALLDATA_DOC};
2020

2121
#[derive(Debug, Args)]
22-
#[command(about = "Execute one or several systems with the given calldata.")]
22+
#[command(about = "Execute one or several systems in the world context with the given calldata.")]
2323
pub struct ExecuteArgs {
2424
#[arg(num_args = 1..)]
2525
#[arg(required = true)]

bin/sozo/src/commands/invoke.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use anyhow::{Context, Result};
2+
use clap::Args;
3+
use dojo_utils::{Invoker, TxnConfig};
4+
use dojo_world::config::calldata_decoder;
5+
use sozo_ui::SozoUi;
6+
use starknet::core::types::{Call, Felt};
7+
use starknet::core::utils::get_selector_from_name;
8+
use tracing::trace;
9+
10+
use super::options::account::AccountOptions;
11+
use super::options::starknet::StarknetOptions;
12+
use super::options::transaction::TransactionOptions;
13+
use crate::utils::{get_account_from_env, CALLDATA_DOC};
14+
15+
#[derive(Debug, Args)]
16+
#[command(about = "Invoke a contract entrypoint on Starknet. This command does not require the \
17+
world context to be loaded. Use the execute command to execute systems in the \
18+
world context.")]
19+
pub struct InvokeArgs {
20+
#[arg(value_name = "CONTRACT_ADDRESS", help = "Target contract address.")]
21+
pub contract: Felt,
22+
23+
#[arg(value_name = "ENTRYPOINT", help = "Entrypoint to invoke on the contract.")]
24+
pub entrypoint: String,
25+
26+
#[arg(
27+
value_name = "ARG",
28+
num_args = 0..,
29+
help = format!(
30+
"Calldata elements for the entrypoint (space separated).\n\n{}",
31+
CALLDATA_DOC
32+
)
33+
)]
34+
pub calldata: Vec<String>,
35+
36+
#[command(flatten)]
37+
pub transaction: TransactionOptions,
38+
39+
#[command(flatten)]
40+
pub starknet: StarknetOptions,
41+
42+
#[command(flatten)]
43+
pub account: AccountOptions,
44+
}
45+
46+
impl InvokeArgs {
47+
pub async fn run(self, ui: &SozoUi) -> Result<()> {
48+
trace!(args = ?self);
49+
50+
let InvokeArgs { contract, entrypoint, calldata, transaction, starknet, account } = self;
51+
52+
let account = get_account_from_env(account, &starknet).await?;
53+
let txn_config: TxnConfig = transaction.try_into()?;
54+
55+
ui.title(format!("Invoke contract {:#066x}", contract));
56+
ui.step(format!("Entrypoint: {}", entrypoint));
57+
58+
let mut decoded = Vec::new();
59+
for item in calldata {
60+
let felt_values = calldata_decoder::decode_single_calldata(&item)
61+
.with_context(|| format!("Failed to parse calldata argument `{item}`"))?;
62+
decoded.extend(felt_values);
63+
}
64+
65+
if decoded.is_empty() {
66+
ui.verbose("Calldata: <empty>");
67+
} else {
68+
ui.verbose(format!("Calldata ({} felt(s))", decoded.len()));
69+
}
70+
71+
let selector = get_selector_from_name(&entrypoint)?;
72+
let call = Call { to: contract, selector, calldata: decoded };
73+
74+
let invoker = Invoker::new(account, txn_config);
75+
let result = invoker.invoke(call).await?;
76+
77+
match result {
78+
dojo_utils::TransactionResult::Noop => {
79+
ui.result("Nothing to invoke (noop).");
80+
}
81+
dojo_utils::TransactionResult::Hash(hash) => {
82+
ui.result(format!("Invocation sent.\n Tx hash : {hash:#066x}"));
83+
}
84+
dojo_utils::TransactionResult::HashReceipt(hash, receipt) => {
85+
ui.result(format!("Invocation included on-chain.\n Tx hash : {hash:#066x}"));
86+
ui.debug(format!("Receipt: {:?}", receipt));
87+
}
88+
}
89+
90+
Ok(())
91+
}
92+
}

bin/sozo/src/commands/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) mod execute;
1818
pub(crate) mod hash;
1919
pub(crate) mod init;
2020
pub(crate) mod inspect;
21+
pub(crate) mod invoke;
2122
pub(crate) mod mcp;
2223
pub(crate) mod migrate;
2324
pub(crate) mod model;
@@ -37,6 +38,7 @@ use execute::ExecuteArgs;
3738
use hash::HashArgs;
3839
use init::InitArgs;
3940
use inspect::InspectArgs;
41+
use invoke::InvokeArgs;
4042
use mcp::McpArgs;
4143
use migrate::MigrateArgs;
4244
use model::ModelArgs;
@@ -59,6 +61,8 @@ pub enum Commands {
5961
Events(Box<EventsArgs>),
6062
#[command(about = "Execute one or several systems with the given calldata.")]
6163
Execute(Box<ExecuteArgs>),
64+
#[command(about = "Invoke a contract entrypoint on Starknet.")]
65+
Invoke(Box<InvokeArgs>),
6266
#[command(about = "Clean the build directory")]
6367
Clean(Box<CleanArgs>),
6468
#[command(about = "Computes hash with different hash functions")]
@@ -98,6 +102,7 @@ impl fmt::Display for Commands {
98102
Commands::Events(_) => write!(f, "Events"),
99103
Commands::Execute(_) => write!(f, "Execute"),
100104
Commands::Hash(_) => write!(f, "Hash"),
105+
Commands::Invoke(_) => write!(f, "Invoke"),
101106
Commands::Declare(_) => write!(f, "Declare"),
102107
Commands::Deploy(_) => write!(f, "Deploy"),
103108
Commands::Init(_) => write!(f, "Init"),
@@ -126,6 +131,7 @@ pub async fn run(command: Commands, scarb_metadata: &Metadata, ui: &SozoUi) -> R
126131
Commands::Clean(args) => args.run(scarb_metadata),
127132
Commands::Events(args) => args.run(scarb_metadata, ui).await,
128133
Commands::Execute(args) => args.run(scarb_metadata, ui).await,
134+
Commands::Invoke(args) => args.run(ui).await,
129135
Commands::Hash(args) => args.run(scarb_metadata),
130136
Commands::Declare(args) => args.run(ui).await,
131137
Commands::Deploy(args) => args.run(ui).await,

bin/sozo/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ async fn cli_main(args: SozoArgs, ui: &SozoUi) -> Result<()> {
4747
args.run(ui).await
4848
} else if let Commands::Deploy(args) = args.command {
4949
args.run(ui).await
50+
} else if let Commands::Invoke(args) = args.command {
51+
args.run(ui).await
5052
} else {
5153
// Default to the current directory to mimic how Scarb works.
5254
let manifest_path = if let Some(manifest_path) = &args.manifest_path {

0 commit comments

Comments
 (0)