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
//! # Escrow Pallet
//!
//! - [`Config`]
//! - [`Call`]
//!
//! ## Overview
//!
//! The escrow pallet allows accounts to lock the native currency and receive vote-escrowed tokens.
//! This voting power linearly decreases per block and tends toward zero as the height approaches
//! the max lockup period.
//!
//! This implementation is based in part on Curve's implementation, but explicitly follows
//! the specification at <https://spec.interlay.io/spec/escrow.html>.

#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod default_weights;
pub use default_weights::WeightInfo;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
    ensure,
    traits::{
        BalanceStatus, Currency, ExistenceRequirement, Get, Imbalance, LockIdentifier, LockableCurrency,
        ReservableCurrency, SignedImbalance, WithdrawReasons,
    },
    transactional,
};
use frame_system::pallet_prelude::BlockNumberFor;
use reward::RewardsApi;
use scale_info::TypeInfo;
use sp_runtime::{
    traits::{AtLeast32BitUnsigned, CheckedSub, Convert, Saturating, Zero},
    DispatchError, DispatchResult,
};

const LOCK_ID: LockIdentifier = *b"escrowed";

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type PositiveImbalanceOf<T> =
    <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::PositiveImbalance;
type NegativeImbalanceOf<T> =
    <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;

#[derive(Default, Encode, Decode, Debug, Clone, TypeInfo, MaxEncodedLen)]
pub struct Point<Balance, BlockNumber> {
    bias: Balance,
    slope: Balance,
    ts: BlockNumber,
}

impl<Balance: AtLeast32BitUnsigned + Default + Copy, BlockNumber: AtLeast32BitUnsigned + Copy>
    Point<Balance, BlockNumber>
{
    fn new<BlockNumberToBalance: Convert<BlockNumber, Balance>>(
        amount: Balance,
        start_height: BlockNumber,
        end_height: BlockNumber,
        max_period: BlockNumber,
    ) -> Self {
        let max_period = BlockNumberToBalance::convert(max_period);
        let height_diff = BlockNumberToBalance::convert(end_height.saturating_sub(start_height));

        let slope = amount.checked_div(&max_period).unwrap_or_default();
        let bias = slope.saturating_mul(height_diff);

        Self {
            bias,
            slope,
            ts: start_height,
        }
    }

    // w ^
    // 1 +
    //   | *
    //   |   *
    //   |     *
    //   |       *
    // 0 +---------+--> t
    //
    // Calculates the balance at some point in the future,
    // linearly **decreasing** since the start height.
    fn balance_at<BlockNumberToBalance: Convert<BlockNumber, Balance>>(&self, height: BlockNumber) -> Balance {
        let height_diff = BlockNumberToBalance::convert(height.saturating_sub(self.ts));
        self.bias.saturating_sub(self.slope.saturating_mul(height_diff))
    }

    // w ^
    // 1 +
    //   |       *
    //   |     *
    //   |   *
    //   | *
    // 0 +-------+----> t
    //
    // Calculates the balance at some point in the future,
    // linearly **increasing** since the start height.
    fn reverse_balance_at<BlockNumberToBalance: Convert<BlockNumber, Balance>>(
        &self,
        end: BlockNumber,
        now: BlockNumber,
    ) -> Balance {
        // NOTE: we could store the end height in `Point`, but this code is only
        // temporary whilst we rollout governance voting via restricted accounts
        let height_diff = BlockNumberToBalance::convert(end.saturating_sub(now));
        self.bias.saturating_sub(self.slope.saturating_mul(height_diff))
    }
}

#[derive(Default, Encode, Decode, Clone, TypeInfo, MaxEncodedLen)]
pub struct LockedBalance<Balance, BlockNumber> {
    pub amount: Balance,
    end: BlockNumber,
}

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
    use frame_system::pallet_prelude::*;

    /// ## Configuration
    /// The pallet's configuration trait.
    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// Convert the block number into a balance.
        type BlockNumberToBalance: Convert<BlockNumberFor<Self>, BalanceOf<Self>>;

        /// The currency trait.
        type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>;

        /// All future times are rounded by this.
        #[pallet::constant]
        type Span: Get<BlockNumberFor<Self>>;

        /// The maximum time for locks.
        #[pallet::constant]
        type MaxPeriod: Get<BlockNumberFor<Self>>;

        /// Escrow reward pool.
        type EscrowRewards: reward::RewardsApi<(), Self::AccountId, BalanceOf<Self>>;

        /// Weight information for the extrinsics in this module.
        type WeightInfo: WeightInfo;
    }

    // The pallet's events
    #[pallet::event]
    #[pallet::generate_deposit(pub(crate) fn deposit_event)]
    pub enum Event<T: Config> {
        Deposit {
            who: T::AccountId,
            amount: BalanceOf<T>,
            unlock_height: BlockNumberFor<T>,
        },
        Withdraw {
            who: T::AccountId,
            amount: BalanceOf<T>,
        },
    }

    #[pallet::error]
    pub enum Error<T> {
        /// Input amount must be non-zero.
        InputAmountZero,
        /// Lock already exists.
        LockFound,
        /// Lock does not exist.
        LockNotFound,
        /// Unlock height is not in the future.
        UnlockHeightNotInTheFuture,
        /// Unlock height is greater than max period.
        UnlockHeightTooFarInTheFuture,
        /// Lock amount must be non-zero.
        LockAmountZero,
        /// Unlock height should be greater than lock.
        UnlockHeightMustIncrease,
        /// Previous lock has not expired.
        LockNotExpired,
        /// Previous lock has expired.
        LockHasExpired,
        /// Lock amount is too large.
        LockAmountTooLow,
        /// Insufficient account balance.
        InsufficientFunds,
        /// Not supported.
        NotSupported,
        /// Incorrect Percent
        IncorrectPercent,
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

    #[pallet::storage]
    #[pallet::getter(fn reserved_balance)]
    pub type Reserved<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn locked_balance)]
    pub type Locked<T: Config> =
        StorageMap<_, Blake2_128Concat, T::AccountId, LockedBalance<BalanceOf<T>, BlockNumberFor<T>>, ValueQuery>;

    #[pallet::storage]
    pub type Epoch<T: Config> = StorageValue<_, T::Nonce, ValueQuery>;

    #[pallet::storage]
    pub type PointHistory<T: Config> =
        StorageMap<_, Identity, T::Nonce, Point<BalanceOf<T>, BlockNumberFor<T>>, ValueQuery>;

    #[pallet::storage]
    pub type UserPointHistory<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        T::AccountId,
        Identity,
        T::Nonce,
        Point<BalanceOf<T>, BlockNumberFor<T>>,
        ValueQuery,
    >;

    #[pallet::storage]
    pub type UserPointEpoch<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Nonce, ValueQuery>;

    #[pallet::storage]
    pub type SlopeChanges<T: Config> = StorageMap<_, Blake2_128Concat, BlockNumberFor<T>, BalanceOf<T>, ValueQuery>;

    // Accounts that are limited in how much they can mint.
    #[pallet::storage]
    pub type Limits<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, (BlockNumberFor<T>, BlockNumberFor<T>)>;

    // Accounts that are prohibited from locking tokens for voting.
    #[pallet::storage]
    pub type Blocks<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, bool, ValueQuery>;

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    // The pallet's dispatchable functions.
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::create_lock())]
        #[transactional]
        pub fn create_lock(
            origin: OriginFor<T>,
            #[pallet::compact] amount: BalanceOf<T>,
            unlock_height: BlockNumberFor<T>,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;
            let now = Self::current_height();

            // lock time is rounded down to weeks
            let unlock_height = Self::round_height(unlock_height);

            // value MUST be non-zero
            ensure!(!amount.is_zero(), Error::<T>::InputAmountZero);

            // user MUST withdraw first
            ensure!(Self::locked_balance(&who).amount.is_zero(), Error::<T>::LockFound);

            // unlock MUST be in the future
            ensure!(unlock_height > now, Error::<T>::UnlockHeightNotInTheFuture);

            // height MUST NOT be greater than max
            let max_period = T::MaxPeriod::get();
            let end_height = now.saturating_add(max_period);
            ensure!(unlock_height <= end_height, Error::<T>::UnlockHeightTooFarInTheFuture);

            Self::deposit_for(&who, amount, unlock_height)
        }

        #[pallet::call_index(1)]
        #[pallet::weight(<T as Config>::WeightInfo::increase_amount())]
        #[transactional]
        pub fn increase_amount(origin: OriginFor<T>, #[pallet::compact] amount: BalanceOf<T>) -> DispatchResult {
            let who = ensure_signed(origin)?;
            let locked_balance = Self::locked_balance(&who);
            let now = Self::current_height();

            // value MUST be non-zero
            ensure!(!amount.is_zero(), Error::<T>::InputAmountZero);

            // lock MUST exist first
            ensure!(!locked_balance.amount.is_zero(), Error::<T>::LockNotFound);

            // lock MUST NOT be expired
            ensure!(locked_balance.end > now, Error::<T>::LockHasExpired);

            Self::deposit_for(&who, amount, Zero::zero()).into()
        }

        #[pallet::call_index(2)]
        #[pallet::weight(<T as Config>::WeightInfo::increase_unlock_height())]
        #[transactional]
        pub fn increase_unlock_height(origin: OriginFor<T>, unlock_height: BlockNumberFor<T>) -> DispatchResult {
            let who = ensure_signed(origin)?;
            let locked_balance = Self::locked_balance(&who);
            let now = Self::current_height();

            // lock time is rounded down to weeks
            let unlock_height = Self::round_height(unlock_height);

            // lock MUST NOT be expired
            ensure!(locked_balance.end > now, Error::<T>::LockHasExpired);

            // lock amount MUST be non-zero
            ensure!(!locked_balance.amount.is_zero(), Error::<T>::LockAmountZero);

            // lock duration MUST increase
            ensure!(unlock_height > locked_balance.end, Error::<T>::UnlockHeightMustIncrease);

            // height MUST NOT be greater than max
            let max_period = T::MaxPeriod::get();
            let end_height = now.saturating_add(max_period);
            ensure!(unlock_height <= end_height, Error::<T>::UnlockHeightTooFarInTheFuture);

            Self::deposit_for(&who, Zero::zero(), unlock_height)
        }

        #[pallet::call_index(3)]
        #[pallet::weight(<T as Config>::WeightInfo::withdraw())]
        #[transactional]
        pub fn withdraw(origin: OriginFor<T>) -> DispatchResult {
            let who = ensure_signed(origin)?;
            Self::remove_lock(&who)
        }

        #[pallet::call_index(4)]
        #[pallet::weight(<T as Config>::WeightInfo::set_account_limit())]
        #[transactional]
        pub fn set_account_limit(
            origin: OriginFor<T>,
            who: T::AccountId,
            start: BlockNumberFor<T>,
            end: BlockNumberFor<T>,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            <Limits<T>>::insert(&who, (start, end));
            Ok(().into())
        }

        #[pallet::call_index(5)]
        #[pallet::weight(<T as Config>::WeightInfo::withdraw())]
        #[transactional]
        pub fn set_account_block(origin: OriginFor<T>, who: T::AccountId) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            <Blocks<T>>::insert(&who, true);
            Ok(().into())
        }

        /// Update the stake amount for a user.
        ///
        /// # Arguments
        ///
        /// * `origin` - Sender of the transaction.
        /// * `target_user` - The account ID of the user whose stake amount needs to be updated.
        #[pallet::call_index(6)]
        #[pallet::weight(<T as Config>::WeightInfo::update_user_stake())]
        #[transactional]
        pub fn update_user_stake(origin: OriginFor<T>, target_user: T::AccountId) -> DispatchResult {
            ensure_signed(origin)?;
            // call `deposit_for` for re calculation of stake amount
            Self::deposit_for(&target_user, Zero::zero(), Zero::zero())
        }
    }
}

type DefaultPoint<T> = Point<BalanceOf<T>, BlockNumberFor<T>>;
type DefaultLockedBalance<T> = LockedBalance<BalanceOf<T>, BlockNumberFor<T>>;

// "Internal" functions, callable by code.
impl<T: Config> Pallet<T> {
    fn current_height() -> BlockNumberFor<T> {
        frame_system::Pallet::<T>::block_number()
    }

    fn round_height(height: BlockNumberFor<T>) -> BlockNumberFor<T> {
        let span = T::Span::get();
        (height / span) * span
    }

    // As in the Curve contract, we record global and per-user data, this
    // may necessitate multiple writes if the global points are outdated.
    // We do not interpret the zero-address as a global checkpoint.
    fn checkpoint(who: &T::AccountId, old_locked: DefaultLockedBalance<T>, new_locked: DefaultLockedBalance<T>) {
        let now = Self::current_height();
        let max_period = T::MaxPeriod::get();

        let u_old = if old_locked.end > now && old_locked.amount > Zero::zero() {
            Point::new::<T::BlockNumberToBalance>(old_locked.amount, now, old_locked.end, max_period)
        } else {
            Default::default()
        };

        let u_new = if new_locked.end > now && new_locked.amount > Zero::zero() {
            Point::new::<T::BlockNumberToBalance>(new_locked.amount, now, new_locked.end, max_period)
        } else {
            Default::default()
        };

        let mut old_dslope = <SlopeChanges<T>>::get(old_locked.end);
        let mut new_dslope = if !new_locked.end.is_zero() {
            if new_locked.end == old_locked.end {
                old_dslope
            } else {
                <SlopeChanges<T>>::get(new_locked.end)
            }
        } else {
            Zero::zero()
        };

        let mut epoch = <Epoch<T>>::get();
        let mut last_point = <PointHistory<T>>::get(epoch);
        let mut last_checkpoint = last_point.ts;

        let mut t_i = Self::round_height(last_checkpoint);
        while t_i < now {
            t_i.saturating_accrue(T::Span::get());
            let d_slope = if t_i > now {
                t_i = now;
                Zero::zero()
            } else {
                <SlopeChanges<T>>::get(t_i)
            };
            let height_diff = T::BlockNumberToBalance::convert(t_i.saturating_sub(last_checkpoint));
            last_point.bias.saturating_reduce(last_point.slope * height_diff);
            last_point.slope.saturating_accrue(d_slope);
            last_checkpoint = t_i;
            last_point.ts = t_i;
            epoch.saturating_inc();

            if t_i == now {
                break;
            }

            <PointHistory<T>>::insert(epoch, last_point.clone());
        }

        <Epoch<T>>::put(epoch);

        last_point
            .slope
            .saturating_accrue(u_new.slope.saturating_sub(u_old.slope));
        last_point.bias.saturating_accrue(u_new.bias.saturating_sub(u_old.bias));
        <PointHistory<T>>::insert(epoch, last_point);

        // schedule the slope change
        if old_locked.end > now {
            old_dslope.saturating_accrue(u_old.slope);
            if new_locked.end == old_locked.end {
                // new deposit
                old_dslope = old_dslope.saturating_sub(u_new.slope);
            }
            <SlopeChanges<T>>::insert(old_locked.end, old_dslope);
        }

        if new_locked.end > now && new_locked.end > old_locked.end {
            new_dslope = new_dslope.saturating_sub(u_new.slope);
            <SlopeChanges<T>>::insert(new_locked.end, new_dslope);
        }

        // finally update user history
        let user_epoch = <UserPointEpoch<T>>::mutate(who, |i| {
            i.saturating_inc();
            *i
        });
        <UserPointHistory<T>>::insert(who, user_epoch, u_new);
    }

    /// amount of kint/intr that use can lock, taking into consideration the Limits.
    fn get_free_balance(who: &T::AccountId) -> BalanceOf<T> {
        let free_balance = T::Currency::free_balance(who);
        // prevent blocked accounts from minting
        if <Blocks<T>>::get(who) {
            Zero::zero()
        }
        // limit total deposit of restricted accounts
        else if let Some((start, end)) = <Limits<T>>::get(who) {
            // TODO: remove these restrictions in the future when the token distribution is complete
            let current_height = Self::current_height();
            let point = Point::new::<T::BlockNumberToBalance>(free_balance, start, end, end.saturating_sub(start));
            point.reverse_balance_at::<T::BlockNumberToBalance>(end, current_height)
        } else {
            free_balance
        }
    }

    pub fn free_stakable(who: &T::AccountId) -> BalanceOf<T> {
        let total_stakable = Self::get_free_balance(who);
        let used = <Locked<T>>::get(who).amount;
        total_stakable.saturating_sub(used)
    }

    fn deposit_for(who: &T::AccountId, amount: BalanceOf<T>, unlock_height: BlockNumberFor<T>) -> DispatchResult {
        let old_locked = Self::locked_balance(who);
        let mut new_locked = old_locked.clone();
        new_locked.amount.saturating_accrue(amount);
        if unlock_height > Zero::zero() {
            new_locked.end = unlock_height;
        }

        // total amount can't be less than the max period to prevent rounding errors
        ensure!(
            new_locked.amount >= T::BlockNumberToBalance::convert(T::MaxPeriod::get()),
            Error::<T>::LockAmountTooLow,
        );

        ensure!(
            Self::get_free_balance(who) >= new_locked.amount,
            Error::<T>::InsufficientFunds,
        );
        T::Currency::set_lock(LOCK_ID, &who, new_locked.amount, WithdrawReasons::all());
        <Locked<T>>::insert(who, new_locked.clone());

        Self::checkpoint(who, old_locked, new_locked);

        // withdraw all stake and re-deposit escrow balance
        T::EscrowRewards::withdraw_all_stake(&(), who)?;
        T::EscrowRewards::deposit_stake(&(), who, Self::balance_at(who, None))?;

        Self::deposit_event(Event::<T>::Deposit {
            who: who.clone(),
            amount,
            unlock_height,
        });

        Ok(())
    }

    /// RPC helper
    pub fn round_height_and_deposit_for(
        who: &T::AccountId,
        amount: BalanceOf<T>,
        unlock_height: BlockNumberFor<T>,
    ) -> DispatchResult {
        Self::deposit_for(who, amount, Self::round_height(unlock_height))
    }

    fn remove_lock(who: &T::AccountId) -> DispatchResult {
        let old_locked = <Locked<T>>::take(who);
        let amount = old_locked.amount;
        let current_height = Self::current_height();

        // lock MUST have expired
        ensure!(current_height >= old_locked.end, Error::<T>::LockNotExpired);

        // withdraw all stake
        T::EscrowRewards::withdraw_all_stake(&(), who)?;

        Self::checkpoint(who, old_locked, Default::default());

        T::Currency::remove_lock(LOCK_ID, &who);
        let _ = <UserPointHistory<T>>::clear_prefix(who, u32::MAX, None);

        Self::deposit_event(Event::<T>::Withdraw {
            who: who.clone(),
            amount,
        });

        Ok(())
    }

    /// vKINT/vINTR balance at given height
    pub fn balance_at(who: &T::AccountId, height: Option<BlockNumberFor<T>>) -> BalanceOf<T> {
        let height = height.unwrap_or(Self::current_height());
        let last_point = <UserPointHistory<T>>::get(who, <UserPointEpoch<T>>::get(who));
        last_point.balance_at::<T::BlockNumberToBalance>(height)
    }

    pub fn supply_at(point: DefaultPoint<T>, height: BlockNumberFor<T>) -> BalanceOf<T> {
        let mut last_point = point;

        let mut t_i = Self::round_height(last_point.ts);
        while t_i < height {
            t_i.saturating_accrue(T::Span::get());

            let d_slope = if t_i > height {
                t_i = height;
                Zero::zero()
            } else {
                <SlopeChanges<T>>::get(t_i)
            };

            let height_diff = T::BlockNumberToBalance::convert(t_i.saturating_sub(last_point.ts));
            last_point.bias.saturating_reduce(last_point.slope * height_diff);

            if t_i == height {
                break;
            }

            last_point.slope.saturating_accrue(d_slope);
            last_point.ts = t_i;
        }

        last_point.bias
    }

    pub fn total_supply(height: Option<BlockNumberFor<T>>) -> BalanceOf<T> {
        let height = height.unwrap_or(Self::current_height());
        let last_point = <PointHistory<T>>::get(<Epoch<T>>::get());
        Self::supply_at(last_point, height)
    }
}

impl<T: Config> Currency<T::AccountId> for Pallet<T> {
    type Balance = BalanceOf<T>;
    type PositiveImbalance = PositiveImbalanceOf<T>;
    type NegativeImbalance = NegativeImbalanceOf<T>;

    fn total_balance(who: &T::AccountId) -> Self::Balance {
        Pallet::<T>::balance_at(who, None)
    }

    // NOT SUPPORTED
    fn can_slash(_who: &T::AccountId, _value: Self::Balance) -> bool {
        false
    }

    fn total_issuance() -> Self::Balance {
        Pallet::<T>::total_supply(None)
    }

    fn minimum_balance() -> Self::Balance {
        T::Currency::minimum_balance()
    }

    // NOT SUPPORTED
    fn burn(_amount: Self::Balance) -> Self::PositiveImbalance {
        Imbalance::zero()
    }

    // NOT SUPPORTED
    fn issue(_amount: Self::Balance) -> Self::NegativeImbalance {
        Imbalance::zero()
    }

    fn free_balance(who: &T::AccountId) -> Self::Balance {
        Pallet::<T>::balance_at(who, None).saturating_sub(Pallet::<T>::reserved_balance(who))
    }

    // NOT SUPPORTED
    fn ensure_can_withdraw(
        _who: &T::AccountId,
        _amount: Self::Balance,
        _reasons: WithdrawReasons,
        _new_balance: Self::Balance,
    ) -> DispatchResult {
        Err(Error::<T>::NotSupported.into())
    }

    // NOT SUPPORTED
    fn transfer(
        _source: &T::AccountId,
        _dest: &T::AccountId,
        _value: Self::Balance,
        _existence_requirement: ExistenceRequirement,
    ) -> DispatchResult {
        Err(Error::<T>::NotSupported.into())
    }

    // NOT SUPPORTED
    fn slash(_who: &T::AccountId, _value: Self::Balance) -> (Self::NegativeImbalance, Self::Balance) {
        (Imbalance::zero(), Zero::zero())
    }

    // NOT SUPPORTED
    fn deposit_into_existing(
        _who: &T::AccountId,
        _value: Self::Balance,
    ) -> sp_std::result::Result<Self::PositiveImbalance, DispatchError> {
        Err(Error::<T>::NotSupported.into())
    }

    // NOT SUPPORTED
    fn deposit_creating(_who: &T::AccountId, _value: Self::Balance) -> Self::PositiveImbalance {
        Imbalance::zero()
    }

    // NOT SUPPORTED
    fn withdraw(
        _who: &T::AccountId,
        _value: Self::Balance,
        _reasons: WithdrawReasons,
        _liveness: ExistenceRequirement,
    ) -> sp_std::result::Result<Self::NegativeImbalance, DispatchError> {
        Err(Error::<T>::NotSupported.into())
    }

    fn make_free_balance_be(
        who: &T::AccountId,
        balance: Self::Balance,
    ) -> SignedImbalance<Self::Balance, Self::PositiveImbalance> {
        let now = Self::current_height();
        let max_period = T::MaxPeriod::get();
        let end_height = now.saturating_add(max_period);
        <UserPointHistory<T>>::insert(
            who,
            <UserPointEpoch<T>>::get(who),
            Point::new::<T::BlockNumberToBalance>(balance, now, end_height, max_period),
        );
        SignedImbalance::zero()
    }
}

impl<T: Config> ReservableCurrency<T::AccountId> for Pallet<T> {
    fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool {
        Pallet::<T>::free_balance(who).checked_sub(&value).is_some()
    }

    // NOT SUPPORTED
    fn slash_reserved(_who: &T::AccountId, _value: Self::Balance) -> (Self::NegativeImbalance, Self::Balance) {
        (Imbalance::zero(), Zero::zero())
    }

    fn reserved_balance(who: &T::AccountId) -> Self::Balance {
        Pallet::<T>::reserved_balance(who)
    }

    fn reserve(who: &T::AccountId, value: Self::Balance) -> DispatchResult {
        if !Pallet::<T>::can_reserve(who, value) {
            return Err(Error::<T>::InsufficientFunds.into());
        }
        <Reserved<T>>::mutate(who, |previous| previous.saturating_accrue(value));
        Ok(())
    }

    fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance {
        <Reserved<T>>::mutate(who, |previous| {
            if value > *previous {
                let remainder = value.saturating_sub(*previous);
                *previous = Zero::zero();
                remainder
            } else {
                previous.saturating_reduce(value);
                Zero::zero()
            }
        })
    }

    // NOT SUPPORTED
    fn repatriate_reserved(
        _slashed: &T::AccountId,
        _beneficiary: &T::AccountId,
        _value: Self::Balance,
        _status: BalanceStatus,
    ) -> sp_std::result::Result<Self::Balance, DispatchError> {
        Err(Error::<T>::NotSupported.into())
    }
}