Skip to content

Commit

Permalink
feat: --log-traffic for logging provider traffic
Browse files Browse the repository at this point in the history
Adds a new option `--log-traffic` to commands that use a provider. When
this option is used, raw request/response data will be logged to the
standard error stream.
  • Loading branch information
xJonathanLEI committed Jul 16, 2023
1 parent 2d3cebd commit b0d90fb
Show file tree
Hide file tree
Showing 24 changed files with 126 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ colored_json = "3.2.0"
env_logger = "0.10.0"
hex = "0.4.3"
hex-literal = "0.4.1"
log = "0.4.19"
num-bigint = "0.4.3"
regex = "1.8.4"
rpassword = "7.2.0"
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod provider;
mod signer;
mod subcommands;
mod utils;
mod verbosity;

const VERSION_STRING: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")");

Expand Down Expand Up @@ -101,8 +102,6 @@ enum Subcommands {

#[tokio::main]
async fn main() {
env_logger::init();

if let Err(err) = run_command(Cli::parse()).await {
eprintln!("{}", format!("Error: {err}").red());
std::process::exit(1);
Expand Down
5 changes: 5 additions & 0 deletions src/subcommands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
account::{AccountConfig, AccountVariant, DeployedStatus, DeploymentStatus},
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
ProviderArgs,
};

Expand All @@ -30,10 +31,14 @@ pub struct Deploy {
estimate_only: bool,
#[clap(help = "Path to the account config file")]
file: PathBuf,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Deploy {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = Arc::new(self.provider.into_provider());
let signer = Arc::new(self.signer.into_signer()?);

Expand Down
5 changes: 5 additions & 0 deletions src/subcommands/account/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
AccountConfig, AccountVariant, AccountVariantType, DeployedStatus, DeploymentStatus,
OzAccountConfig, KNOWN_ACCOUNT_CLASSES,
},
verbosity::VerbosityArgs,
ProviderArgs,
};

Expand All @@ -27,10 +28,14 @@ pub struct Fetch {
output: Option<PathBuf>,
#[clap(help = "Contract address")]
address: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Fetch {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

// We allow not saving the config to just identify the account contract
if self.output.is_none() {
eprintln!(
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::providers::Provider;

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

#[derive(Debug, Parser)]
pub struct Block {
Expand All @@ -16,10 +16,14 @@ pub struct Block {
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Block {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();

let block_id = parse_block_id(&self.block_id)?;
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ use anyhow::Result;
use clap::Parser;
use starknet::providers::Provider;

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct BlockHash {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl BlockHash {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();

let block = provider.block_hash_and_number().await?;
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ use anyhow::Result;
use clap::Parser;
use starknet::providers::Provider;

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct BlockNumber {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl BlockNumber {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();

let block = provider.block_hash_and_number().await?;
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/block_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use chrono::{TimeZone, Utc};
use clap::Parser;
use starknet::{core::types::MaybePendingBlockWithTxHashes, providers::Provider};

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

#[derive(Debug, Parser)]
pub struct BlockTime {
Expand All @@ -26,10 +26,14 @@ pub struct BlockTime {
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl BlockTime {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();

let block_id = parse_block_id(&self.block_id)?;
Expand Down
8 changes: 7 additions & 1 deletion src/subcommands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use starknet::{
providers::Provider,
};

use crate::{address_book::AddressBookResolver, decode::FeltDecoder, ProviderArgs};
use crate::{
address_book::AddressBookResolver, decode::FeltDecoder, verbosity::VerbosityArgs, ProviderArgs,
};

#[derive(Debug, Parser)]
pub struct Call {
Expand All @@ -22,10 +24,14 @@ pub struct Call {
selector: String,
#[clap(help = "Raw function call arguments")]
calldata: Vec<String>,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Call {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = Arc::new(self.provider.into_provider());
let felt_decoder = FeltDecoder::new(AddressBookResolver::new(provider.clone()));

Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use clap::Parser;
use starknet::{core::utils::parse_cairo_short_string, providers::Provider};

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct ChainId {
Expand All @@ -20,10 +20,14 @@ pub struct ChainId {
help = "Block number, hash, or tag (latest/pending)"
)]
block_id: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl ChainId {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();

let raw_chain_id = provider.chain_id().await?;
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/class_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ use starknet::{
providers::Provider,
};

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct ClassAt {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(help = "Contract address")]
address: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl ClassAt {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();
let address = FieldElement::from_hex_be(&self.address)?;

Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/class_by_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ use starknet::{
providers::Provider,
};

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct ClassByHash {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(help = "Class hash")]
hash: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl ClassByHash {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();
let class_hash = FieldElement::from_hex_be(&self.hash)?;

Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/class_hash_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ use starknet::{
providers::Provider,
};

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct ClassHashAt {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(help = "Contract address")]
address: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl ClassHashAt {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();
let address = FieldElement::from_hex_be(&self.address)?;

Expand Down
5 changes: 5 additions & 0 deletions src/subcommands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
casm::{CasmArgs, CasmHashSource},
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
ProviderArgs,
};

Expand All @@ -43,10 +44,14 @@ pub struct Declare {
watch: bool,
#[clap(help = "Path to contract artifact file")]
file: PathBuf,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Declare {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = Arc::new(self.provider.into_provider());

if !self.account.exists() {
Expand Down
5 changes: 5 additions & 0 deletions src/subcommands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
decode::FeltDecoder,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
ProviderArgs,
};

Expand Down Expand Up @@ -58,10 +59,14 @@ pub struct Deploy {
class_hash: String,
#[clap(help = "Raw constructor arguments")]
ctor_args: Vec<String>,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Deploy {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = Arc::new(self.provider.into_provider());
let felt_decoder = FeltDecoder::new(AddressBookResolver::new(provider.clone()));

Expand Down
5 changes: 5 additions & 0 deletions src/subcommands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
decode::FeltDecoder,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
ProviderArgs,
};

Expand All @@ -42,10 +43,14 @@ pub struct Invoke {
watch: bool,
#[clap(help = "One or more contract calls. See documentation for more details")]
calls: Vec<String>,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Invoke {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = Arc::new(self.provider.into_provider());
let felt_decoder = FeltDecoder::new(AddressBookResolver::new(provider.clone()));

Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ use starknet::{
providers::Provider,
};

use crate::ProviderArgs;
use crate::{verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct Nonce {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(help = "Contract address")]
address: String,
#[clap(flatten)]
verbosity: VerbosityArgs,
}

impl Nonce {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();

let provider = self.provider.into_provider();
let address = FieldElement::from_hex_be(&self.address)?;

Expand Down
Loading

0 comments on commit b0d90fb

Please sign in to comment.