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
//! Perpetuals/INTX API endpoints.
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{
AllocatePortfolioRequest, GetPerpetualsPortfolioSummaryResponse, GetPerpetualsPositionResponse,
GetPortfolioBalancesResponse, IntxPortfolioSummary, IntxPosition,
ListPerpetualsPositionsResponse, SetMultiAssetCollateralRequest,
};
/// API for perpetuals (INTX) trading.
///
/// This API provides endpoints for managing perpetual futures positions and portfolios.
pub struct PerpetualsApi<'a> {
client: &'a RestClient,
}
impl<'a> PerpetualsApi<'a> {
/// Create a new Perpetuals API instance.
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
/// List all perpetuals positions for a portfolio.
///
/// # 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.perpetuals().list_positions("portfolio-uuid").await?;
/// for position in response.positions {
/// println!("{}: {:?}", position.product_id, position.net_size);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list_positions(
&self,
portfolio_uuid: &str,
) -> Result<ListPerpetualsPositionsResponse> {
let endpoint = format!("/intx/positions/{}", portfolio_uuid);
self.client.get(&endpoint).await
}
/// Get a specific perpetuals 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.perpetuals()
/// .get_position("portfolio-uuid", "BTC-PERP")
/// .await?;
/// println!("Position: {:?}", position.net_size);
/// # Ok(())
/// # }
/// ```
pub async fn get_position(
&self,
portfolio_uuid: &str,
symbol: &str,
) -> Result<IntxPosition> {
let endpoint = format!("/intx/positions/{}/{}", portfolio_uuid, symbol);
let response: GetPerpetualsPositionResponse = self.client.get(&endpoint).await?;
Ok(response.position)
}
/// Get portfolio balances for perpetuals.
///
/// # 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.perpetuals()
/// .get_portfolio_balances("portfolio-uuid")
/// .await?;
/// for balance in response.portfolio_balances {
/// println!("{:?}: {:?}", balance.asset, balance.quantity);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_portfolio_balances(
&self,
portfolio_uuid: &str,
) -> Result<GetPortfolioBalancesResponse> {
let endpoint = format!("/intx/balances/{}", portfolio_uuid);
self.client.get(&endpoint).await
}
/// Get portfolio summary for perpetuals.
///
/// # 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.perpetuals()
/// .get_portfolio_summary("portfolio-uuid")
/// .await?;
/// println!("Total balance: {:?}", summary.total_balance);
/// # Ok(())
/// # }
/// ```
pub async fn get_portfolio_summary(
&self,
portfolio_uuid: &str,
) -> Result<IntxPortfolioSummary> {
let endpoint = format!("/intx/portfolio/{}", portfolio_uuid);
let response: GetPerpetualsPortfolioSummaryResponse = self.client.get(&endpoint).await?;
Ok(response.summary)
}
/// Allocate funds to a perpetuals portfolio.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::AllocatePortfolioRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let request = AllocatePortfolioRequest::new(
/// "portfolio-uuid",
/// "BTC-PERP",
/// "1000",
/// "USD",
/// );
///
/// client.perpetuals().allocate(request).await?;
/// # Ok(())
/// # }
/// ```
pub async fn allocate(&self, request: AllocatePortfolioRequest) -> Result<()> {
let _response: serde_json::Value = self.client.post("/intx/allocate", &request).await?;
Ok(())
}
/// Set multi-asset collateral for a portfolio.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::SetMultiAssetCollateralRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let request = SetMultiAssetCollateralRequest::new(true);
/// client.perpetuals()
/// .set_multi_asset_collateral("portfolio-uuid", request)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn set_multi_asset_collateral(
&self,
portfolio_uuid: &str,
request: SetMultiAssetCollateralRequest,
) -> Result<()> {
let endpoint = format!("/intx/balances/{}", portfolio_uuid);
let _response: serde_json::Value = self.client.post(&endpoint, &request).await?;
Ok(())
}
}