dpp/data_contract/associated_token/token_distribution_rules/
mod.rs1#[cfg(feature = "json-conversion")]
2use crate::serialization::JsonConvertible;
3#[cfg(feature = "value-conversion")]
4use crate::serialization::ValueConvertible;
5use bincode::{Decode, DecodeUntrusted, Encode};
6use derive_more::From;
7use serde::{Deserialize, Serialize};
8
9pub mod accessors;
10pub mod v0;
11
12#[cfg_attr(feature = "json-conversion", derive(JsonConvertible))]
13#[cfg_attr(feature = "value-conversion", derive(ValueConvertible))]
14#[derive(
15 Serialize, Deserialize, Encode, Decode, Debug, Clone, PartialEq, Eq, From, DecodeUntrusted,
16)]
17#[serde(tag = "$formatVersion")]
18pub enum TokenDistributionRules {
19 #[serde(rename = "0")]
20 V0(TokenDistributionRulesV0),
21}
22
23use crate::data_contract::associated_token::token_distribution_rules::v0::TokenDistributionRulesV0;
24use std::fmt;
25
26impl fmt::Display for TokenDistributionRules {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 TokenDistributionRules::V0(v0) => {
30 write!(f, "{}", v0) }
32 }
33 }
34}
35
36#[cfg(all(
37 test,
38 feature = "json-conversion",
39 feature = "value-conversion",
40 feature = "serde-conversion"
41))]
42mod json_convertible_tests {
43 use super::*;
44 use crate::data_contract::associated_token::token_distribution_rules::v0::TokenDistributionRulesV0;
45 use crate::data_contract::change_control_rules::v0::ChangeControlRulesV0;
46 use crate::data_contract::change_control_rules::ChangeControlRules;
47 use platform_value::{platform_value, Identifier, Value};
48 use serde_json::json;
49
50 fn fixture() -> TokenDistributionRules {
54 let ccr = || ChangeControlRules::V0(ChangeControlRulesV0::default());
55 TokenDistributionRules::V0(TokenDistributionRulesV0 {
56 perpetual_distribution: None,
57 perpetual_distribution_rules: ccr(),
58 pre_programmed_distribution: None,
59 new_tokens_destination_identity: Some(Identifier::new([0x42; 32])),
60 new_tokens_destination_identity_rules: ccr(),
61 minting_allow_choosing_destination: true,
62 minting_allow_choosing_destination_rules: ccr(),
63 change_direct_purchase_pricing_rules: ccr(),
64 })
65 }
66
67 fn default_ccr_json() -> serde_json::Value {
68 json!({
69 "$formatVersion": "0",
70 "authorizedToMakeChange": {"$type": "noOne"},
71 "adminActionTakers": {"$type": "noOne"},
72 "changingAuthorizedActionTakersToNoOneAllowed": false,
73 "changingAdminActionTakersToNoOneAllowed": false,
74 "selfChangingAdminActionTakersAllowed": false,
75 })
76 }
77
78 fn default_ccr_value() -> Value {
79 platform_value!({
80 "$formatVersion": "0",
81 "authorizedToMakeChange": {"$type": "noOne"},
82 "adminActionTakers": {"$type": "noOne"},
83 "changingAuthorizedActionTakersToNoOneAllowed": false,
84 "changingAdminActionTakersToNoOneAllowed": false,
85 "selfChangingAdminActionTakersAllowed": false,
86 })
87 }
88
89 #[test]
90 fn json_round_trip_with_full_wire_shape() {
91 use crate::serialization::JsonConvertible;
92 let original = fixture();
93 let json = original.to_json().expect("to_json");
94 assert_eq!(
98 json,
99 json!({
100 "$formatVersion": "0",
101 "perpetualDistribution": null,
102 "perpetualDistributionRules": default_ccr_json(),
103 "preProgrammedDistribution": null,
104 "newTokensDestinationIdentity": "5TeWSsjg2gbxCyWVniXeCmwM7UtHTCK7svzJr5xYJzHf",
105 "newTokensDestinationIdentityRules": default_ccr_json(),
106 "mintingAllowChoosingDestination": true,
107 "mintingAllowChoosingDestinationRules": default_ccr_json(),
108 "changeDirectPurchasePricingRules": default_ccr_json(),
109 })
110 );
111 let recovered = TokenDistributionRules::from_json(json).expect("from_json");
112 assert_eq!(original, recovered);
113 }
114
115 #[test]
116 fn value_round_trip_with_full_wire_shape() {
117 use crate::serialization::ValueConvertible;
118 let original = fixture();
119 let value = original.to_object().expect("to_object");
120 let id = Identifier::new([0x42; 32]);
124 assert_eq!(
125 value,
126 platform_value!({
127 "$formatVersion": "0",
128 "perpetualDistribution": Value::Null,
129 "perpetualDistributionRules": default_ccr_value(),
130 "preProgrammedDistribution": Value::Null,
131 "newTokensDestinationIdentity": id,
132 "newTokensDestinationIdentityRules": default_ccr_value(),
133 "mintingAllowChoosingDestination": true,
134 "mintingAllowChoosingDestinationRules": default_ccr_value(),
135 "changeDirectPurchasePricingRules": default_ccr_value(),
136 })
137 );
138 let recovered = TokenDistributionRules::from_object(value).expect("from_object");
139 assert_eq!(original, recovered);
140 }
141}