Skip to content

Commit c39fc60

Browse files
committed
feat(cast): add tx command
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.
1 parent aadd958 commit c39fc60

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

cast/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- [x] `calldata`
3636
- [x] `chain`
3737
- [x] `chain-id`
38-
- [ ] `code`
38+
- [x] `code`
3939
- [ ] `debug`
4040
- [ ] `estimate`
4141
- [ ] `etherscan-source`
@@ -56,4 +56,4 @@
5656
- [x] `send` (partial)
5757
- [ ] `sign`
5858
- [x] `storage`
59-
- [ ] `tx`
59+
- [x] `tx`

cast/src/lib.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
/// Makes a read-only call to the specified address
4545
///
4646
/// ```no_run
47-
///
47+
///
4848
/// use cast::Cast;
4949
/// use ethers_core::types::Address;
5050
/// use ethers_providers::{Provider, Http};
@@ -308,6 +308,45 @@ where
308308
) -> Result<String> {
309309
Ok(format!("{}", self.provider.get_code(who, block).await?))
310310
}
311+
312+
/// ```no_run
313+
/// use cast::Cast;
314+
/// use ethers_providers::{Provider, Http};
315+
/// use std::convert::TryFrom;
316+
///
317+
/// # async fn foo() -> eyre::Result<()> {
318+
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
319+
/// let cast = Cast::new(provider);
320+
/// let tx_hash = "0xf8d1713ea15a81482958fb7ddf884baee8d3bcc478c5f2f604e008dc788ee4fc";
321+
/// let tx = cast.transaction(tx_hash.to_string(), None, false).await?;
322+
/// println!("{}", tx);
323+
/// # Ok(())
324+
/// # }
325+
/// ```
326+
pub async fn transaction(
327+
&self,
328+
tx_hash: String,
329+
field: Option<String>,
330+
to_json: bool,
331+
) -> Result<String> {
332+
let transaction_result = self
333+
.provider
334+
.get_transaction(H256::from_str(&tx_hash)?)
335+
.await?
336+
.ok_or_else(|| eyre::eyre!("transaction {:?} not found", tx_hash))?;
337+
338+
let transaction = if let Some(ref field) = field {
339+
serde_json::to_value(&transaction_result)?
340+
.get(field)
341+
.cloned()
342+
.ok_or_else(|| eyre::eyre!("field {} not found", field))?
343+
} else {
344+
serde_json::to_value(&transaction_result)?
345+
};
346+
347+
let transaction = if to_json { serde_json::to_string(&transaction)? } else { to_table(transaction) };
348+
Ok(transaction)
349+
}
311350
}
312351

313352
pub struct SimpleCast;

cli/src/cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ async fn main() -> eyre::Result<()> {
124124
Subcommands::Namehash { name } => {
125125
println!("{}", SimpleCast::namehash(&name)?);
126126
}
127+
Subcommands::Tx { rpc_url, hash, field, to_json } => {
128+
let provider = Provider::try_from(rpc_url)?;
129+
println!("{}", Cast::new(&provider).transaction(hash, field, to_json).await?)
130+
}
127131
Subcommands::SendTx { eth, to, sig, cast_async, args } => {
128132
let provider = Provider::try_from(eth.rpc_url.as_str())?;
129133
let chain_id = Cast::new(&provider).chain_id().await?;

cli/src/opts/cast.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ pub enum Subcommands {
107107
#[structopt(name = "namehash")]
108108
#[structopt(about = "returns ENS namehash of provided name")]
109109
Namehash { name: String },
110+
#[structopt(name = "tx")]
111+
#[structopt(about = "Show information about the transaction <tx-hash>")]
112+
Tx {
113+
hash: String,
114+
field: Option<String>,
115+
#[structopt(long = "--json", short = "-j")]
116+
to_json: bool,
117+
#[structopt(long, env = "ETH_RPC_URL")]
118+
rpc_url: String
119+
},
110120
#[structopt(name = "send")]
111121
#[structopt(about = "Publish a transaction signed by <from> to call <to> with <data>")]
112122
SendTx {

0 commit comments

Comments
 (0)