Skip to content

Commit 45b8a29

Browse files
authored
feat(cast): transaction to transaction request (#11151)
* feat(cast): transaction to transaction request * fixes * fixes * fixes * fixes * kept json
1 parent 54d2561 commit 45b8a29

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

crates/cast/src/args.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ pub async fn run_command(args: CastArgs) -> Result<()> {
507507
}
508508
CastSubcommand::Run(cmd) => cmd.run().await?,
509509
CastSubcommand::SendTx(cmd) => cmd.run().await?,
510-
CastSubcommand::Tx { tx_hash, from, nonce, field, raw, rpc } => {
510+
CastSubcommand::Tx { tx_hash, from, nonce, field, raw, rpc, to_request } => {
511511
let config = rpc.load_config()?;
512512
let provider = utils::get_provider(&config)?;
513513

@@ -516,7 +516,9 @@ pub async fn run_command(args: CastArgs) -> Result<()> {
516516

517517
sh_println!(
518518
"{}",
519-
Cast::new(&provider).transaction(tx_hash, from, nonce, field, raw).await?
519+
Cast::new(&provider)
520+
.transaction(tx_hash, from, nonce, field, raw, to_request)
521+
.await?
520522
)?
521523
}
522524

crates/cast/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
763763
/// ProviderBuilder::<_, _, AnyNetwork>::default().connect("http://localhost:8545").await?;
764764
/// let cast = Cast::new(provider);
765765
/// let tx_hash = "0xf8d1713ea15a81482958fb7ddf884baee8d3bcc478c5f2f604e008dc788ee4fc";
766-
/// let tx = cast.transaction(Some(tx_hash.to_string()), None, None, None, false).await?;
766+
/// let tx = cast.transaction(Some(tx_hash.to_string()), None, None, None, false, false).await?;
767767
/// println!("{}", tx);
768768
/// # Ok(())
769769
/// # }
@@ -775,6 +775,7 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
775775
nonce: Option<u64>,
776776
field: Option<String>,
777777
raw: bool,
778+
to_request: bool,
778779
) -> Result<String> {
779780
let tx = if let Some(tx_hash) = tx_hash {
780781
let tx_hash = TxHash::from_str(&tx_hash).wrap_err("invalid tx hash")?;
@@ -811,6 +812,10 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
811812
} else if shell::is_json() {
812813
// to_value first to sort json object keys
813814
serde_json::to_value(&tx)?.to_string()
815+
} else if to_request {
816+
serde_json::to_string_pretty(&TransactionRequest::from_recovered_transaction(
817+
tx.into(),
818+
))?
814819
} else {
815820
tx.pretty()
816821
})

crates/cast/src/opts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ pub enum CastSubcommand {
496496

497497
#[command(flatten)]
498498
rpc: RpcOpts,
499+
500+
/// If specified, the transaction will be converted to a TransactionRequest JSON format.
501+
#[arg(long)]
502+
to_request: bool,
499503
},
500504

501505
/// Get the transaction receipt for a transaction.

crates/cast/tests/cli/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,34 @@ casttest!(tx_raw, |_prj, cmd| {
17301730
"#]]);
17311731
});
17321732

1733+
casttest!(tx_to_request_json, |_prj, cmd| {
1734+
let rpc = next_http_rpc_endpoint();
1735+
1736+
// <https://etherscan.io/getRawTx?tx=0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e>
1737+
cmd.args([
1738+
"tx",
1739+
"0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e",
1740+
"--to-request",
1741+
"--rpc-url",
1742+
rpc.as_str(),
1743+
])
1744+
.assert_success()
1745+
.stdout_eq(str![[r#"
1746+
{
1747+
"from": "0x199d5ed7f45f4ee35960cf22eade2076e95b253f",
1748+
"to": "0x91da5bf3f8eb72724e6f50ec6c3d199c6355c59c",
1749+
"gasPrice": "0x2743b6508",
1750+
"gas": "0x7530",
1751+
"value": "0xa0a73f33e9e4cc",
1752+
"input": "0x",
1753+
"nonce": "0x4c54",
1754+
"chainId": "0x1",
1755+
"type": "0x0"
1756+
}
1757+
1758+
"#]]);
1759+
});
1760+
17331761
casttest!(tx_using_sender_and_nonce, |_prj, cmd| {
17341762
let rpc = "https://reth-ethereum.ithaca.xyz/rpc";
17351763
// <https://etherscan.io/tx/0x5bcd22734cca2385dc25b2d38a3d33a640c5961bd46d390dff184c894204b594>

0 commit comments

Comments
 (0)