Skip to main content

coinbase_advanced/models/
convert.rs

1//! Convert API types.
2
3use serde::{Deserialize, Serialize};
4
5/// Trade status for a conversion.
6#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
7pub enum ConvertTradeStatus {
8    /// Unspecified status.
9    #[serde(rename = "TRADE_STATUS_UNSPECIFIED")]
10    Unspecified,
11    /// Trade has been created.
12    #[serde(rename = "TRADE_STATUS_CREATED")]
13    Created,
14    /// Trade has started.
15    #[serde(rename = "TRADE_STATUS_STARTED")]
16    Started,
17    /// Trade has been completed.
18    #[serde(rename = "TRADE_STATUS_COMPLETED")]
19    Completed,
20    /// Trade has been canceled.
21    #[serde(rename = "TRADE_STATUS_CANCELED")]
22    Canceled,
23    /// Unknown status.
24    #[serde(other)]
25    Unknown,
26}
27
28/// Amount with currency.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct ConvertAmount {
31    /// The amount value.
32    pub value: String,
33    /// The currency.
34    pub currency: String,
35}
36
37/// Fee information for a conversion.
38#[derive(Debug, Clone, Deserialize)]
39pub struct ConvertFee {
40    /// Fee title.
41    #[serde(default)]
42    pub title: Option<String>,
43    /// Fee description.
44    #[serde(default)]
45    pub description: Option<String>,
46    /// Fee amount.
47    pub amount: ConvertAmount,
48    /// Fee label.
49    #[serde(default)]
50    pub label: Option<String>,
51}
52
53/// Account details for conversion source/target.
54#[derive(Debug, Clone, Deserialize)]
55pub struct ConvertAccountDetail {
56    /// Account type.
57    #[serde(rename = "type", default)]
58    pub account_type: Option<String>,
59    /// Network.
60    #[serde(default)]
61    pub network: Option<String>,
62    /// Ledger account details.
63    #[serde(default)]
64    pub ledger_account: Option<serde_json::Value>,
65}
66
67/// A conversion trade.
68#[derive(Debug, Clone, Deserialize)]
69pub struct ConvertTrade {
70    /// The trade ID.
71    pub id: String,
72    /// Trade status.
73    pub status: ConvertTradeStatus,
74    /// User entered amount.
75    #[serde(default)]
76    pub user_entered_amount: Option<ConvertAmount>,
77    /// Converted amount.
78    #[serde(default)]
79    pub amount: Option<ConvertAmount>,
80    /// Subtotal.
81    #[serde(default)]
82    pub subtotal: Option<ConvertAmount>,
83    /// Total.
84    #[serde(default)]
85    pub total: Option<ConvertAmount>,
86    /// Fees.
87    #[serde(default)]
88    pub fees: Vec<ConvertFee>,
89    /// Total fee.
90    #[serde(default)]
91    pub total_fee: Option<ConvertFee>,
92    /// Source account details.
93    #[serde(default)]
94    pub source: Option<ConvertAccountDetail>,
95    /// Target account details.
96    #[serde(default)]
97    pub target: Option<ConvertAccountDetail>,
98    /// Source currency.
99    #[serde(default)]
100    pub source_currency: Option<String>,
101    /// Target currency.
102    #[serde(default)]
103    pub target_currency: Option<String>,
104    /// Source account ID.
105    #[serde(default)]
106    pub source_id: Option<String>,
107    /// Target account ID.
108    #[serde(default)]
109    pub target_id: Option<String>,
110    /// Exchange rate.
111    #[serde(default)]
112    pub exchange_rate: Option<ConvertAmount>,
113    /// User reference.
114    #[serde(default)]
115    pub user_reference: Option<String>,
116}
117
118/// Response containing a trade.
119#[derive(Debug, Clone, Deserialize)]
120pub struct ConvertTradeResponse {
121    /// The trade.
122    pub trade: ConvertTrade,
123}
124
125/// Trade incentive metadata for waiving fees.
126#[derive(Debug, Clone, Default, Serialize)]
127pub struct TradeIncentiveMetadata {
128    /// User incentive ID.
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub user_incentive_id: Option<String>,
131    /// Promo code.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub code_val: Option<String>,
134}
135
136/// Request to create a convert quote.
137#[derive(Debug, Clone, Serialize)]
138pub struct CreateConvertQuoteRequest {
139    /// Source account ID (the account to convert from).
140    pub from_account: String,
141    /// Target account ID (the account to convert to).
142    pub to_account: String,
143    /// Amount to convert.
144    pub amount: String,
145    /// Trade incentive metadata.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub trade_incentive_metadata: Option<TradeIncentiveMetadata>,
148}
149
150impl CreateConvertQuoteRequest {
151    /// Create a new convert quote request.
152    pub fn new(
153        from_account: impl Into<String>,
154        to_account: impl Into<String>,
155        amount: impl Into<String>,
156    ) -> Self {
157        Self {
158            from_account: from_account.into(),
159            to_account: to_account.into(),
160            amount: amount.into(),
161            trade_incentive_metadata: None,
162        }
163    }
164
165    /// Set the trade incentive metadata.
166    pub fn with_incentive(mut self, metadata: TradeIncentiveMetadata) -> Self {
167        self.trade_incentive_metadata = Some(metadata);
168        self
169    }
170}
171
172/// Request to commit a convert trade.
173#[derive(Debug, Clone, Serialize)]
174pub struct CommitConvertTradeRequest {
175    /// Source account ID.
176    pub from_account: String,
177    /// Target account ID.
178    pub to_account: String,
179}
180
181impl CommitConvertTradeRequest {
182    /// Create a new commit convert trade request.
183    pub fn new(from_account: impl Into<String>, to_account: impl Into<String>) -> Self {
184        Self {
185            from_account: from_account.into(),
186            to_account: to_account.into(),
187        }
188    }
189}
190
191/// Parameters for getting a convert trade.
192#[derive(Debug, Clone, Serialize)]
193pub struct GetConvertTradeParams {
194    /// Source account ID.
195    pub from_account: String,
196    /// Target account ID.
197    pub to_account: String,
198}
199
200impl GetConvertTradeParams {
201    /// Create new get convert trade parameters.
202    pub fn new(from_account: impl Into<String>, to_account: impl Into<String>) -> Self {
203        Self {
204            from_account: from_account.into(),
205            to_account: to_account.into(),
206        }
207    }
208}