Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ toml = "0.8"
[dev-dependencies]
httpmock = "0.7"
serde_json = "1"
wiremock = "0.6"
244 changes: 30 additions & 214 deletions packages/core/src/explain/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use serde::{Deserialize, Serialize};

use crate::explain::memo::explain_memo;
use crate::models::fee::FeeStats;
use crate::models::transaction::Transaction;
use crate::explain::memo::explain_memo;

use super::operation::payment::{explain_payment, PaymentExplanation};
use super::operation::payment::{
explain_payment,
explain_payment_with_fee,
PaymentExplanation,
};

/// Complete explanation of a transaction.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand All @@ -21,6 +25,8 @@ pub struct TransactionExplanation {
/// Human-readable explanation of the transaction memo.
/// None if the transaction has no memo.
pub memo_explanation: Option<String>,
/// Human-readable explanation of transaction fee context.
pub fee_explanation: Option<String>,
}

/// Result type for transaction explanation.
Expand Down Expand Up @@ -72,7 +78,6 @@ pub fn explain_transaction(
transaction: &Transaction,
fee_stats: Option<&FeeStats>,
) -> ExplainResult {
pub fn explain_transaction(transaction: &Transaction) -> ExplainResult {
let total_operations = transaction.operations.len();

if total_operations == 0 {
Expand All @@ -85,21 +90,15 @@ pub fn explain_transaction(transaction: &Transaction) -> ExplainResult {
let payment_explanations = transaction
.payment_operations()
.into_iter()
.map(|payment| explain_payment(payment))
.map(|payment| match fee_stats {
Some(stats) => explain_payment_with_fee(payment, transaction.fee_charged, stats),
None => explain_payment(payment),
})
.collect::<Vec<_>>();

let summary = build_transaction_summary(
transaction.successful,
payment_count,
skipped_operations,
);

let memo_explanation = transaction.memo.as_ref().and_then(|m| explain_memo(m));

let fee_explanation = explain_fee(transaction.fee_charged, fee_stats);

// Wire in memo explanation — None if transaction has no memo
let memo_explanation = transaction.memo.as_ref().and_then(|m| explain_memo(m));
let summary = build_transaction_summary(transaction.successful, payment_count, skipped_operations);
let memo_explanation = transaction.memo.as_ref().and_then(explain_memo);
let fee_explanation = Some(explain_fee(transaction.fee_charged, fee_stats));

Ok(TransactionExplanation {
transaction_hash: transaction.hash.clone(),
Expand Down Expand Up @@ -146,14 +145,13 @@ fn build_transaction_summary(successful: bool, payment_count: usize, skipped: us
#[cfg(test)]
mod tests {
use super::*;
use crate::models::fee::FeeStats;
use crate::models::memo::Memo;
use crate::models::operation::{OtherOperation, PaymentOperation, Operation};
use crate::models::operation::{Operation, OtherOperation, PaymentOperation};

fn create_payment_operation(id: &str, amount: &str) -> Operation {
Operation::Payment(PaymentOperation {
id: id.to_string(),
source_account: None,
source_account: Some("GSENDER".to_string()),
destination: "GDESTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX".to_string(),
asset_type: "native".to_string(),
asset_code: None,
Expand All @@ -169,10 +167,6 @@ mod tests {
})
}

fn default_fee_stats() -> FeeStats {
FeeStats::default_network_fees()
}

#[test]
fn test_explain_fee_standard() {
let stats = FeeStats::new(100, 100, 5000, 100, 250);
Expand All @@ -190,40 +184,7 @@ mod tests {
}

#[test]
fn test_explain_fee_no_stats_fallback() {
let result = explain_fee(100, None);
assert!(result.contains("0.0000100"));
// No context — just the raw amount
assert!(!result.contains("standard"));
assert!(!result.contains("above average"));
}

#[test]
fn test_explain_transaction_includes_fee_explanation() {
fn test_explain_single_payment_no_memo() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
memo: None,
};
let stats = default_fee_stats();
let explanation = explain_transaction(&tx, Some(&stats)).unwrap();
assert!(!explanation.fee_explanation.is_empty());
assert!(explanation.fee_explanation.contains("standard"));

let explanation = explain_transaction(&tx).unwrap();
assert_eq!(explanation.transaction_hash, "abc123");
assert!(explanation.successful);
assert_eq!(explanation.payment_explanations.len(), 1);
assert_eq!(explanation.skipped_operations, 0);
assert!(explanation.summary.contains("1 payment"));
assert_eq!(explanation.memo_explanation, None);
}

#[test]
fn test_explain_transaction_with_text_memo() {
fn test_explain_transaction_with_memo() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
Expand All @@ -232,123 +193,33 @@ mod tests {
memo: Some(Memo::text("Invoice #12345").unwrap()),
};

let explanation = explain_transaction(&tx).unwrap();
assert!(explanation.memo_explanation.is_some());
let memo_text = explanation.memo_explanation.unwrap();
assert!(memo_text.contains("Invoice #12345"));
assert!(memo_text.contains("text memo"));
}

#[test]
fn test_explain_transaction_with_id_memo() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
memo: Some(Memo::id(987654321)),
};

let explanation = explain_transaction(&tx).unwrap();
let explanation = explain_transaction(&tx, None).unwrap();
assert_eq!(explanation.transaction_hash, "abc123");
assert!(explanation.memo_explanation.is_some());
let memo_text = explanation.memo_explanation.unwrap();
assert!(memo_text.contains("987654321"));
assert!(memo_text.contains("ID memo"));
}

#[test]
fn test_explain_transaction_memo_none_variant() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
memo: Some(Memo::None),
};

let explanation = explain_transaction(&tx).unwrap();
// Memo::None should produce no explanation
assert_eq!(explanation.memo_explanation, None);
}

#[test]
fn test_explain_transaction_high_fee() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 10000,
operations: vec![create_payment_operation("1", "50.0")],
memo: None,
};
let stats = default_fee_stats();
let explanation = explain_transaction(&tx, Some(&stats)).unwrap();
assert!(explanation.fee_explanation.contains("above average"));
}

#[test]
fn test_explain_transaction_fee_stats_fallback() {

let explanation = explain_transaction(&tx).unwrap();
assert_eq!(explanation.payment_explanations.len(), 3);
assert_eq!(explanation.skipped_operations, 0);
assert!(explanation.summary.contains("3 payments"));
assert_eq!(explanation.memo_explanation, None);
assert!(explanation
.memo_explanation
.unwrap()
.contains("Invoice #12345"));
assert!(explanation.fee_explanation.is_some());
}

#[test]
fn test_explain_no_payments_returns_ok() {
let tx = Transaction {
hash: "abc123".to_string(),
hash: "ghi789".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
operations: vec![create_other_operation("1"), create_other_operation("2")],
memo: None,
};
// No fee stats available — should not panic, should produce basic message
let explanation = explain_transaction(&tx, None).unwrap();
assert!(!explanation.fee_explanation.is_empty());
}

#[test]
fn test_explain_single_payment_no_memo() {

// Non-payment transactions should return Ok with empty payment_explanations
let result = explain_transaction(&tx);
let result = explain_transaction(&tx, None);
assert!(result.is_ok());
let explanation = result.unwrap();
assert_eq!(explanation.payment_explanations.len(), 0);
assert_eq!(explanation.skipped_operations, 2);
}

#[test]
fn test_explain_empty_transaction_returns_err() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
memo: None,
};
let explanation = explain_transaction(&tx, None).unwrap();
assert_eq!(explanation.transaction_hash, "abc123");
assert_eq!(explanation.payment_explanations.len(), 1);
assert_eq!(explanation.memo_explanation, None);
}

#[test]
fn test_explain_transaction_with_text_memo() {
let tx = Transaction {
hash: "abc123".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_payment_operation("1", "50.0")],
memo: Some(Memo::text("Invoice #12345").unwrap()),
};
let explanation = explain_transaction(&tx, None).unwrap();
assert!(explanation.memo_explanation.is_some());
assert!(explanation.memo_explanation.unwrap().contains("Invoice #12345"));
}

#[test]
fn test_explain_empty_transaction_returns_err() {
let tx = Transaction {
Expand All @@ -358,68 +229,13 @@ mod tests {
operations: vec![],
memo: None,
};
assert!(explain_transaction(&tx, None).is_err());

let explanation = explain_transaction(&tx).unwrap();
assert_eq!(explanation.payment_explanations.len(), 2);
assert_eq!(explanation.skipped_operations, 3);
assert!(explanation.summary.contains("2 payments"));
assert!(explanation.summary.contains("3 other operations were skipped"));
assert_eq!(explanation.memo_explanation, None);
}

#[test]
fn test_explain_no_payments_returns_ok() {
let tx = Transaction {
hash: "ghi789".to_string(),
successful: true,
fee_charged: 100,
operations: vec![create_other_operation("1"), create_other_operation("2")],
memo: None,
};
let result = explain_transaction(&tx, None);
assert!(result.is_ok());
let explanation = result.unwrap();
assert_eq!(explanation.payment_explanations.len(), 0);
assert_eq!(explanation.skipped_operations, 2);

let explanation = explain_transaction(&tx).unwrap();
assert!(!explanation.successful);
assert!(explanation.summary.contains("failed"));
assert_eq!(explanation.memo_explanation, None);
}

#[test]
fn test_build_transaction_summary_single_payment() {
let summary = build_transaction_summary(true, 1, 0);
assert_eq!(summary, "This successful transaction contains 1 payment.");
}

#[test]
fn test_build_transaction_summary_multiple_payments() {
let summary = build_transaction_summary(true, 3, 0);
assert_eq!(summary, "This successful transaction contains 3 payments.");
}

#[test]
fn test_build_transaction_summary_with_skipped() {
let summary = build_transaction_summary(true, 2, 3);
assert_eq!(
summary,
"This successful transaction contains 2 payments. 3 other operations were skipped."
);
}

#[test]
fn test_build_transaction_summary_no_payments() {
let summary = build_transaction_summary(true, 0, 2);
assert!(summary.contains("does not yet support"));
assert!(summary.contains("2 operations"));
assert!(explain_transaction(&tx, None).is_err());
}

#[test]
fn test_build_transaction_summary_failed() {
let summary = build_transaction_summary(false, 1, 0);
assert_eq!(summary, "This failed transaction contains 1 payment.");
}
}
}
4 changes: 3 additions & 1 deletion packages/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

pub mod errors;
pub mod explain;
pub mod middleware;
pub mod models;
pub mod services;
pub mod routes;
pub mod services;
Loading
Loading