whisky/builder/
tx_eval.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use async_trait::async_trait;
use sidan_csl_rs::csl::JsError;
use sidan_csl_rs::model::{Action, EvalResult, Network, UTxO};
use sidan_csl_rs::core::utils::evaluate_tx_scripts;
use uplc::tx::SlotConfig;
use crate::service::Evaluator;

#[derive(Clone, Debug)]
pub struct MeshTxEvaluator {}

impl MeshTxEvaluator {
    pub fn new() -> Self {
        MeshTxEvaluator {}
    }
}

impl Default for MeshTxEvaluator {
    fn default() -> Self {
        MeshTxEvaluator::new()
    }
}

impl MeshTxEvaluator {
    fn evaluate_tx_sync(
        &self,
        tx_hex: &str,
        inputs: &[UTxO],
        additional_txs: &[String],
        network: &Network,
        slot_config: &SlotConfig
    ) -> Result<Vec<Action>, JsError> {
        consolidate_errors(evaluate_tx_scripts(tx_hex, inputs, additional_txs, network, slot_config)?)
    }
}

fn consolidate_errors(eval_results: Vec<EvalResult>) -> Result<Vec<Action>, JsError> {
    let mut actions = Vec::new();
    let mut errors_texts = Vec::new();
    for eval_result in eval_results {
        match eval_result {
            EvalResult::Success(action) => actions.push(action),
            EvalResult::Error(error) => {
                errors_texts.push(format!("Error at index: [ {} ] - Budget: [ {:?} ] - Tag: [ {:?} ] - Error message: [ {} ] - Logs: [ {:?} ]",
                                          error.index, error.budget, error.tag, error.error_message, error.logs));
            }
        }
    }
    if errors_texts.is_empty() {
        Ok(actions)
    } else {
        Err(JsError::from_str(&format!("Errors found during evaluation: [ {:?} ]", errors_texts)))
    }
}

#[async_trait]
impl Evaluator for MeshTxEvaluator {
    async fn evaluate_tx(
        &self,
        tx_hex: &str,
        inputs: &[UTxO],
        additional_txs: &[String],
        network: &Network,
        slot_config: &SlotConfig
    ) -> Result<Vec<Action>, JsError> {
        self.evaluate_tx_sync(tx_hex, inputs, additional_txs, network, slot_config)
    }
}