Skip to content

Commit

Permalink
feat: add prefixes for sozo call and better output message (#2366)
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm authored Aug 29, 2024
1 parent 1d32ab1 commit fea03f4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
21 changes: 17 additions & 4 deletions bin/sozo/src/commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use clap::Args;
use dojo_world::contracts::naming::ensure_namespace;
use dojo_world::metadata::get_default_namespace_from_ws;
use scarb::core::Config;
use starknet::core::types::Felt;
use tracing::trace;

use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
use crate::commands::calldata_decoder;
use crate::utils;

#[derive(Debug, Args)]
Expand All @@ -22,8 +22,14 @@ pub struct CallArgs {
#[arg(short, long)]
#[arg(value_delimiter = ',')]
#[arg(help = "The calldata to be passed to the entrypoint. Comma separated values e.g., \
0x12345,0x69420.")]
pub calldata: Vec<Felt>,
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(short, long)]
#[arg(help = "The block ID (could be a hash, a number, 'pending' or 'latest')")]
Expand Down Expand Up @@ -57,11 +63,18 @@ impl CallArgs {
.await
.unwrap();

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

sozo_ops::call::call(
&config.ui(),
world_reader,
tag_or_address,
self.entrypoint,
self.calldata,
calldata,
self.block_id,
)
.await
Expand Down
26 changes: 21 additions & 5 deletions crates/sozo/ops/src/call.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use anyhow::{Context, Result};
use anyhow::Result;
use dojo_world::contracts::WorldContractReader;
use scarb_ui::Ui;
use starknet::core::types::{BlockId, BlockTag, Felt, FunctionCall};
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;

use crate::migration::ui::MigrationUi;
use crate::utils::{get_contract_address_from_reader, parse_block_id};

pub async fn call<P: Provider + Sync + Send>(
ui: &Ui,
world_reader: WorldContractReader<P>,
tag_or_address: String,
entrypoint: String,
Expand All @@ -20,7 +23,7 @@ pub async fn call<P: Provider + Sync + Send>(
BlockId::Tag(BlockTag::Pending)
};

let output = world_reader
let res = world_reader
.provider()
.call(
FunctionCall {
Expand All @@ -30,10 +33,23 @@ pub async fn call<P: Provider + Sync + Send>(
},
block_id,
)
.await
.with_context(|| format!("Failed to call {entrypoint}"))?;
.await;

println!("[ {} ]", output.iter().map(|o| format!("0x{:x}", o)).collect::<Vec<_>>().join(" "));
match res {
Ok(output) => {
println!(
"[ {} ]",
output.iter().map(|o| format!("0x{:x}", o)).collect::<Vec<_>>().join(" ")
);
}
Err(e) => {
ui.print_hidden_sub(format!("{:?}", e));
anyhow::bail!(format!(
"Error calling entrypoint `{}` on address: {:#066x}",
entrypoint, contract_address
));
}
}

Ok(())
}
30 changes: 27 additions & 3 deletions crates/sozo/ops/src/tests/call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dojo_world::contracts::WorldContractReader;
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use scarb_ui::Ui;
use starknet::accounts::SingleOwnerAccount;
use starknet::core::types::Felt;
use starknet::providers::jsonrpc::HttpTransport;
Expand All @@ -21,8 +22,11 @@ async fn call_with_bad_address() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

assert!(
call::call(
&ui,
world_reader,
"0xBadCoffeeBadCode".to_string(),
ENTRYPOINT.to_string(),
Expand All @@ -43,8 +47,11 @@ async fn call_with_bad_name() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

assert!(
call::call(
&ui,
world_reader,
"BadName".to_string(),
ENTRYPOINT.to_string(),
Expand All @@ -65,8 +72,11 @@ async fn call_with_bad_entrypoint() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

assert!(
call::call(
&ui,
world_reader,
CONTRACT_TAG.to_string(),
"BadEntryPoint".to_string(),
Expand All @@ -87,8 +97,11 @@ async fn call_with_bad_calldata() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

assert!(
call::call(
&ui,
world_reader,
CONTRACT_TAG.to_string(),
ENTRYPOINT.to_string(),
Expand All @@ -109,9 +122,17 @@ async fn call_with_contract_name() {
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);

let r =
call::call(world_reader, CONTRACT_TAG.to_string(), ENTRYPOINT.to_string(), vec![], None)
.await;
let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

let r = call::call(
&ui,
world_reader,
CONTRACT_TAG.to_string(),
ENTRYPOINT.to_string(),
vec![],
None,
)
.await;

assert!(r.is_ok());
}
Expand All @@ -121,6 +142,8 @@ async fn call_with_contract_address() {
let config = KatanaRunnerConfig::default().with_db_dir("/tmp/spawn-and-move-db");
let sequencer = KatanaRunner::new_with_config(config).expect("Failed to start runner.");

let ui = Ui::new(scarb_ui::Verbosity::Verbose, scarb_ui::OutputFormat::Text);

let world = setup::setup_with_world(&sequencer).await.unwrap();
let provider = sequencer.provider();
let world_reader = WorldContractReader::new(world.address, provider);
Expand All @@ -133,6 +156,7 @@ async fn call_with_contract_address() {

assert!(
call::call(
&ui,
world_reader,
format!("{:#x}", contract_address),
ENTRYPOINT.to_string(),
Expand Down

0 comments on commit fea03f4

Please sign in to comment.