1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::internal_prelude::*;
#[cfg(feature = "radix_engine_fuzzing")]
use arbitrary::Arbitrary;

#[cfg_attr(
    feature = "radix_engine_fuzzing",
    derive(Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, ManifestSbor, ScryptoCategorize, ScryptoEncode, ScryptoDecode,
)]
pub enum RoyaltyAmount {
    Free,
    Xrd(Decimal),
    Usd(Decimal),
}

impl Describe<ScryptoCustomTypeKind> for RoyaltyAmount {
    const TYPE_ID: RustTypeId =
        RustTypeId::WellKnown(well_known_scrypto_custom_types::ROYALTY_AMOUNT_TYPE);

    fn type_data() -> ScryptoTypeData<RustTypeId> {
        well_known_scrypto_custom_types::royalty_amount_type_data()
    }
}

impl RoyaltyAmount {
    pub fn is_zero(&self) -> bool {
        match self {
            RoyaltyAmount::Xrd(x) => x.is_zero(),
            RoyaltyAmount::Usd(x) => x.is_zero(),
            RoyaltyAmount::Free => true,
        }
    }

    pub fn is_non_zero(&self) -> bool {
        !self.is_zero()
    }

    pub fn is_negative(&self) -> bool {
        match self {
            Self::Free => false,
            Self::Usd(amount) | Self::Xrd(amount) => amount.is_negative(),
        }
    }
}