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
use crate::value::Value;
use std::ops::{Add, AddAssign};

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Stake(pub u64);

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct StakeUnit(Stake);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PercentStake {
    pub stake: Stake,
    pub total: Stake,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SplitValueIn {
    pub parts: StakeUnit,
    pub remaining: Stake,
}

impl Stake {
    pub fn from_value(v: Value) -> Self {
        Stake(v.0)
    }

    pub fn zero() -> Self {
        Stake(0)
    }

    pub fn sum<I>(values: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        values.fold(Stake(0), |acc, v| acc + v)
    }

    #[must_use = "internal state is not modified"]
    pub fn checked_add(&self, rhs: Self) -> Option<Self> {
        self.0.checked_add(rhs.0).map(Self)
    }

    #[must_use = "internal state is not modified"]
    pub fn checked_sub(&self, rhs: Self) -> Option<Self> {
        self.0.checked_sub(rhs.0).map(Self)
    }

    #[must_use = "internal state is not modified"]
    pub fn wrapping_add(&self, rhs: Self) -> Self {
        Self(self.0.wrapping_add(rhs.0))
    }

    #[must_use = "internal state is not modified"]
    pub fn wrapping_sub(&self, rhs: Self) -> Self {
        Self(self.0.wrapping_sub(rhs.0))
    }

    /// Divide a value by n equals parts, with a potential remainder
    pub fn split_in(self, n: u32) -> SplitValueIn {
        let n = n as u64;
        SplitValueIn {
            parts: StakeUnit(Stake(self.0 / n)),
            remaining: Stake(self.0 % n),
        }
    }
}

impl From<Stake> for u64 {
    fn from(s: Stake) -> u64 {
        s.0
    }
}

impl AsRef<u64> for Stake {
    fn as_ref(&self) -> &u64 {
        &self.0
    }
}

impl StakeUnit {
    #[must_use = "operation does not change the value state"]
    pub fn scale(self, n: u32) -> Stake {
        Stake((self.0).0.checked_mul(n as u64).unwrap())
    }
}

impl PercentStake {
    pub fn new(stake: Stake, total: Stake) -> Self {
        assert!(stake <= total);
        PercentStake { stake, total }
    }

    pub fn as_float(&self) -> f64 {
        (self.stake.0 as f64) / (self.total.0 as f64)
    }

    /// Apply this ratio to a value
    ///
    /// Returned Value = (Value / Total) * Stake
    ///
    /// note that we augment the precision by 10^18 to prevent
    /// early zeroing, as we do the operation using fixed sized integers
    pub fn scale_value(&self, v: Value) -> Value {
        const SCALE: u128 = 1_000_000_000_000_000_000;
        let scaled_divided = (v.0 as u128) * SCALE / self.total.0 as u128;
        let r = (scaled_divided * self.stake.0 as u128) / SCALE;
        Value(r as u64)
    }
}

impl Add for Stake {
    type Output = Stake;

    fn add(self, other: Self) -> Self {
        Stake(self.0 + other.0)
    }
}

impl AddAssign for Stake {
    fn add_assign(&mut self, other: Self) {
        *self = Self(self.0 + other.0)
    }
}

impl std::fmt::Display for Stake {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for Stake {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            Stake::from_value(Arbitrary::arbitrary(g))
        }
    }
}