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
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
mod pallet {
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use frame_support::{
fail,
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::KeepAlive, LockIdentifier, WithdrawReasons},
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::{Convert, Zero};
use sp_std::{fmt::Debug, prelude::*, result};
use totem_primitives::escrow::{EscrowableCurrency, Reason, TotemLocksError};
type EscrowableBalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[pallet::pallet]
#[pallet::generate_store(trait Store)]
pub struct Pallet<T>(_);
#[pallet::storage]
#[pallet::getter(fn escrowed)]
pub type Escrowed<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Blake2_128Concat,
LockIdentifier,
EscrowedAmount<EscrowableBalanceOf<T>, T::BlockNumber>,
>;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Currency: Currency<Self::AccountId>;
type EscrowConverter: Convert<[u8; 32], Self::AccountId>;
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::event]
pub enum Event<T: Config> {}
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo)]
pub struct EscrowedAmount<Balance, BlockNumber> {
pub amount: Balance,
pub reason: Reason,
pub until: BlockNumber,
}
const ESCROW: WithdrawReasons = unsafe { WithdrawReasons::from_bits_unchecked(0b1000_0000) };
impl<T: Config> EscrowableCurrency<T::AccountId> for Pallet<T> {
type Moment = T::BlockNumber;
type Currency = T::Currency;
fn escrow_account() -> T::AccountId {
let escrow_account: [u8; 32] = *b"TotemsEscrowAddress4LockingFunds";
T::EscrowConverter::convert(escrow_account)
}
fn set_lock(
id: LockIdentifier,
who: &T::AccountId,
amount: EscrowableBalanceOf<T>,
until: T::BlockNumber,
reason: Reason,
) -> Result<(), TotemLocksError> {
if amount.is_zero() {
fail!(TotemLocksError::ZeroAmount)
}
let now = frame_system::Pallet::<T>::block_number();
if now > until {
fail!(TotemLocksError::InvalidDeadline)
}
Escrowed::<T>::try_mutate(who, id, |maybe_escrowed| match maybe_escrowed {
Some(_) => return Err(TotemLocksError::IdAlreadyExists),
slot @ &mut None => {
Self::transfer_to_the_escrow(who, amount)?;
*slot = Some(EscrowedAmount {
amount,
reason,
until,
});
Ok(())
}
})
}
fn remove_lock(id: LockIdentifier, who: &T::AccountId) -> Result<(), TotemLocksError> {
Escrowed::<T>::try_mutate_exists(who, id, |maybe_escrowed| match maybe_escrowed {
Some(escrowed) => {
Self::transfer_from_the_escrow(who, escrowed.amount)?;
*maybe_escrowed = None;
Ok(())
}
None => return Err(TotemLocksError::IdDoesNotExist),
})
}
}
impl<T: Config> Pallet<T> {
fn transfer_to_the_escrow(
who: &T::AccountId,
amount: EscrowableBalanceOf<T>,
) -> result::Result<(), TotemLocksError> {
let imba = <T as Config>::Currency::withdraw(who, amount, ESCROW, KeepAlive)?;
<T as Config>::Currency::make_free_balance_be(&Self::escrow_account(), amount);
let _imba_resolved = imba;
Ok(())
}
fn transfer_from_the_escrow(
who: &T::AccountId,
amount: EscrowableBalanceOf<T>,
) -> result::Result<(), TotemLocksError> {
let imba = <T as Config>::Currency::withdraw(
&Self::escrow_account(),
amount,
ESCROW,
KeepAlive,
)?;
<T as Config>::Currency::make_free_balance_be(who, amount);
let _imba_resolved = imba;
Ok(())
}
}
}