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

Fix/invoke view only #1697

Merged
merged 14 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions cmd/soroban-cli/src/commands/contract/arg_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ pub fn build_host_function_parameters(
}
cmd.build();
let long_help = cmd.render_long_help();

match cmd.clone().try_get_matches_from(slop) {
elizabethengelman marked this conversation as resolved.
Show resolved Hide resolved
Ok(m) => m,
Err(e) => {
println!("Failed to match subcommand: {}", e);
std::process::exit(1);
}
};

let mut matches_ = cmd.get_matches_from(slop);
let Some((function, matches_)) = &matches_.remove_subcommand() else {
println!("{long_help}");
Expand Down
61 changes: 44 additions & 17 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{fmt::Debug, fs, io};

use clap::{arg, command, Parser, ValueEnum};

use soroban_rpc::{SimulateHostFunctionResult, SimulateTransactionResponse};
use soroban_rpc::{Client, SimulateHostFunctionResult, SimulateTransactionResponse};
use soroban_spec::read::FromWasmError;

use super::super::events;
Expand Down Expand Up @@ -163,7 +163,7 @@ impl Cmd {
.transpose()
}

fn send(&self, sim_res: &SimulateTransactionResponse) -> Result<ShouldSend, Error> {
fn should_send_tx(&self, sim_res: &SimulateTransactionResponse) -> Result<ShouldSend, Error> {
elizabethengelman marked this conversation as resolved.
Show resolved Hide resolved
Ok(match self.send {
Send::Default => {
if self.is_view {
Expand All @@ -179,6 +179,27 @@ impl Cmd {
Send::Yes => ShouldSend::Yes,
})
}

async fn check_should_send_tx_with_default_account(
&self,
host_function_params: InvokeContractArgs,
rpc_client: Client,
) -> Result<ShouldSend, Error> {
let account_details = default_account_entry();
let sequence: i64 = account_details.seq_num.into();
let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id;

let tx = build_invoke_contract_tx(
host_function_params.clone(),
sequence + 1,
self.fee.fee,
account_id,
)?;
let txn = simulate_and_assemble_transaction(&rpc_client, &tx).await?;
let txn = self.fee.apply_to_assembled_txn(txn); // do we need this part?
let sim_res = txn.sim_response();
self.should_send_tx(sim_res)
}
}

#[async_trait::async_trait]
Expand All @@ -205,19 +226,6 @@ impl NetworkRunnable for Cmd {
let _ = build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?;
}
let client = network.rpc_client()?;
let account_details = if self.is_view {
default_account_entry()
} else {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;

client
.get_account(&config.source_account()?.to_string())
.await?
};
let sequence: i64 = account_details.seq_num.into();
let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id;

let spec_entries = get_remote_contract_spec(
&contract_id.0,
Expand All @@ -229,9 +237,27 @@ impl NetworkRunnable for Cmd {
.await
.map_err(Error::from)?;

// Get the ledger footprint
let (function, spec, host_function_params, signers) =
build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?;

let should_send_tx = self
.check_should_send_tx_with_default_account(host_function_params.clone(), client.clone())
.await?;

let account_details = if should_send_tx == ShouldSend::Yes {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;

client
.get_account(&config.source_account()?.to_string())
.await?
} else {
default_account_entry()
};
let sequence: i64 = account_details.seq_num.into();
let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id;

let tx = build_invoke_contract_tx(
host_function_params.clone(),
sequence + 1,
Expand All @@ -250,7 +276,7 @@ impl NetworkRunnable for Cmd {
if global_args.map_or(true, |a| !a.no_cache) {
data::write(sim_res.clone().into(), &network.rpc_uri()?)?;
}
let should_send = self.send(sim_res)?;
let should_send = self.should_send_tx(sim_res)?;
let (return_value, events) = match should_send {
ShouldSend::Yes => {
let global::Args { no_cache, .. } = global_args.cloned().unwrap_or_default();
Expand Down Expand Up @@ -365,6 +391,7 @@ pub enum Send {
Yes,
}

#[derive(Debug, PartialEq)]
enum ShouldSend {
DefaultNo,
No,
Expand Down
Loading