Skip to main content

dfns_sdk_rust/swaps/
mod.rs

1// Code generated by rust-sdk-generator. DO NOT EDIT.
2
3pub mod types;
4
5#[allow(unused_imports)]
6use types::*;
7
8/// Client for swaps operations.
9#[derive(Clone)]
10pub struct SwapsClient {
11    client: crate::client::Client,
12}
13
14impl SwapsClient {
15    pub fn new(client: crate::client::Client) -> Self {
16        SwapsClient { client }
17    }
18
19    /// List all swaps with pagination
20    pub async fn list_swaps(
21        &self,
22        query: Option<ListSwapsQuery>,
23    ) -> Result<ListSwapsResponse, crate::error::Error> {
24        let mut path = String::from("/swaps");
25        if let Some(query) = &query {
26            let mut q: Vec<String> = Vec::new();
27            if let Some(v) = &query.limit {
28                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
29            }
30            if let Some(v) = &query.pagination_token {
31                q.push(format!(
32                    "paginationToken={}",
33                    urlencoding::encode(&v.to_string())
34                ));
35            }
36            if !q.is_empty() {
37                path.push('?');
38                path.push_str(&q.join("&"));
39            }
40        }
41        self.client
42            .request::<ListSwapsResponse>(reqwest::Method::GET, &path, None, false)
43            .await
44    }
45
46    /// Create a new swap based on an existing quote. This is the second step of the [Swap flow](https://docs.dfns.co/api-reference/swaps#flow-overview).
47    pub async fn create_swap(
48        &self,
49        body: CreateSwapRequest,
50    ) -> Result<CreateSwapResponse, crate::error::Error> {
51        let path = String::from("/swaps");
52        let body = serde_json::to_value(&body)?;
53        self.client
54            .request::<CreateSwapResponse>(reqwest::Method::POST, &path, Some(&body), true)
55            .await
56    }
57
58    /// Request a quote from a given provider for swapping assets. This is the first step of the [Swap flow](https://docs.dfns.co/api-reference/swaps#flow-overview).
59    pub async fn request_swap_quote(
60        &self,
61        body: RequestSwapQuoteRequest,
62    ) -> Result<RequestSwapQuoteResponse, crate::error::Error> {
63        let path = String::from("/swaps/quotes");
64        let body = serde_json::to_value(&body)?;
65        self.client
66            .request::<RequestSwapQuoteResponse>(reqwest::Method::POST, &path, Some(&body), false)
67            .await
68    }
69
70    /// Get details of a specific swap by its ID
71    pub async fn get_swap(&self, swap_id: String) -> Result<GetSwapResponse, crate::error::Error> {
72        let path = format!("/swaps/{}", urlencoding::encode(&swap_id));
73        self.client
74            .request::<GetSwapResponse>(reqwest::Method::GET, &path, None, false)
75            .await
76    }
77
78    /// Get details of a specific swap quote by its ID
79    pub async fn get_swap_quote(
80        &self,
81        quote_id: String,
82    ) -> Result<GetSwapQuoteResponse, crate::error::Error> {
83        let path = format!("/swaps/quotes/{}", urlencoding::encode(&quote_id));
84        self.client
85            .request::<GetSwapQuoteResponse>(reqwest::Method::GET, &path, None, false)
86            .await
87    }
88}