1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use super::*;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
    Uint128, Addr,
};

use self::metadata::Metadata;

#[cfg(test)]
use crate::state::metadata::Extension;


/////////////////////////////////////////////////////////////////////////////////
// Contract and Token Id configs
/////////////////////////////////////////////////////////////////////////////////

/// contract configuration, spanning all `token_ids` generated by this contract
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct ContractConfig {
    pub admin: Option<Addr>,
    /// These are contract-level curators that can curate new token_ids and mint initial balances. They cannot 
    /// mint additional tokens of existing token_ids, unless they are also minters of the specific
    /// fungible token
    pub curators: Vec<Addr>,
    pub token_id_list: Vec<String>,
    pub tx_cnt: u64,
    pub prng_seed: Vec<u8>,
    pub contract_address: Addr,
}

/// message sent my instantiator and curators for a specific `token_id`'s token info
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TokenInfoMsg {
    pub token_id: String,
    pub name: String,
    pub symbol: String,
    pub token_config: TknConfig,
    pub public_metadata: Option<Metadata>,
    pub private_metadata: Option<Metadata>,
}

impl TokenInfoMsg {
    pub fn to_store(&self, curator: &Addr) -> StoredTokenInfo {
        StoredTokenInfo { 
            token_id: self.token_id.clone(), 
            name: self.name.clone(), 
            symbol: self.symbol.clone(), 
            token_config: self.token_config.clone(), 
            public_metadata: self.public_metadata.clone(), 
            private_metadata: self.private_metadata.clone(),
            curator: curator.clone(),
        }
    }
}

/// information for a specific `token_id`
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct StoredTokenInfo {
    pub token_id: String,
    pub name: String,
    pub symbol: String,
    pub token_config: TknConfig,
    pub public_metadata: Option<Metadata>,
    pub private_metadata: Option<Metadata>,
    pub curator: Addr,
}



#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TknConfig {
    /// no `owner_may_update_metadata`because there can be multiple owners
    Fungible {
        minters: Vec<Addr>,
        /// Decimals play no part in the contract logic of the base specification of SNIP1155, 
        /// as there are no `deposit` and `redeem` features as seen in SNIP20. The UI application
        /// has discretion in handling decimals
        decimals: u8,
        public_total_supply: bool,
        enable_mint: bool,
        enable_burn: bool,
        minter_may_update_metadata: bool,
    },
    /// no `enable_mint` option because NFT can be minted only once using `CurateTokenIds`
    Nft {
        /// NFTs' minters cannot mint additional tokens, but may be able to change metadata
        minters: Vec<Addr>,
        /// total supply can be zero if the token has been burnt
        public_total_supply: bool,
        owner_is_public: bool,
        enable_burn: bool,
        owner_may_update_metadata: bool,
        minter_may_update_metadata: bool,
    }
}

impl TknConfig {
    /// Combines variables in the TknConfig enum into a single struct for easier handling in contract logic.
    pub fn flatten(&self) -> TknConfigFlat {
        match self {
            TknConfig::Fungible { 
                minters,
                decimals, 
                public_total_supply, 
                enable_mint, 
                enable_burn, 
                minter_may_update_metadata 
            } => {
                TknConfigFlat {
                    is_nft: false,
                    minters: minters.clone(),
                    decimals: *decimals,
                    public_total_supply: *public_total_supply,
                    owner_is_public: false,
                    enable_mint: *enable_mint,
                    enable_burn: *enable_burn,
                    minter_may_update_metadata: *minter_may_update_metadata,
                    /// there can be multiple owners, so owners cannot update metadata
                    owner_may_update_metadata: false,
                }
            },
            TknConfig::Nft { 
                minters,
                public_total_supply, 
                owner_is_public, 
                enable_burn, 
                owner_may_update_metadata, 
                minter_may_update_metadata
            } => {
                TknConfigFlat {
                    is_nft: true,
                    /// NFTs' minters cannot mint additional tokens, but may be able to change metadata
                    minters: minters.clone(),
                    decimals: 0_u8,
                    public_total_supply: *public_total_supply,
                    owner_is_public: *owner_is_public,
                    /// NFT can be minted only once using `CurateTokenIds`
                    enable_mint: false,
                    enable_burn: *enable_burn,
                    minter_may_update_metadata: *minter_may_update_metadata,
                    owner_may_update_metadata: *owner_may_update_metadata,
                }
            },
        } 
    }

    // note that default is normally `false`. These default to `true` is for unit testing purposes
    #[cfg(test)]
    pub fn default_fungible() -> Self {
        TknConfig::Fungible { 
            minters: vec![Addr::unchecked("addr0".to_string())],
            decimals: 6_u8,
            public_total_supply: true, 
            enable_mint: true,
            enable_burn: true, 
            minter_may_update_metadata: true, 
        }
    }

    #[cfg(test)]
    pub fn default_nft() -> Self {
        TknConfig::Nft { 
            minters: vec![],
            public_total_supply: true, 
            owner_is_public: true,
            enable_burn: true, 
            owner_may_update_metadata: true, 
            minter_may_update_metadata: true,
        }
    }
}

/// Constructed from input enum `TknConfig`. Flattened for easier handling in contract logic  
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TknConfigFlat {
    pub is_nft: bool,
    pub minters: Vec<Addr>,
    pub decimals: u8,
    pub public_total_supply: bool,
    pub owner_is_public: bool,
    pub enable_mint: bool,
    pub enable_burn: bool,
    pub minter_may_update_metadata: bool,
    pub owner_may_update_metadata: bool,
}

impl TknConfigFlat {
    pub fn to_enum(&self) -> TknConfig {
        match self.is_nft {
            true => {
                TknConfig::Nft { 
                    minters: self.minters.clone(),
                    public_total_supply: self.public_total_supply, 
                    owner_is_public: self.owner_is_public, 
                    enable_burn: self.enable_burn, 
                    owner_may_update_metadata: self.owner_may_update_metadata,
                    minter_may_update_metadata: self.minter_may_update_metadata,
                }
            },
            false => {
                TknConfig::Fungible { 
                    minters: self.minters.clone(), 
                    decimals: self.decimals, 
                    public_total_supply: self.public_total_supply, 
                    enable_mint: self.enable_mint, 
                    enable_burn: self.enable_burn, 
                    minter_may_update_metadata: self.minter_may_update_metadata 
                }
            },
        }
    }
}


/////////////////////////////////////////////////////////////////////////////////
// Other structs, enums and functions
/////////////////////////////////////////////////////////////////////////////////

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct CurateTokenId {
    pub token_info: TokenInfoMsg,
    pub balances: Vec<TokenIdBalance>,
}

#[cfg(test)]
impl Default for CurateTokenId {
    fn default() -> Self {
        Self { 
            token_info: TokenInfoMsg { 
                token_id: "0".to_string(), 
                name: "token0".to_string(), 
                symbol: "TKN".to_string(), 
                token_config: TknConfig::default_fungible(),
                public_metadata: Some(Metadata {
                    token_uri: Some("public uri".to_string()),
                    extension: Some(Extension::default()),
                }), 
                private_metadata: Some(Metadata {
                    token_uri: Some("private uri".to_string()),
                    extension: Some(Extension::default()),
                }),  
            }, 
            balances: vec![TokenIdBalance { 
                address: Addr::unchecked("addr0".to_string()), 
                amount: Uint128::from(1000_u64) 
            }],
        }
    }
}

/// used for MintToken and BurnToken in the base specifications
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TokenAmount {
    pub token_id: String,
    /// For BurnToken, only `Balance.amount` is relevant. `Balance.address` need to be the 
    /// owner's address. This design decision is to allow `BurnToken` to apply to other addresses, 
    /// possible in the additional specifications
    pub balances: Vec<TokenIdBalance>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TokenIdBalance {
    /// For BurnToken, `address` needs to be the owner's address. This design decision is 
    /// to allow `BurnToken` to apply to other addresses, possible in the additional 
    /// specifications
    pub address: Addr,
    pub amount: Uint128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OwnerBalance {
    pub token_id: String,
    pub amount: Uint128,
}