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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Types used for protocol request inputs (arguments) and outputs. Types
//! specific to a single protocol request will live within the respetitive
//! module. For example `protocol::place_order`.

use crate::errors::{ProtocolError, Result};
use bigdecimal::BigDecimal;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::blockchain::bigdecimal_to_nash_prec;
use lazy_static::lazy_static;

/// Representation of blockchains to help navigate encoding issues

#[derive(Clone, Debug, Copy, PartialEq, Hash, Eq)]
pub enum Blockchain {
    NEO,
    Ethereum,
    Bitcoin,
}

lazy_static! {
    static ref BLOCKCHAINS: Vec<Blockchain> = {
        vec![Blockchain::Bitcoin, Blockchain::Ethereum, Blockchain::NEO]
    };
}

impl Blockchain {
    pub fn all() -> &'static Vec<Blockchain> {
        &BLOCKCHAINS
    }
}

/// Assets are the units of value that can be traded in a market
/// We also use this type as a root for encodings for smart contract
/// operations defined in super::sc_payloads
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Asset {
    ETH,
    BAT,
    OMG,
    USDC,
    USDT,
    ZRX,
    LINK,
    QNT,
    RLC,
    ANT,
    BTC,
    NEO,
    GAS,
    TRAC,
    GUNTHY,
    NNN,
    NOIA
}

impl Asset {
    /// Get blockchain associated with a specific asset
    pub fn blockchain(&self) -> Blockchain {
        match self {
            Self::ETH => Blockchain::Ethereum,
            Self::USDC => Blockchain::Ethereum,
            Self::USDT => Blockchain::Ethereum,
            Self::BAT => Blockchain::Ethereum,
            Self::OMG => Blockchain::Ethereum,
            Self::ZRX => Blockchain::Ethereum,
            Self::LINK => Blockchain::Ethereum,
            Self::QNT => Blockchain::Ethereum,
            Self::RLC => Blockchain::Ethereum,
            Self::ANT => Blockchain::Ethereum,
            Self::TRAC => Blockchain::Ethereum,
            Self::GUNTHY => Blockchain::Ethereum,
            Self::BTC => Blockchain::Bitcoin,
            Self::NEO => Blockchain::NEO,
            Self::GAS => Blockchain::NEO,
            Self::NNN => Blockchain::NEO,
            Self::NOIA => Blockchain::Ethereum
        }
    }

    /// Get asset name as an `&str`. This name will be compatible with the
    /// what Nash's GraphQL protocol wants
    // FIXME: This can probably be more cleverly automated
    pub fn name(&self) -> &'static str {
        match self {
            Self::ETH => "eth",
            Self::USDC => "usdc",
            Self::USDT => "usdt",
            Self::BAT => "bat",
            Self::OMG => "omg",
            Self::ZRX => "zrx",
            Self::LINK => "link",
            Self::QNT => "qnt",
            Self::RLC => "rlc",
            Self::ANT => "ant",
            Self::BTC => "btc",
            Self::NEO => "neo",
            Self::GAS => "gas",
            Self::TRAC => "trac",
            Self::GUNTHY => "gunthy",
            Self::NNN => "nnn",
            Self::NOIA => "noia"
        }
    }

    pub fn from_str(asset_str: &str) -> Result<Self> {
        match asset_str {
            "eth" => Ok(Self::ETH),
            "usdc" => Ok(Self::USDC),
            "usdt" => Ok(Self::USDT),
            "bat" => Ok(Self::BAT),
            "omg" => Ok(Self::OMG),
            "zrx" => Ok(Self::ZRX),
            "link" => Ok(Self::LINK),
            "qnt" => Ok(Self::QNT),
            "rlc" => Ok(Self::RLC),
            "ant" => Ok(Self::ANT),
            "btc" => Ok(Self::BTC),
            "neo" => Ok(Self::NEO),
            "gas" => Ok(Self::GAS),
            "trac" => Ok(Self::TRAC),
            "gunthy" => Ok(Self::GUNTHY),
            "nnn" => Ok(Self::NNN),
            "noia" => Ok(Self::NOIA),
            _ => Err(ProtocolError("Asset not known")),
        }
    }

    // FIXME: can this be more cleverly automated?
    /// Return list of all supported `Asset` types
    pub fn assets() -> Vec<Self> {
        vec![
            Self::ETH,
            Self::USDC,
            Self::USDT,
            Self::BAT,
            Self::OMG,
            Self::ZRX,
            Self::LINK,
            Self::QNT,
            Self::ANT,
            Self::BTC,
            Self::NEO,
            Self::GAS,
            Self::TRAC,
            Self::GUNTHY,
            Self::NNN,
        ]
    }
}

/// Assets can potentially have different precisions across markets.
/// This keeps track of what precision we are dealing with
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AssetofPrecision {
    pub asset: Asset,
    pub precision: u32,
}

/// Convert AssetOfPrecision back into asset, e.g., for encoding
impl Into<Asset> for AssetofPrecision {
    fn into(self) -> Asset {
        self.asset
    }
}

impl AssetofPrecision {
    /// Starting with an asset of some precision, create a new asset that holds
    /// a specific amount of value
    pub fn with_amount(&self, amount_str: &str) -> Result<AssetAmount> {
        let amount = Amount::new(amount_str, self.precision)?;
        Ok(AssetAmount {
            asset: *self,
            amount,
        })
    }
}

// Extending Asset here with helper to convert with precision
impl Asset {
    /// Starting with an asset, create a new asset of desired precision.
    pub fn with_precision(&self, precision: u32) -> AssetofPrecision {
        AssetofPrecision {
            asset: *self,
            precision,
        }
    }
}

/// A specific amount of an asset being traded
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssetAmount {
    pub asset: AssetofPrecision,
    pub amount: Amount,
}

impl AssetAmount {
    /// Create a new amount based on an exchange of `self` into `into_asset` at `rate`
    pub fn exchange_at(&self, rate: &Rate, into_asset: AssetofPrecision) -> Result<AssetAmount> {
        let new_amount = self.amount.to_bigdecimal() * rate.to_bigdecimal()?;
        Ok(AssetAmount {
            asset: into_asset,
            amount: Amount::from_bigdecimal(new_amount, into_asset.precision),
        })
    }
}

/// This type encodes all the information necessary for a client operating
/// over the protocol to understand a market.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Market {
    // FIXME: probably more things here?
    pub asset_a: AssetofPrecision,
    pub asset_b: AssetofPrecision,
    pub min_trade_size_a: AssetAmount,
    pub min_trade_size_b: AssetAmount,
}

impl Market {
    /// Create a new market from assets with precision
    /// ```
    /// use nash_protocol::types::{Market, Asset};
    /// Market::new(Asset::ETH.with_precision(4), Asset::USDC.with_precision(2));
    /// ```
    pub fn new(
        asset_a: AssetofPrecision,
        asset_b: AssetofPrecision,
        min_trade_size_a: AssetAmount,
        min_trade_size_b: AssetAmount
    ) -> Self {
        Self {
            asset_a,
            asset_b,
            min_trade_size_a,
            min_trade_size_b
        }
    }

    /// Return name of A/B market as "A_B"
    pub fn market_name(&self) -> String {
        format!(
            "{}_{}",
            self.asset_a.asset.name(),
            self.asset_b.asset.name()
        )
    }

    /// Get list of blockchains associated with this market. There will always be
    /// either one or two blockchains associated with a market.
    pub fn blockchains(&self) -> Vec<Blockchain> {
        let chain_a = self.asset_a.asset.blockchain();
        let chain_b = self.asset_b.asset.blockchain();
        if chain_a == chain_b {
            vec![chain_a]
        } else {
            vec![chain_a, chain_b]
        }
    }

    /// Get market asset by string name
    pub fn get_asset(&self, asset_name: &str) -> Result<AssetofPrecision> {
        if asset_name == self.asset_a.asset.name() {
            Ok(self.asset_a.clone())
        } else if asset_name == self.asset_b.asset.name() {
            Ok(self.asset_b.clone())
        } else {
            Err(ProtocolError("Asset not associated with market"))
        }
    }

    pub fn invert(&self) -> Market {
        Market::new(
            self.asset_b.clone(),
            self.asset_a.clone(),
            self.min_trade_size_b.clone(),
            self.min_trade_size_a.clone(),
        )
    }
}

/// Buy or sell type for Nash protocol. We don't use the one generated automatically
/// from the GraphQL schema as it does not implement necessary traits like Clone
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum BuyOrSell {
    Buy,
    Sell,
}

/// Type of order execution in Nash ME
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderType {
    Market,
    Limit,
    StopMarket,
    StopLimit,
}

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

/// The Rate enum describes behavior common to rates/prices.

#[derive(Clone, Debug, PartialEq)]
pub enum Rate {
    OrderRate(OrderRate),
    MaxOrderRate,
    MinOrderRate,
    FeeRate(FeeRate),
    MaxFeeRate,
    MinFeeRate,
}

/// Enum wrappers are kind of annoying. This allows us to use .into()
/// on a value to generate a wrapped value. For example:
/// `OrderRate::new("1").unwrap().into() => Rate::OrderRate(...)`

impl From<OrderRate> for Rate {
    fn from(rate: OrderRate) -> Self {
        Self::OrderRate(rate)
    }
}

impl Rate {
    /// Return new bigdecimal inner value based on Rate
    pub fn to_bigdecimal(&self) -> Result<BigDecimal> {
        let num = match self {
            Self::FeeRate(rate) | Self::OrderRate(rate) => rate.inner.clone(),
            // FIXME: this could be wrong
            Self::MaxOrderRate | Self::MaxFeeRate => {
                // FIXME: be bytes could be wrong for NEO
                BigDecimal::from_str("0.0025").unwrap()
            }
            Self::MinOrderRate | Self::MinFeeRate => 0.into(),
        };
        Ok(num)
    }

    /// Round in the specified precision.
    pub fn round(&self, precision: i64) -> Result<Self> {
        match self {
            Self::OrderRate(rate) => Ok(Self::OrderRate(rate.round(precision))),
            _ => Err(ProtocolError(
                "Cannot round a Rate that is not an OrderRate"
            ))
        }
    }

    pub fn invert_rate(&self, precision: Option<u32>) -> Result<Self> {
        match self {
            Self::OrderRate(rate) => Ok(Self::OrderRate(rate.invert_rate(precision))),
            _ => Err(ProtocolError(
                "Cannot invert a Rate that is not an OrderRate",
            )),
        }
    }

    /// Subtract fee from user by adjusting the order rate downwards
    pub fn subtract_fee(&self, fee: BigDecimal) -> Result<OrderRate> {
        let as_order_rate = OrderRate {
            inner: self.to_bigdecimal()?,
        };
        Ok(as_order_rate.subtract_fee(fee))
    }
}

/// Order rates impose limits on what trades the smart contract is allowed to
/// execute. For example, the smart contract will reject a payload that requests
/// a less favorable amount of asset_to than is imposed by the minimum order rate.
/// For Sell orders, an order rate represented by currency B an in A/B market. For
/// Buy orders, an order rate is represented in the protocol by currency A. Note
/// that OrderRates are always created w.r.t. currency B, then potentially inverted
/// during payload creation. This is because, unlike the smart contract, the ME
/// always wants order prices expressed in currency B. In either case, these rates
/// are encoded as 64 bit integers which take the initial price ratio, multiply
/// by 10^8, and then drop all precision after the decimal.

#[derive(Clone, Debug, PartialEq)]
pub struct OrderRate {
    inner: BigDecimal
}

impl OrderRate {
    /// Construct a new OrderRate from a numerical string
    pub fn new(str_num: &str) -> Result<Self> {
        BigDecimal::from_str(str_num)
            .map_err(|_| ProtocolError("String to BigDecimal failed in creating OrderRate"))
            .map(|inner| Self { inner })
    }

    /// Create new OrderRate from bigdecimal
    pub fn from_bigdecimal(decimal: BigDecimal) -> Self {
        Self { inner: decimal }
    }

    /// Round the price in the specified precision.
    pub fn round(&self, precision: i64) -> Self {
        let inner = self.inner.round(precision);
        Self { inner }
    }

    /// Invert the price to units of the other market pair. For example, if price is
    /// in terms of ETH in an ETH/USDC market, this will convert it to terms of USDC
    pub fn invert_rate(&self, precision: Option<u32>) -> Self {
        let mut inverse = self.inner.inverse();
        if let Some(precision) = precision {
            let scale_num = BigDecimal::from(u64::pow(10, precision));
            inverse = (&self.inner * &scale_num).with_scale(0) / scale_num;
        }
        Self { inner: inverse }
    }

    /// Return a new `BigDecimal` based on `OrderRate`
    pub fn to_bigdecimal(&self) -> BigDecimal {
        self.inner.clone()
    }

    /// Subtract fee from user by adjusting the order rate downwards. This will keep track of as
    /// much precision as BigDecimal is capable of. However, this method is exclusively used by
    /// the smart contract and will be reduced to an integer in scale of 10^8 before encoding
    pub fn subtract_fee(&self, fee: BigDecimal) -> Self {
        let fee_multiplier = BigDecimal::from(1) - fee;
        let inner = &self.inner * &fee_multiplier;
        OrderRate { inner }
    }
}

type FeeRate = OrderRate;

/// Amount encodes the amount of asset being bought or sold in an order
/// It is encoded with a precision that depends on the market and asset
/// being traded. For example, in the ETH/USD market, ETH has a precision
/// of 4. In an A/B market, amount is always in units of A.

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Amount {
    pub precision: u32,
    pub value: BigDecimal,
}

impl Amount {
    /// Construct a new Amount from string and precision
    pub fn new(str_num: &str, precision: u32) -> Result<Self> {
        let value = BigDecimal::from_str(str_num)
            .map_err(|_| ProtocolError("String to BigDecimal failed in creating Amount"))?;
        let adjust_precision = bigdecimal_to_nash_prec(&value, precision);
        Ok(Self { value: adjust_precision, precision })
    }

    pub fn from_bigdecimal(value: BigDecimal, precision: u32) -> Self {
        Self { value, precision }
    }

    // FIXME: this is a helper for rate conversion. Can be improved
    pub fn to_bigdecimal(&self) -> BigDecimal {
        self.value.clone()
    }
}

/// Nonces are 32 bit integers. They increment over time such that data
/// with lower nonces that has already been observed are rejected by the
/// matching engine and smart contract.

#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
pub enum Nonce {
    Value(u32),
    Crosschain,
}

impl Nonce {
    pub fn crosschain() -> u32 {
        0xffff_ffff
    }
}

// GraphQL wants this as i64
impl Into<i64> for Nonce {
    fn into(self) -> i64 {
        match self {
            Self::Value(value) => value as i64,
            Self::Crosschain => Nonce::crosschain() as i64,
        }
    }
}

impl Into<u32> for Nonce {
    fn into(self) -> u32 {
        match self {
            Self::Value(value) => value as u32,
            Self::Crosschain => Nonce::crosschain() as u32,
        }
    }
}

impl From<u32> for Nonce {
    fn from(val: u32) -> Self {
        if val == Nonce::crosschain() {
            Self::Crosschain
        } else {
            Self::Value(val)
        }
    }
}

impl From<&u32> for Nonce {
    fn from(val: &u32) -> Self {
        if val == &Nonce::crosschain() {
            Self::Crosschain
        } else {
            Self::Value(*val)
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum CandleInterval {
    FifteenMinute,
    FiveMinute,
    FourHour,
    OneDay,
    OneHour,
    OneMinute,
    OneMonth,
    OneWeek,
    SixHour,
    ThirtyMinute,
    ThreeHour,
    TwelveHour,
}

#[derive(Debug)]
pub struct Candle {
    pub a_volume: BigDecimal,
    pub b_volume: BigDecimal,
    pub close_price: BigDecimal,
    pub high_price: BigDecimal,
    pub low_price: BigDecimal,
    pub open_price: BigDecimal,
    pub interval: CandleInterval,
    pub interval_start: DateTime<Utc>,
}
#[derive(Clone, Copy, Debug)]
pub struct DateTimeRange {
    pub start: DateTime<Utc>,
    pub stop: DateTime<Utc>,
}

/// Status of an order on Nash
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderStatus {
    Pending,
    Open,
    Filled,
    Canceled,
}

/// Relation of an account to a trade, whether maker, taker, or not related (none)
#[derive(Clone, Debug, PartialEq)]
pub enum AccountTradeSide {
    Maker,
    Taker,
    // account played no role in this trade
    None,
}

#[derive(Clone, Debug)]
pub struct Trade {
    pub id: String,
    pub taker_order_id: String,
    pub maker_order_id: String,
    pub amount: BigDecimal,
    pub executed_at: DateTime<Utc>,
    pub account_side: AccountTradeSide,
    pub maker_fee: BigDecimal,
    pub taker_fee: BigDecimal,
    pub maker_recieved: BigDecimal,
    pub taker_recieved: BigDecimal,
    pub market: String,
    pub direction: BuyOrSell,
    pub limit_price: BigDecimal,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum OrderCancellationPolicy {
    FillOrKill,
    GoodTilCancelled,
    GoodTilTime(DateTime<Utc>),
    ImmediateOrCancel,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum OrderCancellationReason {
    AdminCancelled,
    Expiration,
    InvalidForOrderbookState,
    NoFill,
    User,
}

#[derive(Clone, Debug)]
pub struct Order {
    pub id: String,
    pub client_order_id: Option<String>,
    // Amount the order was placed for
    pub amount_placed: BigDecimal,
    // Amount remaining in order
    pub amount_remaining: BigDecimal,
    // Amount executed in order
    pub amount_executed: BigDecimal,
    pub limit_price: Option<BigDecimal>,
    pub stop_price: Option<BigDecimal>,
    pub placed_at: DateTime<Utc>,
    pub buy_or_sell: BuyOrSell,
    pub cancellation_policy: Option<OrderCancellationPolicy>,
    pub cancellation_reason: Option<OrderCancellationReason>,
    pub market: String,
    pub order_type: OrderType,
    pub status: OrderStatus,
    pub trades: Vec<Trade>,
}

/// Compressed representation for Order as returned by Orderbook queries and subscriptions
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrderbookOrder {
    pub price: String,
    pub amount: BigDecimal,
}

#[cfg(test)]
mod tests {
    use super::{BigDecimal, FromStr, OrderRate};
    use std::convert::TryInto;

    #[test]
    fn fee_rate_conversion_precision() {
        let rate = OrderRate::new("150").unwrap();
        let inverted_rate = rate.invert_rate(None);
        let minus_fee = inverted_rate.subtract_fee(BigDecimal::from_str("0.0025").unwrap());
        let payload = minus_fee.to_be_bytes(8).unwrap();
        assert_eq!(665000, u64::from_be_bytes(payload.try_into().unwrap()));
    }

    #[test]
    fn round() {
        let n = OrderRate::new("26.249999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996325").expect("Couldn't create OrderRate.");
        assert_eq!(n.round(3), OrderRate::new("26.25").unwrap());
        assert_eq!(n.round(2), OrderRate::new("26.25").unwrap());
        assert_eq!(n.round(1), OrderRate::new("26.2").unwrap());
        assert_eq!(n.round(0), OrderRate::new("26.0").unwrap());
        let n = OrderRate::new("14.45652173").unwrap();
        assert_eq!(n.round(7), OrderRate::new("14.4565217").unwrap());
        assert_eq!(n.round(6), OrderRate::new("14.456522").unwrap());
        assert_eq!(n.round(0), OrderRate::new("14.0").unwrap());
    }
}