Skip to content

Commit

Permalink
Wrap RET: batch 7 (Transfers) (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Mar 12, 2024
1 parent 497a255 commit a9dce96
Show file tree
Hide file tree
Showing 28 changed files with 2,104 additions and 81 deletions.
1 change: 1 addition & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ inout = "inout"
pool_tdx_2_1c4ml86h8lvfk7jma0jy0vksh8srcxhmtax8nd3aur29qtd2k2wmlzk = "pool_tdx_2_1c4ml86h8lvfk7jma0jy0vksh8srcxhmtax8nd3aur29qtd2k2wmlzk"
account_sim1c8s2hass5g62ckwpv78y8ykdqljtetv4ve6etcz64gveykxznj36tr = "account_sim1c8s2hass5g62ckwpv78y8ykdqljtetv4ve6etcz64gveykxznj36tr"
resource_tdx_2_1thw7yclz24h5xjp3086cj8z2ya0d7p9mydk0yh68c28ha02uhzrnyy = "resource_tdx_2_1thw7yclz24h5xjp3086cj8z2ya0d7p9mydk0yh68c28ha02uhzrnyy"
resource_tdx_2_1ntuaekqexa73m9en04jj3vdt3fk9u9kdk8q9su4efldun2y7nd3cga = "resource_tdx_2_1ntuaekqexa73m9en04jj3vdt3fk9u9kdk8q9su4efldun2y7nd3cga"
ec4892a8ba3b86f1 = "ec4892a8ba3b86f1"
f87611f279e0daa3 = "f87611f279e0daa3"

Expand Down
35 changes: 26 additions & 9 deletions src/core/types/decimal192.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,28 @@ impl Decimal {
}
}

/// Rounds this number to the specified decimal places, or if
/// None, rounds to `Decimal192::SCALE` places, using the
/// rounding mode `ToNearestMidpointAwayFromZero`.
///
/// # Panics
/// - Panic if the number of decimal places is not within [0..SCALE(=18)]
pub fn round(&self, decimal_places: impl Into<Option<i32>>) -> Self {
let mode = RoundingMode::ToNearestMidpointAwayFromZero;
self.round_with_mode(
decimal_places.into().unwrap_or(Decimal192::SCALE as i32),
mode,
)
.unwrap_or_else(|_| {
panic!("Should always be able to round using mode {}", mode)
})
}

/// Rounds this number to the specified decimal places.
///
/// # Panics
/// - Panic if the number of decimal places is not within [0..SCALE(=18)]
pub fn round(
pub fn round_with_mode(
&self,
decimal_places: i32,
rounding_mode: RoundingMode,
Expand Down Expand Up @@ -510,7 +527,7 @@ pub fn decimal_round(
decimal_places: i32,
rounding_mode: RoundingMode,
) -> Result<Decimal192> {
decimal.round(decimal_places, rounding_mode)
decimal.round_with_mode(decimal_places, rounding_mode)
}

#[cfg(test)]
Expand Down Expand Up @@ -1104,22 +1121,22 @@ mod uniffi_tests {
.unwrap();

assert!(max
.round(0, RoundingMode::ToNearestMidpointAwayFromZero)
.round_with_mode(0, RoundingMode::ToNearestMidpointAwayFromZero)
.is_ok());
assert!(max
.round(0, RoundingMode::ToNearestMidpointTowardZero)
.round_with_mode(0, RoundingMode::ToNearestMidpointTowardZero)
.is_ok());
assert!(max.round(0, RoundingMode::ToZero).is_ok());
assert!(max.round_with_mode(0, RoundingMode::ToZero).is_ok());

assert!(max
.round(18, RoundingMode::ToNearestMidpointAwayFromZero)
.round_with_mode(18, RoundingMode::ToNearestMidpointAwayFromZero)
.is_ok());
assert!(max
.round(18, RoundingMode::ToNearestMidpointTowardZero)
.round_with_mode(18, RoundingMode::ToNearestMidpointTowardZero)
.is_ok());
assert!(max.round(18, RoundingMode::ToZero).is_ok());
assert!(max.round_with_mode(18, RoundingMode::ToZero).is_ok());

assert!(max.round(0, RoundingMode::AwayFromZero).is_err());
assert!(max.round_with_mode(0, RoundingMode::AwayFromZero).is_err());
}

#[test]
Expand Down
9 changes: 8 additions & 1 deletion src/core/types/rounding_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ use crate::prelude::*;
///
/// Following the same naming convention as https://docs.rs/rust_decimal/latest/rust_decimal/enum.RoundingStrategy.html.
#[derive(
Clone, Copy, Debug, PartialEq, Eq, enum_iterator::Sequence, uniffi::Enum,
Clone,
Copy,
Debug,
PartialEq,
Eq,
enum_iterator::Sequence,
strum::Display,
uniffi::Enum,
)]
pub enum RoundingMode {
/// The number is always rounded toward positive infinity, e.g. `3.1 -> 4`, `-3.1 -> -3`.
Expand Down
8 changes: 8 additions & 0 deletions src/profile/v100/address/non_fungible_resource_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ macro_rules! decl_specialized_address {
PartialEq,
Eq,
Hash,
Ord,
PartialOrd,
derive_more::Display,
derive_more::Debug,
derive_more::FromStr,
Expand Down Expand Up @@ -203,6 +205,12 @@ mod tests {
assert_ne!(SUT::sample_mainnet_other(), SUT::sample_stokenet_other());
}

#[test]
fn ord() {
assert!(SUT::sample_mainnet_other() < SUT::sample_mainnet());
assert!(SUT::sample_stokenet_other() > SUT::sample_stokenet());
}

#[test]
fn display() {
let s = "resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa";
Expand Down
33 changes: 33 additions & 0 deletions src/profile/v100/address/resource_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ impl ResourceAddress {
Self::new(radix_engine::types::XRD, id)
.expect("Should never fail to get XRD on network.")
}

pub fn is_xrd_on_network(&self, id: NetworkID) -> bool {
self == &Self::xrd_on_network(id)
}
}

#[uniffi::export]
Expand Down Expand Up @@ -184,6 +188,35 @@ mod tests {
assert!(SUT::sample_mainnet_xrd().is_fungible());
}

#[test]
fn is_xrd_on_network() {
assert!(SUT::sample_mainnet_xrd().is_xrd_on_network(NetworkID::Mainnet));
assert!(
SUT::sample_stokenet_xrd().is_xrd_on_network(NetworkID::Stokenet)
);

// Not XRD
assert!(
!SUT::sample_mainnet_xrd().is_xrd_on_network(NetworkID::Stokenet)
);
assert!(
!SUT::sample_stokenet_xrd().is_xrd_on_network(NetworkID::Mainnet)
);

assert!(
!SUT::sample_mainnet_candy().is_xrd_on_network(NetworkID::Mainnet)
);
assert!(
!SUT::sample_mainnet_candy().is_xrd_on_network(NetworkID::Stokenet)
);

assert!(!SUT::sample_stokenet_candy()
.is_xrd_on_network(NetworkID::Stokenet));
assert!(
!SUT::sample_stokenet_candy().is_xrd_on_network(NetworkID::Mainnet)
);
}

#[test]
fn hash() {
assert_eq!(
Expand Down
Loading

0 comments on commit a9dce96

Please sign in to comment.