pub struct Int(_);

Implementations§

Examples found in repository?
src/utils.rs (line 951)
943
944
945
946
947
948
949
950
951
952
953
954
    pub fn as_int(&self) -> Option<Int> {
        let (sign, u64_digits) = self.0.to_u64_digits();
        let u64_digit = match u64_digits.len() {
            0 => Some(to_bignum(0)),
            1 => Some(to_bignum(*u64_digits.first().unwrap())),
            _ => None,
        }?;
        match sign {
            num_bigint::Sign::NoSign | num_bigint::Sign::Plus => Some(Int::new(&u64_digit)),
            num_bigint::Sign::Minus => Some(Int::new_negative(&u64_digit)),
        }
    }
More examples
Hide additional examples
src/metadata.rs (lines 522-524)
520
521
522
523
524
525
526
527
528
529
530
531
532
    fn encode_number(x: serde_json::Number) -> Result<TransactionMetadatum, JsError> {
        if let Some(x) = x.as_u64() {
            Ok(TransactionMetadatum::new_int(&Int::new(&utils::to_bignum(
                x,
            ))))
        } else if let Some(x) = x.as_i64() {
            Ok(TransactionMetadatum::new_int(&Int::new_negative(
                &utils::to_bignum(-x as u64),
            )))
        } else {
            Err(JsError::from_str("floats not allowed in metadata"))
        }
    }
src/tx_builder/mint_builder.rs (line 75)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    fn update_mint_value(&mut self, mint: &MintWitness, asset_name: &AssetName, amount: &Int, overwrite: bool) {
        match &mint.0 {
            MintWitnessEnum::NativeScript(native_script) => {
                let script_mint = self.mints.entry(native_script.hash()).or_insert(ScriptMint::Native(NativeMints {
                    script: native_script.clone(),
                    mints: BTreeMap::new(),
                }));
                match script_mint {
                    ScriptMint::Native(native_mints) => {
                        let mint = native_mints.mints.entry(asset_name.clone()).or_insert(Int::new(&BigNum::zero()));
                        if overwrite {
                            mint.0 = amount.0;
                        } else {
                            mint.0 += amount.0;
                        }
                    },
                    _ => {},
                }
            },
            MintWitnessEnum::Plutus(plutus_script, redeemer) => {
                let script_mint = self.mints.entry(plutus_script.script_hash()).or_insert(ScriptMint::Plutus(PlutusMints {
                    script: plutus_script.clone(),
                    redeemer_mints: BTreeMap::new(),
                }));
                match script_mint {
                    ScriptMint::Plutus(plutus_mints) => {
                        let redeemer_mints = plutus_mints.redeemer_mints.entry(redeemer.clone()).or_insert(BTreeMap::new());
                        let mint = redeemer_mints.entry(asset_name.clone()).or_insert(Int::new(&BigNum::zero()));
                        if overwrite {
                            mint.0 = amount.0;
                        } else {
                            mint.0 += amount.0;
                        }
                    },
                    _ => {},
                }
            },
        }
    }
Examples found in repository?
src/utils.rs (line 952)
943
944
945
946
947
948
949
950
951
952
953
954
    pub fn as_int(&self) -> Option<Int> {
        let (sign, u64_digits) = self.0.to_u64_digits();
        let u64_digit = match u64_digits.len() {
            0 => Some(to_bignum(0)),
            1 => Some(to_bignum(*u64_digits.first().unwrap())),
            _ => None,
        }?;
        match sign {
            num_bigint::Sign::NoSign | num_bigint::Sign::Plus => Some(Int::new(&u64_digit)),
            num_bigint::Sign::Minus => Some(Int::new_negative(&u64_digit)),
        }
    }
More examples
Hide additional examples
src/metadata.rs (lines 526-528)
520
521
522
523
524
525
526
527
528
529
530
531
532
    fn encode_number(x: serde_json::Number) -> Result<TransactionMetadatum, JsError> {
        if let Some(x) = x.as_u64() {
            Ok(TransactionMetadatum::new_int(&Int::new(&utils::to_bignum(
                x,
            ))))
        } else if let Some(x) = x.as_i64() {
            Ok(TransactionMetadatum::new_int(&Int::new_negative(
                &utils::to_bignum(-x as u64),
            )))
        } else {
            Err(JsError::from_str("floats not allowed in metadata"))
        }
    }
Examples found in repository?
src/metadata.rs (line 45)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    pub fn insert_i32(
        &mut self,
        key: i32,
        value: &TransactionMetadatum,
    ) -> Option<TransactionMetadatum> {
        self.insert(&TransactionMetadatum::new_int(&Int::new_i32(key)), value)
    }

    pub fn get(&self, key: &TransactionMetadatum) -> Result<TransactionMetadatum, JsError> {
        self.0
            .get(key)
            .map(|v| v.clone())
            .ok_or_else(|| JsError::from_str(&format!("key {:?} not found", key)))
    }

    // convenience function for retrieving a string key
    pub fn get_str(&self, key: &str) -> Result<TransactionMetadatum, JsError> {
        self.get(&TransactionMetadatum::new_text(key.to_owned())?)
    }

    // convenience function for retrieving 32-bit integer keys - for higher-precision integers use get() with an Int struct
    pub fn get_i32(&self, key: i32) -> Result<TransactionMetadatum, JsError> {
        self.get(&TransactionMetadatum::new_int(&Int::new_i32(key)))
    }
More examples
Hide additional examples
src/plutus.rs (line 292)
287
288
289
290
291
292
293
294
295
296
297
298
    pub fn set(&mut self, operation: usize, cost: &Int) -> Result<Int, JsError> {
        let len = self.0.len();
        let idx = operation.clone();
        if idx >= len {
            for _ in 0..(idx - len + 1) {
                self.0.push(Int::new_i32(0));
            }
        }
        let old = self.0[idx].clone();
        self.0[idx] = cost.clone();
        Ok(old)
    }
Examples found in repository?
src/utils.rs (line 642)
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
    pub fn as_positive(&self) -> Option<BigNum> {
        if self.is_positive() {
            Some(to_bignum(self.0 as u64))
        } else {
            None
        }
    }

    /// BigNum can only contain unsigned u64 values
    ///
    /// This function will return the *absolute* BigNum representation
    /// only in case the underlying i128 value is negative.
    ///
    /// Otherwise nothing will be returned (undefined).
    pub fn as_negative(&self) -> Option<BigNum> {
        if !self.is_positive() {
            Some(to_bignum((-self.0) as u64))
        } else {
            None
        }
    }
More examples
Hide additional examples
src/lib.rs (line 3591)
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
    fn as_multiasset(&self, is_positive: bool) -> MultiAsset {
        self.0.iter().fold(MultiAsset::new(), |res, e : &(PolicyID, MintAssets) | {
            let assets: Assets = (e.1).0.iter().fold(Assets::new(), |res, e| {
                let mut assets = res;
                if e.1.is_positive() == is_positive {
                    let amount = match is_positive {
                        true => e.1.as_positive(),
                        false => e.1.as_negative(),
                    };
                    assets.insert(&e.0, &amount.unwrap());
                }
                assets
            });
            let mut ma = res;
            if !assets.0.is_empty() {
                ma.insert(&e.0, &assets);
            }
            ma
        })
    }
src/tx_builder.rs (line 1178)
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
    pub fn add_mint_asset_and_output(
        &mut self,
        policy_script: &NativeScript,
        asset_name: &AssetName,
        amount: Int,
        output_builder: &TransactionOutputAmountBuilder,
        output_coin: &Coin,
    ) -> Result<(), JsError> {
        if !amount.is_positive() {
            return Err(JsError::from_str("Output value must be positive!"));
        }
        let policy_id: PolicyID = policy_script.hash();
        self.add_mint_asset(policy_script, asset_name, amount.clone());
        let multiasset = Mint::new_from_entry(
            &policy_id,
            &MintAssets::new_from_entry(asset_name, amount.clone()),
        )
        .as_positive_multiasset();

        self.add_output(
            &output_builder
                .with_coin_and_asset(&output_coin, &multiasset)
                .build()?,
        )
    }

    /// Add a mint entry together with an output to this builder
    /// Using a PolicyID, AssetName, Int for amount, and Address objects
    /// The asset will be securely added to existing or new Mint in this builder
    /// A new output will be added with the specified Address and the minted asset
    /// The output will be set to contain the minimum required amount of Coin
    pub fn add_mint_asset_and_output_min_required_coin(
        &mut self,
        policy_script: &NativeScript,
        asset_name: &AssetName,
        amount: Int,
        output_builder: &TransactionOutputAmountBuilder,
    ) -> Result<(), JsError> {
        if !amount.is_positive() {
            return Err(JsError::from_str("Output value must be positive!"));
        }
        let policy_id: PolicyID = policy_script.hash();
        self.add_mint_asset(policy_script, asset_name, amount.clone());
        let multiasset = Mint::new_from_entry(
            &policy_id,
            &MintAssets::new_from_entry(asset_name, amount.clone()),
        )
        .as_positive_multiasset();

        self.add_output(
            &output_builder
                .with_asset_and_min_required_coin_by_utxo_cost(
                    &multiasset,
                    &self.config.utxo_cost(),
                )?
                .build()?,
        )
    }

BigNum can only contain unsigned u64 values

This function will return the BigNum representation only in case the underlying i128 value is positive.

Otherwise nothing will be returned (undefined).

Examples found in repository?
src/lib.rs (line 3593)
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
    fn as_multiasset(&self, is_positive: bool) -> MultiAsset {
        self.0.iter().fold(MultiAsset::new(), |res, e : &(PolicyID, MintAssets) | {
            let assets: Assets = (e.1).0.iter().fold(Assets::new(), |res, e| {
                let mut assets = res;
                if e.1.is_positive() == is_positive {
                    let amount = match is_positive {
                        true => e.1.as_positive(),
                        false => e.1.as_negative(),
                    };
                    assets.insert(&e.0, &amount.unwrap());
                }
                assets
            });
            let mut ma = res;
            if !assets.0.is_empty() {
                ma.insert(&e.0, &assets);
            }
            ma
        })
    }

BigNum can only contain unsigned u64 values

This function will return the absolute BigNum representation only in case the underlying i128 value is negative.

Otherwise nothing will be returned (undefined).

Examples found in repository?
src/lib.rs (line 3594)
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
    fn as_multiasset(&self, is_positive: bool) -> MultiAsset {
        self.0.iter().fold(MultiAsset::new(), |res, e : &(PolicyID, MintAssets) | {
            let assets: Assets = (e.1).0.iter().fold(Assets::new(), |res, e| {
                let mut assets = res;
                if e.1.is_positive() == is_positive {
                    let amount = match is_positive {
                        true => e.1.as_positive(),
                        false => e.1.as_negative(),
                    };
                    assets.insert(&e.0, &amount.unwrap());
                }
                assets
            });
            let mut ma = res;
            if !assets.0.is_empty() {
                ma.insert(&e.0, &assets);
            }
            ma
        })
    }
👎Deprecated since 10.0.0: Unsafe ignoring of possible boundary error and it’s not clear from the function name. Use as_i32_or_nothing, as_i32_or_fail, or to_str

!!! DEPRECATED !!! Returns an i32 value in case the underlying original i128 value is within the limits. Otherwise will just return an empty value (undefined).

Returns the underlying value converted to i32 if possible (within limits) Otherwise will just return an empty value (undefined).

Examples found in repository?
src/utils.rs (line 671)
670
671
672
    pub fn as_i32(&self) -> Option<i32> {
        self.as_i32_or_nothing()
    }

Returns the underlying value converted to i32 if possible (within limits) JsError in case of out of boundary overflow

Returns string representation of the underlying i128 value directly. Might contain the minus sign (-) in case of negative value.

Examples found in repository?
src/utils.rs (line 741)
737
738
739
740
741
742
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_str())
    }
Examples found in repository?
src/utils.rs (line 751)
746
747
748
749
750
751
752
753
754
755
756
757
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        Self::from_str(&s).map_err(|_e| {
            serde::de::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &"string rep of a number",
            )
        })
    }

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
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. 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.