Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

token-client: Check simulation results for error #7523

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions token/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait SimulateTransaction {
/// Trait for the output of a simulation
pub trait SimulationResult {
fn get_compute_units_consumed(&self) -> ProgramClientResult<u64>;
fn err(self) -> Option<ProgramClientError>;
}

/// Extends basic `SendTransaction` trait with function `send` where client is
Expand Down Expand Up @@ -78,6 +79,9 @@ impl SimulationResult for BanksTransactionResultWithSimulation {
.map(|x| x.units_consumed)
.ok_or("No simulation results found".into())
}
fn err(self) -> Option<ProgramClientError> {
self.result.and_then(|r| r.err().map(|e| e.into()))
}
}

impl SimulateTransaction for ProgramBanksClientProcessTransaction {
Expand Down Expand Up @@ -165,6 +169,15 @@ impl SimulationResult for RpcClientResponse {
.ok_or("No simulation results found".into()),
}
}
fn err(self) -> Option<ProgramClientError> {
match self {
// `Transaction` is the result of an offline simulation. The error
// should be properly handled by a caller that supports offline
// signing
Self::Signature(_) | Self::Transaction(_) => Some("Not a simulation result".into()),
Self::Simulation(simulation_result) => simulation_result.err.map(|e| e.into()),
}
}
}

impl SimulateTransaction for ProgramRpcClientSendTransaction {
Expand Down
3 changes: 3 additions & 0 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ where
let units_consumed = simulation_result
.get_compute_units_consumed()
.map_err(TokenError::Client)?;
if let Some(err) = simulation_result.err() {
return Err(TokenError::Client(err));
}
// Overwrite the compute unit limit instruction with the actual units consumed
let compute_unit_limit =
u32::try_from(units_consumed).map_err(|x| TokenError::Client(x.into()))?;
Expand Down
Loading