Skip to content

Commit

Permalink
feat(cast): add tx command (#260)
Browse files Browse the repository at this point in the history
This commit ports the `seth tx` command to `cast`.
It follows the implementation of `cast block` and includes
the feature to print a specific field in the transaction
as well as printing the result in JSON.

```
$ cast tx --rpc-url <url> <tx-hash> [field]
```

The flag `-j`/`--json` is used to print the result
as JSON.
  • Loading branch information
tynes authored Dec 19, 2021
1 parent aadd958 commit 5679a42
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- [x] `calldata`
- [x] `chain`
- [x] `chain-id`
- [ ] `code`
- [x] `code`
- [ ] `debug`
- [ ] `estimate`
- [ ] `etherscan-source`
Expand All @@ -56,4 +56,4 @@
- [x] `send` (partial)
- [ ] `sign`
- [x] `storage`
- [ ] `tx`
- [x] `tx`
42 changes: 41 additions & 1 deletion cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
/// Makes a read-only call to the specified address
///
/// ```no_run
///
///
/// use cast::Cast;
/// use ethers_core::types::Address;
/// use ethers_providers::{Provider, Http};
Expand Down Expand Up @@ -308,6 +308,46 @@ where
) -> Result<String> {
Ok(format!("{}", self.provider.get_code(who, block).await?))
}

/// ```no_run
/// use cast::Cast;
/// use ethers_providers::{Provider, Http};
/// use std::convert::TryFrom;
///
/// # async fn foo() -> eyre::Result<()> {
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
/// let cast = Cast::new(provider);
/// let tx_hash = "0xf8d1713ea15a81482958fb7ddf884baee8d3bcc478c5f2f604e008dc788ee4fc";
/// let tx = cast.transaction(tx_hash.to_string(), None, false).await?;
/// println!("{}", tx);
/// # Ok(())
/// # }
/// ```
pub async fn transaction(
&self,
tx_hash: String,
field: Option<String>,
to_json: bool,
) -> Result<String> {
let transaction_result = self
.provider
.get_transaction(H256::from_str(&tx_hash)?)
.await?
.ok_or_else(|| eyre::eyre!("transaction {:?} not found", tx_hash))?;

let transaction = if let Some(ref field) = field {
serde_json::to_value(&transaction_result)?
.get(field)
.cloned()
.ok_or_else(|| eyre::eyre!("field {} not found", field))?
} else {
serde_json::to_value(&transaction_result)?
};

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

pub struct SimpleCast;
Expand Down
4 changes: 4 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ async fn main() -> eyre::Result<()> {
Subcommands::Namehash { name } => {
println!("{}", SimpleCast::namehash(&name)?);
}
Subcommands::Tx { rpc_url, hash, field, to_json } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Cast::new(&provider).transaction(hash, field, to_json).await?)
}
Subcommands::SendTx { eth, to, sig, cast_async, args } => {
let provider = Provider::try_from(eth.rpc_url.as_str())?;
let chain_id = Cast::new(&provider).chain_id().await?;
Expand Down
10 changes: 10 additions & 0 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ pub enum Subcommands {
#[structopt(name = "namehash")]
#[structopt(about = "returns ENS namehash of provided name")]
Namehash { name: String },
#[structopt(name = "tx")]
#[structopt(about = "Show information about the transaction <tx-hash>")]
Tx {
hash: String,
field: Option<String>,
#[structopt(long = "--json", short = "-j")]
to_json: bool,
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "send")]
#[structopt(about = "Publish a transaction signed by <from> to call <to> with <data>")]
SendTx {
Expand Down

0 comments on commit 5679a42

Please sign in to comment.