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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use cosmwasm_std::{Uint128, Addr, Binary, StdResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::state::{
        state_structs::{CurateTokenId, TokenAmount, StoredTokenInfo, OwnerBalance}, 
        permissions::{PermissionKey, Permission,},
        txhistory::Tx, 
        metadata::Metadata, 
     expiration::Expiration,
    };

use secret_toolkit::permit::Permit;


/////////////////////////////////////////////////////////////////////////////////
// Init messages
/////////////////////////////////////////////////////////////////////////////////

#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)] //PartialEq
pub struct InstantiateMsg {
    /// if `false` the contract will instantiate permanently as a no-admin (permissionless) contract
    pub has_admin: bool,
    /// if `admin` == `None` && `has_admin` == `true`, the instantiator will be admin
    /// if `has_admin` == `false`, this field will be ignore (ie: there will be no admin)
    pub admin: Option<Addr>,
    /// sets initial list of curators, which can create new token_ids 
    pub curators: Vec<Addr>,
    /// curates initial list of tokens
    pub initial_tokens: Vec<CurateTokenId>,
    /// for `create_viewing_key` function
    pub entropy: String,
}

/////////////////////////////////////////////////////////////////////////////////
// Handle Messages
/////////////////////////////////////////////////////////////////////////////////

/// Handle messages to SNIP1155 contract. 
/// 
/// Mostly responds with `HandleAnswer { <variant_name>: { status: success }}` if successful.
/// See [HandleAnswer](crate::msg::HandleAnswer) for the response messages for each variant.
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    /// curates new token_ids. Only curators can access this function. 
    CurateTokenIds {
        initial_tokens: Vec<CurateTokenId>,
        memo: Option<String>,
        padding: Option<String>,
    },
    /// mints additional tokens of existing fungible token_ids, if configuration allows this, ie
    /// `enable_mint == true`.
    /// Only minters can access this function
    MintTokens {
        mint_tokens: Vec<TokenAmount>,
        memo: Option<String>,
        padding: Option<String>,
    },
    /// burns existing tokens, if configuration allows this, ie
    /// `enable_burn == true`.
    /// Only owners can burn their own tokens in the base specifications. Flexibility is built
    /// into the contract functions to allow other addresses to burn tokens, allowed in additional specifications.
    BurnTokens {
        burn_tokens: Vec<TokenAmount>,
        memo: Option<String>,
        padding: Option<String>,
    },
    /// allows owner or minter to change metadata if allowed by token_id configuration.
    ChangeMetadata {
        token_id: String,
        /// does not attempt to change if left blank. Can effectively remove metadata by setting 
        /// metadata to `Some(Metadata {token_uri: None, extension: None})`
        /// used Box<T> to reduce the total size of the enum variant, to decrease size difference 
        /// between variants. Not strictly necessary.
        public_metadata: Box<Option<Metadata>>,
        /// does not attempt to change if left blank. Can effectively remove metadata by setting 
        /// metadata to `Some(Metadata {token_uri: None, extension: None})`
        /// used Box<T> to reduce the total size of the enum variant, to decrease size difference 
        /// between variants. Not strictly necessary.
        private_metadata: Box<Option<Metadata>>,
    },
    /// transfers one or more tokens of a single token_id. Other third address can perform this function
    /// if it has permission to transfer. ie: if addr3 can call this function to transfer tokens from addr0
    /// to addr2, if addr0 gives addr3 enough transfer allowance.
    Transfer {
        token_id: String,
        // equivalent to `owner` in SNIP20. Tokens are sent from this address. 
        from: Addr,
        recipient: Addr,
        amount: Uint128,
        memo: Option<String>,
        padding: Option<String>,
    },
    /// performs `transfer`s of multiple token_ids in a single transaction
    BatchTransfer {
        actions: Vec<TransferAction>,
        padding: Option<String>,
    },
    /// similar to transfer, but also sends a cosmos message. The recipient needs to be a contract that 
    /// has a SNIP1155Receive handle function. See [receiver](crate::receiver) for more information. 
    Send {
        token_id: String,
        // equivalent to `owner` in SNIP20. Tokens are sent from this address. 
        from: Addr,
        recipient: Addr,
        recipient_code_hash: Option<String>,
        amount: Uint128,
        msg: Option<Binary>,
        memo: Option<String>,
        padding: Option<String>,
    },
    /// performs `send` of multiple token_ids in a single transaction
    BatchSend {
        actions: Vec<SendAction>,
        padding: Option<String>,
    },
    /// allows an owner of token_ids to change transfer or viewership permissions to other addresses.  
    /// 
    /// The base specification has three types of permissions:
    /// * view balance permission: owner can allow another address to view owner's balance of specific token_ids
    /// * view private metadata: owner can allow another address to view private metadata of specific token_ids
    /// * transfer allowance: owner can give permission to another address to transfer tokens up to a certain limit (cumulatively)
    /// Owners can set an [expiry](crate::state::expiration) for each of these permissions. 
    /// 
    /// SNIP1155 gives flexibility for permissions to have any combination of
    /// * type of permission granted
    /// * on which token_ids
    GivePermission {
        /// address being granted/revoked permission
        allowed_address: Addr,
        /// token id to apply approval/revocation to.
        /// Additional Spec feature: if == None, perform action for all owner's `token_id`s
        token_id: String,
        /// optional permission level for viewing balance. If ignored, leaves current permission settings
        view_balance: Option<bool>,
        view_balance_expiry: Option<Expiration>,
        /// optional permission level for viewing private metadata. If ignored, leaves current permission settings
        view_private_metadata: Option<bool>,
        view_private_metadata_expiry: Option<Expiration>,
        /// set allowance by for transfer approvals. If ignored, leaves current permission settings
        transfer: Option<Uint128>,
        transfer_expiry: Option<Expiration>,
        /// optional message length padding
        padding: Option<String>,
    },
    /// Removes all permissions that a specific owner has granted to a specific address, for a specific token_id.
    /// A permission grantee can use this function to renounce a permission it has been given.
    /// For owners, the `GivePermission` message can be used instead to have the same effect as `RevokePermission`.
    RevokePermission {
        token_id: String,
        /// token owner
        owner: Addr,
        /// address which has permission
        allowed_address: Addr,
        padding: Option<String>,
    },
    CreateViewingKey {
        entropy: String,
        padding: Option<String>,
    },
    SetViewingKey {
        key: String,
        padding: Option<String>,
    },
    /// disallow the use of a query permit
    RevokePermit {
        permit_name: String,
        padding: Option<String>,
    },
    AddCurators {
        add_curators: Vec<Addr>,
        padding: Option<String>,
    },
    RemoveCurators {
        remove_curators: Vec<Addr>,
        padding: Option<String>,
    },
    AddMinters {
        token_id: String,
        add_minters: Vec<Addr>,
        padding: Option<String>,
    },
    RemoveMinters {
        token_id: String,
        remove_minters: Vec<Addr>,
        padding: Option<String>,
    },
    ChangeAdmin {
        new_admin: Addr,
        padding: Option<String>,
    },
    /// Permanently breaks admin keys for this contract. No admin function can be called after this
    /// action. Any existing curators or minters will remain as curators or minters; no new curators can be 
    /// added and no current curator can be removed. 
    /// 
    /// Requires caller to input current admin address and contract address. These inputs are not strictly 
    /// necessary, but as a safety precaution to reduce the chances of accidentally calling this function.
    RemoveAdmin {
        current_admin: Addr,
        contract_address: Addr,
        padding: Option<String>,
    },
    RegisterReceive {
        code_hash: String,
        padding: Option<String>,
    },
}

/// Handle answers in the `data` field of `HandleResponse`. See
/// [HandleMsg](crate::msg::HandleMsg), which has more details
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteAnswer {
    CurateTokenIds { status: ResponseStatus },
    MintTokens { status: ResponseStatus },
    BurnTokens { status: ResponseStatus },
    ChangeMetadata { status: ResponseStatus },
    Transfer { status: ResponseStatus },
    BatchTransfer { status: ResponseStatus },
    Send { status: ResponseStatus },
    BatchSend { status: ResponseStatus },
    GivePermission { status: ResponseStatus },
    RevokePermission { status: ResponseStatus },
    CreateViewingKey { key: String },
    SetViewingKey { status: ResponseStatus },
    RevokePermit { status: ResponseStatus },
    AddCurators { status: ResponseStatus },
    RemoveCurators { status: ResponseStatus },
    AddMinters { status: ResponseStatus },
    RemoveMinters { status: ResponseStatus },
    ChangeAdmin { status: ResponseStatus },
    RemoveAdmin { status: ResponseStatus },
    RegisterReceive { status: ResponseStatus },
}



/////////////////////////////////////////////////////////////////////////////////
// Query messages
/////////////////////////////////////////////////////////////////////////////////


/// Query messages to SNIP1155 contract. See [QueryAnswer](crate::msg::QueryAnswer) 
/// for the response messages for each variant, which has more detail.
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    /// returns public information of the SNIP1155 contract
    ContractInfo {  },
    Balance {
        owner: Addr,
        viewer: Addr,
        key: String,
        token_id: String,
    },
    AllBalances {
        owner: Addr,
        key: String,
        tx_history_page: Option<u32>,
        tx_history_page_size: Option<u32>,
    },
    TransactionHistory {
        address: Addr,
        key: String,
        page: Option<u32>,
        page_size: u32,
    },
    Permission {
        owner: Addr,
        allowed_address: Addr,
        key: String,
        token_id: String,
    },
    /// displays all permissions that a given address has granted
    AllPermissions {
        /// address that has granted permissions to others
        address: Addr,
        key: String,
        page: Option<u32>,
        page_size: u32,
    },
    TokenIdPublicInfo { token_id: String },
    TokenIdPrivateInfo { 
        address: Addr,
        key: String,
        token_id: String,
    },
    RegisteredCodeHash {
        contract: Addr
    },
    WithPermit {
        permit: Permit,
        query: QueryWithPermit,
    }
}

impl QueryMsg {
    pub fn get_validation_params(&self) -> StdResult<(Vec<&Addr>, String)> {
        match self {
            Self::Balance { owner, viewer, key, .. } => Ok((vec![owner, viewer], key.clone())),
            Self::AllBalances { owner, key, .. } => Ok((vec![owner], key.clone())),
            Self::TransactionHistory { address, key, .. } => Ok((vec![address], key.clone())),
            Self::Permission {
                owner,
                allowed_address,
                key,
                ..
            } => Ok((vec![owner, allowed_address], key.clone())),
            Self::AllPermissions { address, key, .. } => Ok((vec![address], key.clone())),
            Self::TokenIdPrivateInfo { address, key, .. } => Ok((vec![address], key.clone())),
            Self::ContractInfo {  } |
            Self::TokenIdPublicInfo { .. } |
            Self::RegisteredCodeHash { .. } |
            Self::WithPermit { .. } => unreachable!("This query type does not require viewing key authentication"),
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryWithPermit {
    Balance { 
        owner: Addr, 
        token_id: String 
    },
    AllBalances { 
        tx_history_page: Option<u32>,
        tx_history_page_size: Option<u32>,
    },
    TransactionHistory {
        page: Option<u32>,
        page_size: u32,
    },
    Permission {
        owner: Addr,
        allowed_address: Addr,
        token_id: String,
    },
    AllPermissions {
        page: Option<u32>,
        page_size: u32,
    },
    TokenIdPrivateInfo { 
        token_id: String,
    },
}

/// the query responses for each [QueryMsg](crate::msg::QueryMsg) variant
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum QueryAnswer {
    /// returns contract-level information:
    ContractInfo {
        // the address of the admin, or `None` for an admin-free contract
        admin: Option<Addr>,
        /// the list of curators in the contract
        curators: Vec<Addr>,
        /// the list of all token_ids that have been curated
        all_token_ids: Vec<String>,
    },
    /// returns balance of a specific token_id. Owners can give permission to other addresses to query their balance
    Balance {
        amount: Uint128,
    },
    /// returns all token_id balances owned by an address. Only owners can use this query
    AllBalances(Vec<OwnerBalance>),
    /// all permissions related to a particular address. Note that "curation" is not recorded as a transaction per se, but
    /// the tokens minted as part of the initial_balances set by the curator is recorded under `TxAction::Mint`  
    TransactionHistory {
        txs: Vec<Tx>,
        total: u64,
    },
    Permission(Option<Permission>),
    /// all permissions granted, viewable by the permission granter.
    /// Users or applications can match the permission_keys that corresponds to each permission as
    /// they have a similar order, ie: the index of `permission_keys` vector corresponds to the index 
    /// of the `permissions` vector.
    AllPermissions{
        permission_keys: Vec<PermissionKey>,
        permissions: Vec<Permission>,
        /// the total number of permission entries stored for a given granter, which may include "blank" 
        /// permissions, ie: where all permissions are set to `false` or `Uint128(0)`
        total: u64,
    },
    TokenIdPublicInfo {
        /// token_id_info.private_metadata will always = None
        token_id_info: StoredTokenInfo,
        /// if public_total_supply == false, total_supply = None
        total_supply: Option<Uint128>,
        /// if owner_is_public == false, total_supply = None
        owner: Option<Addr>
    },
    TokenIdPrivateInfo {
        token_id_info: StoredTokenInfo,
        /// if public_total_supply == false, total_supply = None
        total_supply: Option<Uint128>,
        /// if owner_is_public == false, total_supply = None
        owner: Option<Addr>
    },
    /// returns None if contract has not registered with SNIP1155 contract
    RegisteredCodeHash {
        code_hash: Option<String>,
    },
    /// returned when an viewing_key-specific errors occur during a user's attempt to 
    /// perform an authenticated query 
    ViewingKeyError {
        msg: String,
    },
}

/////////////////////////////////////////////////////////////////////////////////
// Structs, Enums and other functions
/////////////////////////////////////////////////////////////////////////////////

#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum ResponseStatus {
    Success,
    Failure,
}

#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct TransferAction {
    pub token_id: String,
    // equivalent to `owner` in SNIP20. Tokens are sent from this address. 
    pub from: Addr,
    pub recipient: Addr,
    pub amount: Uint128,
    pub memo: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct SendAction {
    pub token_id: String,
    // equivalent to `owner` in SNIP20. Tokens are sent from this address. 
    pub from: Addr,
    pub recipient: Addr,
    pub recipient_code_hash: Option<String>,
    pub amount: Uint128,
    pub msg: Option<Binary>,
    pub memo: Option<String>,
}

// Take a Vec<u8> and pad it up to a multiple of `block_size`, using spaces at the end.
pub fn space_pad(block_size: usize, message: &mut Vec<u8>) -> &mut Vec<u8> {
    let len = message.len();
    let surplus = len % block_size;
    if surplus == 0 {
        return message;
    }

    let missing = block_size - surplus;
    message.reserve(missing);
    message.extend(std::iter::repeat(b' ').take(missing));
    message
}