Struct cardano_serialization_lib::utils::Int
source · pub struct Int(_);
Implementations§
source§impl Int
impl Int
pub fn from_bytes(bytes: Vec<u8>) -> Result<Int, DeserializeError>
source§impl Int
impl Int
sourcepub fn new(x: &BigNum) -> Self
pub fn new(x: &BigNum) -> Self
Examples found in repository?
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
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"))
}
}
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;
}
},
_ => {},
}
},
}
}
sourcepub fn new_negative(x: &BigNum) -> Self
pub fn new_negative(x: &BigNum) -> Self
Examples found in repository?
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
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"))
}
}
sourcepub fn new_i32(x: i32) -> Self
pub fn new_i32(x: i32) -> Self
Examples found in repository?
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
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)
}
sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Examples found in repository?
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
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
})
}
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()?,
)
}
sourcepub fn as_positive(&self) -> Option<BigNum>
pub fn as_positive(&self) -> Option<BigNum>
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?
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
})
}
sourcepub fn as_negative(&self) -> Option<BigNum>
pub fn as_negative(&self) -> Option<BigNum>
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?
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
})
}
sourcepub fn as_i32(&self) -> Option<i32>
👎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
pub fn as_i32(&self) -> Option<i32>
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).
sourcepub fn as_i32_or_nothing(&self) -> Option<i32>
pub fn as_i32_or_nothing(&self) -> Option<i32>
Returns the underlying value converted to i32 if possible (within limits) Otherwise will just return an empty value (undefined).
sourcepub fn as_i32_or_fail(&self) -> Result<i32, JsError>
pub fn as_i32_or_fail(&self) -> Result<i32, JsError>
Returns the underlying value converted to i32 if possible (within limits) JsError in case of out of boundary overflow
sourcepub fn to_str(&self) -> String
pub fn to_str(&self) -> String
Returns string representation of the underlying i128 value directly. Might contain the minus sign (-) in case of negative value.
sourcepub fn from_str(string: &str) -> Result<Int, JsError>
pub fn from_str(string: &str) -> Result<Int, JsError>
Examples found in repository?
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§
source§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl Deserialize for Int
impl Deserialize for Int
fn deserialize<R: BufRead + Seek>(
raw: &mut Deserializer<R>
) -> Result<Self, DeserializeError>
source§impl JsonSchema for Int
impl JsonSchema for Int
source§fn schema_name() -> String
fn schema_name() -> String
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl Ord for Int
impl Ord for Int
source§impl PartialOrd<Int> for Int
impl PartialOrd<Int> for Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more