whisky/builder/
tx_eval.rs1use crate::*;
2use async_trait::async_trait;
3use uplc::tx::SlotConfig;
4use whisky_common::Evaluator;
5
6#[derive(Clone, Debug)]
7pub struct OfflineTxEvaluator {}
8
9impl OfflineTxEvaluator {
10 pub fn new() -> Self {
11 OfflineTxEvaluator {}
12 }
13}
14
15impl Default for OfflineTxEvaluator {
16 fn default() -> Self {
17 OfflineTxEvaluator::new()
18 }
19}
20
21impl OfflineTxEvaluator {
22 fn evaluate_tx_sync(
23 &self,
24 tx_hex: &str,
25 inputs: &[UTxO],
26 additional_txs: &[String],
27 network: &Network,
28 slot_config: &SlotConfig,
29 ) -> Result<Vec<Action>, WError> {
30 consolidate_errors(evaluate_tx_scripts(
31 tx_hex,
32 inputs,
33 additional_txs,
34 network,
35 slot_config,
36 )?)
37 }
38}
39
40fn consolidate_errors(eval_results: Vec<EvalResult>) -> Result<Vec<Action>, WError> {
41 let mut actions = Vec::new();
42 let mut errors_texts = Vec::new();
43 for eval_result in eval_results {
44 match eval_result {
45 EvalResult::Success(action) => actions.push(action),
46 EvalResult::Error(error) => {
47 errors_texts.push(format!("Error at index: [ {} ] - Budget: [ {:?} ] - Tag: [ {:?} ] - Error message: [ {} ] - Logs: [ {:?} ]",
48 error.index, error.budget, error.tag, error.error_message, error.logs));
49 }
50 }
51 }
52 if errors_texts.is_empty() {
53 Ok(actions)
54 } else {
55 Err(WError::new(
56 "consolidate_errors",
57 &format!("Errors found during evaluation: [ {:?} ]", errors_texts),
58 ))
59 }
60}
61
62#[async_trait]
63impl Evaluator for OfflineTxEvaluator {
64 async fn evaluate_tx(
65 &self,
66 tx_hex: &str,
67 inputs: &[UTxO],
68 additional_txs: &[String],
69 network: &Network,
70 slot_config: &SlotConfig,
71 ) -> Result<Vec<Action>, WError> {
72 self.evaluate_tx_sync(tx_hex, inputs, additional_txs, network, slot_config)
73 }
74}