Skip to content

Commit

Permalink
feat: add methods to GetTransactionResponse
Browse files Browse the repository at this point in the history
And split up prepare_and_send_transaction to allow creating an assembled transaction and passing it when sending a transaction.
  • Loading branch information
willemneal committed Jan 17, 2024
1 parent 7ee51fa commit 7151f3b
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 85 deletions.
24 changes: 21 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,40 @@ version = "=20.0.2"
default-features = true

[workspace.dependencies]
stellar-strkey = "0.0.7"
sep5 = "0.0.2"
base64 = "0.21.2"
thiserror = "1.0.46"
sha2 = "0.10.7"
ethnum = "1.3.2"
hex = "0.4.3"
itertools = "0.10.0"
sep5 = "0.0.2"

serde-aux = "4.1.2"
serde_json = "1.0.82"
serde = "1.0.82"
stellar-strkey = "0.0.7"

clap = { version = "4.1.8", features = [
"derive",
"env",
"deprecated",
"string",
] }
clap_complete = "4.1.4"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tracing-appender = "0.2.2"
which = "4.4.0"
wasmparser = "0.90.0"

termcolor = "1.1.3"
termcolor_output = "1.0.1"
ed25519-dalek = "2.0.0"

# networking
http = "1.0.0"
jsonrpsee-http-client = "0.20.1"
jsonrpsee-core = "0.20.1"
tokio = "1.28.1"

# [patch."https://github.com/stellar/rs-soroban-env"]
# soroban-env-host = { path = "../rs-soroban-env/soroban-env-host/" }
Expand Down
5 changes: 3 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ async fn contract_data_read() {
const KEY: &str = "COUNTER";
let sandbox = &TestEnv::default();
let id = &deploy_hello(sandbox);
println!("{id}");
let res = sandbox.invoke(&["--id", id, "--", "inc"]).await.unwrap();
assert_eq!(res.trim(), "1");
extend(sandbox, id, Some(KEY)).await;

sandbox
.new_assert_cmd("contract")
.arg("read")
Expand All @@ -207,6 +206,8 @@ async fn contract_data_read() {
.success()
.stdout(predicates::str::starts_with("COUNTER,1"));

extend(sandbox, id, Some(KEY)).await;

sandbox
.new_assert_cmd("contract")
.arg("invoke")
Expand Down
14 changes: 8 additions & 6 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,34 @@ soroban-spec-typescript = { workspace = true }
soroban-ledger-snapshot = { workspace = true }
stellar-strkey = { workspace = true }
soroban-sdk = { workspace = true }
clap = { version = "4.1.8", features = [

clap = { workspace = true, features = [
"derive",
"env",
"deprecated",
"string",
] }
clap_complete = {workspace = true}

base64 = { workspace = true }
thiserror = { workspace = true }
serde = "1.0.82"
serde_derive = "1.0.82"
serde_json = "1.0.82"
serde-aux = "4.1.2"
serde-aux = { workspace = true }
hex = { workspace = true }
num-bigint = "0.4"
tokio = { version = "1", features = ["full"] }
termcolor = "1.1.3"
termcolor_output = "1.0.1"
clap_complete = "4.1.4"
termcolor = { workspace = true }
termcolor_output = { workspace = true }
rand = "0.8.5"
wasmparser = { workspace = true }
sha2 = { workspace = true }
csv = "1.1.6"
ed25519-dalek = "=2.0.0"
jsonrpsee-http-client = "0.20.1"
jsonrpsee-core = "0.20.1"
hyper = "0.14.27"
hyper = "0.14.27"
hyper-tls = "0.5"
http = "0.2.9"
regex = "1.6.0"
Expand Down
9 changes: 6 additions & 3 deletions cmd/soroban-cli/src/commands/contract/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,18 @@ impl Cmd {
}),
};

let (result, meta, events) = client
let res = client
.prepare_and_send_transaction(&tx, &key, &[], &network.network_passphrase, None, None)
.await?;

tracing::trace!(?result);
tracing::trace!(?meta);
let events = res.events()?;
if !events.is_empty() {
tracing::info!("Events:\n {events:#?}");
}
let meta = res
.result_meta
.as_ref()
.ok_or(Error::MissingOperationResult)?;

// The transaction from core will succeed regardless of whether it actually found & extended
// the entry, so we have to inspect the result meta to tell if it worked or not.
Expand Down
14 changes: 6 additions & 8 deletions cmd/soroban-cli/src/commands/contract/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,10 @@ impl Cmd {
build_install_contract_code_tx(contract, sequence + 1, self.fee.fee, &key)?;

// Currently internal errors are not returned if the contract code is expired
if let (
TransactionResult {
result: TransactionResultResult::TxInternalError,
..
},
_,
_,
) = client
if let Some(TransactionResult {
result: TransactionResultResult::TxInternalError,
..
}) = client
.prepare_and_send_transaction(
&tx_without_preflight,
&key,
Expand All @@ -129,6 +125,8 @@ impl Cmd {
None,
)
.await?
.result
.as_ref()
{
// Now just need to restore it and don't have to install again
restore::Cmd {
Expand Down
44 changes: 25 additions & 19 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub struct Cmd {
/// Output the cost execution to stderr
#[arg(long = "cost")]
pub cost: bool,
/// Number of instructions to simulate
#[arg(long)]
pub instructions: Option<u32>,
/// Function name as subcommand, then arguments for that function as `--arg-name value`
#[arg(last = true, id = "CONTRACT_FN_AND_ARGS")]
pub slop: Vec<OsString>,
Expand Down Expand Up @@ -300,28 +303,31 @@ impl Cmd {
&key,
)?;

let (result, meta, events) = client
.prepare_and_send_transaction(
&tx,
&key,
&signers,
&network.network_passphrase,
Some(log_events),
(global_args.verbose || global_args.very_verbose || self.cost)
.then_some(log_resources),
let mut txn = client.create_assembled_transaction(&tx).await?;
let (return_value, events) = if txn.is_view() {
(
txn.sim_res().results()?[0].xdr.clone(),
txn.sim_res().events()?,
)
.await?;

tracing::debug!(?result);
crate::log::diagnostic_events(&events, tracing::Level::INFO);
let xdr::TransactionMeta::V3(xdr::TransactionMetaV3 {
soroban_meta: Some(xdr::SorobanTransactionMeta { return_value, .. }),
..
}) = meta
else {
return Err(Error::MissingOperationResult);
} else {
if let Some(instructions) = self.instructions {
txn = txn.set_max_instructions(instructions);
}
let res = client
.send_assembled_transaction(
txn,
&key,
&signers,
&network.network_passphrase,
Some(log_events),
(global_args.verbose || global_args.very_verbose || self.cost)
.then_some(log_resources),
)
.await?;
(res.return_value()?, res.contract_events()?)
};

crate::log::diagnostic_events(&events, tracing::Level::INFO);
output_to_string(&spec, &return_value, &function)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Cmd {
let (
LedgerKey::ContractData(LedgerKeyContractData { key, .. }),
LedgerEntryData::ContractData(ContractDataEntry { val, .. }),
) = (key, val)
) = &(key, val)
else {
return Err(Error::OnlyDataAllowed);
};
Expand Down
10 changes: 7 additions & 3 deletions cmd/soroban-cli/src/commands/contract/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ impl Cmd {
}),
};

let (result, meta, events) = client
let res = client
.prepare_and_send_transaction(&tx, &key, &[], &network.network_passphrase, None, None)
.await?;

tracing::trace!(?result);
let meta = res
.result_meta
.as_ref()
.ok_or(Error::MissingOperationResult)?;
let events = res.events()?;
tracing::trace!(?meta);
if !events.is_empty() {
tracing::info!("Events:\n {events:#?}");
Expand All @@ -177,7 +181,7 @@ impl Cmd {
operations[0].changes.len()
);
}
parse_operations(&operations).ok_or(Error::MissingOperationResult)
parse_operations(operations).ok_or(Error::MissingOperationResult)
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/soroban-cli/src/rpc/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod diagnostic_events;
pub use diagnostic_events::*;
11 changes: 11 additions & 0 deletions cmd/soroban-cli/src/rpc/log/diagnostic_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn diagnostic_events(events: &[impl std::fmt::Debug], level: tracing::Level) {
for (i, event) in events.iter().enumerate() {
if level == tracing::Level::TRACE {
tracing::trace!("{i}: {event:#?}");
} else if level == tracing::Level::INFO {
tracing::info!("{i}: {event:#?}");
} else if level == tracing::Level::ERROR {
tracing::error!("{i}: {event:#?}");
}
}
}
Loading

0 comments on commit 7151f3b

Please sign in to comment.