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
//! Implementation of stored positive and negative balances

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode /*EncodeLike*/, Encode, FullCodec};
use core::ops::{Add, Sub};
use frame_support::traits::{Imbalance, SignedImbalance};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{
    traits::{AtLeast32Bit, MaybeSerializeDeserialize, Member, Zero},
    RuntimeDebug,
};
use sp_std::fmt::Debug;

/// Balance representation based on number generic `Balance`
#[derive(Encode, Decode, Clone, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum SignedBalance<Balance>
where
    Balance: Member,
{
    Positive(Balance),
    Negative(Balance),
}

impl<Balance> Zero for SignedBalance<Balance>
where
    Balance: Member + AtLeast32Bit,
{
    fn zero() -> Self {
        SignedBalance::Positive(Balance::zero())
    }

    fn is_zero(&self) -> bool {
        match self {
            Self::Positive(value) => value.is_zero(),
            Self::Negative(value) => value.is_zero(),
        }
    }
}

impl<Balance> Add for SignedBalance<Balance>
where
    Balance: Member + AtLeast32Bit,
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        match rhs {
            SignedBalance::Positive(value) => self.add_balance(value),
            SignedBalance::Negative(value) => self.sub_balance(value),
        }
        .unwrap()
    }
}

impl<Balance> Sub for SignedBalance<Balance>
where
    Balance: Member + AtLeast32Bit,
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        match rhs {
            SignedBalance::Negative(value) => self.add_balance(value),
            SignedBalance::Positive(value) => self.sub_balance(value),
        }
        .unwrap()
    }
}

impl<Balance> SignedBalance<Balance>
where
    Balance: Member + AtLeast32Bit,
{
    pub fn sub_balance(&self, other: Balance) -> Option<Self> {
        match self {
            SignedBalance::Positive(value) => {
                let min_to_remove = value.min(&other);
                let new_value = value.checked_sub(&min_to_remove)?;
                let new_other = other.checked_sub(&min_to_remove)?;
                if new_other.is_zero() {
                    Some(SignedBalance::Positive(new_value))
                } else {
                    Some(SignedBalance::Negative(new_other))
                }
            }
            SignedBalance::Negative(value) => {
                let new_value = other.checked_add(value)?;
                Some(SignedBalance::Negative(new_value))
            }
        }
    }

    pub fn add_balance(&self, other: Balance) -> Option<Self> {
        match self {
            SignedBalance::Negative(value) => {
                let min_to_remove = value.min(&other);
                let new_value = value.checked_sub(&min_to_remove)?;
                let new_other = other.checked_sub(&min_to_remove)?;
                if new_value.is_zero() {
                    Some(SignedBalance::Positive(new_other))
                } else {
                    Some(SignedBalance::Negative(new_value))
                }
            }
            SignedBalance::Positive(value) => {
                let new_value = other.checked_add(value)?;
                Some(SignedBalance::Positive(new_value))
            }
        }
    }

    pub fn abs(&self) -> Balance {
        match self {
            SignedBalance::Positive(value) => value.clone(),
            SignedBalance::Negative(value) => value.clone(),
        }
    }

    pub fn negate(&self) -> SignedBalance<Balance> {
        match self {
            SignedBalance::Positive(value) => SignedBalance::Negative(value.clone()),
            SignedBalance::Negative(value) => SignedBalance::Positive(value.clone()),
        }
    }
}

impl<Balance> Default for SignedBalance<Balance>
where
    Balance: Member + Default,
{
    fn default() -> SignedBalance<Balance> {
        SignedBalance::Positive(Default::default())
    }
}

impl<
        B: AtLeast32Bit + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default + Member,
        P: Imbalance<B, Opposite = N>,
        N: Imbalance<B, Opposite = P>,
    > From<&SignedImbalance<B, P>> for SignedBalance<B>
{
    fn from(imbalance: &SignedImbalance<B, P>) -> SignedBalance<B> {
        match imbalance {
            SignedImbalance::Positive(x) => SignedBalance::Positive(x.peek()),
            SignedImbalance::Negative(x) => SignedBalance::Negative(x.peek()),
        }
    }
}