Skip to main content

radix_engine/blueprints/account/
package.rs

1use crate::blueprints::account::AccountBlueprint;
2use crate::errors::ApplicationError;
3use crate::errors::RuntimeError;
4use crate::internal_prelude::*;
5use radix_engine_interface::api::SystemApi;
6use radix_engine_interface::blueprints::account::*;
7use radix_engine_interface::blueprints::hooks::OnVirtualizeInput;
8use radix_engine_interface::blueprints::package::PackageDefinition;
9
10pub const ACCOUNT_ON_VIRTUALIZE_EXPORT_NAME: &str = "on_virtualize";
11
12pub struct AccountNativePackage;
13
14impl AccountNativePackage {
15    pub fn definition() -> PackageDefinition {
16        let blueprints = indexmap!(
17            ACCOUNT_BLUEPRINT.to_string() => AccountBlueprint::get_definition()
18        );
19
20        PackageDefinition { blueprints }
21    }
22
23    pub fn invoke_export<Y: SystemApi<RuntimeError>>(
24        export_name: &str,
25        input: &IndexedScryptoValue,
26        api: &mut Y,
27    ) -> Result<IndexedScryptoValue, RuntimeError> {
28        match export_name {
29            ACCOUNT_ON_VIRTUALIZE_EXPORT_NAME => {
30                let input: OnVirtualizeInput = input.as_typed().map_err(|e| {
31                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
32                })?;
33
34                let rtn = AccountBlueprint::on_virtualize(input, api)?;
35
36                Ok(IndexedScryptoValue::from_typed(&rtn))
37            }
38            ACCOUNT_CREATE_ADVANCED_IDENT => {
39                let input: AccountCreateAdvancedInput = input.as_typed().map_err(|e| {
40                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
41                })?;
42
43                let rtn = AccountBlueprint::create_advanced(
44                    input.owner_role,
45                    input.address_reservation,
46                    api,
47                )?;
48
49                Ok(IndexedScryptoValue::from_typed(&rtn))
50            }
51            ACCOUNT_CREATE_IDENT => {
52                let _input: AccountCreateInput = input.as_typed().map_err(|e| {
53                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
54                })?;
55
56                let rtn = AccountBlueprint::create(api)?;
57
58                Ok(IndexedScryptoValue::from_typed(&rtn))
59            }
60            ACCOUNT_SECURIFY_IDENT => {
61                let _input: AccountSecurifyInput = input.as_typed().map_err(|e| {
62                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
63                })?;
64                let rtn = AccountBlueprint::securify(api)?;
65
66                Ok(IndexedScryptoValue::from_typed(&rtn))
67            }
68            ACCOUNT_LOCK_FEE_IDENT => {
69                let input: AccountLockFeeInput = input.as_typed().map_err(|e| {
70                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
71                })?;
72                let rtn = AccountBlueprint::lock_fee(input.amount, api)?;
73                Ok(IndexedScryptoValue::from_typed(&rtn))
74            }
75            ACCOUNT_LOCK_CONTINGENT_FEE_IDENT => {
76                let input: AccountLockContingentFeeInput = input.as_typed().map_err(|e| {
77                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
78                })?;
79
80                let rtn = AccountBlueprint::lock_contingent_fee(input.amount, api)?;
81                Ok(IndexedScryptoValue::from_typed(&rtn))
82            }
83            ACCOUNT_DEPOSIT_IDENT => {
84                let input: AccountDepositInput = input.as_typed().map_err(|e| {
85                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
86                })?;
87
88                let rtn = AccountBlueprint::deposit(input.bucket, api)?;
89                Ok(IndexedScryptoValue::from_typed(&rtn))
90            }
91            ACCOUNT_DEPOSIT_BATCH_IDENT => {
92                let input: AccountDepositBatchInput = input.as_typed().map_err(|e| {
93                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
94                })?;
95
96                let rtn = AccountBlueprint::deposit_batch(input.buckets, api)?;
97                Ok(IndexedScryptoValue::from_typed(&rtn))
98            }
99            ACCOUNT_TRY_DEPOSIT_OR_REFUND_IDENT => {
100                let AccountTryDepositOrRefundInput {
101                    bucket,
102                    authorized_depositor_badge,
103                } = input.as_typed().map_err(|e| {
104                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
105                })?;
106
107                let rtn = AccountBlueprint::try_deposit_or_refund(
108                    bucket,
109                    authorized_depositor_badge,
110                    api,
111                )?;
112                Ok(IndexedScryptoValue::from_typed(&rtn))
113            }
114            ACCOUNT_TRY_DEPOSIT_BATCH_OR_REFUND_IDENT => {
115                let AccountTryDepositBatchOrRefundInput {
116                    buckets,
117                    authorized_depositor_badge,
118                } = input.as_typed().map_err(|e| {
119                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
120                })?;
121
122                let rtn = AccountBlueprint::try_deposit_batch_or_refund(
123                    buckets,
124                    authorized_depositor_badge,
125                    api,
126                )?;
127                Ok(IndexedScryptoValue::from_typed(&rtn))
128            }
129            ACCOUNT_TRY_DEPOSIT_OR_ABORT_IDENT => {
130                let AccountTryDepositOrAbortInput {
131                    bucket,
132                    authorized_depositor_badge,
133                } = input.as_typed().map_err(|e| {
134                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
135                })?;
136
137                let rtn = AccountBlueprint::try_deposit_or_abort(
138                    bucket,
139                    authorized_depositor_badge,
140                    api,
141                )?;
142                Ok(IndexedScryptoValue::from_typed(&rtn))
143            }
144            ACCOUNT_TRY_DEPOSIT_BATCH_OR_ABORT_IDENT => {
145                let AccountTryDepositBatchOrAbortInput {
146                    buckets,
147                    authorized_depositor_badge,
148                } = input.as_typed().map_err(|e| {
149                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
150                })?;
151
152                let rtn = AccountBlueprint::try_deposit_batch_or_abort(
153                    buckets,
154                    authorized_depositor_badge,
155                    api,
156                )?;
157                Ok(IndexedScryptoValue::from_typed(&rtn))
158            }
159            ACCOUNT_WITHDRAW_IDENT => {
160                let input: AccountWithdrawInput = input.as_typed().map_err(|e| {
161                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
162                })?;
163
164                let rtn = AccountBlueprint::withdraw(input.resource_address, input.amount, api)?;
165                Ok(IndexedScryptoValue::from_typed(&rtn))
166            }
167            ACCOUNT_WITHDRAW_NON_FUNGIBLES_IDENT => {
168                let input: AccountWithdrawNonFungiblesInput = input.as_typed().map_err(|e| {
169                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
170                })?;
171                let rtn = AccountBlueprint::withdraw_non_fungibles(
172                    input.resource_address,
173                    input.ids,
174                    api,
175                )?;
176                Ok(IndexedScryptoValue::from_typed(&rtn))
177            }
178            ACCOUNT_BURN_IDENT => {
179                let input: AccountBurnInput = input.as_typed().map_err(|e| {
180                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
181                })?;
182
183                let rtn = AccountBlueprint::burn(input.resource_address, input.amount, api)?;
184                Ok(IndexedScryptoValue::from_typed(&rtn))
185            }
186            ACCOUNT_BURN_NON_FUNGIBLES_IDENT => {
187                let input: AccountBurnNonFungiblesInput = input.as_typed().map_err(|e| {
188                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
189                })?;
190                let rtn =
191                    AccountBlueprint::burn_non_fungibles(input.resource_address, input.ids, api)?;
192                Ok(IndexedScryptoValue::from_typed(&rtn))
193            }
194            ACCOUNT_LOCK_FEE_AND_WITHDRAW_IDENT => {
195                let input: AccountLockFeeAndWithdrawInput = input.as_typed().map_err(|e| {
196                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
197                })?;
198                let rtn = AccountBlueprint::lock_fee_and_withdraw(
199                    input.amount_to_lock,
200                    input.resource_address,
201                    input.amount,
202                    api,
203                )?;
204                Ok(IndexedScryptoValue::from_typed(&rtn))
205            }
206            ACCOUNT_LOCK_FEE_AND_WITHDRAW_NON_FUNGIBLES_IDENT => {
207                let input: AccountLockFeeAndWithdrawNonFungiblesInput =
208                    input.as_typed().map_err(|e| {
209                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
210                    })?;
211                let rtn = AccountBlueprint::lock_fee_and_withdraw_non_fungibles(
212                    input.amount_to_lock,
213                    input.resource_address,
214                    input.ids,
215                    api,
216                )?;
217                Ok(IndexedScryptoValue::from_typed(&rtn))
218            }
219            ACCOUNT_CREATE_PROOF_OF_AMOUNT_IDENT => {
220                let input: AccountCreateProofOfAmountInput = input.as_typed().map_err(|e| {
221                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
222                })?;
223                let rtn = AccountBlueprint::create_proof_of_amount(
224                    input.resource_address,
225                    input.amount,
226                    api,
227                )?;
228                Ok(IndexedScryptoValue::from_typed(&rtn))
229            }
230            ACCOUNT_CREATE_PROOF_OF_NON_FUNGIBLES_IDENT => {
231                let input: AccountCreateProofOfNonFungiblesInput =
232                    input.as_typed().map_err(|e| {
233                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
234                    })?;
235                let rtn = AccountBlueprint::create_proof_of_non_fungibles(
236                    input.resource_address,
237                    input.ids,
238                    api,
239                )?;
240                Ok(IndexedScryptoValue::from_typed(&rtn))
241            }
242            ACCOUNT_SET_DEFAULT_DEPOSIT_RULE_IDENT => {
243                let AccountSetDefaultDepositRuleInput { default } =
244                    input.as_typed().map_err(|e| {
245                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
246                    })?;
247                let rtn = AccountBlueprint::set_default_deposit_rule(default, api)?;
248                Ok(IndexedScryptoValue::from_typed(&rtn))
249            }
250            ACCOUNT_SET_RESOURCE_PREFERENCE_IDENT => {
251                let AccountSetResourcePreferenceInput {
252                    resource_address,
253                    resource_preference,
254                } = input.as_typed().map_err(|e| {
255                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
256                })?;
257                let rtn = AccountBlueprint::set_resource_preference(
258                    resource_address,
259                    resource_preference,
260                    api,
261                )?;
262                Ok(IndexedScryptoValue::from_typed(&rtn))
263            }
264            ACCOUNT_REMOVE_RESOURCE_PREFERENCE_IDENT => {
265                let AccountRemoveResourcePreferenceInput { resource_address } =
266                    input.as_typed().map_err(|e| {
267                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
268                    })?;
269                let rtn = AccountBlueprint::remove_resource_preference(resource_address, api)?;
270                Ok(IndexedScryptoValue::from_typed(&rtn))
271            }
272            ACCOUNT_ADD_AUTHORIZED_DEPOSITOR_IDENT => {
273                let AccountAddAuthorizedDepositorInput { badge } =
274                    input.as_typed().map_err(|e| {
275                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
276                    })?;
277                let rtn = AccountBlueprint::add_authorized_depositor(badge, api)?;
278                Ok(IndexedScryptoValue::from_typed(&rtn))
279            }
280            ACCOUNT_REMOVE_AUTHORIZED_DEPOSITOR_IDENT => {
281                let AccountRemoveAuthorizedDepositorInput { badge } =
282                    input.as_typed().map_err(|e| {
283                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
284                    })?;
285                let rtn = AccountBlueprint::remove_authorized_depositor(badge, api)?;
286                Ok(IndexedScryptoValue::from_typed(&rtn))
287            }
288
289            _ => Err(RuntimeError::ApplicationError(
290                ApplicationError::ExportDoesNotExist(export_name.to_string()),
291            )),
292        }
293    }
294}