Skip to main content

jupiter_sdk/api/
swap.rs

1use std::collections::HashMap;
2
3use crate::{swap::{SwapInstructionsRes, SwapQuoteReq, SwapQuoteRes, SwapReq, SwapRes}, JupiterClient, JupiterError};
4
5
6
7
8
9
10
11
12
13#[derive(Clone)]
14pub struct SwapService<'a>{
15    client: &'a JupiterClient,
16}
17
18
19impl<'a> SwapService<'a> {
20    pub fn new(client: &'a JupiterClient) -> Self {
21        Self { client }
22    }
23
24    pub async fn quote(
25        &self,
26        req: &SwapQuoteReq,
27    ) -> Result<SwapQuoteRes, JupiterError> {
28        let path = "/swap/v1/quote";
29        self.client.post(&path, req).await
30    }
31
32    pub async fn swap(
33        &self,
34        req: &SwapReq,
35    ) -> Result<SwapRes, JupiterError> {
36        let path = "/swap/v1/swap";
37        self.client.post(&path, req).await
38    }
39
40    pub async fn swap_instructions(
41        &self,
42        req: &SwapReq,
43    ) -> Result<SwapInstructionsRes, JupiterError> {
44        let path = "/swap/v1/swap-instructions";
45        self.client.post(&path, req).await
46    }
47
48    pub async fn program_id_to_label(
49        &self,
50    ) -> Result<HashMap<String, String>, JupiterError> {
51        let path = "/swap/v1/program-id-to-label";
52        self.client.get_json(&path).await        
53    }
54}
55
56
57
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[tokio::test]
64    async fn test_program_id_to_label() {
65        let client = JupiterClient::new(crate::JupiterConfig::default()).unwrap();
66        let swap_service = SwapService::new(&client);
67
68        let res = swap_service.program_id_to_label().await;
69        match res {
70            Ok(res) => {
71                println!("program_id_to_label: {}", serde_json::to_string_pretty(&res).unwrap());
72            }
73            Err(e) => {
74                panic!("program_id_to_label error: {}", e);
75            }
76        }
77    }
78}
79