Skip to content

Commit

Permalink
feat(sozo): support arrays in calldata arguments (#2917)
Browse files Browse the repository at this point in the history
* sozo: support arrays in calldata arguments

* fix: return error if the prefix is unknown

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
remybar and glihm authored Jan 18, 2025
1 parent 8d735a2 commit 30e574c
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 67 deletions.
23 changes: 6 additions & 17 deletions bin/sozo/src/commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing::trace;

use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
use crate::utils;
use crate::utils::{self, CALLDATA_DOC};

#[derive(Debug, Args)]
#[command(about = "Call a system with the given calldata.")]
Expand All @@ -25,17 +25,10 @@ pub struct CallArgs {
#[arg(help = "The name of the entrypoint to call.")]
pub entrypoint: String,

#[arg(short, long)]
#[arg(value_delimiter = ',')]
#[arg(help = "The calldata to be passed to the entrypoint. Comma separated values e.g., \
0x12345,128,u256:9999999999. Sozo supports some prefixes that you can use to \
automatically parse some types. The supported prefixes are:
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
- str: A cairo string (ByteArray).
- int: A signed integer.
- no prefix: A cairo felt or any type that fit into one felt.")]
pub calldata: Option<String>,
#[arg(num_args = 0..)]
#[arg(help = format!("The calldata to be passed to the system.
{CALLDATA_DOC}"))]
pub calldata: Vec<String>,

#[arg(short, long)]
#[arg(help = "The block ID (could be a hash, a number, 'pending' or 'latest')")]
Expand Down Expand Up @@ -66,11 +59,7 @@ impl CallArgs {
config.tokio_handle().block_on(async {
let local_manifest = ws.read_manifest_profile()?;

let calldata = if let Some(cd) = self.calldata {
calldata_decoder::decode_calldata(&cd)?
} else {
vec![]
};
let calldata = calldata_decoder::decode_calldata(&self.calldata)?;

let contract_address = match &descriptor {
ResourceDescriptor::Address(address) => Some(*address),
Expand Down
18 changes: 5 additions & 13 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use super::options::world::WorldOptions;
use crate::utils;
use crate::utils::{self, CALLDATA_DOC};

#[derive(Debug, Args)]
#[command(about = "Execute one or several systems with the given calldata.")]
pub struct ExecuteArgs {
#[arg(num_args = 1..)]
#[arg(required = true)]
#[arg(help = "A list of calls to execute, separated by a /.
#[arg(help = format!("A list of calls to execute, separated by a /.
A call is made up of a <TAG_OR_ADDRESS>, an <ENTRYPOINT> and an optional <CALLDATA>:
Expand All @@ -31,23 +31,15 @@ A call is made up of a <TAG_OR_ADDRESS>, an <ENTRYPOINT> and an optional <CALLDA
- <ENTRYPOINT>: the name of the entry point to be called,
- <CALLDATA>: the calldata to be passed to the system.
Space separated values e.g., 0x12345 128 u256:9999999999.
Sozo supports some prefixes that you can use to automatically parse some types. The supported \
prefixes are:
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
- str: A cairo string (ByteArray).
- int: A signed integer.
- no prefix: A cairo felt or any type that fit into one felt.
- <CALLDATA>: the calldata to be passed to the system.
{CALLDATA_DOC}
EXAMPLE
sozo execute 0x1234 run / ns-Actions move 1 2
Executes the run function of the contract at the address 0x1234 without calldata,
and the move function of the ns-Actions contract, with the calldata [1,2].")]
and the move function of the ns-Actions contract, with the calldata [1,2]."))]
pub calls: Vec<String>,

#[arg(long)]
Expand Down
16 changes: 16 additions & 0 deletions bin/sozo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ use crate::commands::options::starknet::StarknetOptions;
use crate::commands::options::world::WorldOptions;
use crate::commands::LOG_TARGET;

pub const CALLDATA_DOC: &str = "
Space separated values e.g., 0x12345 128 u256:9999999999 str:'hello world'.
Sozo supports some prefixes that you can use to automatically parse some types. The supported \
prefixes are:
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
If the string contains spaces it must be between quotes (ex: sstr:'hello world')
- str: A cairo string (ByteArray).
If the string contains spaces it must be between quotes (ex: sstr:'hello world')
- int: A signed integer.
- arr: A dynamic array where each item fits on a single felt252.
- u256arr: A dynamic array of u256.
- farr: A fixed-size array where each item fits on a single felt252.
- u256farr: A fixed-size array of u256.
- no prefix: A cairo felt or any type that fit into one felt.";

/// Computes the world address based on the provided options.
pub fn get_world_address(
profile_config: &ProfileConfig,
Expand Down
Loading

0 comments on commit 30e574c

Please sign in to comment.