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
//! Portfolios API endpoints.
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{
CreatePortfolioRequest, EditPortfolioRequest, GetPortfolioBreakdownResponse,
ListPortfoliosParams, ListPortfoliosResponse, MoveFundsRequest, MoveFundsResponse, Portfolio,
PortfolioBreakdown, PortfolioResponse,
};
/// API for managing portfolios.
///
/// This API provides endpoints for creating, editing, deleting, and querying portfolios.
pub struct PortfoliosApi<'a> {
client: &'a RestClient,
}
impl<'a> PortfoliosApi<'a> {
/// Create a new Portfolios API instance.
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
/// List all portfolios.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let portfolios = client.portfolios().list().await?;
/// for portfolio in portfolios {
/// println!("{}: {}", portfolio.uuid, portfolio.name);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list(&self) -> Result<Vec<Portfolio>> {
self.list_with_params(ListPortfoliosParams::default()).await
}
/// List portfolios with custom parameters.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::ListPortfoliosParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let params = ListPortfoliosParams::new().portfolio_type("CONSUMER");
/// let portfolios = client.portfolios().list_with_params(params).await?;
/// # Ok(())
/// # }
/// ```
pub async fn list_with_params(&self, params: ListPortfoliosParams) -> Result<Vec<Portfolio>> {
let response: ListPortfoliosResponse = self
.client
.get_with_query("/portfolios", ¶ms)
.await?;
Ok(response.portfolios)
}
/// Get a portfolio breakdown by UUID.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let breakdown = client.portfolios().get_breakdown("portfolio-uuid").await?;
/// println!("Portfolio: {}", breakdown.portfolio.name);
/// # Ok(())
/// # }
/// ```
pub async fn get_breakdown(&self, portfolio_uuid: &str) -> Result<PortfolioBreakdown> {
let endpoint = format!("/portfolios/{}", portfolio_uuid);
let response: GetPortfolioBreakdownResponse = self.client.get(&endpoint).await?;
Ok(response.breakdown)
}
/// Create a new portfolio.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::CreatePortfolioRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let portfolio = client.portfolios()
/// .create(CreatePortfolioRequest::new("My New Portfolio"))
/// .await?;
/// println!("Created portfolio: {}", portfolio.uuid);
/// # Ok(())
/// # }
/// ```
pub async fn create(&self, request: CreatePortfolioRequest) -> Result<Portfolio> {
let response: PortfolioResponse = self.client.post("/portfolios", &request).await?;
Ok(response.portfolio)
}
/// Edit an existing portfolio.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::EditPortfolioRequest};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let portfolio = client.portfolios()
/// .edit("portfolio-uuid", EditPortfolioRequest::new("New Name"))
/// .await?;
/// println!("Updated portfolio: {}", portfolio.name);
/// # Ok(())
/// # }
/// ```
pub async fn edit(
&self,
portfolio_uuid: &str,
request: EditPortfolioRequest,
) -> Result<Portfolio> {
let endpoint = format!("/portfolios/{}", portfolio_uuid);
let response: PortfolioResponse = self.client.put(&endpoint, &request).await?;
Ok(response.portfolio)
}
/// Delete 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()?;
///
/// client.portfolios().delete("portfolio-uuid").await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete(&self, portfolio_uuid: &str) -> Result<()> {
let endpoint = format!("/portfolios/{}", portfolio_uuid);
let _response: serde_json::Value = self.client.delete(&endpoint).await?;
Ok(())
}
/// Move funds between portfolios.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::{MoveFundsRequest, MoveFunds}};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let request = MoveFundsRequest::new(
/// MoveFunds::new("100.00", "USD"),
/// "source-portfolio-uuid",
/// "target-portfolio-uuid",
/// );
///
/// let response = client.portfolios().move_funds(request).await?;
/// # Ok(())
/// # }
/// ```
pub async fn move_funds(&self, request: MoveFundsRequest) -> Result<MoveFundsResponse> {
self.client.post("/portfolios/move_funds", &request).await
}
}