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/// A transfer of a token from one address to another.
9#[derive(Builder, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
10pub struct Swap {
11    chain: NamedChain,
12    router_address: Address,
13    signer_address: Address,
14    output_recipient: Address,
15    token_address: Address,
16    token_amount: U256,
17    path_id: String,
18}
19
20impl Swap {
21    pub fn chain(&self) -> NamedChain {
22        self.chain
23    }
24
25    pub fn output_recipient(&self) -> Address {
26        self.output_recipient
27    }
28
29    pub fn router_address(&self) -> Address {
30        self.router_address
31    }
32
33    pub fn signer_address(&self) -> Address {
34        self.signer_address
35    }
36
37    pub fn token_address(&self) -> Address {
38        self.token_address
39    }
40
41    pub fn token_amount(&self) -> U256 {
42        self.token_amount
43    }
44
45    pub fn path_id(&self) -> &str {
46        &self.path_id
47    }
48}
49
50impl Serialize for Swap {
51    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
52    where
53        S: Serializer,
54    {
55        let chain_id: u64 = self.chain.into();
56        let data = (
57            chain_id,
58            self.router_address,
59            self.signer_address,
60            self.output_recipient,
61            self.token_address,
62            self.token_amount,
63            &self.path_id,
64        );
65        data.serialize(serializer)
66    }
67}
68
69impl<'de> Deserialize<'de> for Swap {
70    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
71    where
72        D: Deserializer<'de>,
73    {
74        let (
75            chain_id,
76            router_address,
77            signer_address,
78            output_recipient,
79            token_address,
80            token_amount,
81            path_id,
82        ): (u64, Address, Address, Address, Address, U256, String) =
83            Deserialize::deserialize(deserializer)?;
84
85        let chain = NamedChain::try_from(chain_id).map_err(serde::de::Error::custom)?;
86
87        Ok(Swap {
88            chain,
89            router_address,
90            signer_address,
91            output_recipient,
92            token_address,
93            token_amount,
94            path_id,
95        })
96    }
97}
98
99impl Display for Swap {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        write!(
102            f,
103            "Swap {{ chain: {}, router_address: {}, signer_address: {}, output_recipient: {}, token_address: {}, token_amount: {}, path_id: {} }}",
104            self.chain,
105            self.router_address,
106            self.signer_address,
107            self.output_recipient,
108            self.token_address,
109            self.token_amount,
110            self.path_id
111        )
112    }
113}