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::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	Debug,
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, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
75)]
76pub struct BalanceLock<Balance> {
77	/// An identifier for this lock. Only one lock may be in existence for each identifier.
78	pub id: LockIdentifier,
79	/// The amount which the free balance may not drop below when this lock is in effect.
80	pub amount: Balance,
81	/// If true, then the lock remains in effect even for payment of transaction fees.
82	pub reasons: Reasons,
83}
84
85/// Store named reserved balance.
86#[derive(
87	Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
88)]
89pub struct ReserveData<ReserveIdentifier, Balance> {
90	/// The identifier for the named reserve.
91	pub id: ReserveIdentifier,
92	/// The amount of the named reserve.
93	pub amount: Balance,
94}
95
96/// All balance information for an account.
97#[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	/// Non-reserved part of the balance which the account holder may be able to control.
111	///
112	/// This is the only balance that matters in terms of most operations on tokens.
113	pub free: Balance,
114	/// Balance which is has active holds on it and may not be used at all.
115	///
116	/// This is the sum of all individual holds together with any sums still under the (deprecated)
117	/// reserves API.
118	pub reserved: Balance,
119	/// The amount that `free + reserved` may not drop below when reducing the balance, except for
120	/// actions where the account owner cannot reasonably benefit from the balance reduction, such
121	/// as slashing.
122	pub frozen: Balance,
123	/// Extra information about this account. The MSB is a flag indicating whether the new ref-
124	/// counting logic is in place for this account.
125	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	/// The total balance in this account including any that is reserved and ignoring any frozen.
157	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/// Whether something should be interpreted as an increase or a decrease.
176#[derive(
177	Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo,
178)]
179pub enum AdjustmentDirection {
180	/// Increase the amount.
181	Increase,
182	/// Decrease the amount.
183	Decrease,
184}