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::Saturating;
26
27#[derive(
29 Encode,
30 Decode,
31 DecodeWithMemTracking,
32 Clone,
33 Copy,
34 PartialEq,
35 Eq,
36 Debug,
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, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
75)]
76pub struct BalanceLock<Balance> {
77 pub id: LockIdentifier,
79 pub amount: Balance,
81 pub reasons: Reasons,
83}
84
85#[derive(
87 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
88)]
89pub struct ReserveData<ReserveIdentifier, Balance> {
90 pub id: ReserveIdentifier,
92 pub amount: Balance,
94}
95
96#[derive(
98 Encode,
99 Decode,
100 DecodeWithMemTracking,
101 Clone,
102 PartialEq,
103 Eq,
104 Default,
105 Debug,
106 MaxEncodedLen,
107 TypeInfo,
108)]
109pub struct AccountData<Balance> {
110 pub free: Balance,
114 pub reserved: Balance,
119 pub frozen: Balance,
123 pub flags: ExtraFlags,
126}
127
128const IS_NEW_LOGIC: u128 = 0x80000000_00000000_00000000_00000000u128;
129
130#[derive(
131 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
132)]
133pub struct ExtraFlags(pub(crate) u128);
134impl Default for ExtraFlags {
135 fn default() -> Self {
136 Self(IS_NEW_LOGIC)
137 }
138}
139impl ExtraFlags {
140 pub fn old_logic() -> Self {
141 Self(0)
142 }
143 pub fn set_new_logic(&mut self) {
144 self.0 = self.0 | IS_NEW_LOGIC
145 }
146 pub fn is_new_logic(&self) -> bool {
147 (self.0 & IS_NEW_LOGIC) == IS_NEW_LOGIC
148 }
149}
150
151impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
152 pub fn usable(&self) -> Balance {
153 self.free.saturating_sub(self.frozen)
154 }
155
156 pub fn total(&self) -> Balance {
158 self.free.saturating_add(self.reserved)
159 }
160}
161
162pub struct DustCleaner<T: Config<I>, I: 'static = ()>(
163 pub(crate) Option<(T::AccountId, CreditOf<T, I>)>,
164);
165
166impl<T: Config<I>, I: 'static> Drop for DustCleaner<T, I> {
167 fn drop(&mut self) {
168 if let Some((who, dust)) = self.0.take() {
169 Pallet::<T, I>::deposit_event(Event::DustLost { account: who, amount: dust.peek() });
170 T::DustRemoval::on_unbalanced(dust);
171 }
172 }
173}
174
175#[derive(
177 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
178)]
179pub enum AdjustmentDirection {
180 Increase,
182 Decrease,
184}