Skip to content

Commit

Permalink
chore(cast): improve transaction & block print formatting (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bind authored Apr 12, 2022
1 parent 96d0294 commit 8c4ab48
Show file tree
Hide file tree
Showing 2 changed files with 506 additions and 19 deletions.
45 changes: 26 additions & 19 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 @@ -298,33 +298,34 @@ where
.await?
.ok_or_else(|| eyre::eyre!("block {:?} not found", block))?;
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))?
get_pretty_block_attr(block, field.to_string())
.unwrap_or_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))?;

if let Some(ref field) = field {
serde_json::to_value(block)?
.get(field)
.cloned()
.ok_or_else(|| eyre::eyre!("field {} not found", field))?
if field == "transactions" {
"use --full to view transactions".to_string()
} else {
get_pretty_block_attr(block, field.to_string())
.unwrap_or_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 +539,14 @@ 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 {
get_pretty_tx_attr(transaction_result, field.to_string())
.unwrap_or_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
Loading

0 comments on commit 8c4ab48

Please sign in to comment.