Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sozo): support arrays in calldata arguments #2917

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines +62 to 64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle missing contract address when descriptor is a name

Ohayo, sensei! In the match statement for contract_address, the ResourceDescriptor::Name(_) arm is unimplemented. To improve the user experience, consider returning a meaningful error message or implementing this case.

Apply this diff to handle the case:

 ResourceDescriptor::Name(name) => {
-    unimplemented!("Expected to be a resolved tag with default namespace.")
+    return Err(anyhow!("Contract name '{}' could not be resolved to an address.", name));
 }

Committable suggestion skipped: line range outside the PR's diff.

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
Loading