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
//! Provides a number of data types, methods, and traits to have more
//! fine-grained and strongly-typed control of numeric data.
//!
//! # Base, quote, notional, and collateral
//!
//! In general markets, [a currency
//! pair](https://www.investopedia.com/terms/c/currencypair.asp) like `BTC/USD`
//! consists of a *base currency* (`BTC`) and a *quote currency* (`USD`). In our
//! platform, we talk about the *notional* and *collateral* assets, where the
//! collateral asset is what gets deposited in the platform and notional is
//! (generally) the risk asset being speculated on.
//!
//! Generally speaking, in most perpetual swaps platforms, the base and notional
//! assets are the same, and the quote and collateral assets are the same.
//! However, our platform supports a concept called *crypto denominated pairs*.
//! In these, we use the base/risk asset as collateral and quote is the
//! notional. This causes a cascade of changes around leverage and price
//! management.
//!
//! However, all those changes are _internal to the protocol_. The user-facing
//! API should almost exclusively care about base and quote (besides the fact
//! that the user will interact with the contracts by depositing and withdrawing
//! collateral assets). The purpose of this module is to provide data types that
//! provide safety and clarity around whether we're dealing with the base/quote
//! view of the world (user-facing) or notional/collateral (internal) view.
//!
//! # Decimal256, NonZero, Signed, and UnsignedDecimal
//!
//! Math generally uses [Decimal256](cosmwasm_std::Decimal256).
//! However, this type alone cannot express negative numbers, and we often want
//! to ensure additional constraints at compile time.
//!
//! A combination of traits and newtype wrappers gives us a robust framework:
//!
//! * `UnsignedDecimal`: a _trait_, not a concrete type, which is implemented
//! for `Collateral`, `Notional`, and several other numeric types.
//!
//! * `NonZero<T>`: a newtype wrapper which ensures that the value is not zero.
//! It's generally used for types where `T: UnsignedDecimal`.
//!
//! * `Signed<T>`: a newtype wrapper which allows for positive or negative
//! values. It's also generally used for types where `T: UnsignedDecimal`.
//!
//! Putting it all together, here are some examples. Note that these are merely
//! illustrative. Real-world problems would require a price point to convert
//! between Collateral and Notional:
//!
//! ### UnsignedDecimal
//!
//! `Collateral` implements `UnsignedDecimal`, and so we can add two `Collateral`
//! values together via `.checked_add()`.
//!
//! However, we cannot add a `Collateral` and some other `Decimal256`. Instead
//! we need to call `.into_decimal256()`, do our math with another `Decimal256`,
//! and then convert it to any `T: UnsignedDecimal` via `T::from_decimal256()`.
//!
//! *example*
//!
//! ```
//! use levana_perpswap_cosmos::number::*;
//! use cosmwasm_std::Decimal256;
//! use std::str::FromStr;
//!
//! let lhs:Collateral = "1.23".parse().unwrap();
//! let rhs:Decimal256 = "4.56".parse().unwrap();
//! let decimal_result = lhs.into_decimal256().checked_add(rhs).unwrap();
//! let output:Notional = Notional::from_decimal256(decimal_result);
//! ```
//!
//! ### NonZero
//!
//! `NonZero<Collateral>` allows us to call various `.checked_*` math methods
//! with another `NonZero<Collateral>`.
//!
//! However, if we want to do math with a different underlying type - we do need
//! to drop down to that common type. There's two approaches (both of which
//! return an Option, in case the resulting value is zero):
//!
//!   1. If the inner NonZero type stays the same (i.e. it's all `Collateral`)
//!     then call `.raw()` to get the inner type, do your math, and then convert
//!     back to the NonZero wrapper via `NonZero::new()`
//!   2. If you need a `Decimal256`, then call `.into_decimal256()` to get the
//!     underlying `Decimal256` type, do your math, and then convert back to
//!     `NonZero<T>` via `NonZero::new(T::from_decimal256(value))`. This is
//!     usually the case when the type of `T` has changed
//!     (i.e. from `Collateral` to `Notional`)
//!
//! *example 1*
//!
//! ```
//! use levana_perpswap_cosmos::number::*;
//! use cosmwasm_std::Decimal256;
//! use std::str::FromStr;
//!
//! let lhs:NonZero<Collateral> = "1.23".parse().unwrap();
//! let rhs:Collateral = "4.56".parse().unwrap();
//! let collateral_result = lhs.raw().checked_add(rhs).unwrap();
//! let output:NonZero<Collateral> = NonZero::new(collateral_result).unwrap();
//!
//! ```
//!
//! *example 2*
//!
//! ```
//! use levana_perpswap_cosmos::number::*;
//! use cosmwasm_std::Decimal256;
//! use std::str::FromStr;
//!
//! let lhs:NonZero<Collateral> = "1.23".parse().unwrap();
//! let rhs:Decimal256 = "4.56".parse().unwrap();
//! let decimal_result = lhs.into_decimal256().checked_add(rhs).unwrap();
//! let notional_result = Notional::from_decimal256(decimal_result);
//! let output:NonZero<Notional> = NonZero::new(notional_result).unwrap();
//!
//! ```
//! ### Signed
//!
//! `Signed<Collateral>` also allows us to call various `.checked_*` math methods
//! when the inner type is the same. However, there are some differences when
//! comparing to the `NonZero` methods:
//!
//!   1. To get the underlying `T`, call `.abs_unsigned()` instead of `.raw()`.
//!   The sign is now lost, it's not a pure raw conversion.
//!
//!   2. To get back from the underlying `T`, call `T::into_signed()`
//!
//!   3. There is no direct conversion to `Decimal256`.
//!
//!   4. There are helpers for the ubiquitous use-case of `Signed<Decimal256>`
//!   This is such a common occurance, it has its own type alias: `Number`.
//!
//! *example 1*
//!
//! ```
//! use levana_perpswap_cosmos::number::*;
//! use cosmwasm_std::Decimal256;
//! use std::str::FromStr;
//!
//! let lhs:Signed<Collateral> = "-1.23".parse().unwrap();
//! let rhs:Decimal256 = "4.56".parse().unwrap();
//! let decimal_result = lhs.abs_unsigned().into_decimal256().checked_mul(rhs).unwrap();
//! let notional_result = Notional::from_decimal256(decimal_result);
//! // bring back our negative sign
//! let output:Signed<Notional> = -notional_result.into_signed();
//! ```
//!
//! *example 2*
//! ```
//! use levana_perpswap_cosmos::number::*;
//! use cosmwasm_std::Decimal256;
//! use std::str::FromStr;
//!
//! let lhs:Signed<Collateral> = "-1.23".parse().unwrap();
//! let rhs:Number = "4.56".parse().unwrap();
//! let number_result = lhs.into_number().checked_mul(rhs).unwrap();
//! let output:Signed<Notional> = Signed::<Notional>::from_number(number_result);
//! ```

mod convert;
mod ops;
mod serialize;
use schemars::schema::{InstanceType, Metadata, SchemaObject};
use schemars::JsonSchema;
mod nonzero;
pub use self::types::*;

pub mod ratio;
mod types;

// schemars could not figure out that it is serialized as a string
// so gotta impl it manually
impl<T: UnsignedDecimal> JsonSchema for Signed<T> {
    fn schema_name() -> String {
        "Signed decimal".to_owned()
    }

    fn is_referenceable() -> bool {
        false
    }

    fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let mut obj = SchemaObject {
            instance_type: Some(InstanceType::String.into()),
            ..Default::default()
        };

        let mut meta = match obj.metadata {
            None => Box::<Metadata>::default(),
            Some(m) => m,
        };

        // would be nice to re-use the doc comments above...
        meta.description = Some(
            r#"
            A signed number type with high fidelity.
            Similar in spirit to cosmwasm_bignumber::Decimal256 - it is
            a more ergonomic wrapper around cosmwasm-std by making more things public
            but we also add negative values and other methods as-needed
        "#
            .to_string(),
        );

        obj.metadata = Some(meta);

        obj.into()
    }
}

impl<T: UnsignedDecimal> Signed<T> {
    /// absolute value
    pub fn abs(self) -> Self {
        Self::new_positive(self.value())
    }

    /// Absolute value, but return the `T` underlying type directly
    pub fn abs_unsigned(self) -> T {
        self.value()
    }

    /// Checks if this number is greater than 0.
    pub fn is_strictly_positive(&self) -> bool {
        !self.is_zero() && !self.is_negative()
    }

    /// Checks if this number is greater than or equal to 0.
    pub fn is_positive_or_zero(&self) -> bool {
        !self.is_negative()
    }

    /// Is the value 0?
    pub fn is_zero(&self) -> bool {
        self.value().is_zero()
    }

    /// Apply a function to the inner value and rewrap.
    ///
    /// This will keep the current sign (positive or negative) in place,
    /// respecting invariants that a value of 0 must have negative set to false.
    pub fn map<U: UnsignedDecimal, F: FnOnce(T) -> U>(self, f: F) -> Signed<U> {
        let value = f(self.value());
        if self.is_negative() {
            Signed::new_negative(value)
        } else {
            Signed::new_positive(value)
        }
    }

    /// Like `map` but may fail
    pub fn try_map<E, U: UnsignedDecimal, F: FnOnce(T) -> Result<U, E>>(
        self,
        f: F,
    ) -> Result<Signed<U>, E> {
        f(self.value()).map(|value| {
            if self.is_negative() {
                Signed::new_negative(value)
            } else {
                Signed::new_positive(value)
            }
        })
    }
}

#[cfg(test)]
mod test {
    use super::Number;
    use std::str::FromStr;

    #[test]
    fn number_default() {
        assert_eq!(Number::ZERO, Number::default());
    }

    #[test]
    fn number_serde() {
        let a = Number::from(300u64);
        let b = Number::from(7u64);
        let res = (a / b).unwrap();

        assert_eq!(serde_json::to_value(res).unwrap(), "42.857142857142857142");
        assert_eq!(
            serde_json::from_str::<Number>("\"42.857142857142857142\"").unwrap(),
            res
        );

        let res = -res;

        assert_eq!(serde_json::to_value(res).unwrap(), "-42.857142857142857142");
        assert_eq!(
            serde_json::from_str::<Number>("\"-42.857142857142857142\"").unwrap(),
            res
        );
    }

    #[test]
    fn number_arithmetic() {
        let a = Number::from(300u64);
        let b = Number::from(7u64);

        assert_eq!((a + b).unwrap().to_string(), "307");
        assert_eq!((a - b).unwrap().to_string(), "293");
        assert_eq!((b - a).unwrap().to_string(), "-293");
        assert_eq!((a * b).unwrap().to_string(), "2100");
        assert_eq!((a / b).unwrap().to_string(), "42.857142857142857142");

        let a = -a;
        let b = -b;
        assert_eq!((a + b).unwrap().to_string(), "-307");
        assert_eq!((a - b).unwrap().to_string(), "-293");
        assert_eq!((b - a).unwrap().to_string(), "293");
        assert_eq!((a * b).unwrap().to_string(), "2100");
        assert_eq!((a / b).unwrap().to_string(), "42.857142857142857142");

        let a = -a;
        assert_eq!((a + b).unwrap().to_string(), "293");
        assert_eq!((a - b).unwrap().to_string(), "307");
        assert_eq!((b - a).unwrap().to_string(), "-307");
        assert_eq!((a * b).unwrap().to_string(), "-2100");
        assert_eq!((a / b).unwrap().to_string(), "-42.857142857142857142");
    }

    #[test]
    fn number_cmp() {
        let a = Number::from_str("4.2").unwrap();
        let b = Number::from_str("0.007").unwrap();

        assert!(a > b);
        assert!(a.approx_gt_strict(b).unwrap());
        assert!(a.approx_gt_relaxed(b).unwrap());
        assert!(a != b);

        let a = Number::from_str("4.2").unwrap();
        let b = Number::from_str("4.2").unwrap();

        assert!(a <= b);
        assert!(a >= b);
        assert!(a.approx_eq(b).unwrap());
        assert!(a == b);

        let a = Number::from_str("4.2").unwrap();
        let b = Number::from_str("-4.2").unwrap();

        assert!(a > b);
        assert!(a.approx_gt_strict(b).unwrap());
        assert!(a.approx_gt_relaxed(b).unwrap());
        assert!(a != b);

        let a = Number::from_str("-4.2").unwrap();
        let b = Number::from_str("4.2").unwrap();

        assert!(a < b);
        assert!(a.approx_lt_relaxed(b).unwrap());
        assert!(a != b);

        let a = Number::from_str("-4.5").unwrap();
        let b = Number::from_str("-4.2").unwrap();

        assert!(a < b);
        assert!(a.approx_lt_relaxed(b).unwrap());
        assert!(a != b);

        let a = Number::from_str("-4.2").unwrap();
        let b = Number::from_str("-4.5").unwrap();

        assert!(a > b);
        assert!(a.approx_gt_strict(b).unwrap());
        assert!(a.approx_gt_relaxed(b).unwrap());
        assert!(a != b);
    }

    #[test]
    fn unsigned_key_bytes() {
        let a = Number::from_str("0.9")
            .unwrap()
            .to_unsigned_key_bytes()
            .unwrap();
        let b = Number::from_str("1.0")
            .unwrap()
            .to_unsigned_key_bytes()
            .unwrap();
        let c = Number::from_str("1.9")
            .unwrap()
            .to_unsigned_key_bytes()
            .unwrap();
        let d = Number::from_str("9.0")
            .unwrap()
            .to_unsigned_key_bytes()
            .unwrap();
        let e = Number::from_str("9.1")
            .unwrap()
            .to_unsigned_key_bytes()
            .unwrap();
        assert!(a < b);
        assert!(b < c);
        assert!(c < d);
        assert!(d < e);

        assert!(Number::from_str("-1.0")
            .unwrap()
            .to_unsigned_key_bytes()
            .is_none());
    }

    #[test]
    fn zero_str() {
        let mut a = Number::from_str("0").unwrap();
        a = -a;
        assert_eq!(a.to_string(), "0");

        let a = Number::from_str("-0").unwrap();
        assert_eq!(a.to_string(), "0");
    }

    #[test]
    fn number_u128_with_precision() {
        let _a = Number::from_str("270.15").unwrap();
        let b = Number::from_str("1.000000001").unwrap();
        let c = Number::from(u128::MAX);

        // Typcial use - we will send this number in a BankMsg normally
        assert_eq!(_a.to_u128_with_precision(6).unwrap(), 270_150_000);

        // Demonstrate inherent lossy-ness of doing Number -> u128
        assert_eq!(b.to_u128_with_precision(6).unwrap(), 1_000_000);
        assert_eq!(b.to_u128_with_precision(9).unwrap(), 1_000_000_001);

        // Try 6-decimal precision on a number that would overflow
        assert_eq!(c.to_u128_with_precision(6), None);

        // Try 0-decimal precision on the largest number we can handle
        assert_eq!(c.to_u128_with_precision(0).unwrap(), u128::MAX);
    }

    #[test]
    fn catch_overflow() {
        match Number::MAX.checked_mul(Number::MAX) {
            Ok(_) => {
                panic!("should overflow!");
            }
            Err(e) => {
                if !e.to_string().contains("Overflow") {
                    panic!("wrong error! (got {e})");
                }
            }
        }
    }

    #[test]
    fn basic_multiplication() {
        let num = Number::from_str("1.1").unwrap();
        let twopointtwo = (num * 2u64).unwrap();
        assert_eq!(twopointtwo, Number::from_str("2.2").unwrap());
    }
}