Skip to content

Commit 62aded7

Browse files
feat: token config presets
1 parent 83a660f commit 62aded7

File tree

3 files changed

+195
-101
lines changed

3 files changed

+195
-101
lines changed

packages/rs-dpp/src/data_contract/associated_token/token_configuration/v0/mod.rs

Lines changed: 181 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::data_contract::associated_token::token_distribution_rules::v0::TokenD
77
use crate::data_contract::associated_token::token_distribution_rules::TokenDistributionRules;
88
use crate::data_contract::associated_token::token_keeps_history_rules::v0::TokenKeepsHistoryRulesV0;
99
use crate::data_contract::associated_token::token_keeps_history_rules::TokenKeepsHistoryRules;
10+
use crate::data_contract::associated_token::token_perpetual_distribution::TokenPerpetualDistribution;
11+
use crate::data_contract::associated_token::token_pre_programmed_distribution::TokenPreProgrammedDistribution;
1012
use crate::data_contract::change_control_rules::authorized_action_takers::AuthorizedActionTakers;
1113
use crate::data_contract::change_control_rules::v0::ChangeControlRulesV0;
1214
use crate::data_contract::change_control_rules::ChangeControlRules;
@@ -31,7 +33,7 @@ pub struct TokenConfigurationV0 {
3133
/// The rules for keeping history.
3234
#[serde(default = "default_token_keeps_history_rules")]
3335
pub keeps_history: TokenKeepsHistoryRules,
34-
/// Do we start off as paused, meaning that we can not transfer till we unpause.
36+
/// True if we start off as paused, meaning that we can not transfer till we unpause.
3537
#[serde(default = "default_starts_as_paused")]
3638
pub start_as_paused: bool,
3739
/// Allow to transfer and mint tokens to frozen identity token balances
@@ -173,133 +175,212 @@ impl fmt::Display for TokenConfigurationV0 {
173175
}
174176
}
175177

176-
impl TokenConfigurationV0 {
177-
pub fn default_most_restrictive() -> Self {
178-
Self {
179-
conventions: TokenConfigurationConvention::V0(TokenConfigurationConventionV0 {
180-
localizations: Default::default(),
181-
decimals: 8,
182-
}),
183-
conventions_change_rules: ChangeControlRulesV0 {
178+
#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
179+
pub enum TokenConfigurationPresetFeatures {
180+
/// Nothing can be changed once it is set
181+
MostRestrictive,
182+
/// The action taker can not mint or burn tokens, or any advanced action.
183+
/// They can pause the token if needed.
184+
WithOnlyEmergencyAction,
185+
WithMintingAndBurningActions,
186+
/// The action taker can do advanced actions like freezing
187+
WithAllAdvancedActions,
188+
/// The action taker is a god, he can do everything, even taking away his own power.
189+
WithExtremeActions,
190+
}
191+
192+
#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, PartialEq, Eq, PartialOrd)]
193+
pub struct TokenConfigurationPreset {
194+
features: TokenConfigurationPresetFeatures,
195+
action_taker: AuthorizedActionTakers,
196+
}
197+
198+
impl TokenConfigurationPreset {
199+
pub fn default_main_control_group_can_be_modified(&self) -> AuthorizedActionTakers {
200+
match self.features {
201+
TokenConfigurationPresetFeatures::MostRestrictive
202+
| TokenConfigurationPresetFeatures::WithOnlyEmergencyAction
203+
| TokenConfigurationPresetFeatures::WithMintingAndBurningActions
204+
| TokenConfigurationPresetFeatures::WithAllAdvancedActions => {
205+
AuthorizedActionTakers::NoOne
206+
}
207+
TokenConfigurationPresetFeatures::WithExtremeActions => self.action_taker,
208+
}
209+
}
210+
pub fn default_basic_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
211+
match self.features {
212+
TokenConfigurationPresetFeatures::MostRestrictive
213+
| TokenConfigurationPresetFeatures::WithOnlyEmergencyAction => ChangeControlRulesV0 {
184214
authorized_to_make_change: AuthorizedActionTakers::NoOne,
185215
admin_action_takers: AuthorizedActionTakers::NoOne,
186216
changing_authorized_action_takers_to_no_one_allowed: false,
187217
changing_admin_action_takers_to_no_one_allowed: false,
188218
self_changing_admin_action_takers_allowed: false,
189-
}
190-
.into(),
191-
base_supply: 100000,
192-
max_supply: None,
193-
keeps_history: TokenKeepsHistoryRules::V0(TokenKeepsHistoryRulesV0 {
194-
keeps_transfer_history: true,
195-
keeps_freezing_history: true,
196-
keeps_minting_history: true,
197-
keeps_burning_history: true,
198-
keeps_direct_pricing_history: true,
199-
keeps_direct_purchase_history: true,
200-
}),
201-
start_as_paused: false,
202-
allow_transfer_to_frozen_balance: true,
203-
max_supply_change_rules: ChangeControlRulesV0 {
204-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
205-
admin_action_takers: AuthorizedActionTakers::NoOne,
219+
},
220+
TokenConfigurationPresetFeatures::WithMintingAndBurningActions
221+
| TokenConfigurationPresetFeatures::WithAllAdvancedActions => ChangeControlRulesV0 {
222+
authorized_to_make_change: self.action_taker,
223+
admin_action_takers: self.action_taker,
206224
changing_authorized_action_takers_to_no_one_allowed: false,
207225
changing_admin_action_takers_to_no_one_allowed: false,
208-
self_changing_admin_action_takers_allowed: false,
209-
}
210-
.into(),
211-
distribution_rules: TokenDistributionRules::V0(TokenDistributionRulesV0 {
212-
perpetual_distribution: None,
213-
perpetual_distribution_rules: ChangeControlRulesV0 {
214-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
215-
admin_action_takers: AuthorizedActionTakers::NoOne,
216-
changing_authorized_action_takers_to_no_one_allowed: false,
217-
changing_admin_action_takers_to_no_one_allowed: false,
218-
self_changing_admin_action_takers_allowed: false,
219-
}
220-
.into(),
221-
pre_programmed_distribution: None,
222-
new_tokens_destination_identity: None,
223-
new_tokens_destination_identity_rules: ChangeControlRulesV0 {
224-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
225-
admin_action_takers: AuthorizedActionTakers::NoOne,
226-
changing_authorized_action_takers_to_no_one_allowed: false,
227-
changing_admin_action_takers_to_no_one_allowed: false,
228-
self_changing_admin_action_takers_allowed: false,
229-
}
230-
.into(),
231-
minting_allow_choosing_destination: true,
232-
minting_allow_choosing_destination_rules: ChangeControlRulesV0 {
226+
self_changing_admin_action_takers_allowed: true,
227+
},
228+
TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
229+
authorized_to_make_change: self.action_taker,
230+
admin_action_takers: self.action_taker,
231+
changing_authorized_action_takers_to_no_one_allowed: true,
232+
changing_admin_action_takers_to_no_one_allowed: true,
233+
self_changing_admin_action_takers_allowed: true,
234+
},
235+
}
236+
}
237+
238+
pub fn default_advanced_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
239+
match self.features {
240+
TokenConfigurationPresetFeatures::MostRestrictive
241+
| TokenConfigurationPresetFeatures::WithOnlyEmergencyAction
242+
| TokenConfigurationPresetFeatures::WithMintingAndBurningActions => {
243+
ChangeControlRulesV0 {
233244
authorized_to_make_change: AuthorizedActionTakers::NoOne,
234245
admin_action_takers: AuthorizedActionTakers::NoOne,
235246
changing_authorized_action_takers_to_no_one_allowed: false,
236247
changing_admin_action_takers_to_no_one_allowed: false,
237248
self_changing_admin_action_takers_allowed: false,
238249
}
239-
.into(),
240-
change_direct_purchase_pricing_rules: ChangeControlRules::V0(
241-
ChangeControlRulesV0 {
242-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
243-
admin_action_takers: AuthorizedActionTakers::NoOne,
244-
changing_authorized_action_takers_to_no_one_allowed: false,
245-
changing_admin_action_takers_to_no_one_allowed: false,
246-
self_changing_admin_action_takers_allowed: false,
247-
},
248-
),
249-
}),
250-
manual_minting_rules: ChangeControlRulesV0 {
251-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
252-
admin_action_takers: AuthorizedActionTakers::NoOne,
253-
changing_authorized_action_takers_to_no_one_allowed: false,
254-
changing_admin_action_takers_to_no_one_allowed: false,
255-
self_changing_admin_action_takers_allowed: false,
256250
}
257-
.into(),
258-
manual_burning_rules: ChangeControlRulesV0 {
259-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
260-
admin_action_takers: AuthorizedActionTakers::NoOne,
251+
TokenConfigurationPresetFeatures::WithAllAdvancedActions => ChangeControlRulesV0 {
252+
authorized_to_make_change: self.action_taker,
253+
admin_action_takers: self.action_taker,
261254
changing_authorized_action_takers_to_no_one_allowed: false,
262255
changing_admin_action_takers_to_no_one_allowed: false,
263-
self_changing_admin_action_takers_allowed: false,
264-
}
265-
.into(),
266-
freeze_rules: ChangeControlRulesV0 {
267-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
268-
admin_action_takers: AuthorizedActionTakers::NoOne,
269-
changing_authorized_action_takers_to_no_one_allowed: false,
270-
changing_admin_action_takers_to_no_one_allowed: false,
271-
self_changing_admin_action_takers_allowed: false,
272-
}
273-
.into(),
274-
unfreeze_rules: ChangeControlRulesV0 {
275-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
276-
admin_action_takers: AuthorizedActionTakers::NoOne,
277-
changing_authorized_action_takers_to_no_one_allowed: false,
278-
changing_admin_action_takers_to_no_one_allowed: false,
279-
self_changing_admin_action_takers_allowed: false,
280-
}
281-
.into(),
282-
destroy_frozen_funds_rules: ChangeControlRulesV0 {
256+
self_changing_admin_action_takers_allowed: true,
257+
},
258+
TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
259+
authorized_to_make_change: self.action_taker,
260+
admin_action_takers: self.action_taker,
261+
changing_authorized_action_takers_to_no_one_allowed: true,
262+
changing_admin_action_takers_to_no_one_allowed: true,
263+
self_changing_admin_action_takers_allowed: true,
264+
},
265+
}
266+
}
267+
268+
pub fn default_emergency_action_change_control_rules_v0(&self) -> ChangeControlRulesV0 {
269+
match self.features {
270+
TokenConfigurationPresetFeatures::MostRestrictive => ChangeControlRulesV0 {
283271
authorized_to_make_change: AuthorizedActionTakers::NoOne,
284272
admin_action_takers: AuthorizedActionTakers::NoOne,
285273
changing_authorized_action_takers_to_no_one_allowed: false,
286274
changing_admin_action_takers_to_no_one_allowed: false,
287275
self_changing_admin_action_takers_allowed: false,
288-
}
289-
.into(),
290-
emergency_action_rules: ChangeControlRulesV0 {
291-
authorized_to_make_change: AuthorizedActionTakers::NoOne,
292-
admin_action_takers: AuthorizedActionTakers::NoOne,
276+
},
277+
TokenConfigurationPresetFeatures::WithAllAdvancedActions
278+
| TokenConfigurationPresetFeatures::WithMintingAndBurningActions
279+
| TokenConfigurationPresetFeatures::WithOnlyEmergencyAction => ChangeControlRulesV0 {
280+
authorized_to_make_change: self.action_taker,
281+
admin_action_takers: self.action_taker,
293282
changing_authorized_action_takers_to_no_one_allowed: false,
294283
changing_admin_action_takers_to_no_one_allowed: false,
295-
self_changing_admin_action_takers_allowed: false,
296-
}
284+
self_changing_admin_action_takers_allowed: true,
285+
},
286+
TokenConfigurationPresetFeatures::WithExtremeActions => ChangeControlRulesV0 {
287+
authorized_to_make_change: self.action_taker,
288+
admin_action_takers: self.action_taker,
289+
changing_authorized_action_takers_to_no_one_allowed: true,
290+
changing_admin_action_takers_to_no_one_allowed: true,
291+
self_changing_admin_action_takers_allowed: true,
292+
},
293+
}
294+
}
295+
296+
pub fn default_distribution_rules_v0(
297+
&self,
298+
perpetual_distribution: Option<TokenPerpetualDistribution>,
299+
pre_programmed_distribution: Option<TokenPreProgrammedDistribution>,
300+
with_direct_pricing: bool,
301+
) -> TokenDistributionRulesV0 {
302+
TokenDistributionRulesV0 {
303+
perpetual_distribution,
304+
perpetual_distribution_rules: self.default_advanced_change_control_rules_v0().into(),
305+
pre_programmed_distribution,
306+
new_tokens_destination_identity: None,
307+
new_tokens_destination_identity_rules: self
308+
.default_basic_change_control_rules_v0()
309+
.into(),
310+
minting_allow_choosing_destination: true,
311+
minting_allow_choosing_destination_rules: self
312+
.default_basic_change_control_rules_v0()
313+
.into(),
314+
change_direct_purchase_pricing_rules: if with_direct_pricing {
315+
self.default_basic_change_control_rules_v0().into()
316+
} else {
317+
ChangeControlRulesV0 {
318+
authorized_to_make_change: AuthorizedActionTakers::NoOne,
319+
admin_action_takers: AuthorizedActionTakers::NoOne,
320+
changing_authorized_action_takers_to_no_one_allowed: false,
321+
changing_admin_action_takers_to_no_one_allowed: false,
322+
self_changing_admin_action_takers_allowed: false,
323+
}
324+
.into()
325+
},
326+
}
327+
}
328+
329+
pub fn token_configuration_v0(
330+
&self,
331+
conventions: TokenConfigurationConvention,
332+
base_supply: TokenAmount,
333+
max_supply: Option<TokenAmount>,
334+
keeps_all_history: bool,
335+
with_direct_pricing: bool,
336+
) -> TokenConfigurationV0 {
337+
TokenConfigurationV0 {
338+
conventions,
339+
conventions_change_rules: self.default_basic_change_control_rules_v0().into(),
340+
base_supply,
341+
max_supply,
342+
keeps_history: TokenKeepsHistoryRulesV0::default_for_keeping_all_history(
343+
keeps_all_history,
344+
)
297345
.into(),
346+
start_as_paused: false,
347+
allow_transfer_to_frozen_balance: true,
348+
max_supply_change_rules: self.default_advanced_change_control_rules_v0().into(),
349+
distribution_rules: self
350+
.default_distribution_rules_v0(None, None, with_direct_pricing)
351+
.into(),
352+
manual_minting_rules: self.default_basic_change_control_rules_v0().into(),
353+
manual_burning_rules: self.default_basic_change_control_rules_v0().into(),
354+
freeze_rules: self.default_advanced_change_control_rules_v0().into(),
355+
unfreeze_rules: self.default_advanced_change_control_rules_v0().into(),
356+
destroy_frozen_funds_rules: self.default_advanced_change_control_rules_v0().into(),
357+
emergency_action_rules: self
358+
.default_emergency_action_change_control_rules_v0()
359+
.into(),
298360
main_control_group: None,
299-
main_control_group_can_be_modified: AuthorizedActionTakers::NoOne,
361+
main_control_group_can_be_modified: self.default_main_control_group_can_be_modified(),
300362
description: None,
301363
}
302364
}
365+
}
366+
367+
impl TokenConfigurationV0 {
368+
pub fn default_most_restrictive() -> Self {
369+
TokenConfigurationPreset {
370+
features: TokenConfigurationPresetFeatures::MostRestrictive,
371+
action_taker: AuthorizedActionTakers::NoOne,
372+
}
373+
.token_configuration_v0(
374+
TokenConfigurationConvention::V0(TokenConfigurationConventionV0 {
375+
localizations: Default::default(),
376+
decimals: 8,
377+
}),
378+
100000,
379+
None,
380+
true,
381+
false,
382+
)
383+
}
303384

304385
pub fn with_base_supply(mut self, base_supply: TokenAmount) -> Self {
305386
self.base_supply = base_supply;

packages/rs-dpp/src/data_contract/associated_token/token_keeps_history_rules/v0/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ pub struct TokenKeepsHistoryRulesV0 {
3535
pub keeps_direct_purchase_history: bool,
3636
}
3737

38+
impl TokenKeepsHistoryRulesV0 {
39+
pub fn default_for_keeping_all_history(keeps_all_history: bool) -> TokenKeepsHistoryRulesV0 {
40+
TokenKeepsHistoryRulesV0 {
41+
keeps_transfer_history: keeps_all_history,
42+
keeps_freezing_history: keeps_all_history,
43+
keeps_minting_history: keeps_all_history,
44+
keeps_burning_history: keeps_all_history,
45+
keeps_direct_pricing_history: keeps_all_history,
46+
keeps_direct_purchase_history: keeps_all_history,
47+
}
48+
}
49+
}
50+
3851
impl Default for TokenKeepsHistoryRulesV0 {
3952
fn default() -> Self {
4053
TokenKeepsHistoryRulesV0 {

packages/rs-dpp/src/data_contract/change_control_rules/authorized_action_takers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::collections::BTreeMap;
99
use std::fmt;
1010

1111
#[derive(
12-
Serialize, Deserialize, Decode, Encode, Debug, Clone, PartialEq, Eq, PartialOrd, Default,
12+
Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default,
1313
)]
1414
pub enum AuthorizedActionTakers {
1515
#[default]

0 commit comments

Comments
 (0)