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
use resources::{Amount, AssetIdentifier};
use std::fmt;

/// The ratio between the asking and selling price
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
pub struct PriceRatio {
    #[serde(rename = "n")]
    numerator: u64,
    #[serde(rename = "d")]
    denominator: u64,
}

impl fmt::Display for PriceRatio {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "n: {}, d: {}", self.numerator(), self.denominator())
    }
}

impl PriceRatio {
    /// Constructs a new price ratio struct
    pub fn new(numerator: u64, denominator: u64) -> Self {
        Self {
            numerator,
            denominator,
        }
    }

    /// The numerator
    pub fn numerator(&self) -> u64 {
        self.numerator
    }

    /// The denominator
    pub fn denominator(&self) -> u64 {
        self.denominator
    }
}

/// Summary of an offer to be shown in an orderbook
#[derive(Deserialize, Debug)]
pub struct OfferSummary {
    amount: Amount,
    #[serde(rename = "price_r")]
    price_ratio: PriceRatio,
    price: Amount,
}

impl OfferSummary {
    /// Returns the amount of an asset the offer is willing to buy or sell
    pub fn amount(&self) -> Amount {
        self.amount
    }

    /// Returns the amount of an asset the offer is willing to buy or sell
    pub fn price_ratio(&self) -> PriceRatio {
        self.price_ratio
    }

    /// Returns the price for an asset the offer is willing to buy or sell at
    pub fn price(&self) -> Amount {
        self.price
    }
}

#[cfg(test)]
mod offer_summary_tests {
    use super::*;
    use serde_json;

    fn offer_summary_json() -> &'static str {
        include_str!("../../fixtures/offer_summary.json")
    }

    #[test]
    fn it_parses_an_offer_summary_from_json() {
        let offer_summary: OfferSummary = serde_json::from_str(&offer_summary_json()).unwrap();
        assert_eq!(offer_summary.price_ratio().numerator, 100000000);
        assert_eq!(offer_summary.price(), Amount::new(77200005));
        assert_eq!(offer_summary.amount(), Amount::new(120000000));
    }
}

/// An offer being made for particular assets at a particular exchange rate.
#[derive(Deserialize, Debug, Clone)]
pub struct Offer {
    id: i64,
    paging_token: String,
    seller: String,
    selling: AssetIdentifier,
    buying: AssetIdentifier,
    amount: Amount,
    #[serde(rename = "price_r")]
    price_ratio: PriceRatio,
    price: Amount,
}

impl Offer {
    /// The id of the offer
    pub fn id(&self) -> i64 {
        self.id
    }

    /// A paging_token suitable for use as a cursor parameter.
    pub fn paging_token(&self) -> &str {
        &self.paging_token
    }

    /// The account id fo the account making this offer.
    pub fn seller(&self) -> &str {
        &self.seller
    }

    /// The asset being sold
    pub fn selling(&self) -> &AssetIdentifier {
        &self.selling
    }

    /// The asset being bought
    pub fn buying(&self) -> &AssetIdentifier {
        &self.buying
    }

    /// Returns the numerator and denominator representing the buy and sell
    /// prices of the currencies on offer.
    pub fn price_ratio(&self) -> (u64, u64) {
        (self.price_ratio.numerator, self.price_ratio.denominator)
    }

    /// The amount of the `selling` asset willing to be sold
    pub fn amount(&self) -> Amount {
        self.amount
    }

    /// How many units of the `buying` asset it takes to get 10 million of `selling`
    /// asset. This is the smallest divisible unit of the asset.
    pub fn price(&self) -> Amount {
        self.price
    }
}

#[cfg(test)]
mod offer_tests {
    use super::*;
    use serde_json;

    fn offer_json() -> &'static str {
        include_str!("../../fixtures/offer.json")
    }

    #[test]
    fn it_parses_an_offer_from_json() {
        let offer: Offer = serde_json::from_str(&offer_json()).unwrap();
        assert_eq!(offer.id(), 121);
        assert_eq!(offer.paging_token(), "121");
        assert_eq!(offer.selling().code(), "BAR");
        assert_eq!(offer.buying().code(), "FOO");
        assert_eq!(offer.price_ratio(), (387, 50));
        assert_eq!(offer.amount(), Amount::new(236_692_509));
        assert_eq!(offer.price(), Amount::new(77_400_000));
    }
}