cml_chain_wasm/
fees.rs

1use cml_core_wasm::impl_wasm_conversions;
2use wasm_bindgen::prelude::{wasm_bindgen, JsError};
3
4use crate::{plutus::ExUnitPrices, transaction::Transaction, Coin};
5
6/// Careful: although the linear fee is the same for Byron & Shelley
7/// The value of the parameters and how fees are computed is not the same
8#[wasm_bindgen]
9#[derive(Clone, Debug)]
10pub struct LinearFee(cml_chain::fees::LinearFee);
11
12impl_wasm_conversions!(cml_chain::fees::LinearFee, LinearFee);
13
14#[wasm_bindgen]
15impl LinearFee {
16    /**
17     * * `coefficient` - minfee_a from protocol params
18     * * `constant` - minfee_b from protocol params
19     * * `ref_script_cost_per_bytes` - min_fee_ref_script_cost_per_byte from protocol params. New in Conway
20     */
21    pub fn new(coefficient: Coin, constant: Coin, ref_script_cost_per_byte: Coin) -> Self {
22        cml_chain::fees::LinearFee::new(coefficient, constant, ref_script_cost_per_byte).into()
23    }
24
25    /// minfee_a
26    pub fn coefficient(&self) -> Coin {
27        self.0.coefficient
28    }
29
30    /// minfee_b
31    pub fn constant(&self) -> Coin {
32        self.0.constant
33    }
34
35    // minfee_ref_script_cost_per_byte
36    pub fn ref_script_cost_per_byte(&self) -> Coin {
37        self.0.ref_script_cost_per_byte
38    }
39}
40
41/**
42 * Min fee for JUST the script, NOT including ref inputs
43 */
44#[wasm_bindgen]
45pub fn min_script_fee(tx: &Transaction, ex_unit_prices: &ExUnitPrices) -> Result<Coin, JsError> {
46    cml_chain::fees::min_script_fee(tx.as_ref(), ex_unit_prices.as_ref()).map_err(Into::into)
47}
48
49/**
50 * Calculates the cost of all ref scripts
51 * * `total_ref_script_size` - Total size (original, not hashes) of all ref scripts. Duplicate scripts are counted as many times as they occur
52 */
53pub fn min_ref_script_fee(
54    linear_fee: &LinearFee,
55    total_ref_script_size: u64,
56) -> Result<Coin, JsError> {
57    cml_chain::fees::min_ref_script_fee(linear_fee.as_ref(), total_ref_script_size)
58        .map_err(Into::into)
59}
60
61#[wasm_bindgen]
62pub fn min_no_script_fee(tx: &Transaction, linear_fee: &LinearFee) -> Result<Coin, JsError> {
63    cml_chain::fees::min_no_script_fee(tx.as_ref(), linear_fee.as_ref()).map_err(Into::into)
64}
65
66/**
67 * Calculates the cost of all ref scripts
68 * * `total_ref_script_size` - Total size (original, not hashes) of all ref scripts. Duplicate scripts are counted as many times as they occur
69 */
70#[wasm_bindgen]
71pub fn min_fee(
72    tx: &Transaction,
73    linear_fee: &LinearFee,
74    ex_unit_prices: &ExUnitPrices,
75    total_ref_script_size: u64,
76) -> Result<Coin, JsError> {
77    cml_chain::fees::min_fee(
78        tx.as_ref(),
79        linear_fee.as_ref(),
80        ex_unit_prices.as_ref(),
81        total_ref_script_size,
82    )
83    .map_err(Into::into)
84}