|
| 1 | +use stellar_xdr::curr::MuxedAccount; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + commands::{ |
| 5 | + global, |
| 6 | + tx::xdr::{tx_envelope_from_input, Error as XdrParsingError}, |
| 7 | + }, |
| 8 | + config::{self, locator, network}, |
| 9 | + xdr::{self, SequenceNumber, TransactionEnvelope, WriteXdr}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(clap::Parser, Debug, Clone)] |
| 13 | +pub struct Cmd { |
| 14 | + #[command(flatten)] |
| 15 | + pub network: network::Args, |
| 16 | + #[command(flatten)] |
| 17 | + pub locator: locator::Args, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(thiserror::Error, Debug)] |
| 21 | +pub enum Error { |
| 22 | + #[error(transparent)] |
| 23 | + XdrStdin(#[from] XdrParsingError), |
| 24 | + #[error(transparent)] |
| 25 | + Xdr(#[from] xdr::Error), |
| 26 | + #[error("V0 and fee bump transactions are not supported")] |
| 27 | + Unsupported, |
| 28 | + #[error(transparent)] |
| 29 | + RpcClient(#[from] crate::rpc::Error), |
| 30 | + #[error(transparent)] |
| 31 | + Config(#[from] config::Error), |
| 32 | + #[error(transparent)] |
| 33 | + Network(#[from] config::network::Error), |
| 34 | +} |
| 35 | + |
| 36 | +impl Cmd { |
| 37 | + pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { |
| 38 | + let mut tx = tx_envelope_from_input(&None)?; |
| 39 | + self.update_tx_env(&mut tx, global_args).await?; |
| 40 | + println!("{}", tx.to_xdr_base64(xdr::Limits::none())?); |
| 41 | + Ok(()) |
| 42 | + } |
| 43 | + |
| 44 | + pub async fn update_tx_env( |
| 45 | + &self, |
| 46 | + tx_env: &mut TransactionEnvelope, |
| 47 | + _global: &global::Args, |
| 48 | + ) -> Result<(), Error> { |
| 49 | + match tx_env { |
| 50 | + TransactionEnvelope::Tx(transaction_v1_envelope) => { |
| 51 | + let tx_source_acct = &transaction_v1_envelope.tx.source_account; |
| 52 | + let current_seq_num = self.current_seq_num(tx_source_acct).await?; |
| 53 | + let next_seq_num = current_seq_num + 1; |
| 54 | + transaction_v1_envelope.tx.seq_num = SequenceNumber(next_seq_num); |
| 55 | + } |
| 56 | + TransactionEnvelope::TxV0(_) | TransactionEnvelope::TxFeeBump(_) => { |
| 57 | + return Err(Error::Unsupported); |
| 58 | + } |
| 59 | + }; |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + |
| 63 | + async fn current_seq_num(&self, tx_source_acct: &MuxedAccount) -> Result<i64, Error> { |
| 64 | + let network = &self.network.get(&self.locator)?; |
| 65 | + let client = network.rpc_client()?; |
| 66 | + client |
| 67 | + .verify_network_passphrase(Some(&network.network_passphrase)) |
| 68 | + .await?; |
| 69 | + |
| 70 | + let address = tx_source_acct.to_string(); |
| 71 | + |
| 72 | + let account = client.get_account(&address).await?; |
| 73 | + Ok(*account.seq_num.as_ref()) |
| 74 | + } |
| 75 | +} |
0 commit comments