Skip to main content

dpp/data_contract/associated_token/token_distribution_rules/
mod.rs

1#[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) //just pass through
31            }
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    /// Non-default values per inner field (set destination_identity to a
51    /// specific identifier and `minting_allow_choosing_destination` to true)
52    /// so the wire-shape assertion catches silent zero-out / flip on round-trip.
53    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        // `Identifier` renders as base58 string in JSON. None Options become
95        // `null`. Inner `ChangeControlRules` round-trips its own envelope.
96        // No sized integers in this fixture.
97        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        // `Identifier`'s Serialize emits `Value::Identifier`; interpolating the
121        // Identifier through `platform_value!{...}` runs Serialize and produces
122        // the typed variant. None becomes `Value::Null`.
123        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}