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(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
29pub enum Reasons {
30 Fee = 0,
32 Misc = 1,
34 All = 2,
36}
37
38impl From<WithdrawReasons> for Reasons {
39 fn from(r: WithdrawReasons) -> Reasons {
40 if r == WithdrawReasons::TRANSACTION_PAYMENT {
41 Reasons::Fee
42 } else if r.contains(WithdrawReasons::TRANSACTION_PAYMENT) {
43 Reasons::All
44 } else {
45 Reasons::Misc
46 }
47 }
48}
49
50impl BitOr for Reasons {
51 type Output = Reasons;
52 fn bitor(self, other: Reasons) -> Reasons {
53 if self == other {
54 return self
55 }
56 Reasons::All
57 }
58}
59
60#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
63pub struct BalanceLock<Balance> {
64 pub id: LockIdentifier,
66 pub amount: Balance,
68 pub reasons: Reasons,
70}
71
72#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
74pub struct ReserveData<ReserveIdentifier, Balance> {
75 pub id: ReserveIdentifier,
77 pub amount: Balance,
79}
80
81#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, MaxEncodedLen, TypeInfo)]
83pub struct AccountData<Balance> {
84 pub free: Balance,
88 pub reserved: Balance,
93 pub frozen: Balance,
97 pub flags: ExtraFlags,
100}
101
102const IS_NEW_LOGIC: u128 = 0x80000000_00000000_00000000_00000000u128;
103
104#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
105pub struct ExtraFlags(pub(crate) u128);
106impl Default for ExtraFlags {
107 fn default() -> Self {
108 Self(IS_NEW_LOGIC)
109 }
110}
111impl ExtraFlags {
112 pub fn old_logic() -> Self {
113 Self(0)
114 }
115 pub fn set_new_logic(&mut self) {
116 self.0 = self.0 | IS_NEW_LOGIC
117 }
118 pub fn is_new_logic(&self) -> bool {
119 (self.0 & IS_NEW_LOGIC) == IS_NEW_LOGIC
120 }
121}
122
123impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
124 pub fn usable(&self) -> Balance {
125 self.free.saturating_sub(self.frozen)
126 }
127
128 pub fn total(&self) -> Balance {
130 self.free.saturating_add(self.reserved)
131 }
132}
133
134pub struct DustCleaner<T: Config<I>, I: 'static = ()>(
135 pub(crate) Option<(T::AccountId, CreditOf<T, I>)>,
136);
137
138impl<T: Config<I>, I: 'static> Drop for DustCleaner<T, I> {
139 fn drop(&mut self) {
140 if let Some((who, dust)) = self.0.take() {
141 Pallet::<T, I>::deposit_event(Event::DustLost { account: who, amount: dust.peek() });
142 T::DustRemoval::on_unbalanced(dust);
143 }
144 }
145}
146
147#[derive(
149 Encode,
150 Decode,
151 DecodeWithMemTracking,
152 Clone,
153 PartialEq,
154 Eq,
155 RuntimeDebug,
156 MaxEncodedLen,
157 TypeInfo,
158)]
159pub enum AdjustmentDirection {
160 Increase,
162 Decrease,
164}