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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use std::cmp::Ordering;
use std::fmt;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{Signed, Zero};
use serde::{Deserialize, Serialize, Serializer};

use crate::bigint::bigint_ser;

/// A quantity of native tokens.
/// A token amount is an integer, but has a human interpretation as a value with
/// 18 decimal places.
/// This is a new-type in order to prevent accidental conversion from other BigInts.
/// From/Into BigInt is missing by design.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct TokenAmount {
    atto: BigInt,
}

// This type doesn't implement all the numeric traits (Num, Signed, etc),
// opting for a minimal useful set. Others can be added if needed.
impl TokenAmount {
    /// The logical number of decimal places of a token unit.
    pub const DECIMALS: usize = 18;

    /// The logical precision of a token unit.
    pub const PRECISION: u64 = 10u64.pow(Self::DECIMALS as u32);

    /// Creates a token amount from a quantity of indivisible units  (10^-18 whole units).
    pub fn from_atto(atto: impl Into<BigInt>) -> Self {
        Self { atto: atto.into() }
    }

    /// Creates a token amount from nanoFIL.
    pub fn from_nano(nano: impl Into<BigInt>) -> Self {
        const NANO_PRECISION: u64 = 10u64.pow((TokenAmount::DECIMALS as u32) - 9);
        Self {
            atto: nano.into() * NANO_PRECISION,
        }
    }

    /// Creates a token amount from a quantity of whole units (10^18 indivisible units).
    pub fn from_whole(tokens: impl Into<BigInt>) -> Self {
        Self::from_atto(tokens.into() * Self::PRECISION)
    }

    /// Returns the quantity of indivisible units.
    pub fn atto(&self) -> &BigInt {
        &self.atto
    }

    pub fn is_zero(&self) -> bool {
        self.atto.is_zero()
    }

    pub fn is_positive(&self) -> bool {
        self.atto.is_positive()
    }

    pub fn is_negative(&self) -> bool {
        self.atto.is_negative()
    }
}

impl Zero for TokenAmount {
    #[inline]
    fn zero() -> Self {
        Self {
            atto: BigInt::zero(),
        }
    }

    #[inline]
    fn is_zero(&self) -> bool {
        self.atto.is_zero()
    }
}

impl Ord for TokenAmount {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.atto.cmp(&other.atto)
    }
}

impl PartialOrd for TokenAmount {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Default for TokenAmount {
    #[inline]
    fn default() -> TokenAmount {
        TokenAmount::zero()
    }
}

impl fmt::Debug for TokenAmount {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TokenAmount({})", self)
    }
}

#[cfg(feature = "arb")]
impl quickcheck::Arbitrary for TokenAmount {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
        TokenAmount::from_atto(BigInt::arbitrary(g))
    }
}

/// Displays a token amount as a decimal in human units.
/// To avoid any confusion over whether the value is in human-scale or indivisible units,
/// the display always includes a decimal point.
impl fmt::Display for TokenAmount {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Implementation based on the bigdecimal library.
        let (q, r) = self.atto.div_rem(&BigInt::from(Self::PRECISION));
        let before_decimal = q.abs().to_str_radix(10);
        let after_decimal = if r.is_zero() {
            "0".to_string()
        } else {
            let fraction_str = r.abs().to_str_radix(10);
            let render = "0".repeat(Self::DECIMALS - fraction_str.len()) + fraction_str.as_str();
            render.trim_end_matches('0').to_string()
        };

        // Alter precision after the decimal point
        let after_decimal = if let Some(precision) = f.precision() {
            let len = after_decimal.len();
            if len < precision {
                after_decimal + "0".repeat(precision - len).as_str()
            } else {
                after_decimal[0..precision].to_string()
            }
        } else {
            after_decimal
        };

        // Always show the decimal point, even with ".0".
        let complete_without_sign = before_decimal + "." + after_decimal.as_str();
        // Padding works even though we have a decimal point.
        f.pad_integral(!self.atto().is_negative(), "", &complete_without_sign)
    }
}

impl Neg for TokenAmount {
    type Output = TokenAmount;

    #[inline]
    fn neg(self) -> TokenAmount {
        TokenAmount { atto: -self.atto }
    }
}

impl<'a> Neg for &'a TokenAmount {
    type Output = TokenAmount;

    #[inline]
    fn neg(self) -> TokenAmount {
        TokenAmount {
            atto: (&self.atto).neg(),
        }
    }
}

// Implements Add for all combinations of value/reference receiver and parameter.
// (Pattern copied from BigInt multiplication).
macro_rules! impl_add {
    ($(impl<$($a:lifetime),*> Add<$Other:ty> for $Self:ty;)*) => {$(
        impl<$($a),*> Add<$Other> for $Self {
            type Output = TokenAmount;

            #[inline]
            fn add(self, other: $Other) -> TokenAmount {
                // automatically match value/ref
                let TokenAmount { atto: x, .. } = self;
                let TokenAmount { atto: y, .. } = other;
                TokenAmount {atto: x + y}
            }
        }
    )*}
}
impl_add! {
    impl<> Add<TokenAmount> for TokenAmount;
    impl<'b> Add<&'b TokenAmount> for TokenAmount;
    impl<'a> Add<TokenAmount> for &'a TokenAmount;
    impl<'a, 'b> Add<&'b TokenAmount> for &'a TokenAmount;
}

impl AddAssign<TokenAmount> for TokenAmount {
    #[inline]
    fn add_assign(&mut self, other: TokenAmount) {
        self.atto += &other.atto;
    }
}

impl<'a> AddAssign<&'a TokenAmount> for TokenAmount {
    #[inline]
    fn add_assign(&mut self, other: &TokenAmount) {
        self.atto += &other.atto;
    }
}

// Implements Sub for all combinations of value/reference receiver and parameter.
macro_rules! impl_sub {
    ($(impl<$($a:lifetime),*> Sub<$Other:ty> for $Self:ty;)*) => {$(
        impl<$($a),*> Sub<$Other> for $Self {
            type Output = TokenAmount;

            #[inline]
            fn sub(self, other: $Other) -> TokenAmount {
                // automatically match value/ref
                let TokenAmount { atto: x, .. } = self;
                let TokenAmount { atto: y, .. } = other;
                TokenAmount {atto: x - y}
            }
        }
    )*}
}
impl_sub! {
    impl<> Sub<TokenAmount> for TokenAmount;
    impl<'b> Sub<&'b TokenAmount> for TokenAmount;
    impl<'a> Sub<TokenAmount> for &'a TokenAmount;
    impl<'a, 'b> Sub<&'b TokenAmount> for &'a TokenAmount;
}

impl SubAssign<TokenAmount> for TokenAmount {
    #[inline]
    fn sub_assign(&mut self, other: TokenAmount) {
        self.atto -= &other.atto;
    }
}

impl<'a> SubAssign<&'a TokenAmount> for TokenAmount {
    #[inline]
    fn sub_assign(&mut self, other: &TokenAmount) {
        self.atto -= &other.atto;
    }
}

impl<T> Mul<T> for TokenAmount
where
    BigInt: Mul<T, Output = BigInt>,
{
    type Output = TokenAmount;

    fn mul(self, rhs: T) -> Self::Output {
        TokenAmount {
            atto: self.atto * rhs,
        }
    }
}

impl<'a, T> Mul<T> for &'a TokenAmount
where
    &'a BigInt: Mul<T, Output = BigInt>,
{
    type Output = TokenAmount;

    fn mul(self, rhs: T) -> Self::Output {
        TokenAmount {
            atto: &self.atto * rhs,
        }
    }
}

macro_rules! impl_mul {
    ($(impl<$($a:lifetime),*> Mul<$Other:ty> for $Self:ty;)*) => {$(
        impl<$($a),*> Mul<$Other> for $Self {
            type Output = TokenAmount;

            #[inline]
            fn mul(self, other: $Other) -> TokenAmount {
                other * self
            }
        }
    )*}
}

macro_rules! impl_muls {
    ($($t:ty,)*) => {$(
        impl_mul! {
            impl<> Mul<TokenAmount> for $t;
            impl<'b> Mul<&'b TokenAmount> for $t;
            impl<'a> Mul<TokenAmount> for &'a $t;
            impl<'a, 'b> Mul<&'b TokenAmount> for &'a $t;
        }
    )*};
}

impl_muls! {
    u8, u16, u32, u64, u128,
    i8, i16, i32, i64, i128,
    BigInt,
}

impl<T> MulAssign<T> for TokenAmount
where
    BigInt: MulAssign<T>,
{
    #[inline]
    fn mul_assign(&mut self, other: T) {
        self.atto *= other;
    }
}

// Only a single div/rem method is implemented, rather than the full Div and Rem traits.
// Division isn't a common operation with money-like units, and deserves to be treated carefully.
impl TokenAmount {
    #[inline]
    pub fn div_rem(&self, other: impl Into<BigInt>) -> (TokenAmount, TokenAmount) {
        let (q, r) = self.atto.div_rem(&other.into());
        (TokenAmount { atto: q }, TokenAmount { atto: r })
    }

    #[inline]
    pub fn div_ceil(&self, other: impl Into<BigInt>) -> TokenAmount {
        TokenAmount {
            atto: self.atto.div_ceil(&other.into()),
        }
    }

    #[inline]
    pub fn div_floor(&self, other: impl Into<BigInt>) -> TokenAmount {
        TokenAmount {
            atto: self.atto.div_floor(&other.into()),
        }
    }
}

impl Sum for TokenAmount {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        Self::from_atto(iter.map(|t| t.atto).sum::<BigInt>())
    }
}

impl<'a> Sum<&'a TokenAmount> for TokenAmount {
    fn sum<I: Iterator<Item = &'a TokenAmount>>(iter: I) -> Self {
        Self::from_atto(iter.map(|t| &t.atto).sum::<BigInt>())
    }
}

// Serialisation

impl Serialize for TokenAmount {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        bigint_ser::serialize(&self.atto, serializer)
    }
}

impl<'de> Deserialize<'de> for TokenAmount {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        bigint_ser::deserialize(deserializer).map(|v| TokenAmount { atto: v })
    }
}

#[cfg(test)]
mod test {
    use num_bigint::BigInt;
    use num_traits::Zero;

    use crate::TokenAmount;

    fn whole(x: impl Into<BigInt>) -> TokenAmount {
        TokenAmount::from_whole(x)
    }

    fn atto(x: impl Into<BigInt>) -> TokenAmount {
        TokenAmount::from_atto(x.into())
    }

    #[test]
    fn display_basic() {
        fn basic(expected: &str, t: TokenAmount) {
            assert_eq!(expected, format!("{}", t));
        }

        basic("0.0", TokenAmount::zero());
        basic("0.000000000000000001", atto(1));
        basic("0.000000000000001", atto(1000));
        basic("0.1234", atto(123_400_000_000_000_000_u64));
        basic("0.10101", atto(101_010_000_000_000_000_u64));
        basic("1.0", whole(1));
        basic("1.0", atto(1_000_000_000_000_000_000_u128));
        basic("1.1", atto(1_100_000_000_000_000_000_u128));
        basic("1.000000000000000001", atto(1_000_000_000_000_000_001_u128));
        basic(
            "1234.000000000123456789",
            whole(1234) + atto(123_456_789_u64),
        );
    }

    #[test]
    fn display_precision() {
        assert_eq!("0.0", format!("{:.1}", TokenAmount::zero()));
        assert_eq!("0.000", format!("{:.3}", TokenAmount::zero()));
        assert_eq!("0.000", format!("{:.3}", atto(1))); // Truncated.
        assert_eq!(
            "0.123",
            format!("{:.3}", atto(123_456_789_000_000_000_u64)) // Truncated.
        );
        assert_eq!(
            "0.123456789000",
            format!("{:.12}", atto(123_456_789_000_000_000_u64))
        );
    }

    #[test]
    fn display_padding() {
        assert_eq!("0.0", format!("{:01}", TokenAmount::zero()));
        assert_eq!("0.0", format!("{:03}", TokenAmount::zero()));
        assert_eq!("000.0", format!("{:05}", TokenAmount::zero()));
        assert_eq!(
            "0.123",
            format!("{:01.3}", atto(123_456_789_000_000_000_u64))
        );
        assert_eq!(
            "00.123",
            format!("{:06.3}", atto(123_456_789_000_000_000_u64))
        );
    }

    #[test]
    fn display_negative() {
        assert_eq!("-0.000001", format!("{:01}", -TokenAmount::from_nano(1000)));
    }

    #[test]
    fn ops() {
        // Test the basic operations are wired up correctly.
        assert_eq!(atto(15), atto(10) + atto(5));
        assert_eq!(atto(3), atto(10) - atto(7));
        assert_eq!(atto(12), atto(3) * 4);
        let (q, r) = atto(14).div_rem(4);
        assert_eq!((atto(3), atto(2)), (q, r));

        let mut a = atto(1);
        a += atto(2);
        assert_eq!(atto(3), a);
        a *= 2;
        assert_eq!(atto(6), a);
        a -= atto(2);
        assert_eq!(atto(4), a);
    }

    #[test]
    fn nano_fil() {
        assert_eq!(
            TokenAmount::from_nano(1),
            TokenAmount::from_whole(1).div_floor(10u64.pow(9))
        )
    }

    #[test]
    fn test_mul() {
        let a = atto(2) * 3;
        let b = 3 * atto(2);
        assert_eq!(a, atto(6));
        assert_eq!(a, b);
    }

    #[test]
    fn test_sum() {
        assert_eq!(
            [1, 2, 3, 4].into_iter().map(atto).sum::<TokenAmount>(),
            atto(10)
        );
    }
}