1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// wrapping these imbalances in a private module is necessary to ensure absolute
// privacy of the inner member.
use crate::{Config, TotalIssuance};
use frame_support::traits::{tokens::currency::MultiTokenImbalanceWithZeroTrait, Imbalance, SameOrOther, TryDrop};
use sp_runtime::traits::{Saturating, Zero};
use sp_std::{mem, result};

impl<T: Config> MultiTokenImbalanceWithZeroTrait<T::CurrencyId> for PositiveImbalance<T> {
	fn from_zero(currency_id: T::CurrencyId) -> Self {
		Self::zero(currency_id)
	}
}

impl<T: Config> MultiTokenImbalanceWithZeroTrait<T::CurrencyId> for NegativeImbalance<T> {
	fn from_zero(currency_id: T::CurrencyId) -> Self {
		Self::zero(currency_id)
	}
}

/// Opaque, move-only struct with private fields that serves as a token
/// denoting that funds have been created without any equal and opposite
/// accounting.
#[must_use]
pub struct PositiveImbalance<T: Config>(T::CurrencyId, T::Balance);

impl<T: Config> PositiveImbalance<T> {
	/// Create a new positive imbalance from a balance.
	pub fn new(currency_id: T::CurrencyId, amount: T::Balance) -> Self {
		PositiveImbalance(currency_id, amount)
	}

	pub fn zero(currency_id: T::CurrencyId) -> Self {
		PositiveImbalance(currency_id, Zero::zero())
	}
}

impl<T: Config> Default for PositiveImbalance<T> {
	fn default() -> Self {
		PositiveImbalance(Default::default(), Default::default())
	}
}

/// Opaque, move-only struct with private fields that serves as a token
/// denoting that funds have been destroyed without any equal and opposite
/// accounting.
#[must_use]
pub struct NegativeImbalance<T: Config>(pub T::CurrencyId, T::Balance);

impl<T: Config> NegativeImbalance<T> {
	/// Create a new negative imbalance from a balance.
	pub fn new(currency_id: T::CurrencyId, amount: T::Balance) -> Self {
		NegativeImbalance(currency_id, amount)
	}

	pub fn zero(currency_id: T::CurrencyId) -> Self {
		NegativeImbalance(currency_id, Zero::zero())
	}
}

impl<T: Config> Default for NegativeImbalance<T> {
	fn default() -> Self {
		NegativeImbalance(Default::default(), Default::default())
	}
}

impl<T: Config> TryDrop for PositiveImbalance<T> {
	fn try_drop(self) -> result::Result<(), Self> {
		self.drop_zero()
	}
}

impl<T: Config> Imbalance<T::Balance> for PositiveImbalance<T> {
	type Opposite = NegativeImbalance<T>;

	fn zero() -> Self {
		unimplemented!("PositiveImbalance::zero is not implemented");
	}

	fn drop_zero(self) -> result::Result<(), Self> {
		if self.1.is_zero() {
			Ok(())
		} else {
			Err(self)
		}
	}
	fn split(self, amount: T::Balance) -> (Self, Self) {
		let first = self.1.min(amount);
		let second = self.1 - first;
		let currency_id = self.0;

		mem::forget(self);
		(Self::new(currency_id, first), Self::new(currency_id, second))
	}
	fn merge(mut self, other: Self) -> Self {
		assert_eq!(self.0, other.0);
		self.1 = self.1.saturating_add(other.1);
		mem::forget(other);
		self
	}
	fn subsume(&mut self, other: Self) {
		assert_eq!(self.0, other.0);
		self.1 = self.1.saturating_add(other.1);
		mem::forget(other);
	}
	// allow to make the impl same with `pallet-balances`
	#[allow(clippy::comparison_chain)]
	fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> {
		assert_eq!(self.0, other.0);
		let (a, b) = (self.1, other.1);
		let currency_id = self.0;
		mem::forget((self, other));

		if a > b {
			SameOrOther::Same(Self::new(currency_id, a - b))
		} else if b > a {
			SameOrOther::Other(NegativeImbalance::new(currency_id, b - a))
		} else {
			SameOrOther::None
		}
	}
	fn peek(&self) -> T::Balance {
		self.1
	}
}

impl<T: Config> TryDrop for NegativeImbalance<T> {
	fn try_drop(self) -> result::Result<(), Self> {
		self.drop_zero()
	}
}

impl<T: Config> Imbalance<T::Balance> for NegativeImbalance<T> {
	type Opposite = PositiveImbalance<T>;

	fn zero() -> Self {
		unimplemented!("NegativeImbalance::zero is not implemented");
	}
	fn drop_zero(self) -> result::Result<(), Self> {
		if self.1.is_zero() {
			Ok(())
		} else {
			Err(self)
		}
	}
	fn split(self, amount: T::Balance) -> (Self, Self) {
		let first = self.1.min(amount);
		let second = self.1 - first;
		let currency_id = self.0;

		mem::forget(self);
		(Self::new(currency_id, first), Self::new(currency_id, second))
	}
	fn merge(mut self, other: Self) -> Self {
		assert_eq!(self.0, other.0);
		self.1 = self.1.saturating_add(other.1);
		mem::forget(other);
		self
	}
	fn subsume(&mut self, other: Self) {
		assert_eq!(self.0, other.0);
		self.1 = self.1.saturating_add(other.1);
		mem::forget(other);
	}
	// allow to make the impl same with `pallet-balances`
	#[allow(clippy::comparison_chain)]
	fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> {
		assert_eq!(self.0, other.0);
		let (a, b) = (self.1, other.1);
		let currency_id = self.0;
		mem::forget((self, other));
		if a > b {
			SameOrOther::Same(Self::new(currency_id, a - b))
		} else if b > a {
			SameOrOther::Other(PositiveImbalance::new(currency_id, b - a))
		} else {
			SameOrOther::None
		}
	}
	fn peek(&self) -> T::Balance {
		self.1
	}
}

impl<T: Config> Drop for PositiveImbalance<T> {
	/// Basic drop handler will just square up the total issuance.
	fn drop(&mut self) {
		<TotalIssuance<T>>::mutate(self.0, |v| *v = v.saturating_add(self.1));
	}
}

impl<T: Config> Drop for NegativeImbalance<T> {
	/// Basic drop handler will just square up the total issuance.
	fn drop(&mut self) {
		<TotalIssuance<T>>::mutate(self.0, |v| *v = v.saturating_sub(self.1));
	}
}