referrerpolicy=no-referrer-when-downgrade

pallet_balances/
types.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Types used in the pallet.
19
20use 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/// Simplified reasons for withdrawing balance.
28#[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	/// Paying system transaction fees.
42	Fee = 0,
43	/// Any reason other than paying system transaction fees.
44	Misc = 1,
45	/// Any reason at all.
46	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/// A single lock on a balance. There can be many of these on an account and they "overlap", so the
72/// same balance is frozen by multiple locks.
73#[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	/// An identifier for this lock. Only one lock may be in existence for each identifier.
86	pub id: LockIdentifier,
87	/// The amount which the free balance may not drop below when this lock is in effect.
88	pub amount: Balance,
89	/// If true, then the lock remains in effect even for payment of transaction fees.
90	pub reasons: Reasons,
91}
92
93/// Store named reserved balance.
94#[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	/// The identifier for the named reserve.
107	pub id: ReserveIdentifier,
108	/// The amount of the named reserve.
109	pub amount: Balance,
110}
111
112/// All balance information for an account.
113#[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	/// Non-reserved part of the balance which the account holder may be able to control.
127	///
128	/// This is the only balance that matters in terms of most operations on tokens.
129	pub free: Balance,
130	/// Balance which is has active holds on it and may not be used at all.
131	///
132	/// This is the sum of all individual holds together with any sums still under the (deprecated)
133	/// reserves API.
134	pub reserved: Balance,
135	/// The amount that `free + reserved` may not drop below when reducing the balance, except for
136	/// actions where the account owner cannot reasonably benefit from the balance reduction, such
137	/// as slashing.
138	pub frozen: Balance,
139	/// Extra information about this account. The MSB is a flag indicating whether the new ref-
140	/// counting logic is in place for this account.
141	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	/// The total balance in this account including any that is reserved and ignoring any frozen.
181	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/// Whether something should be interpreted as an increase or a decrease.
200#[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 the amount.
213	Increase,
214	/// Decrease the amount.
215	Decrease,
216}