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
use serde::Deserialize;
use std::future::Future;

use rust_decimal::Decimal;

use crate::{client, LimitOrderType, TradingPair};

/// Represents a trade made on the exchange.
#[derive(Debug, Deserialize)]
pub struct Trade {
    pub base: Decimal,
    pub counter: Decimal,
    pub fee_base: Decimal,
    pub is_buy: bool,
    pub order_id: String,
    pub pair: TradingPair,
    pub price: Decimal,
    pub timestamp: u64,
    #[serde(alias = "type")]
    pub order_type: LimitOrderType,
    pub volume: Decimal,
}

/// Contains a list of trades.
#[derive(Debug, Deserialize)]
pub struct TradeList {
    pub trades: Option<Vec<Trade>>,
}

/// A builder for the `list_trades()` method.
pub struct ListTradesBuilder<'a> {
    pub(crate) limit: Option<u64>,
    pub(crate) since: Option<u64>,
    pub(crate) before: Option<u64>,
    pub(crate) after_seq: Option<u64>,
    pub(crate) before_seq: Option<u64>,
    pub(crate) sort_desc: Option<bool>,
    pub(crate) luno_client: &'a client::LunoClient,
    pub(crate) url: reqwest::Url,
}

impl<'a> ListTradesBuilder<'a> {
    pub fn since(&mut self, timestamp: u64) -> &mut ListTradesBuilder<'a> {
        self.since = Some(timestamp);
        self
    }

    pub fn limit(&mut self, count: u64) -> &mut ListTradesBuilder<'a> {
        self.limit = Some(count);
        self
    }

    pub fn before(&mut self, timestamp: u64) -> &mut ListTradesBuilder<'a> {
        self.before = Some(timestamp);
        self
    }

    pub fn after_seq(&mut self, seq: u64) -> &mut ListTradesBuilder<'a> {
        self.after_seq = Some(seq);
        self
    }

    pub fn before_seq(&mut self, seq: u64) -> &mut ListTradesBuilder<'a> {
        self.before_seq = Some(seq);
        self
    }

    pub fn sort_desc(&mut self, sorted: bool) -> &mut ListTradesBuilder<'a> {
        self.sort_desc = Some(sorted);
        self
    }

    pub fn get(&self) -> impl Future<Output = Result<TradeList, reqwest::Error>> + '_ {
        let mut url = self.url.clone();
        if let Some(since) = self.since {
            url.query_pairs_mut()
                .append_pair("since", &since.to_string());
        }
        if let Some(limit) = self.limit {
            url.query_pairs_mut()
                .append_pair("limit", &limit.to_string());
        }
        if let Some(timestamp) = self.before {
            url.query_pairs_mut()
                .append_pair("before", &timestamp.to_string());
        }
        if let Some(seq) = self.after_seq {
            url.query_pairs_mut()
                .append_pair("after_seq", &seq.to_string());
        }
        if let Some(seq) = self.before_seq {
            url.query_pairs_mut()
                .append_pair("before_seq", &seq.to_string());
        }
        if let Some(sorted) = self.sort_desc {
            url.query_pairs_mut()
                .append_pair("sort_desc", &sorted.to_string());
        }
        self.luno_client.get(url)
    }
}

/// Represents the fee info associated with recent trades.
#[derive(Debug, Deserialize)]
pub struct FeeInfo {
    pub maker_fee: Decimal,
    pub taker_fee: Decimal,
    pub thirty_day_volume: Decimal,
}