odos_sdk/
swap.rs

1use std::fmt::Display;
2
3use alloy_chains::NamedChain;
4use alloy_primitives::{Address, U256};
5use bon::Builder;
6use serde::{Deserialize, Deserializer, Serialize, Serializer};
7
8/// Request for assembling a transaction from a quote
9///
10/// Contains all the information needed to assemble a transaction from
11/// a quote path ID, including signer address, recipient, and routing details.
12#[derive(Builder, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
13pub struct AssemblyRequest {
14    /// The chain of the swap.
15    chain: NamedChain,
16    /// The address of the router.
17    router_address: Address,
18    /// The address of the signer.
19    signer_address: Address,
20    /// The address of the recipient of the output token.
21    output_recipient: Address,
22    /// The address of the token to swap.
23    token_address: Address,
24    /// The amount of tokens to swap.
25    token_amount: U256,
26    /// The path ID of the swap.
27    path_id: String,
28}
29
30impl AssemblyRequest {
31    pub fn chain(&self) -> NamedChain {
32        self.chain
33    }
34
35    pub fn output_recipient(&self) -> Address {
36        self.output_recipient
37    }
38
39    pub fn router_address(&self) -> Address {
40        self.router_address
41    }
42
43    pub fn signer_address(&self) -> Address {
44        self.signer_address
45    }
46
47    pub fn token_address(&self) -> Address {
48        self.token_address
49    }
50
51    pub fn token_amount(&self) -> U256 {
52        self.token_amount
53    }
54
55    pub fn path_id(&self) -> &str {
56        &self.path_id
57    }
58}
59
60impl Serialize for AssemblyRequest {
61    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62    where
63        S: Serializer,
64    {
65        let chain_id: u64 = self.chain.into();
66        let data = (
67            chain_id,
68            self.router_address,
69            self.signer_address,
70            self.output_recipient,
71            self.token_address,
72            self.token_amount,
73            &self.path_id,
74        );
75        data.serialize(serializer)
76    }
77}
78
79impl<'de> Deserialize<'de> for AssemblyRequest {
80    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
81    where
82        D: Deserializer<'de>,
83    {
84        let (
85            chain_id,
86            router_address,
87            signer_address,
88            output_recipient,
89            token_address,
90            token_amount,
91            path_id,
92        ): (u64, Address, Address, Address, Address, U256, String) =
93            Deserialize::deserialize(deserializer)?;
94
95        let chain = NamedChain::try_from(chain_id).map_err(serde::de::Error::custom)?;
96
97        Ok(Self {
98            chain,
99            router_address,
100            signer_address,
101            output_recipient,
102            token_address,
103            token_amount,
104            path_id,
105        })
106    }
107}
108
109impl Display for AssemblyRequest {
110    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111        write!(
112            f,
113            "Swap {{ chain: {}, router_address: {}, signer_address: {}, output_recipient: {}, token_address: {}, token_amount: {}, path_id: {} }}",
114            self.chain,
115            self.router_address,
116            self.signer_address,
117            self.output_recipient,
118            self.token_address,
119            self.token_amount,
120            self.path_id
121        )
122    }
123}
124
125/// Deprecated alias for [`AssemblyRequest`]
126///
127/// This type alias is provided for backward compatibility.
128/// Use [`AssemblyRequest`] instead in new code.
129#[deprecated(since = "0.25.0", note = "Use `AssemblyRequest` instead")]
130pub type SwapContext = AssemblyRequest;