|
| 1 | +use anyhow::{anyhow, bail, 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( |
| 21 | + num_args = 1.., |
| 22 | + required = true, |
| 23 | + help = format!( |
| 24 | + "Calls to invoke, separated by '/'. \ |
| 25 | + Each call follows the format <CONTRACT> <ENTRYPOINT> [CALLDATA...]\n\n{}", |
| 26 | + CALLDATA_DOC |
| 27 | + ) |
| 28 | + )] |
| 29 | + pub calls: Vec<String>, |
| 30 | + |
| 31 | + #[command(flatten)] |
| 32 | + pub transaction: TransactionOptions, |
| 33 | + |
| 34 | + #[command(flatten)] |
| 35 | + pub starknet: StarknetOptions, |
| 36 | + |
| 37 | + #[arg(long, default_value = "0x0", help = "Selector for the entrypoint in felt form.")] |
| 38 | + pub selector: Option<Felt>, |
| 39 | + |
| 40 | + #[command(flatten)] |
| 41 | + #[command(next_help_heading = "Account options")] |
| 42 | + pub account: AccountOptions, |
| 43 | +} |
| 44 | + |
| 45 | +impl InvokeArgs { |
| 46 | + pub async fn run(self, ui: &SozoUi) -> Result<()> { |
| 47 | + trace!(args = ?self); |
| 48 | + |
| 49 | + let account = get_account_from_env(self.account, &self.starknet).await?; |
| 50 | + let txn_config: TxnConfig = self.transaction.try_into()?; |
| 51 | + let mut invoker = Invoker::new(account, txn_config); |
| 52 | + |
| 53 | + let mut calls_iter = self.calls.into_iter(); |
| 54 | + let mut call_index = 0usize; |
| 55 | + |
| 56 | + while let Some(target) = calls_iter.next() { |
| 57 | + if matches!(target.as_str(), "/" | "-" | "\\") { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + let entrypoint = calls_iter.next().ok_or_else(|| { |
| 62 | + anyhow!( |
| 63 | + "Missing entrypoint for target `{target}`. Provide calls as `<CONTRACT> \ |
| 64 | + <ENTRYPOINT> [CALLDATA...]`." |
| 65 | + ) |
| 66 | + })?; |
| 67 | + |
| 68 | + let contract_address = parse_contract_address(&target)?; |
| 69 | + let selector = get_selector_from_name(&entrypoint)?; |
| 70 | + |
| 71 | + let mut calldata = Vec::new(); |
| 72 | + for arg in calls_iter.by_ref() { |
| 73 | + match arg.as_str() { |
| 74 | + "/" | "-" | "\\" => break, |
| 75 | + _ => { |
| 76 | + let felts = |
| 77 | + calldata_decoder::decode_single_calldata(&arg).with_context(|| { |
| 78 | + format!("Failed to parse calldata argument `{arg}`") |
| 79 | + })?; |
| 80 | + calldata.extend(felts); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + call_index += 1; |
| 86 | + ui.step(format!("Call #{call_index}: {entrypoint} @ {:#066x}", contract_address)); |
| 87 | + if calldata.is_empty() { |
| 88 | + ui.verbose(" Calldata: <empty>"); |
| 89 | + } else { |
| 90 | + ui.verbose(format!(" Calldata ({} felt(s))", calldata.len())); |
| 91 | + } |
| 92 | + |
| 93 | + invoker.add_call(Call { to: contract_address, selector, calldata }); |
| 94 | + } |
| 95 | + |
| 96 | + if invoker.calls.is_empty() { |
| 97 | + bail!("No calls provided to invoke."); |
| 98 | + } |
| 99 | + |
| 100 | + let results = invoker.multicall().await?; |
| 101 | + |
| 102 | + for (idx, result) in results.iter().enumerate() { |
| 103 | + let display_idx = idx + 1; |
| 104 | + match result { |
| 105 | + dojo_utils::TransactionResult::Noop => { |
| 106 | + ui.result(format!("Call #{display_idx} noop (no transaction sent).")); |
| 107 | + } |
| 108 | + dojo_utils::TransactionResult::Hash(hash) => { |
| 109 | + ui.result(format!("Call #{display_idx} sent.\n Tx hash : {hash:#066x}")); |
| 110 | + } |
| 111 | + dojo_utils::TransactionResult::HashReceipt(hash, receipt) => { |
| 112 | + ui.result(format!("Call #{display_idx} included.\n Tx hash : {hash:#066x}")); |
| 113 | + ui.debug(format!("Receipt: {:?}", receipt)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +fn parse_contract_address(value: &str) -> Result<Felt> { |
| 123 | + if let Ok(felt) = Felt::from_hex(value) { |
| 124 | + return Ok(felt); |
| 125 | + } |
| 126 | + |
| 127 | + Felt::from_dec_str(value).map_err(|_| { |
| 128 | + anyhow!("Invalid contract address `{value}`. Use hex (0x...) or decimal form.") |
| 129 | + }) |
| 130 | +} |
0 commit comments