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
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
mod auth_zone;
mod bucket;
mod fungible;
mod non_fungible;
mod non_fungible_global_id;
mod proof;
mod proof_rule;
mod resource;
mod resource_manager;
mod resource_type;
mod role_assignment;
mod vault;
mod worktop;

pub use auth_zone::*;
pub use bucket::*;
pub use fungible::*;
pub use non_fungible::*;
pub use non_fungible_global_id::*;
pub use proof::*;
pub use proof_rule::*;
pub use resource::*;
pub use resource_manager::ResourceFeature::*;
pub use resource_manager::*;
pub use resource_type::*;
pub use role_assignment::*;
use sbor::Sbor;
pub use vault::*;
pub use worktop::*;

use crate::api::node_modules::auth::RoleDefinition;
#[cfg(feature = "radix_engine_fuzzing")]
use arbitrary::Arbitrary;
use radix_engine_common::math::*;
use radix_engine_common::{ManifestSbor, ScryptoSbor};
use sbor::rust::prelude::*;

pub fn check_fungible_amount(amount: &Decimal, divisibility: u8) -> bool {
    !amount.is_negative()
        && amount.0 % I192::from(10i128.pow((18 - divisibility).into())) == I192::from(0)
}

pub fn check_non_fungible_amount(amount: &Decimal) -> Result<u32, ()> {
    // Integers between [0..u32::MAX]
    u32::try_from(amount).map_err(|_| ())
}

#[macro_export]
macro_rules! resource_roles {
    (
        $roles_struct:ident,
        $actor_field:ident,
        $updater_field:ident,
        $actor_field_name:expr,
        $updater_field_name:expr,
        $default_rule:expr
    ) => {
        #[cfg_attr(feature = "radix_engine_fuzzing", derive(Arbitrary))]
        #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, ScryptoSbor, ManifestSbor)]
        pub struct $roles_struct<T> {
            pub $actor_field: T,
            pub $updater_field: T,
        }

        impl $roles_struct<$crate::api::node_modules::auth::RoleDefinition> {
            pub fn to_role_init(self) -> $crate::blueprints::resource::RoleAssignmentInit {
                let mut roles = $crate::blueprints::resource::RoleAssignmentInit::new();
                roles.define_role($actor_field_name, self.$actor_field);
                roles.define_role($updater_field_name, self.$updater_field);
                roles
            }
        }

        impl Default for $roles_struct<$crate::api::node_modules::auth::RoleDefinition> {
            fn default() -> Self {
                Self {
                    $actor_field: Some($default_rule),
                    $updater_field: Some(AccessRule::DenyAll),
                }
            }
        }
    };
}

resource_roles!(
    MintRoles,
    minter,
    minter_updater,
    MINTER_ROLE,
    MINTER_UPDATER_ROLE,
    AccessRule::DenyAll
);
#[macro_export]
macro_rules! mint_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(MintRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    BurnRoles,
    burner,
    burner_updater,
    BURNER_ROLE,
    BURNER_UPDATER_ROLE,
    AccessRule::DenyAll
);
#[macro_export]
macro_rules! burn_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(BurnRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    RecallRoles,
    recaller,
    recaller_updater,
    RECALLER_ROLE,
    RECALLER_UPDATER_ROLE,
    AccessRule::DenyAll
);
#[macro_export]
macro_rules! recall_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(RecallRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    FreezeRoles,
    freezer,
    freezer_updater,
    FREEZER_ROLE,
    FREEZER_UPDATER_ROLE,
    AccessRule::DenyAll
);
#[macro_export]
macro_rules! freeze_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(FreezeRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    WithdrawRoles,
    withdrawer,
    withdrawer_updater,
    WITHDRAWER_ROLE,
    WITHDRAWER_UPDATER_ROLE,
    AccessRule::AllowAll
);
#[macro_export]
macro_rules! withdraw_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(WithdrawRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    DepositRoles,
    depositor,
    depositor_updater,
    DEPOSITOR_ROLE,
    DEPOSITOR_UPDATER_ROLE,
    AccessRule::AllowAll
);
#[macro_export]
macro_rules! deposit_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(DepositRoles, $($role => $rule;)*))
    });
}

resource_roles!(
    NonFungibleDataUpdateRoles,
    non_fungible_data_updater,
    non_fungible_data_updater_updater,
    NON_FUNGIBLE_DATA_UPDATER_ROLE,
    NON_FUNGIBLE_DATA_UPDATER_UPDATER_ROLE,
    AccessRule::DenyAll
);
#[macro_export]
macro_rules! non_fungible_data_update_roles {
    {$($role:ident => $rule:expr;)*} => ({
        Some($crate::internal_roles_struct!(NonFungibleDataUpdateRoles, $($role => $rule;)*))
    });
}

/// Define the withdraw strategy when request amount does not match underlying
/// resource divisibility.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Sbor)]
pub enum WithdrawStrategy {
    Exact,
    Rounded(RoundingMode),
}

pub trait ForWithdrawal {
    fn for_withdrawal(
        &self,
        divisibility: u8,
        withdraw_strategy: WithdrawStrategy,
    ) -> Option<Decimal>;
}

impl ForWithdrawal for Decimal {
    fn for_withdrawal(
        &self,
        divisibility: u8,
        withdraw_strategy: WithdrawStrategy,
    ) -> Option<Decimal> {
        match withdraw_strategy {
            WithdrawStrategy::Exact => Some(self.clone()),
            WithdrawStrategy::Rounded(mode) => self.checked_round(divisibility, mode),
        }
    }
}

impl Default for WithdrawStrategy {
    fn default() -> Self {
        Self::Exact
    }
}