1mod accessors;
2
3use crate::balances::credits::TokenAmount;
4use crate::data_contract::associated_token::token_configuration_convention::v0::TokenConfigurationConventionV0;
5use crate::data_contract::associated_token::token_configuration_convention::TokenConfigurationConvention;
6use crate::data_contract::associated_token::token_distribution_rules::v0::TokenDistributionRulesV0;
7use crate::data_contract::associated_token::token_distribution_rules::TokenDistributionRules;
8use crate::data_contract::associated_token::token_keeps_history_rules::v0::TokenKeepsHistoryRulesV0;
9use crate::data_contract::associated_token::token_keeps_history_rules::TokenKeepsHistoryRules;
10use crate::data_contract::associated_token::token_marketplace_rules::v0::{
11 TokenMarketplaceRulesV0, TokenTradeMode,
12};
13use crate::data_contract::associated_token::token_marketplace_rules::TokenMarketplaceRules;
14use crate::data_contract::associated_token::token_perpetual_distribution::TokenPerpetualDistribution;
15use crate::data_contract::associated_token::token_pre_programmed_distribution::TokenPreProgrammedDistribution;
16use crate::data_contract::change_control_rules::authorized_action_takers::AuthorizedActionTakers;
17use crate::data_contract::change_control_rules::v0::ChangeControlRulesV0;
18use crate::data_contract::change_control_rules::ChangeControlRules;
19use crate::data_contract::GroupContractPosition;
20#[cfg(feature = "json-conversion")]
21use crate::serialization::json_safe_fields;
22use bincode::{Decode, Encode};
23use serde::{Deserialize, Serialize};
24use std::fmt;
25
26#[cfg_attr(feature = "json-conversion", json_safe_fields)]
36#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, PartialEq, Eq)]
37#[serde(rename_all = "camelCase")]
38pub struct TokenConfigurationV0 {
39 pub conventions: TokenConfigurationConvention,
41
42 #[serde(default = "default_change_control_rules")]
44 pub conventions_change_rules: ChangeControlRules,
45
46 #[serde(default)]
48 pub base_supply: TokenAmount,
49
50 #[serde(default)]
54 pub max_supply: Option<TokenAmount>,
55
56 #[serde(default = "default_token_keeps_history_rules")]
58 pub keeps_history: TokenKeepsHistoryRules,
59
60 #[serde(default = "default_starts_as_paused")]
64 pub start_as_paused: bool,
65
66 #[serde(default = "default_allow_transfer_to_frozen_balance")]
68 pub allow_transfer_to_frozen_balance: bool,
69
70 #[serde(default = "default_change_control_rules")]
74 pub max_supply_change_rules: ChangeControlRules,
75
76 #[serde(default = "default_token_distribution_rules")]
78 pub distribution_rules: TokenDistributionRules,
79
80 #[serde(default = "default_token_marketplace_rules")]
82 pub marketplace_rules: TokenMarketplaceRules,
83
84 #[serde(default = "default_contract_owner_change_control_rules")]
86 pub manual_minting_rules: ChangeControlRules,
87
88 #[serde(default = "default_contract_owner_change_control_rules")]
90 pub manual_burning_rules: ChangeControlRules,
91
92 #[serde(default = "default_change_control_rules")]
94 pub freeze_rules: ChangeControlRules,
95
96 #[serde(default = "default_change_control_rules")]
98 pub unfreeze_rules: ChangeControlRules,
99
100 #[serde(default = "default_change_control_rules")]
102 pub destroy_frozen_funds_rules: ChangeControlRules,
103
104 #[serde(default = "default_change_control_rules")]
106 pub emergency_action_rules: ChangeControlRules,
107
108 #[serde(default)]
110 pub main_control_group: Option<GroupContractPosition>,
111
112 #[serde(default)]
114 pub main_control_group_can_be_modified: AuthorizedActionTakers,
115
116 #[serde(default)]
118 pub description: Option<String>,
119}
120
121fn default_keeps_history() -> bool {
123 true }
125
126fn default_starts_as_paused() -> bool {
128 false
129}
130
131fn default_allow_transfer_to_frozen_balance() -> bool {
133 true
134}
135
136fn default_token_keeps_history_rules() -> TokenKeepsHistoryRules {
137 TokenKeepsHistoryRules::V0(TokenKeepsHistoryRulesV0 {
138 keeps_transfer_history: true,
139 keeps_freezing_history: true,
140 keeps_minting_history: true,
141 keeps_burning_history: true,
142 keeps_direct_pricing_history: true,
143 keeps_direct_purchase_history: true,
144 })
145}
146
147fn default_token_distribution_rules() -> TokenDistributionRules {
148 TokenDistributionRules::V0(TokenDistributionRulesV0 {
149 perpetual_distribution: None,
150 perpetual_distribution_rules: ChangeControlRules::V0(ChangeControlRulesV0 {
151 authorized_to_make_change: AuthorizedActionTakers::NoOne,
152 admin_action_takers: AuthorizedActionTakers::NoOne,
153 changing_authorized_action_takers_to_no_one_allowed: false,
154 changing_admin_action_takers_to_no_one_allowed: false,
155 self_changing_admin_action_takers_allowed: false,
156 }),
157 pre_programmed_distribution: None,
158 new_tokens_destination_identity: None,
159 new_tokens_destination_identity_rules: ChangeControlRules::V0(ChangeControlRulesV0 {
160 authorized_to_make_change: AuthorizedActionTakers::NoOne,
161 admin_action_takers: AuthorizedActionTakers::NoOne,
162 changing_authorized_action_takers_to_no_one_allowed: false,
163 changing_admin_action_takers_to_no_one_allowed: false,
164 self_changing_admin_action_takers_allowed: false,
165 }),
166 minting_allow_choosing_destination: true,
167 minting_allow_choosing_destination_rules: ChangeControlRules::V0(ChangeControlRulesV0 {
168 authorized_to_make_change: AuthorizedActionTakers::NoOne,
169 admin_action_takers: AuthorizedActionTakers::NoOne,
170 changing_authorized_action_takers_to_no_one_allowed: false,
171 changing_admin_action_takers_to_no_one_allowed: false,
172 self_changing_admin_action_takers_allowed: false,
173 }),
174 change_direct_purchase_pricing_rules: ChangeControlRules::V0(ChangeControlRulesV0 {
175 authorized_to_make_change: AuthorizedActionTakers::NoOne,
176 admin_action_takers: AuthorizedActionTakers::NoOne,
177 changing_authorized_action_takers_to_no_one_allowed: false,
178 changing_admin_action_takers_to_no_one_allowed: false,
179 self_changing_admin_action_takers_allowed: false,
180 }),
181 })
182}
183
184fn default_token_marketplace_rules() -> TokenMarketplaceRules {
185 TokenMarketplaceRules::V0(TokenMarketplaceRulesV0 {
186 trade_mode: TokenTradeMode::NotTradeable,
187 trade_mode_change_rules: ChangeControlRules::V0(ChangeControlRulesV0 {
188 authorized_to_make_change: AuthorizedActionTakers::NoOne,
189 admin_action_takers: AuthorizedActionTakers::NoOne,
190 changing_authorized_action_takers_to_no_one_allowed: false,
191 changing_admin_action_takers_to_no_one_allowed: false,
192 self_changing_admin_action_takers_allowed: false,
193 }),
194 })
195}
196
197fn default_change_control_rules() -> ChangeControlRules {
198 ChangeControlRules::V0(ChangeControlRulesV0 {
199 authorized_to_make_change: AuthorizedActionTakers::NoOne,
200 admin_action_takers: AuthorizedActionTakers::NoOne,
201 changing_authorized_action_takers_to_no_one_allowed: false,
202 changing_admin_action_takers_to_no_one_allowed: false,
203 self_changing_admin_action_takers_allowed: false,
204 })
205}
206
207fn default_contract_owner_change_control_rules() -> ChangeControlRules {
208 ChangeControlRules::V0(ChangeControlRulesV0 {
209 authorized_to_make_change: AuthorizedActionTakers::ContractOwner,
210 admin_action_takers: AuthorizedActionTakers::NoOne,
211 changing_authorized_action_takers_to_no_one_allowed: false,
212 changing_admin_action_takers_to_no_one_allowed: false,
213 self_changing_admin_action_takers_allowed: false,
214 })
215}
216
217impl fmt::Display for TokenConfigurationV0 {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 write!(
220 f,
221 "TokenConfigurationV0 {{\n conventions: {:?},\n conventions_change_rules: {:?},\n base_supply: {},\n max_supply: {:?},\n keeps_history: {},\n start_as_paused: {},\n allow_transfer_to_frozen_balance: {},\n max_supply_change_rules: {:?},\n distribution_rules: {},\n manual_minting_rules: {:?},\n manual_burning_rules: {:?},\n freeze_rules: {:?},\n unfreeze_rules: {:?},\n destroy_frozen_funds_rules: {:?},\n emergency_action_rules: {:?},\n main_control_group: {:?},\n main_control_group_can_be_modified: {:?}\n}}",
222 self.conventions,
223 self.conventions_change_rules,
224 self.base_supply,
225 self.max_supply,
226 self.keeps_history,
227 self.start_as_paused,
228 self.allow_transfer_to_frozen_balance,
229 self.max_supply_change_rules,
230 self.distribution_rules,
231 self.manual_minting_rules,
232 self.manual_burning_rules,
233 self.freeze_rules,
234 self.unfreeze_rules,
235 self.destroy_frozen_funds_rules,
236 self.emergency_action_rules,
237 self.main_control_group,
238 self.main_control_group_can_be_modified
239 )
240 }
241}
242
243#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
252pub enum TokenConfigurationPresetFeatures {
253 MostRestrictive,
258
259 WithOnlyEmergencyAction,
265
266 WithMintingAndBurningActions,
270
271 WithAllAdvancedActions,
276
277 WithExtremeActions,
284}
285
286#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, PartialEq, Eq, PartialOrd)]
296pub struct TokenConfigurationPreset {
297 pub features: TokenConfigurationPresetFeatures,
303
304 pub action_taker: AuthorizedActionTakers,
309}
310impl TokenConfigurationPreset {
311 pub fn default_main_control_group_can_be_modified(&self) -> AuthorizedActionTakers {
312 match self.features {
313 TokenConfigurationPresetFeatures::MostRestrictive
314 | TokenConfigurationPresetFeatures::WithOnlyEmergencyAction
315 | TokenConfigurationPresetFeatures::WithMintingAndBurningActions
316 | TokenConfigurationPresetFeatures::WithAllAdvancedActions => {
317 AuthorizedActionTakers::NoOne
318 }
319 TokenConfigurationPresetFeatures::WithExtremeActions => self.action_taker,
320 }
321 }
322 pub fn default_basic_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
323 match self.features {
324 TokenConfigurationPresetFeatures::MostRestrictive
325 | TokenConfigurationPresetFeatures::WithOnlyEmergencyAction => ChangeControlRulesV0 {
326 authorized_to_make_change: AuthorizedActionTakers::NoOne,
327 admin_action_takers: AuthorizedActionTakers::NoOne,
328 changing_authorized_action_takers_to_no_one_allowed: false,
329 changing_admin_action_takers_to_no_one_allowed: false,
330 self_changing_admin_action_takers_allowed: false,
331 },
332 TokenConfigurationPresetFeatures::WithMintingAndBurningActions
333 | TokenConfigurationPresetFeatures::WithAllAdvancedActions => ChangeControlRulesV0 {
334 authorized_to_make_change: self.action_taker,
335 admin_action_takers: self.action_taker,
336 changing_authorized_action_takers_to_no_one_allowed: false,
337 changing_admin_action_takers_to_no_one_allowed: false,
338 self_changing_admin_action_takers_allowed: true,
339 },
340 TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
341 authorized_to_make_change: self.action_taker,
342 admin_action_takers: self.action_taker,
343 changing_authorized_action_takers_to_no_one_allowed: true,
344 changing_admin_action_takers_to_no_one_allowed: true,
345 self_changing_admin_action_takers_allowed: true,
346 },
347 }
348 }
349
350 pub fn default_advanced_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
351 match self.features {
352 TokenConfigurationPresetFeatures::MostRestrictive
353 | TokenConfigurationPresetFeatures::WithOnlyEmergencyAction
354 | TokenConfigurationPresetFeatures::WithMintingAndBurningActions => {
355 ChangeControlRulesV0 {
356 authorized_to_make_change: AuthorizedActionTakers::NoOne,
357 admin_action_takers: AuthorizedActionTakers::NoOne,
358 changing_authorized_action_takers_to_no_one_allowed: false,
359 changing_admin_action_takers_to_no_one_allowed: false,
360 self_changing_admin_action_takers_allowed: false,
361 }
362 }
363 TokenConfigurationPresetFeatures::WithAllAdvancedActions => ChangeControlRulesV0 {
364 authorized_to_make_change: self.action_taker,
365 admin_action_takers: self.action_taker,
366 changing_authorized_action_takers_to_no_one_allowed: false,
367 changing_admin_action_takers_to_no_one_allowed: false,
368 self_changing_admin_action_takers_allowed: true,
369 },
370 TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
371 authorized_to_make_change: self.action_taker,
372 admin_action_takers: self.action_taker,
373 changing_authorized_action_takers_to_no_one_allowed: true,
374 changing_admin_action_takers_to_no_one_allowed: true,
375 self_changing_admin_action_takers_allowed: true,
376 },
377 }
378 }
379
380 pub fn default_emergency_action_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
381 match self.features {
382 TokenConfigurationPresetFeatures::MostRestrictive => ChangeControlRulesV0 {
383 authorized_to_make_change: AuthorizedActionTakers::NoOne,
384 admin_action_takers: AuthorizedActionTakers::NoOne,
385 changing_authorized_action_takers_to_no_one_allowed: false,
386 changing_admin_action_takers_to_no_one_allowed: false,
387 self_changing_admin_action_takers_allowed: false,
388 },
389 TokenConfigurationPresetFeatures::WithAllAdvancedActions
390 | TokenConfigurationPresetFeatures::WithMintingAndBurningActions
391 | TokenConfigurationPresetFeatures::WithOnlyEmergencyAction => ChangeControlRulesV0 {
392 authorized_to_make_change: self.action_taker,
393 admin_action_takers: self.action_taker,
394 changing_authorized_action_takers_to_no_one_allowed: false,
395 changing_admin_action_takers_to_no_one_allowed: false,
396 self_changing_admin_action_takers_allowed: true,
397 },
398 TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
399 authorized_to_make_change: self.action_taker,
400 admin_action_takers: self.action_taker,
401 changing_authorized_action_takers_to_no_one_allowed: true,
402 changing_admin_action_takers_to_no_one_allowed: true,
403 self_changing_admin_action_takers_allowed: true,
404 },
405 }
406 }
407
408 pub fn default_distribution_rules_v0(
409 &self,
410 perpetual_distribution: Option<TokenPerpetualDistribution>,
411 pre_programmed_distribution: Option<TokenPreProgrammedDistribution>,
412 with_direct_pricing: bool,
413 ) -> TokenDistributionRulesV0 {
414 TokenDistributionRulesV0 {
415 perpetual_distribution,
416 perpetual_distribution_rules: self.default_advanced_change_control_rules_v0().into(),
417 pre_programmed_distribution,
418 new_tokens_destination_identity: None,
419 new_tokens_destination_identity_rules: self
420 .default_basic_change_control_rules_v0()
421 .into(),
422 minting_allow_choosing_destination: true,
423 minting_allow_choosing_destination_rules: self
424 .default_basic_change_control_rules_v0()
425 .into(),
426 change_direct_purchase_pricing_rules: if with_direct_pricing {
427 self.default_basic_change_control_rules_v0().into()
428 } else {
429 ChangeControlRulesV0 {
430 authorized_to_make_change: AuthorizedActionTakers::NoOne,
431 admin_action_takers: AuthorizedActionTakers::NoOne,
432 changing_authorized_action_takers_to_no_one_allowed: false,
433 changing_admin_action_takers_to_no_one_allowed: false,
434 self_changing_admin_action_takers_allowed: false,
435 }
436 .into()
437 },
438 }
439 }
440
441 pub fn default_marketplace_rules_v0(&self) -> TokenMarketplaceRulesV0 {
442 TokenMarketplaceRulesV0 {
443 trade_mode: TokenTradeMode::NotTradeable,
444 trade_mode_change_rules: self.default_basic_change_control_rules_v0().into(),
445 }
446 }
447
448 pub fn token_configuration_v0(
449 &self,
450 conventions: TokenConfigurationConvention,
451 base_supply: TokenAmount,
452 max_supply: Option<TokenAmount>,
453 keeps_all_history: bool,
454 with_direct_pricing: bool,
455 ) -> TokenConfigurationV0 {
456 TokenConfigurationV0 {
457 conventions,
458 conventions_change_rules: self.default_basic_change_control_rules_v0().into(),
459 base_supply,
460 max_supply,
461 keeps_history: TokenKeepsHistoryRulesV0::default_for_keeping_all_history(
462 keeps_all_history,
463 )
464 .into(),
465 start_as_paused: false,
466 allow_transfer_to_frozen_balance: true,
467 max_supply_change_rules: self.default_advanced_change_control_rules_v0().into(),
468 distribution_rules: self
469 .default_distribution_rules_v0(None, None, with_direct_pricing)
470 .into(),
471 marketplace_rules: self.default_marketplace_rules_v0().into(),
472 manual_minting_rules: self.default_basic_change_control_rules_v0().into(),
473 manual_burning_rules: self.default_basic_change_control_rules_v0().into(),
474 freeze_rules: self.default_advanced_change_control_rules_v0().into(),
475 unfreeze_rules: self.default_advanced_change_control_rules_v0().into(),
476 destroy_frozen_funds_rules: self.default_advanced_change_control_rules_v0().into(),
477 emergency_action_rules: self
478 .default_emergency_action_change_control_rules_v0()
479 .into(),
480 main_control_group: None,
481 main_control_group_can_be_modified: self.default_main_control_group_can_be_modified(),
482 description: None,
483 }
484 }
485}
486
487impl TokenConfigurationV0 {
488 pub fn default_most_restrictive() -> Self {
489 TokenConfigurationPreset {
490 features: TokenConfigurationPresetFeatures::MostRestrictive,
491 action_taker: AuthorizedActionTakers::NoOne,
492 }
493 .token_configuration_v0(
494 TokenConfigurationConvention::V0(TokenConfigurationConventionV0 {
495 localizations: Default::default(),
496 decimals: 8,
497 }),
498 100000,
499 None,
500 true,
501 false,
502 )
503 }
504
505 pub fn with_base_supply(mut self, base_supply: TokenAmount) -> Self {
506 self.base_supply = base_supply;
507 self
508 }
509}