forked from starkware-libs/blockifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sozo call command (starkware-libs#1704)
* typo * sozo: add call command add `sozo call` command to be able to directly call view functions without using `starkli`. * add sozo call tests * fix fmt * add block-id option to call command
- Loading branch information
Showing
12 changed files
with
429 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use anyhow::Result; | ||
use clap::Args; | ||
use scarb::core::Config; | ||
use starknet::core::types::FieldElement; | ||
|
||
use super::options::starknet::StarknetOptions; | ||
use super::options::world::WorldOptions; | ||
use crate::utils; | ||
|
||
#[derive(Debug, Args)] | ||
#[command(about = "Call a system with the given calldata.")] | ||
pub struct CallArgs { | ||
#[arg(help = "The address or the fully qualified name of the contract to call.")] | ||
pub contract: String, | ||
|
||
#[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,0x69420.")] | ||
pub calldata: Vec<FieldElement>, | ||
|
||
#[arg(short, long)] | ||
#[arg(help = "The block ID (could be a hash, a number, 'pending' or 'latest')")] | ||
pub block_id: Option<String>, | ||
|
||
#[command(flatten)] | ||
pub starknet: StarknetOptions, | ||
|
||
#[command(flatten)] | ||
pub world: WorldOptions, | ||
} | ||
|
||
impl CallArgs { | ||
pub fn run(self, config: &Config) -> Result<()> { | ||
let env_metadata = utils::load_metadata_from_config(config)?; | ||
|
||
config.tokio_handle().block_on(async { | ||
let world_reader = | ||
utils::world_reader_from_env_metadata(self.world, self.starknet, &env_metadata) | ||
.await | ||
.unwrap(); | ||
|
||
sozo_ops::call::call( | ||
world_reader, | ||
self.contract, | ||
self.entrypoint, | ||
self.calldata, | ||
self.block_id, | ||
) | ||
.await | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use anyhow::{Context, Result}; | ||
use dojo_world::contracts::WorldContractReader; | ||
use starknet::core::types::{BlockId, BlockTag, FieldElement, FunctionCall}; | ||
use starknet::core::utils::get_selector_from_name; | ||
use starknet::providers::Provider; | ||
|
||
use crate::utils::{get_contract_address_from_reader, parse_block_id}; | ||
|
||
pub async fn call<P: Provider + Sync + Send>( | ||
world_reader: WorldContractReader<P>, | ||
contract: String, | ||
entrypoint: String, | ||
calldata: Vec<FieldElement>, | ||
block_id: Option<String>, | ||
) -> Result<()> { | ||
let contract_address = get_contract_address_from_reader(&world_reader, contract).await?; | ||
let block_id = if let Some(block_id) = block_id { | ||
parse_block_id(block_id)? | ||
} else { | ||
BlockId::Tag(BlockTag::Pending) | ||
}; | ||
|
||
let output = world_reader | ||
.provider() | ||
.call( | ||
FunctionCall { | ||
contract_address, | ||
entry_point_selector: get_selector_from_name(&entrypoint)?, | ||
calldata, | ||
}, | ||
block_id, | ||
) | ||
.await | ||
.with_context(|| format!("Failed to call {entrypoint}"))?; | ||
|
||
println!("[ {} ]", output.iter().map(|o| format!("0x{:x}", o)).collect::<Vec<_>>().join(" ")); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod auth; | ||
pub mod call; | ||
pub mod events; | ||
pub mod execute; | ||
pub mod migration; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use dojo_test_utils::sequencer::{ | ||
get_default_test_starknet_config, SequencerConfig, TestSequencer, | ||
}; | ||
use dojo_world::contracts::WorldContractReader; | ||
use starknet::accounts::SingleOwnerAccount; | ||
use starknet::providers::jsonrpc::HttpTransport; | ||
use starknet::providers::JsonRpcClient; | ||
use starknet::signers::LocalWallet; | ||
use starknet_crypto::FieldElement; | ||
|
||
use super::setup; | ||
use crate::{call, utils}; | ||
|
||
const CONTRACT_NAME: &str = "dojo_examples::actions::actions"; | ||
const ENTRYPOINT: &str = "tile_terrain"; | ||
|
||
#[tokio::test] | ||
async fn call_with_bad_address() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
assert!( | ||
call::call( | ||
world_reader, | ||
"0xBadCoffeeBadCode".to_string(), | ||
ENTRYPOINT.to_string(), | ||
vec![FieldElement::ZERO, FieldElement::ZERO], | ||
None | ||
) | ||
.await | ||
.is_err() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn call_with_bad_name() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
assert!( | ||
call::call( | ||
world_reader, | ||
"BadName".to_string(), | ||
ENTRYPOINT.to_string(), | ||
vec![FieldElement::ZERO, FieldElement::ZERO], | ||
None | ||
) | ||
.await | ||
.is_err() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn call_with_bad_entrypoint() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
assert!( | ||
call::call( | ||
world_reader, | ||
CONTRACT_NAME.to_string(), | ||
"BadEntryPoint".to_string(), | ||
vec![FieldElement::ZERO, FieldElement::ZERO], | ||
None | ||
) | ||
.await | ||
.is_err() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn call_with_bad_calldata() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
assert!( | ||
call::call(world_reader, CONTRACT_NAME.to_string(), ENTRYPOINT.to_string(), vec![], None) | ||
.await | ||
.is_err() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn call_with_contract_name() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
assert!( | ||
call::call( | ||
world_reader, | ||
CONTRACT_NAME.to_string(), | ||
ENTRYPOINT.to_string(), | ||
vec![FieldElement::ZERO, FieldElement::ZERO], | ||
None, | ||
) | ||
.await | ||
.is_ok() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn call_with_contract_address() { | ||
let sequencer = | ||
TestSequencer::start(SequencerConfig::default(), get_default_test_starknet_config()).await; | ||
|
||
let world = setup::setup(&sequencer).await.unwrap(); | ||
let provider = sequencer.provider(); | ||
let world_reader = WorldContractReader::new(world.address, provider); | ||
|
||
let contract_address = utils::get_contract_address::< | ||
SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>, | ||
>(&world, CONTRACT_NAME.to_string()) | ||
.await | ||
.unwrap(); | ||
|
||
assert!( | ||
call::call( | ||
world_reader, | ||
format!("{:#x}", contract_address), | ||
ENTRYPOINT.to_string(), | ||
vec![FieldElement::ZERO, FieldElement::ZERO], | ||
None, | ||
) | ||
.await | ||
.is_ok() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod auth; | ||
pub mod call; | ||
pub mod setup; | ||
pub mod utils; |
Oops, something went wrong.