Skip to content

Commit

Permalink
feat: add publish to cast (gakonst#491)
Browse files Browse the repository at this point in the history
* add publish

* improve

Co-authored-by: Bjerg <[email protected]>

Co-authored-by: Bjerg <[email protected]>
  • Loading branch information
ayushm2003 and onbjerg authored Jan 18, 2022
1 parent ff46022 commit 80af710
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
- [ ] `mktx`
- [x] `namehash`
- [x] `nonce`
- [ ] `publish`
- [x] `publish`
- [ ] `receipt`
- [x] `resolve-name`
- [ ] `run-tx`
Expand Down
25 changes: 25 additions & 0 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ where
Ok::<_, eyre::Error>(res)
}

/// Publishes a raw transaction to the network
///
/// ```no_run
/// use cast::Cast;
/// use ethers_providers::{Provider, Http};
///
/// # async fn foo() -> eyre::Result<()> {
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
/// let cast = Cast::new(provider);
/// let res = cast.publish("0x1234".to_string()).await?;
/// println!("{:?}", res);
/// # Ok(())
/// # }
/// ```
pub async fn publish(&self, mut raw_tx: String) -> Result<PendingTransaction<'_, M::Provider>> {
raw_tx = match raw_tx.strip_prefix("0x") {
Some(s) => s.to_string(),
None => raw_tx,
};
let tx = Bytes::from(hex::decode(raw_tx)?);
let res = self.provider.send_raw_transaction(tx).await?;

Ok::<_, eyre::Error>(res)
}

/// Estimates the gas cost of a transaction
///
/// ```no_run
Expand Down
14 changes: 14 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ async fn main() -> eyre::Result<()> {
.await?;
}
}
Subcommands::PublishTx { eth, raw_tx, cast_async } => {
let provider = Provider::try_from(eth.rpc_url()?)?;
let cast = Cast::new(&provider);
let pending_tx = cast.publish(raw_tx).await?;
let tx_hash = *pending_tx;

if cast_async {
println!("{:?}", pending_tx);
} else {
let receipt =
pending_tx.await?.ok_or_else(|| eyre::eyre!("tx {} not found", tx_hash))?;
println!("Receipt: {:?}", receipt);
}
}
Subcommands::Estimate { eth, to, sig, args } => {
let provider = Provider::try_from(eth.rpc_url()?)?;
let cast = Cast::new(&provider);
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 @@ -141,6 +141,16 @@ pub enum Subcommands {
#[clap(flatten)]
eth: EthereumOpts,
},
#[clap(name = "publish")]
#[clap(about = "Publish a raw transaction to the network")]
PublishTx {
#[clap(help = "the raw transaction you want to publish")]
raw_tx: String,
#[clap(long, env = "CAST_ASYNC")]
cast_async: bool,
#[clap(flatten)]
eth: EthereumOpts,
},
#[clap(name = "estimate")]
#[clap(about = "Estimate the gas cost of a transaction from <from> to <to> with <data>")]
Estimate {
Expand Down

0 comments on commit 80af710

Please sign in to comment.