Skip to content

Commit

Permalink
fix: address comments and use new tx.into() for tx_env for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jun 21, 2024
1 parent d62de52 commit 0a2e72e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
14 changes: 3 additions & 11 deletions cmd/crates/soroban-test/tests/it/integration/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,20 @@ async fn txn_simulate() {
let sandbox = &TestEnv::new();
let xdr_base64_build_only = deploy_contract(sandbox, HELLO_WORLD, DeployKind::BuildOnly).await;
let xdr_base64_sim_only = deploy_contract(sandbox, HELLO_WORLD, DeployKind::SimOnly).await;
println!("{xdr_base64_build_only}");
let cmd = tx::simulate::Cmd::default();
let tx_env =
TransactionEnvelope::from_xdr_base64(&xdr_base64_build_only, Limits::none()).unwrap();
let TransactionEnvelope::Tx(TransactionV1Envelope { tx, .. }) = &tx_env else {
panic!("Only transaction v1 is supported")
};
let assembled = cmd.simulate(tx, &sandbox.client()).await.unwrap();
let tx = soroban_cli::commands::tx::xdr::unwrap_envelope_v1(tx_env).unwrap();
let assembled_str = sandbox
.new_assert_cmd("tx")
.arg("simulate")
.write_stdin(xdr_base64_build_only.as_bytes())
.assert()
.success()
.stdout_as_str();
println!("{assembled_str}");
assert_eq!(xdr_base64_sim_only, assembled_str);
let txn_env = TransactionEnvelope::Tx(TransactionV1Envelope {
tx: assembled.transaction().clone(),
signatures: VecM::default(),
});

let assembled = cmd.simulate(tx, &sandbox.client()).await.unwrap();
let txn_env: TransactionEnvelope = assembled.transaction().clone().into();
assert_eq!(
txn_env.to_xdr_base64(Limits::none()).unwrap(),
assembled_str
Expand Down
25 changes: 5 additions & 20 deletions cmd/soroban-cli/src/commands/tx/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::xdr::{self, Transaction, TransactionEnvelope, TransactionV1Envelope, VecM, WriteXdr};
use crate::xdr::{self, TransactionEnvelope, WriteXdr};
use async_trait::async_trait;
use soroban_rpc::Assembled;

Expand Down Expand Up @@ -30,25 +30,10 @@ impl Cmd {
let res = self
.run_against_rpc_server(Some(global_args), Some(&self.config))
.await?;
let tx = res.transaction().clone();
println!(
"{}",
TransactionEnvelope::Tx(TransactionV1Envelope {
tx,
signatures: VecM::default()
})
.to_xdr_base64(xdr::Limits::none())?
);
let tx_env: TransactionEnvelope = res.transaction().clone().into();
println!("{}", tx_env.to_xdr_base64(xdr::Limits::none())?);
Ok(())
}

pub async fn simulate(
&self,
tx: &Transaction,
client: &crate::rpc::Client,
) -> Result<Assembled, Error> {
Ok(client.simulate_and_assemble_transaction(tx).await?)
}
}

#[async_trait]
Expand All @@ -64,7 +49,7 @@ impl NetworkRunnable for Cmd {
let config = config.unwrap_or(&self.config);
let network = config.get_network()?;
let client = crate::rpc::Client::new(&network.rpc_url)?;
let tx = super::xdr::unwrap_envelope_v1()?;
self.simulate(&tx, &client).await
let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_stdin()?)?;
Ok(client.simulate_and_assemble_transaction(&tx).await?)
}
}
5 changes: 2 additions & 3 deletions cmd/soroban-cli/src/commands/tx/xdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ pub fn from_stdin<T: ReadXdr>() -> Result<T, Error> {
T::from_xdr_base64(buf.trim(), Limits::none()).map_err(|_| Error::StdinDecode)
}

pub fn unwrap_envelope_v1() -> Result<Transaction, Error> {
let TransactionEnvelope::Tx(TransactionV1Envelope { tx, .. }) = tx_envelope_from_stdin()?
else {
pub fn unwrap_envelope_v1(tx_env: TransactionEnvelope) -> Result<Transaction, Error> {
let TransactionEnvelope::Tx(TransactionV1Envelope { tx, .. }) = tx_env else {
return Err(Error::OnlyTransactionV1Supported);
};
Ok(tx)
Expand Down

0 comments on commit 0a2e72e

Please sign in to comment.