bitpanda_api/model/
trade.rs

1//! # Trade
2//!
3//! Datatypes for Bitpanda API trades
4
5use std::str::FromStr;
6
7use chrono::{DateTime, FixedOffset};
8use rust_decimal::Decimal;
9
10use crate::ApiError;
11
12/// A trade on the Bitpanda exchange
13#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize)]
14pub struct Trade {
15    pub amount_asset: Decimal,
16    pub amount_fiat: Decimal,
17    pub datetime: DateTime<FixedOffset>,
18    pub fiat_to_eur_rate: Decimal,
19    pub fiat_wallet_id: Option<String>,
20    pub id_asset: String,
21    pub id_fiat: String,
22    pub id_wallet: String,
23    pub id: String,
24    pub price: Decimal,
25    /// The Swap trade related to this
26    pub related_swap_trade: Option<Box<Trade>>,
27    pub status: TradeStatus,
28    pub symbol: String,
29    pub r#type: TradeType,
30}
31
32/// Defines the trade status
33#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize)]
34pub enum TradeStatus {
35    Pending,
36    Processing,
37    Finished,
38    Canceled,
39}
40
41impl FromStr for TradeStatus {
42    type Err = ApiError;
43
44    fn from_str(value: &str) -> Result<Self, Self::Err> {
45        match value {
46            "pending" => Ok(Self::Pending),
47            "processing" => Ok(Self::Processing),
48            "finished" => Ok(Self::Finished),
49            "canceled" => Ok(Self::Canceled),
50            _ => Err(ApiError::UnexpectedValue(value.to_string())),
51        }
52    }
53}
54
55/// Defines the trade type
56#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize)]
57pub enum TradeType {
58    Buy,
59    Sell,
60}
61
62impl FromStr for TradeType {
63    type Err = ApiError;
64
65    fn from_str(value: &str) -> Result<Self, Self::Err> {
66        match value {
67            "buy" => Ok(Self::Buy),
68            "sell" => Ok(Self::Sell),
69            _ => Err(ApiError::UnexpectedValue(value.to_string())),
70        }
71    }
72}