pub struct ExUnits { /* private fields */ }

Implementations§

Examples found in repository?
src/plutus.rs (line 944)
939
940
941
942
943
944
945
946
947
948
    pub fn total_ex_units(&self) -> Result<ExUnits, JsError> {
        let mut tot_mem = BigNum::zero();
        let mut tot_steps = BigNum::zero();
        for i in 0..self.0.len() {
            let r: &Redeemer = &self.0[i];
            tot_mem = tot_mem.checked_add(&r.ex_units().mem())?;
            tot_steps = tot_steps.checked_add(&r.ex_units().steps())?;
        }
        Ok(ExUnits::new(&tot_mem, &tot_steps))
    }
More examples
Hide additional examples
src/fees.rs (line 68)
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
68
69
70
71
72
73
74
75
76
77
78
79
pub fn calculate_ex_units_ceil_cost(
    ex_units: &ExUnits,
    ex_unit_prices: &ExUnitPrices,
) -> Result<Coin, JsError> {
    type Ratio = (BigInt, BigInt);
    fn mult(sc: &SubCoin, x: &BigNum) -> Result<Ratio, JsError> {
        let n: BigInt = BigInt::from_str(&sc.numerator.to_str())?;
        let d: BigInt = BigInt::from_str(&sc.denominator.to_str())?;
        let m: BigInt = BigInt::from_str(&x.to_str())?;
        Ok((n.mul(&m), d))
    }
    fn sum(a: &Ratio, b: &Ratio) -> Ratio {
        // Ratio Addition: a/x + b/y = ((a*y) + (b*x))/(x*y)
        let (a_num, a_denum) = &a;
        let (b_num, b_denum) = &b;
        if a_num.is_zero() {
            return b.clone();
        }
        if b_num.is_zero() {
            return a.clone();
        }
        let a_num_fixed = &a_num.mul(b_denum);
        let b_num_fixed = &b_num.mul(a_denum);
        let a_b_num_sum = a_num_fixed.add(b_num_fixed);
        let common_denum = a_denum.mul(b_denum);
        (a_b_num_sum, common_denum)
    }
    let mem_ratio: Ratio = mult(&ex_unit_prices.mem_price(), &ex_units.mem())?;
    let steps_ratio: Ratio = mult(&ex_unit_prices.step_price(), &ex_units.steps())?;
    let (total_num, total_denum) = sum(&mem_ratio, &steps_ratio);
    match total_num.div_ceil(&total_denum).as_u64() {
        Some(coin) => Ok(coin),
        _ => Err(JsError::from_str(&format!(
            "Failed to calculate ceil from ratio {}/{}",
            total_num.to_str(),
            total_denum.to_str(),
        ))),
    }
}
Examples found in repository?
src/plutus.rs (line 945)
939
940
941
942
943
944
945
946
947
948
    pub fn total_ex_units(&self) -> Result<ExUnits, JsError> {
        let mut tot_mem = BigNum::zero();
        let mut tot_steps = BigNum::zero();
        for i in 0..self.0.len() {
            let r: &Redeemer = &self.0[i];
            tot_mem = tot_mem.checked_add(&r.ex_units().mem())?;
            tot_steps = tot_steps.checked_add(&r.ex_units().steps())?;
        }
        Ok(ExUnits::new(&tot_mem, &tot_steps))
    }
More examples
Hide additional examples
src/fees.rs (line 69)
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
68
69
70
71
72
73
74
75
76
77
78
79
pub fn calculate_ex_units_ceil_cost(
    ex_units: &ExUnits,
    ex_unit_prices: &ExUnitPrices,
) -> Result<Coin, JsError> {
    type Ratio = (BigInt, BigInt);
    fn mult(sc: &SubCoin, x: &BigNum) -> Result<Ratio, JsError> {
        let n: BigInt = BigInt::from_str(&sc.numerator.to_str())?;
        let d: BigInt = BigInt::from_str(&sc.denominator.to_str())?;
        let m: BigInt = BigInt::from_str(&x.to_str())?;
        Ok((n.mul(&m), d))
    }
    fn sum(a: &Ratio, b: &Ratio) -> Ratio {
        // Ratio Addition: a/x + b/y = ((a*y) + (b*x))/(x*y)
        let (a_num, a_denum) = &a;
        let (b_num, b_denum) = &b;
        if a_num.is_zero() {
            return b.clone();
        }
        if b_num.is_zero() {
            return a.clone();
        }
        let a_num_fixed = &a_num.mul(b_denum);
        let b_num_fixed = &b_num.mul(a_denum);
        let a_b_num_sum = a_num_fixed.add(b_num_fixed);
        let common_denum = a_denum.mul(b_denum);
        (a_b_num_sum, common_denum)
    }
    let mem_ratio: Ratio = mult(&ex_unit_prices.mem_price(), &ex_units.mem())?;
    let steps_ratio: Ratio = mult(&ex_unit_prices.step_price(), &ex_units.steps())?;
    let (total_num, total_denum) = sum(&mem_ratio, &steps_ratio);
    match total_num.div_ceil(&total_denum).as_u64() {
        Some(coin) => Ok(coin),
        _ => Err(JsError::from_str(&format!(
            "Failed to calculate ceil from ratio {}/{}",
            total_num.to_str(),
            total_denum.to_str(),
        ))),
    }
}
Examples found in repository?
src/plutus.rs (line 947)
939
940
941
942
943
944
945
946
947
948
    pub fn total_ex_units(&self) -> Result<ExUnits, JsError> {
        let mut tot_mem = BigNum::zero();
        let mut tot_steps = BigNum::zero();
        for i in 0..self.0.len() {
            let r: &Redeemer = &self.0[i];
            tot_mem = tot_mem.checked_add(&r.ex_units().mem())?;
            tot_steps = tot_steps.checked_add(&r.ex_units().steps())?;
        }
        Ok(ExUnits::new(&tot_mem, &tot_steps))
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
The name of the generated JSON Schema. Read more
Generates a JSON Schema for this type. Read more
Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.