forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
forc-submit
plugin command for submitting txs to a given node (…
…FuelLabs#3885) Closes FuelLabs#3875.
- Loading branch information
1 parent
9a00b5a
commit 220325d
Showing
18 changed files
with
192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# forc submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use clap::Parser; | ||
use forc_tracing::init_tracing_subscriber; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
init_tracing_subscriber(Default::default()); | ||
let command = forc_client::cmd::Submit::parse(); | ||
if let Err(err) = forc_client::op::submit(command).await { | ||
tracing::error!("Error: {:?}", err); | ||
std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod deploy; | ||
pub mod run; | ||
pub mod submit; | ||
|
||
pub use deploy::Command as Deploy; | ||
pub use run::Command as Run; | ||
pub use submit::Command as Submit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use devault::Devault; | ||
use std::path::PathBuf; | ||
|
||
/// Submit a transaction to the specified fuel node. | ||
#[derive(Debug, Default, clap::Parser)] | ||
#[clap(about, version)] | ||
pub struct Command { | ||
#[clap(flatten)] | ||
pub network: Network, | ||
#[clap(flatten)] | ||
pub tx_status: TxStatus, | ||
/// Path to the Transaction that is to be submitted to the Fuel node. | ||
/// | ||
/// Paths to files ending with `.json` will be deserialized from JSON. | ||
/// Paths to files ending with `.bin` will be deserialized from bytes | ||
/// using the `fuel_tx::Transaction::try_from_bytes` constructor. | ||
pub tx_path: PathBuf, | ||
} | ||
|
||
/// Options related to networking. | ||
#[derive(Debug, Devault, clap::Args)] | ||
pub struct Network { | ||
/// The URL of the Fuel node to which we're submitting the transaction. | ||
#[clap(long, env = "FUEL_NODE_URL", default_value_t = String::from(crate::default::NODE_URL))] | ||
#[devault("String::from(crate::default::NODE_URL)")] | ||
pub node_url: String, | ||
/// Whether or not to await confirmation that the transaction has been committed. | ||
/// | ||
/// When `true`, await commitment and output the transaction status. | ||
/// When `false`, do not await confirmation and simply output the transaction ID. | ||
#[clap(long = "await", default_value_t = true)] | ||
#[devault("true")] | ||
pub await_: bool, | ||
} | ||
|
||
/// Options related to the transaction status. | ||
#[derive(Debug, Default, clap::Args)] | ||
pub struct TxStatus { | ||
/// Output the resulting transaction status as JSON rather than the default output. | ||
#[clap(long = "tx-status-json", default_value_t = false)] | ||
pub json: bool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
pub mod cmd; | ||
pub mod op; | ||
mod util; | ||
|
||
pub mod default { | ||
/// Default to localhost to favour the common case of testing. | ||
pub const NODE_URL: &str = sway_utils::constants::DEFAULT_NODE_URL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod deploy; | ||
mod run; | ||
mod submit; | ||
|
||
pub use deploy::deploy; | ||
pub use run::run; | ||
pub use submit::submit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::cmd; | ||
use anyhow::Context; | ||
use fuel_core_client::client::{types::TransactionStatus, FuelClient}; | ||
|
||
/// A command for submitting transactions to a Fuel network. | ||
pub async fn submit(cmd: cmd::Submit) -> anyhow::Result<()> { | ||
let tx = read_tx(&cmd.tx_path)?; | ||
let client = FuelClient::new(&cmd.network.node_url)?; | ||
if cmd.network.await_ { | ||
let status = client | ||
.submit_and_await_commit(&tx) | ||
.await | ||
.context("Submission of tx or awaiting commit failed")?; | ||
if cmd.tx_status.json { | ||
print_status_json(&status)?; | ||
} else { | ||
print_status(&status); | ||
} | ||
} else { | ||
let id = client.submit(&tx).await.context("Failed to submit tx")?; | ||
println!("{id}"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Deserialize a `Transaction` from the given file into memory. | ||
pub fn read_tx(path: &std::path::Path) -> anyhow::Result<fuel_tx::Transaction> { | ||
let file = std::fs::File::open(path)?; | ||
let reader = std::io::BufReader::new(file); | ||
fn has_extension(path: &std::path::Path, ext: &str) -> bool { | ||
path.extension().and_then(|ex| ex.to_str()) == Some(ext) | ||
} | ||
let tx: fuel_tx::Transaction = if has_extension(path, "json") { | ||
serde_json::from_reader(reader)? | ||
} else if has_extension(path, "bin") { | ||
let tx_bytes = std::fs::read(path)?; | ||
let (_bytes, tx) = fuel_tx::Transaction::try_from_bytes(&tx_bytes)?; | ||
tx | ||
} else { | ||
anyhow::bail!(r#"Unsupported transaction file extension, expected ".json" or ".bin""#); | ||
}; | ||
Ok(tx) | ||
} | ||
|
||
/// Format the transaction status in a more human-friendly manner. | ||
pub fn fmt_status(status: &TransactionStatus, s: &mut String) -> anyhow::Result<()> { | ||
use chrono::TimeZone; | ||
use std::fmt::Write; | ||
match status { | ||
TransactionStatus::Submitted { submitted_at } => { | ||
writeln!(s, "Transaction Submitted at {:?}", submitted_at.0)?; | ||
} | ||
TransactionStatus::Success { | ||
block_id, | ||
time, | ||
program_state, | ||
} => { | ||
let utc = chrono::Utc.timestamp_nanos(time.to_unix()); | ||
writeln!(s, "Transaction Succeeded")?; | ||
writeln!(s, " Block ID: {block_id}")?; | ||
writeln!(s, " Time: {utc}",)?; | ||
writeln!(s, " Program State: {program_state:?}")?; | ||
} | ||
TransactionStatus::SqueezedOut { reason } => { | ||
writeln!(s, "Transaction Squeezed Out: {reason}")?; | ||
} | ||
TransactionStatus::Failure { | ||
block_id, | ||
time, | ||
reason, | ||
program_state, | ||
} => { | ||
let utc = chrono::Utc.timestamp_nanos(time.to_unix()); | ||
writeln!(s, "Transaction Failed")?; | ||
writeln!(s, " Reason: {reason}")?; | ||
writeln!(s, " Block ID: {block_id}")?; | ||
writeln!(s, " Time: {utc}")?; | ||
writeln!(s, " Program State: {program_state:?}")?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Print the status to stdout. | ||
pub fn print_status(status: &TransactionStatus) { | ||
let mut string = String::new(); | ||
fmt_status(status, &mut string).expect("formatting to `String` is infallible"); | ||
println!("{string}"); | ||
} | ||
|
||
/// Print the status to stdout in its JSON representation. | ||
pub fn print_status_json(status: &TransactionStatus) -> anyhow::Result<()> { | ||
let json = serde_json::to_string_pretty(status)?; | ||
println!("{json}"); | ||
Ok(()) | ||
} |