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
use std::ops::Deref;

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

use crate::{
    self as fadroma,
    storage::{Segment, iterable::IterableStorage},
    scrt::snip20::client::{TokenConfig, GivenAllowance, ReceivedAllowance},
    cosmwasm_std::{self, BlockInfo, CanonicalAddr, StdResult, Storage, Uint128, Deps},
    prelude::{
        ViewingKey, ViewingKeyHashed, SingleItem, ItemSpace, TypedKey,
        TypedKey2, FadromaSerialize, FadromaDeserialize, Canonize, Humanize,
        Address
    },
    impl_canonize_default
};
use super::{
    safe_math::safe_add,
    decoy::Decoys
};

crate::namespace!(pub ConstantsNs, b"N3QP0mNoPG");
pub const CONSTANTS: SingleItem<Constants, ConstantsNs> = SingleItem::new();

crate::namespace!(pub PrngSeedNs, b"Lwr3sTJZmk");
pub const PRNG_SEED: SingleItem<[u8; 32], PrngSeedNs> = SingleItem::new();

crate::namespace!(pub TotalSupplyNs, b"bx98UUOWYa");
pub const TOTAL_SUPPLY: TotalSupplyStore = TotalSupplyStore(SingleItem::new());

crate::namespace!(pub MintersNs, b"wpitCjS7wB");
pub const MINTERS: MintersStore = MintersStore(SingleItem::new());

crate::namespace!(pub SupportedDenomsNs, b"OxL3tsqB9N");
pub const SUPPORTED_DENOMS: SingleItem<
    Vec<String>,
    SupportedDenomsNs
> = SingleItem::new();

#[doc(hidden)]
pub struct MintersStore(pub SingleItem<Vec<CanonicalAddr>, MintersNs>);

#[doc(hidden)]
pub struct TotalSupplyStore(pub SingleItem<Uint128, TotalSupplyNs>);

#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct Constants {
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
    pub token_settings: TokenSettings
}

#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Clone, Copy, JsonSchema, PartialEq, Default, Debug)]
pub struct TokenSettings(u8);

#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum TokenPermission {
    PublicTotalSupply = 1 << 0,
    Deposit = 1 << 1,
    Redeem = 1 << 2,
    Mint = 1 << 3,
    Burn = 1 << 4,
    ModifyDenoms = 1 << 5
}

crate::namespace!(BalancesNs, b"DyCKbmlEL8");
crate::namespace!(AllowancesNs, b"eXDXajOxRG");
crate::namespace!(AllowancesIndicesNs, b"WHzOdOcMvW");
crate::namespace!(AllowedNs, b"YjRo4tM6pC");
crate::namespace!(ViewingKeyNs, b"MLRCoHCV8x");
crate::namespace!(ReceierHashNs, b"V1SJqXtGju");

#[derive(PartialEq, Debug)]
pub struct Account {
    addr: CanonicalAddr
}

#[derive(Serialize, Deserialize, FadromaSerialize, FadromaDeserialize, Clone, Copy, PartialEq, Default, JsonSchema, Debug)]
pub struct Allowance {
    pub amount: Uint128,
    pub expiration: Option<u64>
}

#[derive(FadromaSerialize, FadromaDeserialize, JsonSchema, Canonize, Debug)]
struct AllowanceEntry<A: Address> {
    spender: A,
    allowance: Allowance
}

#[derive(FadromaSerialize, FadromaDeserialize, JsonSchema, Debug)]
struct AllowedEntry {
    lender: CanonicalAddr,
    index: u64
}

impl_canonize_default!(Allowance);

impl Allowance {
    pub fn is_expired_at(&self, block: &BlockInfo) -> bool {
        match self.expiration {
            Some(time) => block.time.seconds() >= time,
            None => false
        }
    }
}

impl TotalSupplyStore {
    /// Saturates at [`Uint128::MAX`] and thus the return value is the actual amount added.
    #[inline]
    pub fn increase(&self, storage: &mut dyn Storage, amount: Uint128) -> StdResult<Uint128> {
        let mut total_supply = self.load_or_default(storage)?;
        let amount_added = safe_add(&mut total_supply, amount);

        self.save(storage, &total_supply)?;

        Ok(amount_added)
    }

    #[inline]
    pub fn decrease(&self, storage: &mut dyn Storage, amount: Uint128) -> StdResult<()> {
        let total_supply = self.load_or_default(storage)?;
        let new_total = total_supply.checked_sub(amount)?;

        self.save(storage, &new_total)
    }
}

impl MintersStore {
    #[inline]
    pub fn add(
        &self,
        storage: &mut dyn Storage,
        new_minters: Vec<CanonicalAddr>,
    ) -> StdResult<()> {
        let mut minters = self.load_or_default(storage)?;
        minters.extend(new_minters);

        self.save(storage, &minters)
    }

    #[inline]
    pub fn remove_minters(
        &self,
        storage: &mut dyn Storage,
        to_remove: Vec<CanonicalAddr>,
    ) -> StdResult<()> {
        let mut minters = self.load_or_default(storage)?;

        for minter in to_remove {
            minters.retain(|x| *x != minter);
        }

        self.save(storage, &minters)
    }
}

impl Deref for MintersStore {
    type Target = SingleItem<Vec<CanonicalAddr>, MintersNs>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Deref for TotalSupplyStore {
    type Target = SingleItem<Uint128, TotalSupplyNs>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[doc(hidden)]
impl Segment for Account {
    fn size(&self) -> usize {
        self.addr.len()
    }

    fn write_segment(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(&self.addr);
    }
}

impl Account {
    const BALANCE: ItemSpace<
        Uint128,
        BalancesNs,
        TypedKey<'_, Self>
    > = ItemSpace::new();

    const VIEWING_KEY: ItemSpace<
        ViewingKeyHashed,
        ViewingKeyNs,
        TypedKey<'_, Self>
    > = ItemSpace::new();

    const ALLOWANCES_INDICES: ItemSpace<
        u64,
        AllowancesIndicesNs,
        TypedKey2<'_, Self, Self>
    > = ItemSpace::new();

    const RECEIVER: ItemSpace<
        String,
        ReceierHashNs,
        TypedKey<'_, Self>
    > = ItemSpace::new();

    #[inline]
    pub fn of(addr: CanonicalAddr) -> Self {
        Self { addr }
    }

    #[inline]
    pub fn addr(&self) -> &CanonicalAddr {
        &self.addr
    }

    #[inline]
    pub fn balance(&self, storage: &dyn Storage) -> StdResult<Uint128> {
        Self::BALANCE.load_or_default(storage, self)
    }

    #[inline]
    pub fn add_balance(
        &self,
        storage: &mut dyn Storage,
        amount: Uint128,
        decoys: Option<&Decoys>
    ) -> StdResult<()> {
        match decoys {
            Some(decoys) => {
                let mut updated = false;

                for (i, decoy) in decoys.shuffle_in(self).enumerate() {
                    // Always load and save account balance to obfuscate the real account.
                    let mut balance = decoy.balance(storage)?;

                    if !updated && decoys.acc_index() == i {
                        updated = true;
                        let _ = safe_add(&mut balance, amount);
                    }

                    Self::BALANCE.save(storage, decoy, &balance)?;
                }

                Ok(())
            }
            None => {
                let mut balance = self.balance(storage)?;
                let _ = safe_add(&mut balance, amount);
        
                Self::BALANCE.save(storage, self, &balance)
            }
        }
    }

    #[inline]
    pub fn subtract_balance(
        &self,
        storage: &mut dyn Storage,
        amount: Uint128,
        decoys: Option<&Decoys>
    ) -> StdResult<()> {
        match decoys {
            Some(decoys) => {
                let mut updated = false;

                for (i, decoy) in decoys.shuffle_in(self).enumerate() {
                    // Always load and save account balance to obfuscate the real account.
                    let balance = decoy.balance(storage)?;

                    let balance = if !updated && decoys.acc_index() == i {
                        updated = true;

                        balance.checked_sub(amount)?
                    } else {
                        balance
                    };

                    Self::BALANCE.save(storage, decoy, &balance)?;
                }

                Ok(())
            }
            None => {
                let balance = self.balance(storage)?;
                let new_balance = balance.checked_sub(amount)?;
        
                Self::BALANCE.save(storage, self, &new_balance)
            }
        }
    }

    pub fn update_allowance<F>(
        &self,
        storage: &mut dyn Storage,
        spender: &Self,
        func: F
    ) -> StdResult<Allowance>
    where
        F: FnOnce(Allowance) -> StdResult<Allowance>
    {
        let mut allowances = self.allowances_storage();

        let key = (self, spender);
        let result = match Self::ALLOWANCES_INDICES.load(storage, key)? {
            Some(index) =>
                allowances.update(
                    storage,
                    index,
                    |mut entry| {
                        entry.allowance = func(entry.allowance)?;

                        Ok(entry)
                    }
                )?
                .unwrap()
                .allowance,
            None => {
                let allowance = func(Allowance::default())?;
                let index = allowances.push(storage, &AllowanceEntry {
                    spender: spender.addr.clone(),
                    allowance
                })?;

                Self::ALLOWANCES_INDICES.save(storage, key, &index)?;

                let mut allowed = spender.allowed_storage();
                allowed.push(storage, &AllowedEntry {
                    lender: self.addr.clone(),
                    index
                })?;

                allowance
            }
        };

        Ok(result)
    }

    #[inline]
    pub fn allowance(
        &self,
        storage: &dyn Storage,
        spender: &Account
    ) -> StdResult<Allowance> {
        match Self::ALLOWANCES_INDICES.load(storage, (self, spender))? {
            Some(index) => {
                let allowances = self.allowances_storage();

                Ok(allowances.get(storage, index)?.unwrap().allowance)
            },
            None => Ok(Allowance::default())
        }
    }

    #[inline]
    pub fn viewing_key(&self, storage: &dyn Storage) -> StdResult<Option<ViewingKeyHashed>> {
        Self::VIEWING_KEY.load(storage, self)
    }

    #[inline]
    pub fn set_viewing_key(&self, storage: &mut dyn Storage, key: &ViewingKey) -> StdResult<()> {
        Self::VIEWING_KEY.save(storage, self, &key.to_hashed())
    }

    #[inline]
    pub fn receiver_hash(&self, storage: &dyn Storage) -> StdResult<Option<String>> {
        Self::RECEIVER.load(storage, self)
    }

    #[inline]
    pub fn set_receiver_hash(&self, storage: &mut dyn Storage, code_hash: String) -> StdResult<()> {
        Self::RECEIVER.save(storage, self, &code_hash)
    }

    pub fn allowances(
        &self,
        deps: Deps,
        page: u32,
        page_size: u32
    ) -> StdResult<(Vec<GivenAllowance>, u64)> {
        let iter = self.allowances_storage().iter(deps.storage)?;
        let total_len = iter.len();

        let iter = iter
            .into_iter()
            .skip((page * page_size) as _)
            .take(page_size as _);

        let mut result = Vec::with_capacity(iter.len());

        for item in iter {
            let item = item?.humanize(deps.api)?;
            result.push(GivenAllowance {
                spender: item.spender,
                allowance: item.allowance.amount,
                expiration: item.allowance.expiration
            });
        }

        Ok((result, total_len))
    }

    pub fn received_allowances(
        &self,
        deps: Deps,
        page: u32,
        page_size: u32
    ) -> StdResult<(Vec<ReceivedAllowance>, u64)> {
        let iter = self.allowed_storage().iter(deps.storage)?;
        let total_len = iter.len();

        let iter = iter
            .into_iter()
            .skip((page * page_size) as _)
            .take(page_size as _);

        let mut result = Vec::with_capacity(iter.len());

        for item in iter {
            let item = item?;
            let account = Self { addr: item.lender };
            
            let allowances = account.allowances_storage();
            let allowance = allowances
                .get(deps.storage, item.index)?
                .unwrap()
                .allowance;

            result.push(ReceivedAllowance {
                owner: account.addr.humanize(deps.api)?,
                allowance: allowance.amount,
                expiration: allowance.expiration
            });
        }

        Ok((result, total_len))
    }

    #[inline]
    fn allowances_storage(&self) -> IterableStorage<
        AllowanceEntry<CanonicalAddr>,
        TypedKey2<AllowancesNs, Self>
    > {
        IterableStorage::new(TypedKey2::from((&AllowancesNs, self)))
    }

    #[inline]
    fn allowed_storage(&self) -> IterableStorage<
        AllowedEntry,
        TypedKey2<AllowedNs, Self>
    > {
        IterableStorage::new(TypedKey2::from((&AllowedNs, self)))
    }
}

impl From<CanonicalAddr> for Account {
    fn from(addr: CanonicalAddr) -> Self {
        Self { addr }
    }
}

impl From<Account> for CanonicalAddr {
    fn from(account: Account) -> Self {
        account.addr
    }
}

impl TokenSettings {
    #[inline(always)]
    pub fn is_set(&self, setting: TokenPermission) -> bool {
        let setting = setting as u8;

        self.0 & setting == setting
    }

    #[inline(always)]
    fn set(&mut self, setting: TokenPermission) {
        self.0 |= setting as u8;
    }
}

impl From<TokenConfig> for TokenSettings {
    fn from(config: TokenConfig) -> Self {
        let mut s = TokenSettings::default();

        if config.public_total_supply {
            s.set(TokenPermission::PublicTotalSupply);
        }

        if config.enable_deposit {
            s.set(TokenPermission::Deposit);
        }

        if config.enable_redeem {
            s.set(TokenPermission::Redeem);
        }

        if config.enable_mint {
            s.set(TokenPermission::Mint);
        }

        if config.enable_burn {
            s.set(TokenPermission::Burn);
        }

        if config.enable_modify_denoms {
            s.set(TokenPermission::ModifyDenoms);
        }

        s
    }
}

#[cfg(test)]
mod tests {
    use super::{TokenSettings, TokenPermission};

    #[test]
    fn token_settings() {
        fn test(s: TokenSettings, e: [bool; 6]) {
            assert_eq!(s.is_set(TokenPermission::PublicTotalSupply), e[0]);
            assert_eq!(s.is_set(TokenPermission::Deposit), e[1]);
            assert_eq!(s.is_set(TokenPermission::Redeem), e[2]);
            assert_eq!(s.is_set(TokenPermission::Mint), e[3]);
            assert_eq!(s.is_set(TokenPermission::Burn), e[4]);
            assert_eq!(s.is_set(TokenPermission::ModifyDenoms), e[5]);
        }

        let mut s = TokenSettings::default();
        test(s, [false, false, false, false, false, false]);

        s.set(TokenPermission::PublicTotalSupply);
        s.set(TokenPermission::Redeem);

        test(s, [true, false, true, false, false, false]);

        s.set(TokenPermission::Deposit);
        test(s, [true, true, true, false, false, false]);

        s.set(TokenPermission::Mint);
        test(s, [true, true, true, true, false, false]);

        s.set(TokenPermission::Burn);
        test(s, [true, true, true, true, true, false]);

        let mut s = TokenSettings::default();

        s.set(TokenPermission::Deposit);
        s.set(TokenPermission::Mint);
        s.set(TokenPermission::ModifyDenoms);

        test(s, [false, true, false, true, false, true]);
    }
}