-
Notifications
You must be signed in to change notification settings - Fork 49
/
lib.rs
133 lines (111 loc) · 3.54 KB
/
lib.rs
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
#![recursion_limit = "128"]
#![cfg_attr(not(feature = "std"), no_std)]
pub use structs::*;
pub use traits::*;
/// An identifier for a lock. Used for disambiguating different locks so that
/// they can be individually replaced or removed.
pub type LockIdentifier = [u8; 8];
pub type TimeStamp = u64;
mod structs {
use codec::{Decode, Encode};
use rstd::{convert::TryInto, vec::Vec};
use sr_primitives::{
traits::{SaturatedConversion, SimpleArithmetic},
RuntimeDebug,
};
use srml_support::traits::WithdrawReasons;
use super::{LockIdentifier, TimeStamp};
#[derive(Clone, PartialEq, Encode, Decode, RuntimeDebug)]
pub struct BalanceLock<Balance, Moment> {
pub id: LockIdentifier,
pub withdraw_lock: WithdrawLock<Balance, Moment>,
pub reasons: WithdrawReasons,
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub enum WithdrawLock<Balance, Moment> {
Normal(NormalLock<Balance, Moment>),
WithStaking(StakingLock<Balance, TimeStamp>),
}
impl<Balance, Moment> WithdrawLock<Balance, Moment>
where
Balance: Copy + Default + SimpleArithmetic,
Moment: Copy + PartialOrd + TryInto<TimeStamp>,
{
pub fn can_withdraw(&self, at: Moment, new_balance: Balance) -> bool {
match self {
WithdrawLock::Normal(lock) => lock.can_withdraw(at, new_balance),
WithdrawLock::WithStaking(lock) => lock.can_withdraw(at.saturated_into::<TimeStamp>(), new_balance),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct NormalLock<Balance, Moment> {
pub amount: Balance,
pub until: Moment,
}
impl<Balance, Moment> NormalLock<Balance, Moment>
where
Balance: Copy + PartialOrd,
Moment: PartialOrd,
{
#[inline]
fn valid_at(&self, at: Moment) -> bool {
self.until > at
}
#[inline]
fn can_withdraw(&self, at: Moment, new_balance: Balance) -> bool {
self.valid_at(at) && self.amount > new_balance
}
}
#[derive(Clone, Default, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct StakingLock<Balance, Moment> {
pub staking_amount: Balance,
pub unbondings: Vec<NormalLock<Balance, Moment>>,
}
impl<Balance, Moment> StakingLock<Balance, Moment>
where
Balance: Copy + PartialOrd + SimpleArithmetic,
Moment: Copy + PartialOrd,
{
#[inline]
fn can_withdraw(&self, at: Moment, new_balance: Balance) -> bool {
let mut locked_amount = self.staking_amount;
for unbonding in &self.unbondings {
if unbonding.valid_at(at) {
// TODO: check overflow?
locked_amount += unbonding.amount;
}
}
new_balance >= locked_amount
}
}
}
mod traits {
use srml_support::traits::{Currency, WithdrawReasons};
use super::{LockIdentifier, WithdrawLock};
pub trait OnMinted<Balance> {
fn on_minted(value: Balance);
}
pub trait OnAccountBalanceChanged<AccountId, Balance> {
fn on_changed(who: &AccountId, old: Balance, new: Balance);
}
/// A currency whose accounts can have liquidity restrictions.
pub trait LockableCurrency<AccountId>: Currency<AccountId> {
/// The quantity used to denote time; usually just a `BlockNumber`.
type Moment;
/// Create a new balance lock on account `who`.
///
/// If the new lock is valid (i.e. not already expired), it will push the struct to
/// the `Locks` vec in storage. Note that you can lock more funds than a user has.
///
/// If the lock `id` already exists, this will update it.
fn set_lock(
id: LockIdentifier,
who: &AccountId,
withdraw_lock: WithdrawLock<Self::Balance, Self::Moment>,
reasons: WithdrawReasons,
);
/// Remove an existing lock.
fn remove_lock(id: LockIdentifier, who: &AccountId);
}
}