Skip to content

Commit

Permalink
feat: tx send
Browse files Browse the repository at this point in the history
Add tx send subcommand to send transaction to the network
  • Loading branch information
willemneal committed Sep 23, 2024
1 parent d56fa09 commit e33f097
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
17 changes: 17 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,7 @@ Sign, Simulate, and Send transactions

* `simulate` — Simulate a transaction envelope from stdin
* `hash` — Calculate the hash of a transaction envelope from stdin
* `send` — Send a transaction envelope to the network
* `sign` — Sign a transaction envelope appending the signature to the envelope


Expand Down Expand Up @@ -1327,6 +1328,22 @@ Calculate the hash of a transaction envelope from stdin



## `stellar tx send`

Send a transaction envelope to the network

**Usage:** `stellar tx send [OPTIONS]`

###### **Options:**

* `--rpc-url <RPC_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config
* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."



## `stellar tx sign`

Sign a transaction envelope appending the signature to the envelope
Expand Down
11 changes: 8 additions & 3 deletions cmd/crates/soroban-test/tests/it/integration/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ async fn send() {
assert_eq!(rpc_result.status, "SUCCESS");
}

async fn send_manually(sandbox: &TestEnv, tx_env: &TransactionEnvelope) -> GetTransactionResponse {
let client = soroban_rpc::Client::new(&sandbox.rpc_url).unwrap();
client.send_transaction_polling(tx_env).await.unwrap()
async fn send_manually(sandbox: &TestEnv, tx_env: &TransactionEnvelope) -> String {
sandbox
.new_assert_cmd("tx")
.arg("send")
.write_stdin(tx_env.to_xdr_base64(Limits::none()).unwrap())
.assert()
.success()
.stdout_as_str()
}

fn sign_manually(sandbox: &TestEnv, tx_env: &TransactionEnvelope) -> TransactionEnvelope {
Expand Down
8 changes: 6 additions & 2 deletions cmd/soroban-cli/src/commands/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::Parser;
use super::global;

pub mod hash;
pub mod send;
pub mod sign;
pub mod simulate;
pub mod xdr;
Expand All @@ -13,19 +14,21 @@ pub enum Cmd {
Simulate(simulate::Cmd),
/// Calculate the hash of a transaction envelope from stdin
Hash(hash::Cmd),
/// Send a transaction envelope to the network
Send(send::Cmd),
/// Sign a transaction envelope appending the signature to the envelope
Sign(sign::Cmd),
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
/// An error during the simulation
#[error(transparent)]
Simulate(#[from] simulate::Error),
/// An error during hash calculation
#[error(transparent)]
Hash(#[from] hash::Error),
#[error(transparent)]
Send(#[from] send::Error),
#[error(transparent)]
Sign(#[from] sign::Error),
}

Expand All @@ -34,6 +37,7 @@ impl Cmd {
match self {
Cmd::Simulate(cmd) => cmd.run(global_args).await?,
Cmd::Hash(cmd) => cmd.run(global_args)?,
Cmd::Send(cmd) => cmd.run(global_args).await?,
Cmd::Sign(cmd) => cmd.run(global_args).await?,
};
Ok(())
Expand Down
61 changes: 61 additions & 0 deletions cmd/soroban-cli/src/commands/tx/send.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use async_trait::async_trait;
use soroban_rpc::GetTransactionResponse;

use crate::commands::{global, NetworkRunnable};
use crate::config::{self, locator, network};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
XdrArgs(#[from] super::xdr::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Locator(#[from] locator::Error),
#[error(transparent)]
Config(#[from] config::Error),
#[error(transparent)]
Rpc(#[from] crate::rpc::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
}

#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
/// Command to send a transaction envelope to the network
/// e.g. `cat file.txt | soroban tx send`
pub struct Cmd {
#[clap(flatten)]
pub network: network::Args,
#[clap(flatten)]
pub locator: locator::Args,
}

impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let response = self.run_against_rpc_server(Some(global_args), None).await?;
println!("{}", serde_json::to_string_pretty(&response)?);
Ok(())
}
}

#[async_trait]
impl NetworkRunnable for Cmd {
type Error = Error;

type Result = GetTransactionResponse;
async fn run_against_rpc_server(
&self,
_: Option<&global::Args>,
config: Option<&config::Args>,
) -> Result<Self::Result, Self::Error> {
let network = if let Some(config) = config {
config.get_network()?
} else {
self.network.get(&self.locator)?
};
let client = crate::rpc::Client::new(&network.rpc_url)?;
let tx_env = super::xdr::tx_envelope_from_stdin()?;
Ok(client.send_transaction_polling(&tx_env).await?)
}
}

0 comments on commit e33f097

Please sign in to comment.