Skip to content

Commit

Permalink
log execution to return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Jun 28, 2023
1 parent 6de2969 commit 6da847e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions libs/marinade-client-rs/src/transaction_executors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anchor_client::RequestBuilder;
use anyhow::anyhow;
use anyhow::{anyhow, bail};
use log::{debug, error, info, warn};
use solana_client::client_error::ClientErrorKind;
use solana_client::rpc_client::RpcClient;
Expand All @@ -25,7 +25,7 @@ impl<'a, C: Deref<Target = impl Signer> + Clone> TransactionExecutor for Request
}
}

pub fn log_execution(execution_result: Result<Signature, anchor_client::ClientError>) {
pub fn log_execution(execution_result: Result<Signature, anchor_client::ClientError>) -> anyhow::Result<()>{
match execution_result {
Ok(signature) => debug!("Transaction {}", signature),
Err(err) => {
Expand Down Expand Up @@ -56,9 +56,11 @@ pub fn log_execution(execution_result: Result<Signature, anchor_client::ClientEr
_ => {
error!("Transaction ERR {:?}", err);
}
bail!("Transaction error: {}", err);
}
}
}
Ok(())
}

pub trait TransactionSimulator {
Expand All @@ -74,7 +76,7 @@ impl<'a, C: Deref<Target = impl Signer> + Clone> TransactionSimulator for Reques
}
}

pub fn log_simulation(simulation_result: RpcResult<RpcSimulateTransactionResult>) {
pub fn log_simulation(simulation_result: RpcResult<RpcSimulateTransactionResult>) -> anyhow::Result<()> {
match simulation_result {
Ok(result) => {
if let Some(logs) = &result.value.logs {
Expand All @@ -84,6 +86,7 @@ pub fn log_simulation(simulation_result: RpcResult<RpcSimulateTransactionResult>
}
if result.value.err.is_some() {
error!("Transaction ERR {:?}", result);
bail!("Transaction error: {}", err);
} else {
info!("Transaction simulation Ok");
}
Expand All @@ -109,8 +112,10 @@ pub fn log_simulation(simulation_result: RpcResult<RpcSimulateTransactionResult>
}
error!("Transaction ERR {:?}", err);
}
bail!("Transaction error: {}", err);
}
}
Ok(())
}

pub fn execute<'a, I, C>(
Expand All @@ -126,15 +131,15 @@ where
let commitment_level = rpc_client.commitment().commitment;
anchor_builders
.into_iter()
.for_each(|builder| log_execution(builder.execute(commitment_level)));
.try_for_each(|builder| log_execution(builder.execute(commitment_level)))?;
} else {
let mut builders_iterator = anchor_builders.into_iter();
log_simulation(
builders_iterator
.next()
.ok_or(anyhow!("No transactions to simulate"))?
.simulate(rpc_client),
);
)?;
if builders_iterator.next().is_some() {
warn!(
"Simulation mode: only the first transaction was simulated. The rest are ignored."
Expand Down

0 comments on commit 6da847e

Please sign in to comment.