Skip to content

Commit

Permalink
feat: --block option for call command
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Jul 7, 2024
1 parent 89690d4 commit 61650d9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 46 deletions.
50 changes: 50 additions & 0 deletions src/block_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use clap::{builder::TypedValueParser, error::ErrorKind, Arg, Command, Error};
use regex::Regex;
use starknet::core::types::{BlockId, BlockTag, Felt};

#[derive(Clone)]
pub struct BlockIdParser;

impl TypedValueParser for BlockIdParser {
type Value = BlockId;

fn parse_ref(
&self,
cmd: &Command,
_arg: Option<&Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, Error> {
if value.is_empty() {
Err(cmd.clone().error(ErrorKind::InvalidValue, "empty block ID"))
} else {
match value.to_str() {
Some(value) => {
let regex_block_number = Regex::new("^[0-9]{1,}$").unwrap();

if value == "latest" {
Ok(BlockId::Tag(BlockTag::Latest))
} else if value == "pending" {
Ok(BlockId::Tag(BlockTag::Pending))
} else if regex_block_number.is_match(value) {
Ok(BlockId::Number(value.parse::<u64>().map_err(|err| {
cmd.clone().error(
ErrorKind::InvalidValue,
&format!("invalid block number: {}", err),
)
})?))
} else {
Ok(BlockId::Hash(Felt::from_hex(value).map_err(|err| {
cmd.clone().error(
ErrorKind::InvalidValue,
&format!("invalid block hash: {}", err),
)
})?))
}
}
None => Err(cmd
.clone()
.error(ErrorKind::InvalidValue, "invalid block ID")),
}
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{provider::ProviderArgs, subcommands::*};
mod account;
mod account_factory;
mod address_book;
mod block_id;
mod casm;
mod chain_id;
mod compiler;
Expand Down
15 changes: 7 additions & 8 deletions src/subcommands/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::providers::Provider;
use starknet::{core::types::BlockId, providers::Provider};

use crate::{utils::parse_block_id, verbosity::VerbosityArgs, ProviderArgs};
use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct Block {
Expand All @@ -14,10 +14,11 @@ pub struct Block {
#[clap(long, help = "Fetch receipts alongside transactions")]
receipts: bool,
#[clap(
value_parser = BlockIdParser,
default_value = "latest",
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
block_id: BlockId,
#[clap(flatten)]
verbosity: VerbosityArgs,
}
Expand All @@ -28,14 +29,12 @@ impl Block {

let provider = self.provider.into_provider()?;

let block_id = parse_block_id(&self.block_id)?;

let block_json = if self.receipts {
serde_json::to_value(provider.get_block_with_receipts(block_id).await?)?
serde_json::to_value(provider.get_block_with_receipts(self.block_id).await?)?
} else if self.full {
serde_json::to_value(provider.get_block_with_txs(block_id).await?)?
serde_json::to_value(provider.get_block_with_txs(self.block_id).await?)?
} else {
serde_json::to_value(provider.get_block_with_tx_hashes(block_id).await?)?
serde_json::to_value(provider.get_block_with_tx_hashes(self.block_id).await?)?
};

let block_json =
Expand Down
14 changes: 8 additions & 6 deletions src/subcommands/block_time.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Result;
use chrono::{TimeZone, Utc};
use clap::Parser;
use starknet::{core::types::MaybePendingBlockWithTxHashes, providers::Provider};
use starknet::{
core::types::{BlockId, MaybePendingBlockWithTxHashes},
providers::Provider,
};

use crate::{utils::parse_block_id, verbosity::VerbosityArgs, ProviderArgs};
use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct BlockTime {
Expand All @@ -22,10 +25,11 @@ pub struct BlockTime {
)]
rfc2822: bool,
#[clap(
value_parser = BlockIdParser,
default_value = "latest",
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
block_id: BlockId,
#[clap(flatten)]
verbosity: VerbosityArgs,
}
Expand All @@ -36,9 +40,7 @@ impl BlockTime {

let provider = self.provider.into_provider()?;

let block_id = parse_block_id(&self.block_id)?;

let block = provider.get_block_with_tx_hashes(block_id).await?;
let block = provider.get_block_with_tx_hashes(self.block_id).await?;
let timestamp = match block {
MaybePendingBlockWithTxHashes::Block(block) => block.timestamp,
MaybePendingBlockWithTxHashes::PendingBlock(block) => block.timestamp,
Expand Down
12 changes: 6 additions & 6 deletions src/subcommands/block_traces.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::providers::Provider;
use starknet::{core::types::BlockId, providers::Provider};

use crate::{utils::parse_block_id, verbosity::VerbosityArgs, ProviderArgs};
use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct BlockTraces {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(
value_parser = BlockIdParser,
default_value = "latest",
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
block_id: BlockId,
#[clap(flatten)]
verbosity: VerbosityArgs,
}
Expand All @@ -24,9 +25,8 @@ impl BlockTraces {

let provider = self.provider.into_provider()?;

let block_id = parse_block_id(&self.block_id)?;

let traces_json = serde_json::to_value(provider.trace_block_transactions(block_id).await?)?;
let traces_json =
serde_json::to_value(provider.trace_block_transactions(self.block_id).await?)?;

let traces_json =
colored_json::to_colored_json(&traces_json, ColorMode::Auto(Output::StdOut))?;
Expand Down
15 changes: 11 additions & 4 deletions src/subcommands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ use std::sync::Arc;
use anyhow::Result;
use clap::Parser;
use starknet::{
core::types::{BlockId, BlockTag, FunctionCall},
core::types::{BlockId, FunctionCall},
providers::Provider,
};

use crate::{
address_book::AddressBookResolver, decode::FeltDecoder, error::provider_error_mapper,
verbosity::VerbosityArgs, ProviderArgs,
address_book::AddressBookResolver, block_id::BlockIdParser, decode::FeltDecoder,
error::provider_error_mapper, verbosity::VerbosityArgs, ProviderArgs,
};

#[derive(Debug, Parser)]
pub struct Call {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(
long,
value_parser = BlockIdParser,
default_value = "pending",
help = "Block number, hash, or tag (latest/pending)"
)]
block: BlockId,
#[clap(help = "Contract address")]
contract_address: String,
#[clap(help = "Name of the function being called")]
Expand Down Expand Up @@ -52,7 +59,7 @@ impl Call {
entry_point_selector: selector,
calldata,
},
BlockId::Tag(BlockTag::Pending),
self.block,
)
.await
.map_err(provider_error_mapper)?;
Expand Down
11 changes: 5 additions & 6 deletions src/subcommands/state_update.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::providers::Provider;
use starknet::{core::types::BlockId, providers::Provider};

use crate::{utils::parse_block_id, verbosity::VerbosityArgs, ProviderArgs};
use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct StateUpdate {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(
value_parser = BlockIdParser,
default_value = "latest",
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
block_id: BlockId,
#[clap(flatten)]
verbosity: VerbosityArgs,
}
Expand All @@ -24,9 +25,7 @@ impl StateUpdate {

let provider = self.provider.into_provider()?;

let block_id = parse_block_id(&self.block_id)?;

let update_json = serde_json::to_value(provider.get_state_update(block_id).await?)?;
let update_json = serde_json::to_value(provider.get_state_update(self.block_id).await?)?;

let update_json =
colored_json::to_colored_json(&update_json, ColorMode::Auto(Output::StdOut))?;
Expand Down
18 changes: 2 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use starknet::{
},
AbiEntry, SierraClass, SierraClassDebugInfo,
},
BlockId, BlockTag, CompressedLegacyContractClass, ExecutionResult, Felt,
FlattenedSierraClass, LegacyContractEntryPoint, StarknetError,
CompressedLegacyContractClass, ExecutionResult, Felt, FlattenedSierraClass,
LegacyContractEntryPoint, StarknetError,
},
providers::{Provider, ProviderError},
};
Expand Down Expand Up @@ -54,20 +54,6 @@ where
}
}

pub fn parse_block_id(id: &str) -> Result<BlockId> {
let regex_block_number = Regex::new("^[0-9]{1,}$").unwrap();

if id == "latest" {
Ok(BlockId::Tag(BlockTag::Latest))
} else if id == "pending" {
Ok(BlockId::Tag(BlockTag::Pending))
} else if regex_block_number.is_match(id) {
Ok(BlockId::Number(id.parse::<u64>()?))
} else {
Ok(BlockId::Hash(Felt::from_hex(id)?))
}
}

pub fn parse_felt_value(felt: &str) -> Result<Felt> {
let regex_dec_number = Regex::new("^[0-9]{1,}$").unwrap();

Expand Down

0 comments on commit 61650d9

Please sign in to comment.