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: add prefixes for sozo call and better output message #2366

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 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 @@
#[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 @@
.await
.unwrap();

let calldata = if let Some(cd) = self.calldata {
calldata_decoder::decode_calldata(&cd)?

Check warning on line 67 in bin/sozo/src/commands/call.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/call.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
} else {
vec![]

Check warning on line 69 in bin/sozo/src/commands/call.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/call.rs#L69

Added line #L69 was not covered by tests
};

sozo_ops::call::call(
&config.ui(),

Check warning on line 73 in bin/sozo/src/commands/call.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/call.rs#L73

Added line #L73 was not covered by tests
world_reader,
tag_or_address,
self.entrypoint,
self.calldata,
calldata,

Check warning on line 77 in bin/sozo/src/commands/call.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/call.rs#L77

Added line #L77 was not covered by tests
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
Loading