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
//! Futures/CFM API endpoints.
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{
FuturesBalanceSummary, FuturesPosition, FuturesSweep, GetCurrentMarginWindowParams,
GetCurrentMarginWindowResponse, GetFuturesBalanceSummaryResponse, GetFuturesPositionResponse,
GetIntradayMarginSettingResponse, ListFuturesPositionsResponse, ListFuturesSweepsResponse,
MarginWindow, ScheduleFuturesSweepRequest, ScheduleFuturesSweepResponse,
SetIntradayMarginSettingRequest,
};
/// API for futures (CFM) trading.
///
/// This API provides endpoints for managing futures positions, balances, margins, and sweeps.
pub struct FuturesApi<'a> {
client: &'a RestClient,
}
impl<'a> FuturesApi<'a> {
/// Create a new Futures API instance.
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
/// List all futures positions.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let positions = client.futures().list_positions().await?;
/// for position in positions {
/// println!("{}: {:?}", position.product_id, position.number_of_contracts);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list_positions(&self) -> Result<Vec<FuturesPosition>> {
let response: ListFuturesPositionsResponse = self.client.get("/cfm/positions").await?;
Ok(response.positions)
}
/// Get a specific futures position.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let position = client.futures().get_position("BIT-28JUN24-CDE").await?;
/// println!("Position: {:?}", position.number_of_contracts);
/// # Ok(())
/// # }
/// ```
pub async fn get_position(&self, product_id: &str) -> Result<FuturesPosition> {
let endpoint = format!("/cfm/positions/{}", product_id);
let response: GetFuturesPositionResponse = self.client.get(&endpoint).await?;
Ok(response.position)
}
/// Get the futures balance summary.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let summary = client.futures().get_balance_summary().await?;
/// println!("Buying power: {:?}", summary.futures_buying_power);
/// # Ok(())
/// # }
/// ```
pub async fn get_balance_summary(&self) -> Result<FuturesBalanceSummary> {
let response: GetFuturesBalanceSummaryResponse =
self.client.get("/cfm/balance_summary").await?;
Ok(response.balance_summary)
}
/// Get the intraday margin setting.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let response = client.futures().get_intraday_margin_setting().await?;
/// println!("Setting: {:?}", response.setting);
/// # Ok(())
/// # }
/// ```
pub async fn get_intraday_margin_setting(&self) -> Result<GetIntradayMarginSettingResponse> {
self.client.get("/cfm/intraday/margin_setting").await
}
/// Set the intraday margin setting.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::SetIntradayMarginSettingRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let request = SetIntradayMarginSettingRequest::new("STANDARD");
/// client.futures().set_intraday_margin_setting(request).await?;
/// # Ok(())
/// # }
/// ```
pub async fn set_intraday_margin_setting(
&self,
request: SetIntradayMarginSettingRequest,
) -> Result<()> {
let _response: serde_json::Value = self
.client
.post("/cfm/intraday/margin_setting", &request)
.await?;
Ok(())
}
/// Get the current margin window.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::GetCurrentMarginWindowParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let window = client.futures()
/// .get_current_margin_window(GetCurrentMarginWindowParams::new())
/// .await?;
/// println!("Window type: {:?}", window.margin_window_type);
/// # Ok(())
/// # }
/// ```
pub async fn get_current_margin_window(
&self,
params: GetCurrentMarginWindowParams,
) -> Result<MarginWindow> {
let response: GetCurrentMarginWindowResponse = self
.client
.get_with_query("/cfm/intraday/current_margin_window", ¶ms)
.await?;
Ok(response.margin_window)
}
/// List all scheduled futures sweeps.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let sweeps = client.futures().list_sweeps().await?;
/// for sweep in sweeps {
/// println!("Sweep: {:?} - {:?}", sweep.id, sweep.status);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list_sweeps(&self) -> Result<Vec<FuturesSweep>> {
let response: ListFuturesSweepsResponse = self.client.get("/cfm/sweeps").await?;
Ok(response.sweeps)
}
/// Schedule a futures sweep.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::ScheduleFuturesSweepRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let request = ScheduleFuturesSweepRequest::new("1000.00");
/// let response = client.futures().schedule_sweep(request).await?;
/// # Ok(())
/// # }
/// ```
pub async fn schedule_sweep(
&self,
request: ScheduleFuturesSweepRequest,
) -> Result<ScheduleFuturesSweepResponse> {
self.client.post("/cfm/sweeps/schedule", &request).await
}
/// Cancel a pending futures sweep.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// client.futures().cancel_pending_sweep().await?;
/// # Ok(())
/// # }
/// ```
pub async fn cancel_pending_sweep(&self) -> Result<()> {
let _response: serde_json::Value = self.client.delete("/cfm/sweeps").await?;
Ok(())
}
}