Skip to content

Commit

Permalink
chore(cast): improve transaction & block print formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bind committed Apr 10, 2022
1 parent bf7b55a commit 5a0241c
Show file tree
Hide file tree
Showing 2 changed files with 505 additions and 24 deletions.
67 changes: 43 additions & 24 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ use ethers_core::{
types::{Chain, *},
utils::{self, get_contract_address, keccak256, parse_units},
};

use ethers_etherscan::Client;
use ethers_providers::{Middleware, PendingTransaction};
use eyre::{Context, Result};
use foundry_utils::{encode_args, to_table};
use print_utils::{get_pretty_block_attr, get_pretty_tx_attr, UIfmt};
use rustc_hex::{FromHexIter, ToHex};
use std::{path::PathBuf, str::FromStr};
pub use tx::TxBuilder;
use tx::{TxBuilderOutput, TxBuilderPeekOutput};

use foundry_utils::{encode_args, to_table};

mod print_utils;
mod tx;

// TODO: CastContract with common contract initializers? Same for CastProviders?
Expand Down Expand Up @@ -53,7 +53,7 @@ where
/// Makes a read-only call to the specified address
///
/// ```no_run
///
///
/// use cast::{Cast, TxBuilder};
/// use ethers_core::types::{Address, Chain};
/// use ethers_providers::{Provider, Http};
Expand Down Expand Up @@ -114,7 +114,7 @@ where
/// Generates an access list for the specified transaction
///
/// ```no_run
///
///
/// use cast::{Cast, TxBuilder};
/// use ethers_core::types::{Address, Chain};
/// use ethers_providers::{Provider, Http};
Expand Down Expand Up @@ -296,35 +296,44 @@ where
.provider
.get_block_with_txs(block)
.await?
.ok_or_else(|| eyre::eyre!("block {:?} not found", block))?;
.ok_or_else(|| eyre::eyre!("block {:?} not found", block))
.unwrap();
if let Some(ref field) = field {
// TODO: Use custom serializer to serialize
// u256s as decimals
serde_json::to_value(&block)?
.get(field)
.cloned()
.ok_or_else(|| eyre::eyre!("field {} not found", field))?
let field_value = get_pretty_block_attr(block, field.to_string());
if field_value.is_some() {
field_value.unwrap()
} else {
format!("{} is not a valid block field", field)
}
} else if to_json {
serde_json::to_value(&block).unwrap().to_string()
} else {
serde_json::to_value(&block)?
block.pretty()
}
} else {
let block = self
.provider
.get_block(block)
.await?
.ok_or_else(|| eyre::eyre!("block {:?} not found", block))?;
.ok_or_else(|| eyre::eyre!("block {:?} not found", block))
.unwrap();

if let Some(ref field) = field {
serde_json::to_value(block)?
.get(field)
.cloned()
.ok_or_else(|| eyre::eyre!("field {} not found", field))?
let field_value = get_pretty_block_attr(block, field.to_string());
if field_value.is_some() {
field_value.unwrap()
} else if field == "transactions" {
"use --full to view transactions".to_string()
} else {
format!("{} is not a valid block field", field)
}
} else if to_json {
serde_json::to_value(&block).unwrap().to_string()
} else {
serde_json::to_value(&block)?
block.pretty()
}
};

let block = if to_json { serde_json::to_string(&block)? } else { to_table(block) };

Ok(block)
}

Expand Down Expand Up @@ -538,8 +547,18 @@ where
serde_json::to_value(&transaction_result)?
};

let transaction =
if to_json { serde_json::to_string(&transaction)? } else { to_table(transaction) };
let transaction = if let Some(ref field) = field {
let field_value = get_pretty_tx_attr(transaction_result, field.to_string());
if field_value.is_some() {
field_value.unwrap()
} else {
format!("{} is not a valid tx field", field)
}
} else if to_json {
serde_json::to_string(&transaction)?
} else {
transaction_result.pretty()
};
Ok(transaction)
}

Expand Down Expand Up @@ -1185,7 +1204,7 @@ impl SimpleCast {
let code = meta.source_code();

if code.is_empty() {
return Err(eyre::eyre!("unverified contract"))
return Err(eyre::eyre!("unverified contract"));
}

Ok(code)
Expand Down
Loading

0 comments on commit 5a0241c

Please sign in to comment.