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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
use std::mem;

use crate::{
    self as fadroma,
    schemars::JsonSchema,
    dsl::*,
    scrt::{
        vk::{auth::VkAuth, ViewingKey},
        permit::Permit,
        ResponseExt
    },
    admin::Admin,
    cosmwasm_std::{
        self, StdResult, Api, Response, Uint128,
        Binary, Addr, CanonicalAddr, Coin, to_binary
    },
    bin_serde::{FadromaSerialize, FadromaDeserialize},
    core::{Humanize, Canonize, Callback}
};
use serde::{Deserialize, Serialize};

/// Interface trait that defines all methods of the SNIP-20 standard.
/// See [`fadroma::scrt::snip20::contract`] for the default implementation.
#[interface]
pub trait Snip20: VkAuth + Admin {
    type Error: std::fmt::Display;

    #[init]
    fn new(
        name: String,
        admin: Option<String>,
        symbol: String,
        decimals: u8,
        initial_balances: Option<Vec<InitialBalance>>,
        prng_seed: Binary,
        config: Option<TokenConfig>,
        supported_denoms: Option<Vec<String>>,
        callback: Option<Callback<String>>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn deposit(
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn redeem(
        amount: Uint128,
        denom: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn transfer(
        recipient: String,
        amount: Uint128,
        memo: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn send(
        recipient: String,
        recipient_code_hash: Option<String>,
        amount: Uint128,
        memo: Option<String>,
        msg: Option<Binary>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn burn(
        amount: Uint128,
        memo: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn register_receive(
        code_hash: String,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn increase_allowance(
        spender: String,
        amount: Uint128,
        expiration: Option<u64>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn decrease_allowance(
        spender: String,
        amount: Uint128,
        expiration: Option<u64>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn transfer_from(
        owner: String,
        recipient: String,
        amount: Uint128,
        memo: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn send_from(
        owner: String,
        recipient: String,
        recipient_code_hash: Option<String>,
        amount: Uint128,
        memo: Option<String>,
        msg: Option<Binary>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn burn_from(
        owner: String,
        amount: Uint128,
        memo: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn mint(
        recipient: String,
        amount: Uint128,
        memo: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn add_minters(
        minters: Vec<String>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn remove_minters(
        minters: Vec<String>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn set_minters(
        minters: Vec<String>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn add_supported_denoms(
        denoms: Vec<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn remove_supported_denoms(
        denoms: Vec<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_transfer(
        actions: Vec<TransferAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_send(
        actions: Vec<SendAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_transfer_from(
        actions: Vec<TransferFromAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_send_from(
        actions: Vec<SendFromAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_burn_from(
        actions: Vec<BurnFromAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn batch_mint(
        actions: Vec<MintAction>,
        entropy: Option<Binary>,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[execute]
    fn revoke_permit(
        permit_name: String,
        padding: Option<String>
    ) -> Result<Response, <Self as Snip20>::Error>;

    #[query]
    fn exchange_rate() -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn token_info() -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn token_config() -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn minters() -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn allowance(
        owner: String,
        spender: String,
        key: String
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn balance(
        address: String,
        key: String
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn transfer_history(
        address: String,
        key: String,
        page: Option<u32>,
        page_size: u32,
        should_filter_decoys: bool
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn transaction_history(
        address: String,
        key: String,
        page: Option<u32>,
        page_size: u32,
        should_filter_decoys: bool
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn allowances_given(
        owner: String,
        key: String,
        page: Option<u32>,
        page_size: u32
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn allowances_received(
        spender: String,
        key: String,
        page: Option<u32>,
        page_size: u32
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;

    #[query]
    fn with_permit(
        permit: Permit<QueryPermission>,
        query: QueryWithPermit
    ) -> Result<QueryAnswer, <Self as Snip20>::Error>;
}

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

/// This type represents optional configuration values which can be overridden.
/// All values are optional and have defaults which are more private by default,
/// but can be overridden if necessary.
#[derive(Serialize, Deserialize, JsonSchema, Clone, Default, Debug)]
#[serde(rename_all = "snake_case")]
pub struct TokenConfig {
    /// Indicates whether the total supply is public or should be kept secret.
    /// default: False
    pub public_total_supply: bool,
    /// Indicates whether deposit functionality should be enabled
    /// default: False
    pub enable_deposit: bool,
    /// Indicates whether redeem functionality should be enabled
    /// default: False
    pub enable_redeem: bool,
    /// Indicates whether mint functionality should be enabled
    /// default: False
    pub enable_mint: bool,
    /// Indicates whether burn functionality should be enabled
    /// default: False
    pub enable_burn: bool,
    /// Indicates whether it's possible to change the allowed
    /// native denoms that can be exchanged for this token.
    /// default: False
    pub enable_modify_denoms: bool
}

#[derive(Serialize, Deserialize, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteAnswer {
    // Native
    Deposit {
        status: ResponseStatus,
    },
    Redeem {
        status: ResponseStatus,
    },
    AddSupportedDenoms {
        status: ResponseStatus,
    },
    RemoveSupportedDenoms {
        status: ResponseStatus,
    },

    // Base
    Transfer {
        status: ResponseStatus,
    },
    Send {
        status: ResponseStatus,
    },
    BatchTransfer {
        status: ResponseStatus,
    },
    BatchSend {
        status: ResponseStatus,
    },
    Burn {
        status: ResponseStatus,
    },
    RegisterReceive {
        status: ResponseStatus,
    },
    CreateViewingKey {
        key: ViewingKey,
    },
    SetViewingKey {
        status: ResponseStatus,
    },

    // Allowance
    IncreaseAllowance {
        spender: Addr,
        owner: Addr,
        allowance: Uint128,
    },
    DecreaseAllowance {
        spender: Addr,
        owner: Addr,
        allowance: Uint128,
    },
    TransferFrom {
        status: ResponseStatus,
    },
    SendFrom {
        status: ResponseStatus,
    },
    BatchTransferFrom {
        status: ResponseStatus,
    },
    BatchSendFrom {
        status: ResponseStatus,
    },
    BurnFrom {
        status: ResponseStatus,
    },
    BatchBurnFrom {
        status: ResponseStatus,
    },

    // Mint
    Mint {
        status: ResponseStatus,
    },
    BatchMint {
        status: ResponseStatus,
    },
    AddMinters {
        status: ResponseStatus,
    },
    RemoveMinters {
        status: ResponseStatus,
    },
    SetMinters {
        status: ResponseStatus,
    },

    // Other
    ChangeAdmin {
        status: ResponseStatus,
    },
    SetStatus {
        status: ResponseStatus,
    },

    // Permit
    RevokePemit {
        status: ResponseStatus
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryWithPermit {
    Allowance {
        owner: String,
        spender: String
    },
    Balance {},
    TransferHistory {
        page: Option<u32>,
        page_size: u32,
        should_filter_decoys: bool
    },
    TransactionHistory {
        page: Option<u32>,
        page_size: u32,
        should_filter_decoys: bool
    },
    AllowancesGiven { 
        owner: String, 
        page: Option<u32>, 
        page_size: u32 
    },
    AllowancesReceived { 
        spender: String, 
        page: Option<u32>, 
        page_size: u32 
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryPermission {
    /// Allowance for SNIP-20 - Permission to query allowance of the owner & spender
    Allowance,
    /// Balance for SNIP-20 - Permission to query balance
    Balance,
    /// History for SNIP-20 - Permission to query transfer_history & transaction_hisotry
    History,
    /// Owner permission indicates that the bearer of this permit should be granted all
    /// the access of the creator/signer of the permit.  SNIP-721 uses this to grant
    /// viewing access to all data that the permit creator owns and is whitelisted for.
    /// For SNIP-721 use, a permit with Owner permission should NEVER be given to
    /// anyone else.  If someone wants to share private data, they should whitelist
    /// the address they want to share with via a SetWhitelistedApproval tx, and that
    /// address will view the data by creating their own permit with Owner permission
    Owner
}

#[derive(Serialize, Deserialize, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum QueryAnswer {
    TokenInfo(TokenInfo),
    TokenConfig {
        public_total_supply: bool,
        deposit_enabled: bool,
        redeem_enabled: bool,
        mint_enabled: bool,
        burn_enabled: bool,
        supported_denoms: Vec<String>
    },
    ExchangeRate {
        rate: Uint128,
        denom: String
    },
    Allowance {
        spender: Addr,
        owner: Addr,
        allowance: Uint128,
        expiration: Option<u64>
    },
    AllowancesGiven {
        owner: Addr,
        allowances: Vec<GivenAllowance>,
        count: u64
    },
    AllowancesReceived {
        spender: Addr,
        allowances: Vec<ReceivedAllowance>,
        count: u64
    },
    Balance {
        amount: Uint128
    },
    TransferHistory {
        txs: Vec<Tx<Addr>>,
        total: Option<u64>
    },
    TransactionHistory {
        txs: Vec<RichTx>,
        total: Option<u64>
    },
    ViewingKeyError {
        msg: String
    },
    Minters {
        minters: Vec<Addr>
    }
}

#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TokenInfo {
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_supply: Option<Uint128>
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Clone, Debug)]
pub struct GivenAllowance {
    pub spender: Addr,
    pub allowance: Uint128,
    pub expiration: Option<u64>
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Clone, Debug)]
pub struct ReceivedAllowance {
    pub owner: Addr,
    pub allowance: Uint128,
    pub expiration: Option<u64>
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct CreateViewingKeyResponse {
    pub key: String
}

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

// Transfer history
#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Canonize, JsonSchema, PartialEq, Clone, Debug)]
pub struct Tx<T> {
    pub id: u64,
    pub from: T,
    pub sender: T,
    pub receiver: T,
    pub coins: Coin,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memo: Option<String>,
    // The block time and block height are optional so that the JSON schema
    // reflects that some SNIP-20 contracts may not include this info.
    pub block_time: Option<u64>,
    pub block_height: Option<u64>
}

#[derive(Serialize, Deserialize, Canonize, JsonSchema, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct RichTx {
    pub id: u64,
    pub action: TxAction,
    pub coins: Coin,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memo: Option<String>,
    pub block_time: u64,
    pub block_height: u64
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TxAction {
    Transfer {
        from: Addr,
        sender: Addr,
        recipient: Addr
    },
    Mint {
        minter: Addr,
        recipient: Addr
    },
    Burn {
        burner: Addr,
        owner: Addr
    },
    Deposit {},
    Redeem {},
    Decoy {
        address: Addr
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
#[repr(u8)]
pub enum TxCode {
    Transfer = 0,
    Mint = 1,
    Burn = 2,
    Deposit = 3,
    Redeem = 4,
    Decoy = 255
}

#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct TxActionCanon {
    tx_type: u8,
    address1: Option<CanonicalAddr>,
    address2: Option<CanonicalAddr>,
    address3: Option<CanonicalAddr>
}

// Batch

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct TransferAction {
    pub recipient: String,
    pub amount: Uint128,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct SendAction {
    pub recipient: String,
    pub recipient_code_hash: Option<String>,
    pub amount: Uint128,
    pub msg: Option<Binary>,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct TransferFromAction {
    pub owner: String,
    pub recipient: String,
    pub amount: Uint128,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct SendFromAction {
    pub owner: String,
    pub recipient_code_hash: Option<String>,
    pub recipient: String,
    pub amount: Uint128,
    pub msg: Option<Binary>,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct MintAction {
    pub recipient: String,
    pub amount: Uint128,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct BurnFromAction {
    pub owner: String,
    pub amount: Uint128,
    pub memo: Option<String>,
    pub decoys: Option<Vec<String>>
}

impl TxActionCanon {
    #[inline]
    pub fn ty(&self) -> TxCode {
        // Safety: TxActionCanon can only be constructed using the action
        // constructors that use the reverse cast which is always safe.
        unsafe { mem::transmute(self.tx_type) }
    }

    #[inline]
    pub fn transfer(
        from: CanonicalAddr,
        sender: CanonicalAddr,
        recipient: CanonicalAddr
    ) -> Self {
        Self {
            tx_type: TxCode::Transfer as u8,
            address1: Some(from),
            address2: Some(sender),
            address3: Some(recipient)
        }
    }

    #[inline]
    pub fn mint(minter: CanonicalAddr, recipient: CanonicalAddr) -> Self {
        Self {
            tx_type: TxCode::Mint as u8,
            address1: Some(minter),
            address2: Some(recipient),
            address3: None
        }
    }

    #[inline]
    pub fn burn(owner: CanonicalAddr, burner: CanonicalAddr) -> Self {
        Self {
            tx_type: TxCode::Burn as u8,
            address1: Some(burner),
            address2: Some(owner),
            address3: None
        }
    }

    #[inline]
    pub fn deposit() -> Self {
        Self {
            tx_type: TxCode::Deposit as u8,
            address1: None,
            address2: None,
            address3: None
        }
    }

    #[inline]
    pub fn redeem() -> Self {
        Self {
            tx_type: TxCode::Redeem as u8,
            address1: None,
            address2: None,
            address3: None
        }
    }

    #[inline]
    pub fn decoy(recipient: CanonicalAddr) -> Self {
        Self {
            tx_type: TxCode::Decoy as u8,
            address1: Some(recipient),
            address2: None,
            address3: None
        }
    }
}

impl Canonize for TxAction {
    type Output = TxActionCanon;

    fn canonize(self, api: &dyn Api) -> StdResult<Self::Output> {
        let action = match self {
            Self::Transfer { from, sender, recipient } =>
                TxActionCanon::transfer(
                    from.canonize(api)?,
                    sender.canonize(api)?,
                    recipient.canonize(api)?
                ),
            Self::Mint { minter, recipient } =>
                TxActionCanon::mint(
                    minter.canonize(api)?,
                    recipient.canonize(api)?
                ),
            Self::Burn { burner, owner } =>
                TxActionCanon::burn(
                    owner.canonize(api)?,
                    burner.canonize(api)?
                ),
            Self::Deposit { } => TxActionCanon::deposit(),
            Self::Redeem { } => TxActionCanon::redeem(),
            Self::Decoy { address } =>
                TxActionCanon::decoy(address.canonize(api)?)
        };

        Ok(action)
    }
}

impl Humanize for TxActionCanon {
    type Output = TxAction;

    fn humanize(self, api: &dyn Api) -> StdResult<Self::Output> {
        let result = match self.ty() {
            TxCode::Transfer => {
                let from = self.address1.unwrap();
                let sender = self.address2.unwrap();
                let recipient = self.address3.unwrap();

                TxAction::Transfer {
                    from: from.humanize(api)?,
                    sender: sender.humanize(api)?,
                    recipient: recipient.humanize(api)?
                }
            }
            TxCode::Mint => {
                let minter = self.address1.unwrap();
                let recipient = self.address2.unwrap();

                TxAction::Mint {
                    minter: minter.humanize(api)?,
                    recipient: recipient.humanize(api)?
                }
            }
            TxCode::Burn => {
                let burner = self.address1.unwrap();
                let owner = self.address2.unwrap();

                TxAction::Burn {
                    burner: burner.humanize(api)?,
                    owner: owner.humanize(api)?
                }
            }
            TxCode::Deposit => TxAction::Deposit {},
            TxCode::Redeem => TxAction::Redeem {},
            TxCode::Decoy => {
                let address = self.address1.unwrap();

                TxAction::Decoy {
                    address: address.humanize(api)?
                }
            }
        };

        Ok(result)
    }
}

impl TokenConfig {
    #[inline]
    pub fn public_total_supply(mut self) -> Self {
        self.public_total_supply = true;

        self
    }

    #[inline]
    pub fn enable_deposit(mut self) -> Self {
        self.enable_deposit = true;

        self
    }

    #[inline]
    pub fn enable_redeem(mut self) -> Self {
        self.enable_redeem = true;

        self
    }

    #[inline]
    pub fn enable_mint(mut self) -> Self {
        self.enable_mint = true;

        self
    }

    #[inline]
    pub fn enable_burn(mut self) -> Self {
        self.enable_burn = true;

        self
    }

    #[inline]
    pub fn enable_modify_denoms(mut self) -> Self {
        self.enable_modify_denoms = true;

        self
    }
}

impl ExecuteAnswer {
    #[inline]
    pub fn with_resp(&self, response: Response) -> StdResult<Response> {
        Ok(response.set_data(to_binary(self)?).pad())
    }
}