|
| 1 | +//! Imbalances implementation. |
| 2 | +
|
| 3 | +use frame_support::traits::{SameOrOther, TryDrop}; |
| 4 | +use sp_std::{cmp::Ordering, mem}; |
| 5 | + |
| 6 | +use super::*; |
| 7 | + |
| 8 | +/// Opaque, move-only struct with private fields that serves as a token denoting that |
| 9 | +/// funds have been created without any equal and opposite accounting. |
| 10 | +#[must_use] |
| 11 | +#[derive(RuntimeDebug, PartialEq, Eq)] |
| 12 | +pub struct PositiveImbalance<T: Config<I>, I: 'static = ()>(T::Balance); |
| 13 | + |
| 14 | +impl<T: Config<I>, I: 'static> PositiveImbalance<T, I> { |
| 15 | + /// Create a new positive imbalance from a balance. |
| 16 | + pub fn new(amount: T::Balance) -> Self { |
| 17 | + PositiveImbalance(amount) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Opaque, move-only struct with private fields that serves as a token denoting that |
| 22 | +/// funds have been destroyed without any equal and opposite accounting. |
| 23 | +#[must_use] |
| 24 | +#[derive(RuntimeDebug, PartialEq, Eq)] |
| 25 | +pub struct NegativeImbalance<T: Config<I>, I: 'static = ()>(T::Balance); |
| 26 | + |
| 27 | +impl<T: Config<I>, I: 'static> NegativeImbalance<T, I> { |
| 28 | + /// Create a new negative imbalance from a balance. |
| 29 | + pub fn new(amount: T::Balance) -> Self { |
| 30 | + NegativeImbalance(amount) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T: Config<I>, I: 'static> TryDrop for PositiveImbalance<T, I> { |
| 35 | + fn try_drop(self) -> result::Result<(), Self> { |
| 36 | + self.drop_zero() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: Config<I>, I: 'static> Default for PositiveImbalance<T, I> { |
| 41 | + fn default() -> Self { |
| 42 | + Self::zero() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<T: Config<I>, I: 'static> Imbalance<T::Balance> for PositiveImbalance<T, I> { |
| 47 | + type Opposite = NegativeImbalance<T, I>; |
| 48 | + |
| 49 | + fn zero() -> Self { |
| 50 | + Self(Zero::zero()) |
| 51 | + } |
| 52 | + |
| 53 | + fn drop_zero(self) -> result::Result<(), Self> { |
| 54 | + if self.0.is_zero() { |
| 55 | + Ok(()) |
| 56 | + } else { |
| 57 | + Err(self) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fn split(self, amount: T::Balance) -> (Self, Self) { |
| 62 | + let first = self.0.min(amount); |
| 63 | + let second = self.0 - first; |
| 64 | + |
| 65 | + mem::forget(self); |
| 66 | + (Self(first), Self(second)) |
| 67 | + } |
| 68 | + |
| 69 | + fn merge(mut self, other: Self) -> Self { |
| 70 | + self.0 = self.0.saturating_add(other.0); |
| 71 | + mem::forget(other); |
| 72 | + |
| 73 | + self |
| 74 | + } |
| 75 | + |
| 76 | + fn subsume(&mut self, other: Self) { |
| 77 | + self.0 = self.0.saturating_add(other.0); |
| 78 | + mem::forget(other); |
| 79 | + } |
| 80 | + |
| 81 | + fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> { |
| 82 | + let (a, b) = (self.0, other.0); |
| 83 | + mem::forget((self, other)); |
| 84 | + |
| 85 | + match a.cmp(&b) { |
| 86 | + Ordering::Greater => SameOrOther::Same(Self(a - b)), |
| 87 | + Ordering::Less => SameOrOther::Other(NegativeImbalance::new(b - a)), |
| 88 | + Ordering::Equal => SameOrOther::None, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn peek(&self) -> T::Balance { |
| 93 | + self.0 |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl<T: Config<I>, I: 'static> TryDrop for NegativeImbalance<T, I> { |
| 98 | + fn try_drop(self) -> result::Result<(), Self> { |
| 99 | + self.drop_zero() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<T: Config<I>, I: 'static> Default for NegativeImbalance<T, I> { |
| 104 | + fn default() -> Self { |
| 105 | + Self::zero() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<T: Config<I>, I: 'static> Imbalance<T::Balance> for NegativeImbalance<T, I> { |
| 110 | + type Opposite = PositiveImbalance<T, I>; |
| 111 | + |
| 112 | + fn zero() -> Self { |
| 113 | + Self(Zero::zero()) |
| 114 | + } |
| 115 | + |
| 116 | + fn drop_zero(self) -> result::Result<(), Self> { |
| 117 | + if self.0.is_zero() { |
| 118 | + Ok(()) |
| 119 | + } else { |
| 120 | + Err(self) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fn split(self, amount: T::Balance) -> (Self, Self) { |
| 125 | + let first = self.0.min(amount); |
| 126 | + let second = self.0 - first; |
| 127 | + |
| 128 | + mem::forget(self); |
| 129 | + (Self(first), Self(second)) |
| 130 | + } |
| 131 | + |
| 132 | + fn merge(mut self, other: Self) -> Self { |
| 133 | + self.0 = self.0.saturating_add(other.0); |
| 134 | + mem::forget(other); |
| 135 | + |
| 136 | + self |
| 137 | + } |
| 138 | + |
| 139 | + fn subsume(&mut self, other: Self) { |
| 140 | + self.0 = self.0.saturating_add(other.0); |
| 141 | + mem::forget(other); |
| 142 | + } |
| 143 | + |
| 144 | + fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> { |
| 145 | + let (a, b) = (self.0, other.0); |
| 146 | + mem::forget((self, other)); |
| 147 | + |
| 148 | + match a.cmp(&b) { |
| 149 | + Ordering::Greater => SameOrOther::Same(Self(a - b)), |
| 150 | + Ordering::Less => SameOrOther::Other(PositiveImbalance::new(b - a)), |
| 151 | + Ordering::Equal => SameOrOther::None, |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + fn peek(&self) -> T::Balance { |
| 156 | + self.0 |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl<T: Config<I>, I: 'static> Drop for PositiveImbalance<T, I> { |
| 161 | + /// Basic drop handler will just square up the total issuance. |
| 162 | + fn drop(&mut self) { |
| 163 | + TotalIssuance::<T, I>::mutate(|v| *v = v.saturating_add(self.0)); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl<T: Config<I>, I: 'static> Drop for NegativeImbalance<T, I> { |
| 168 | + /// Basic drop handler will just square up the total issuance. |
| 169 | + fn drop(&mut self) { |
| 170 | + TotalIssuance::<T, I>::mutate(|v| *v = v.saturating_sub(self.0)); |
| 171 | + } |
| 172 | +} |
0 commit comments