|
| 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 | +} |
0 commit comments