whisky_pallas/utils/
value.rs

1use whisky_common::{Asset, Output, WError};
2
3use crate::converter::convert_output;
4
5pub fn get_min_utxo_value(output: &Output, coins_per_utxo_size: &u64) -> Result<String, WError> {
6    // loop over output's assets and check if it has lovelaces
7    let mut has_lovelaces = false;
8    for asset in &output.amount {
9        if asset.policy() == "lovelace" {
10            has_lovelaces = true;
11            break;
12        }
13    }
14    match has_lovelaces {
15        true => {
16            let wrapped_output = convert_output(output)?;
17            let cbor_length = wrapped_output.encode()?.len();
18            let min_utxo_value = coins_per_utxo_size * (cbor_length as u64 + 160);
19            Ok(min_utxo_value.to_string())
20        }
21        false => {
22            // if it doesn't have lovelaces, we need to add a dummy lovelace to calculate the min utxo value
23            let mut dummy_value = output.amount.clone();
24            dummy_value.push(Asset::new_from_str("lovelace", &u64::MAX.to_string()));
25            let dummy_output = Output {
26                address: output.address.clone(),
27                datum: output.datum.clone(),
28                reference_script: output.reference_script.clone(),
29                amount: dummy_value,
30            };
31            let wrapped_output = convert_output(&dummy_output)?;
32            let cbor_length = wrapped_output.encode()?.len();
33            let min_utxo_value = coins_per_utxo_size * (cbor_length as u64 + 160);
34            Ok(min_utxo_value.to_string())
35        }
36    }
37}