Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tx set #1760

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/tests/it/integration/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use soroban_test::{AssertExt, TestEnv};
use crate::integration::util::{deploy_contract, DeployKind, HELLO_WORLD};

mod operations;
mod set;

#[tokio::test]
async fn simulate() {
Expand Down
34 changes: 34 additions & 0 deletions cmd/crates/soroban-test/tests/it/integration/tx/set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use soroban_cli::xdr::{Limits, ReadXdr, TransactionEnvelope, WriteXdr};
use soroban_test::{AssertExt, TestEnv};

use crate::integration::util::{deploy_contract, DeployKind, HELLO_WORLD};

#[tokio::test]
async fn build_simulate_sign_send() {
let sandbox = &TestEnv::new();
let tx_base64 = sandbox
.new_assert_cmd("contract")
.arg("install")
.args([
"--wasm",
HELLO_WORLD.path().as_os_str().to_str().unwrap(),
"--build-only",
])
.assert()
.success()
.stdout_as_str();
let tx_env = TransactionEnvelope::from_xdr_base64(&tx_base64, Limits::none()).unwrap();
// set transaction options set fee
let new_tx = sandbox
.new_assert_cmd("tx")
.arg("set")
.arg("--fee")
.arg("10000")
.write_stdin(tx_base64.as_bytes())
.assert()
.success()
.stdout_as_str();
let tx_env_two = TransactionEnvelope::from_xdr_base64(&new_tx, Limits::none()).unwrap();
let tx = soroban_cli::commands::tx::xdr::unwrap_envelope_v1(tx_env_two).unwrap();
assert_eq!(tx.fee, 10000);
}
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ mod init;
#[cfg(feature = "it")]
mod integration;
mod plugin;
mod tx;
mod util;
mod version;
31 changes: 31 additions & 0 deletions cmd/crates/soroban-test/tests/it/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use soroban_cli::xdr::{Limits, ReadXdr, TransactionEnvelope, WriteXdr};
use soroban_test::{AssertExt, TestEnv};

const SOURCE: &str = "GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";

#[tokio::test]
async fn build_simulate_sign_send() {
let sandbox = &TestEnv::new();
let tx_base64 = sandbox
.new_assert_cmd("tx")
.args(["new", "payment", "--destination", SOURCE, "--amount", "222"])
.assert()
.success()
.stdout_as_str();
let tx_env = TransactionEnvelope::from_xdr_base64(&tx_base64, Limits::none()).unwrap();
let tx = soroban_cli::commands::tx::xdr::unwrap_envelope_v1(tx_env).unwrap();
assert_eq!(tx.fee, 100);
// set transaction options set fee
let new_tx = sandbox
.new_assert_cmd("tx")
.arg("set")
.arg("--fee")
.arg("10000")
.write_stdin(tx_base64.as_bytes())
.assert()
.success()
.stdout_as_str();
let tx_env_two = TransactionEnvelope::from_xdr_base64(&new_tx, Limits::none()).unwrap();
let tx = soroban_cli::commands::tx::xdr::unwrap_envelope_v1(tx_env_two).unwrap();
assert_eq!(tx.fee, 10000);
}
19 changes: 13 additions & 6 deletions cmd/soroban-cli/src/commands/tx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::global;

pub mod args;

pub mod hash;
pub mod new;
pub mod send;
pub mod set;
pub mod sign;
pub mod simulate;
pub mod xdr;
Expand All @@ -12,17 +14,19 @@ pub use args::Args;

#[derive(Debug, clap::Subcommand)]
pub enum Cmd {
/// Simulate a transaction envelope from stdin
Simulate(simulate::Cmd),
/// Calculate the hash of a transaction envelope from stdin
Hash(hash::Cmd),
/// Sign a transaction envelope appending the signature to the envelope
Sign(sign::Cmd),
/// Send a transaction envelope to the network
Send(send::Cmd),
/// Create a new transaction
#[command(subcommand)]
New(new::Cmd),
/// Send a transaction envelope to the network
Send(send::Cmd),
/// Set various options for a transaction
Set(set::Cmd),
/// Simulate a transaction envelope from stdin
Simulate(simulate::Cmd),
/// Sign a transaction envelope appending the signature to the envelope
Sign(sign::Cmd),
Comment on lines +22 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we organise these commands by order of likely to use next? i.e.:

  • New
  • Set
  • Simulate
  • Sign
  • Send

}

#[derive(thiserror::Error, Debug)]
Expand All @@ -37,6 +41,8 @@ pub enum Error {
Sign(#[from] sign::Error),
#[error(transparent)]
Send(#[from] send::Error),
#[error(transparent)]
Set(#[from] set::Error),
}

impl Cmd {
Expand All @@ -47,6 +53,7 @@ impl Cmd {
Cmd::New(cmd) => cmd.run(global_args).await?,
Cmd::Sign(cmd) => cmd.run(global_args).await?,
Cmd::Send(cmd) => cmd.run(global_args).await?,
Cmd::Set(cmd) => cmd.run(global_args)?,
};
Ok(())
}
Expand Down
211 changes: 211 additions & 0 deletions cmd/soroban-cli/src/commands/tx/set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
use soroban_sdk::xdr::WriteXdr;

use crate::{
commands::global,
config::address::{self, Address},
xdr::{self, TransactionEnvelope},
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
XdrStdin(#[from] super::xdr::Error),
#[error(transparent)]
Xdr(#[from] xdr::Error),
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two distinct xdr error types in use here? There should only ever be one in the cli in use.

#[error(transparent)]
Address(#[from] address::Error),
#[error("Only transaction supported")]
Unsupported,
}

#[derive(clap::Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
/// Set the transactions sequence number.
#[arg(long, visible_alias = "seq_num")]
pub sequence_number: Option<i64>,
/// Set the transactions fee.
#[arg(long)]
pub fee: Option<u32>,

/// Set the transactions memo text.
#[arg(
long,
conflicts_with = "memo_id",
conflicts_with = "memo_hash",
conflicts_with = "memo_return"
)]
pub memo_text: Option<xdr::StringM<28>>,
/// Set the transactions memo id.
#[arg(
long,
conflicts_with = "memo_text",
conflicts_with = "memo_hash",
conflicts_with = "memo_return"
)]
pub memo_id: Option<u64>,
/// Set the transactions memo hash.
#[arg(
long,
conflicts_with = "memo_text",
conflicts_with = "memo_id",
conflicts_with = "memo_return"
)]
pub memo_hash: Option<xdr::Hash>,
/// Set the transactions memo return.
#[arg(
long,
conflicts_with = "memo_text",
conflicts_with = "memo_id",
conflicts_with = "memo_hash"
)]
pub memo_return: Option<xdr::Hash>,
/// Change the source account for the transaction
#[arg(long, visible_alias = "source")]
pub source_account: Option<Address>,

// Time bounds and Preconditions
/// Set the transactions max time bound
#[arg(long)]
pub max_time_bound: Option<u64>,
/// Set the transactions min time bound
#[arg(long)]
pub min_time_bound: Option<u64>,

/// Set the minimum ledger that the transaction is valid
#[arg(long)]
pub min_ledger: Option<u32>,
/// Set the max ledger that the transaction is valid. 0 or not present means to max
#[arg(long)]
pub max_ledger: Option<u32>,
/// set mimimum sequence number
#[arg(long)]
pub min_seq_num: Option<i64>,
// min sequence age in seconds
#[arg(long)]
pub min_seq_age: Option<u64>,
/// min sequeence ledger gap
#[arg(long)]
pub min_seq_ledger_gap: Option<u32>,
/// Extra signers
#[arg(long, num_args = 0..=2)]
pub extra_signers: Vec<xdr::SignerKey>,
/// Set precondition to None
#[arg(
long,
conflicts_with = "extra_signers",
conflicts_with = "min_ledger",
conflicts_with = "max_ledger",
conflicts_with = "min_seq_num",
conflicts_with = "min_seq_age",
conflicts_with = "min_seq_ledger_gap",
conflicts_with = "max_time_bound",
conflicts_with = "min_time_bound"
)]
pub no_preconditions: bool,
}

impl Cmd {
pub fn run(&self, global: &global::Args) -> Result<(), Error> {
let mut tx = super::xdr::tx_envelope_from_stdin()?;
self.update_tx_env(&mut tx, global)?;
println!("{}", tx.to_xdr_base64(xdr::Limits::none())?);
Ok(())
}

pub fn update_tx_env(
&self,
tx_env: &mut TransactionEnvelope,
global: &global::Args,
) -> Result<(), Error> {
match tx_env {
TransactionEnvelope::Tx(transaction_v1_envelope) => {
if let Some(source_account) = self.source_account.as_ref() {
transaction_v1_envelope.tx.source_account =
source_account.resolve_muxed_account(&global.locator, None)?;
};

if let Some(seq_num) = self.sequence_number {
transaction_v1_envelope.tx.seq_num = seq_num.into();
}
if let Some(fee) = self.fee {
transaction_v1_envelope.tx.fee = fee;
}

if let Some(memo) = self.memo_text.as_ref() {
transaction_v1_envelope.tx.memo = xdr::Memo::Text(memo.clone());
}
if let Some(memo) = self.memo_id {
transaction_v1_envelope.tx.memo = xdr::Memo::Id(memo);
}
if let Some(memo) = self.memo_hash.as_ref() {
transaction_v1_envelope.tx.memo = xdr::Memo::Hash(memo.clone());
}
if let Some(memo) = self.memo_return.as_ref() {
transaction_v1_envelope.tx.memo = xdr::Memo::Return(memo.clone());
}
if let Some(preconditions) = self.preconditions()? {
transaction_v1_envelope.tx.cond = preconditions;
}
}
TransactionEnvelope::TxV0(_) | TransactionEnvelope::TxFeeBump(_) => {
return Err(Error::Unsupported);
}
};
Ok(())
}

pub fn has_preconditionv2(&self) -> bool {
self.min_ledger.is_some()
|| self.max_ledger.is_some()
|| self.min_seq_num.is_some()
|| self.min_seq_age.is_some()
|| self.min_seq_ledger_gap.is_some()
|| !self.extra_signers.is_empty()
}

pub fn preconditions(&self) -> Result<Option<xdr::Preconditions>, Error> {
let time_bounds = self.timebounds();

Ok(if self.no_preconditions {
Some(xdr::Preconditions::None)
} else if self.has_preconditionv2() {
let ledger_bounds = if self.min_ledger.is_some() || self.max_ledger.is_some() {
Some(xdr::LedgerBounds {
min_ledger: self.min_ledger.unwrap_or_default(),
max_ledger: self.max_ledger.unwrap_or_default(),
})
} else {
None
};
Some(xdr::Preconditions::V2(xdr::PreconditionsV2 {
ledger_bounds,
time_bounds,
min_seq_num: self.min_seq_num.map(xdr::SequenceNumber),
min_seq_age: self.min_seq_age.unwrap_or_default().into(),
min_seq_ledger_gap: self.min_seq_ledger_gap.unwrap_or_default(),
extra_signers: self.extra_signers.clone().try_into()?,
}))
} else {
None
})
}

pub fn timebounds(&self) -> Option<crate::xdr::TimeBounds> {
match (self.min_time_bound, self.max_time_bound) {
(Some(min_time), Some(max_time)) => Some(crate::xdr::TimeBounds {
min_time: min_time.into(),
max_time: max_time.into(),
}),
(min, Some(max_time)) => Some(crate::xdr::TimeBounds {
min_time: min.unwrap_or_default().into(),
max_time: max_time.into(),
}),
(Some(min_time), max) => Some(crate::xdr::TimeBounds {
min_time: min_time.into(),
max_time: max.unwrap_or(u64::MAX).into(),
}),
_ => None,
}
}
}
Loading
Loading