rango_sdk/swap/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    error::SdkErr,
5    quote::{Asset, QuoteSimulationResult, RoutingResultType},
6};
7
8use self::transaction::Transaction;
9
10mod cosmos;
11mod evm;
12mod transaction;
13mod transfer;
14
15#[derive(Serialize, Deserialize, Debug)]
16#[serde(rename_all = "camelCase")]
17pub struct SwapRequest {
18    pub from: Asset,
19    pub to: Asset,
20    pub amount: String,
21    pub from_address: String,
22    pub to_address: String,
23    pub slippage: String,
24    pub swappers: Option<Vec<String>>,
25    pub swappers_exclude: Option<bool>,
26    pub swapper_groups: Option<Vec<String>>,
27    pub swappers_groups_exclude: Option<bool>,
28    pub messaging_protocols: Option<Vec<String>>,
29    pub source_contract: Option<String>,
30    pub destination_contract: Option<String>,
31    pub im_message: Option<String>,
32    pub contract_call: Option<bool>,
33    pub disable_estimate: Option<bool>,
34    pub referrer_address: Option<String>,
35    pub referrer_fee: Option<String>,
36}
37
38#[derive(Serialize, Deserialize, Debug)]
39#[serde(rename_all = "camelCase")]
40struct SwapRequestQs {
41    from: String,
42    to: String,
43    amount: String,
44    from_address: String,
45    to_address: String,
46    slippage: String,
47    swappers: Option<String>,
48    swappers_exclude: Option<bool>,
49    swapper_groups: Option<String>,
50    swappers_groups_exclude: Option<bool>,
51    messaging_protocols: Option<String>,
52    source_contract: Option<String>,
53    destination_contract: Option<String>,
54    im_message: Option<String>,
55    contract_call: Option<bool>,
56    disable_estimate: Option<bool>,
57    referrer_address: Option<String>,
58    referrer_fee: Option<String>,
59}
60
61impl SwapRequest {
62    pub fn into_qs(&self) -> Result<String, SdkErr> {
63        let from = match self.from.address.clone() {
64            None => format!("{}.{}", self.from.blockchain, self.from.symbol),
65            Some(address) => format!("{}.{}--{}", self.from.blockchain, self.from.symbol, address),
66        };
67        let to = match self.to.address.clone() {
68            None => format!("{}.{}", self.to.blockchain, self.to.symbol),
69            Some(address) => format!("{}.{}--{}", self.to.blockchain, self.to.symbol, address),
70        };
71
72        let request = SwapRequestQs {
73            from,
74            to,
75            amount: self.amount.clone(),
76            from_address: self.from_address.clone(),
77            to_address: self.to_address.clone(),
78            slippage: self.slippage.clone(),
79            contract_call: self.contract_call,
80            destination_contract: self.destination_contract.clone(),
81            im_message: self.im_message.clone(),
82            source_contract: self.source_contract.clone(),
83            messaging_protocols: match self.messaging_protocols.clone() {
84                Some(protocols) => Some(protocols.join(",")),
85                None => None,
86            },
87            swapper_groups: match self.swapper_groups.clone() {
88                Some(groups) => Some(groups.join(",")),
89                None => None,
90            },
91            swappers: match self.swappers.clone() {
92                Some(swappers) => Some(swappers.join(",")),
93                None => None,
94            },
95            swappers_exclude: self.swappers_exclude,
96            swappers_groups_exclude: self.swappers_groups_exclude,
97            disable_estimate: self.disable_estimate,
98            referrer_address: self.referrer_address.clone(),
99            referrer_fee: self.referrer_fee.clone(),
100        };
101
102        let qs = serde_urlencoded::to_string(request)?;
103
104        Ok(qs)
105    }
106}
107
108#[derive(Deserialize, Debug)]
109#[serde(rename_all = "camelCase")]
110pub struct SwapResponse {
111    pub request_id: String,
112    pub result_type: RoutingResultType,
113    pub route: Option<QuoteSimulationResult>,
114    pub error: Option<String>,
115    pub tx: Option<Transaction>,
116}