1use crate::{Config, CreditOf, Event, Pallet};
21use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
22use core::ops::BitOr;
23use frame_support::traits::{Imbalance, LockIdentifier, OnUnbalanced, WithdrawReasons};
24use scale_info::TypeInfo;
25use sp_runtime::{RuntimeDebug, Saturating};
26
27#[derive(
29 Encode,
30 Decode,
31 DecodeWithMemTracking,
32 Clone,
33 Copy,
34 PartialEq,
35 Eq,
36 RuntimeDebug,
37 MaxEncodedLen,
38 TypeInfo,
39)]
40pub enum Reasons {
41 Fee = 0,
43 Misc = 1,
45 All = 2,
47}
48
49impl From<WithdrawReasons> for Reasons {
50 fn from(r: WithdrawReasons) -> Reasons {
51 if r == WithdrawReasons::TRANSACTION_PAYMENT {
52 Reasons::Fee
53 } else if r.contains(WithdrawReasons::TRANSACTION_PAYMENT) {
54 Reasons::All
55 } else {
56 Reasons::Misc
57 }
58 }
59}
60
61impl BitOr for Reasons {
62 type Output = Reasons;
63 fn bitor(self, other: Reasons) -> Reasons {
64 if self == other {
65 return self
66 }
67 Reasons::All
68 }
69}
70
71#[derive(
74 Encode,
75 Decode,
76 DecodeWithMemTracking,
77 Clone,
78 PartialEq,
79 Eq,
80 RuntimeDebug,
81 MaxEncodedLen,
82 TypeInfo,
83)]
84pub struct BalanceLock<Balance> {
85 pub id: LockIdentifier,
87 pub amount: Balance,
89 pub reasons: Reasons,
91}
92
93#[derive(
95 Encode,
96 Decode,
97 DecodeWithMemTracking,
98 Clone,
99 PartialEq,
100 Eq,
101 RuntimeDebug,
102 MaxEncodedLen,
103 TypeInfo,
104)]
105pub struct ReserveData<ReserveIdentifier, Balance> {
106 pub id: ReserveIdentifier,
108 pub amount: Balance,
110}
111
112#[derive(
114 Encode,
115 Decode,
116 DecodeWithMemTracking,
117 Clone,
118 PartialEq,
119 Eq,
120 Default,
121 RuntimeDebug,
122 MaxEncodedLen,
123 TypeInfo,
124)]
125pub struct AccountData<Balance> {
126 pub free: Balance,
130 pub reserved: Balance,
135 pub frozen: Balance,
139 pub flags: ExtraFlags,
142}
143
144const IS_NEW_LOGIC: u128 = 0x80000000_00000000_00000000_00000000u128;
145
146#[derive(
147 Encode,
148 Decode,
149 DecodeWithMemTracking,
150 Clone,
151 PartialEq,
152 Eq,
153 RuntimeDebug,
154 MaxEncodedLen,
155 TypeInfo,
156)]
157pub struct ExtraFlags(pub(crate) u128);
158impl Default for ExtraFlags {
159 fn default() -> Self {
160 Self(IS_NEW_LOGIC)
161 }
162}
163impl ExtraFlags {
164 pub fn old_logic() -> Self {
165 Self(0)
166 }
167 pub fn set_new_logic(&mut self) {
168 self.0 = self.0 | IS_NEW_LOGIC
169 }
170 pub fn is_new_logic(&self) -> bool {
171 (self.0 & IS_NEW_LOGIC) == IS_NEW_LOGIC
172 }
173}
174
175impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
176 pub fn usable(&self) -> Balance {
177 self.free.saturating_sub(self.frozen)
178 }
179
180 pub fn total(&self) -> Balance {
182 self.free.saturating_add(self.reserved)
183 }
184}
185
186pub struct DustCleaner<T: Config<I>, I: 'static = ()>(
187 pub(crate) Option<(T::AccountId, CreditOf<T, I>)>,
188);
189
190impl<T: Config<I>, I: 'static> Drop for DustCleaner<T, I> {
191 fn drop(&mut self) {
192 if let Some((who, dust)) = self.0.take() {
193 Pallet::<T, I>::deposit_event(Event::DustLost { account: who, amount: dust.peek() });
194 T::DustRemoval::on_unbalanced(dust);
195 }
196 }
197}
198
199#[derive(
201 Encode,
202 Decode,
203 DecodeWithMemTracking,
204 Clone,
205 PartialEq,
206 Eq,
207 RuntimeDebug,
208 MaxEncodedLen,
209 TypeInfo,
210)]
211pub enum AdjustmentDirection {
212 Increase,
214 Decrease,
216}