-
Notifications
You must be signed in to change notification settings - Fork 77
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
willemneal
wants to merge
4
commits into
main
Choose a base branch
from
feat/tx_set
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+499
−68
Open
feat: add tx set
#1760
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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); | ||
} |
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ mod init; | |
#[cfg(feature = "it")] | ||
mod integration; | ||
mod plugin; | ||
mod tx; | ||
mod util; | ||
mod version; |
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,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); | ||
} |
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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.: