pub struct TransactionResult { /* private fields */ }Expand description
Wrapper around LiteSVM’s TransactionMetadata with helper methods for testing
This struct provides convenient methods for analyzing transaction results, including log inspection, compute unit tracking, and success assertions.
§Example
let result = svm.send_instruction(ix, &[&signer])?;
result.assert_success();
assert!(result.has_log("Transfer complete"));
println!("Used {} compute units", result.compute_units());Implementations§
Source§impl TransactionResult
impl TransactionResult
Sourcepub fn new(
result: TransactionMetadata,
instruction_name: Option<String>,
) -> Self
pub fn new( result: TransactionMetadata, instruction_name: Option<String>, ) -> Self
Create a new TransactionResult wrapper for successful transaction
§Arguments
result- The transaction metadata from LiteSVMinstruction_name- Optional name of the instruction for debugging
Sourcepub fn new_failed(
error: String,
result: TransactionMetadata,
instruction_name: Option<String>,
) -> Self
pub fn new_failed( error: String, result: TransactionMetadata, instruction_name: Option<String>, ) -> Self
Create a new TransactionResult wrapper for failed transaction
§Arguments
error- The error messageresult- The transaction metadata from LiteSVMinstruction_name- Optional name of the instruction for debugging
Sourcepub fn assert_success(&self) -> &Self
pub fn assert_success(&self) -> &Self
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Sourcepub fn error(&self) -> Option<&String>
pub fn error(&self) -> Option<&String>
Get the error message if the transaction failed
§Returns
The error message if the transaction failed, None otherwise
Sourcepub fn compute_units(&self) -> u64
pub fn compute_units(&self) -> u64
Sourcepub fn print_logs(&self)
pub fn print_logs(&self)
Print the transaction logs
Sourcepub fn inner(&self) -> &TransactionMetadata
pub fn inner(&self) -> &TransactionMetadata
Get the inner TransactionMetadata for direct access
Sourcepub fn assert_failure(&self) -> &Self
pub fn assert_failure(&self) -> &Self
Sourcepub fn assert_error(&self, expected_error: &str) -> &Self
pub fn assert_error(&self, expected_error: &str) -> &Self
Assert that the transaction failed with a specific error message
§Arguments
expected_error- The expected error message (substring match)
§Panics
Panics if the transaction succeeded or failed with a different error
§Returns
Returns self for chaining
§Example
result.assert_error("insufficient funds");Sourcepub fn assert_error_code(&self, error_code: u32) -> &Self
pub fn assert_error_code(&self, error_code: u32) -> &Self
Assert that the transaction failed with a specific error code
This is useful for asserting Anchor custom errors.
§Arguments
error_code- The expected error code number
§Panics
Panics if the transaction succeeded or failed with a different error code
§Returns
Returns self for chaining
§Example
// Assert that transaction failed with custom error code 6000
result.assert_error_code(6000);Sourcepub fn assert_anchor_error(&self, error_name: &str) -> &Self
pub fn assert_anchor_error(&self, error_name: &str) -> &Self
Assert that the transaction failed with a specific Anchor error
This checks for Anchor’s error code format in the logs.
§Arguments
error_name- The name of the Anchor error
§Panics
Panics if the transaction succeeded or the error wasn’t found in logs
§Returns
Returns self for chaining
§Example
// Assert that transaction failed with Anchor error
result.assert_anchor_error("InsufficientFunds");Sourcepub fn assert_log_error(&self, error_message: &str) -> &Self
pub fn assert_log_error(&self, error_message: &str) -> &Self
Assert that the logs contain a specific error message
Unlike assert_error, this only checks the logs, not the error field.
§Arguments
error_message- The expected error message in logs
§Panics
Panics if the error message is not found in logs
§Returns
Returns self for chaining
§Example
result.assert_log_error("Transfer amount exceeds balance");Trait Implementations§
Auto Trait Implementations§
impl Freeze for TransactionResult
impl RefUnwindSafe for TransactionResult
impl Send for TransactionResult
impl Sync for TransactionResult
impl Unpin for TransactionResult
impl UnwindSafe for TransactionResult
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more