odos_sdk/
transfer.rs

1use alloy_chains::NamedChain;
2use alloy_primitives::{Address, U256};
3use bon::Builder;
4use serde::{Deserialize, Deserializer, Serialize, Serializer};
5
6/// A transfer of a token from one address to another.
7#[derive(Builder, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
8pub struct TransferRouterFunds {
9    chain: NamedChain,
10    from: Address,
11    to: Address,
12    token: Address,
13    amount: U256,
14}
15
16impl TransferRouterFunds {
17    /// Get the chain of the transfer.
18    pub fn chain(&self) -> NamedChain {
19        self.chain
20    }
21
22    /// Get the sender of the transfer.
23    pub fn from(&self) -> Address {
24        self.from
25    }
26
27    /// Get the recipient of the transfer.
28    pub fn to(&self) -> Address {
29        self.to
30    }
31
32    /// Get the token that is being transferred.
33    pub fn token(&self) -> Address {
34        self.token
35    }
36
37    /// Get the amount of the token that is being transferred.
38    pub fn amount(&self) -> U256 {
39        self.amount
40    }
41
42    /// Get the parameters for the `transferRouterFunds` function.
43    pub fn transfer_router_funds_params(&self) -> (Vec<Address>, Vec<U256>, Address) {
44        // tokens, amounts, output_recipient
45        (vec![self.token], vec![self.amount], self.to)
46    }
47}
48
49impl Serialize for TransferRouterFunds {
50    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51    where
52        S: Serializer,
53    {
54        let chain_id: u64 = self.chain.into();
55        let data = (chain_id, self.from, self.to, self.token, self.amount);
56        data.serialize(serializer)
57    }
58}
59
60impl<'de> Deserialize<'de> for TransferRouterFunds {
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62    where
63        D: Deserializer<'de>,
64    {
65        let (chain_id, from, to, token, amount): (u64, Address, Address, Address, U256) =
66            Deserialize::deserialize(deserializer)?;
67        let chain = NamedChain::try_from(chain_id).map_err(serde::de::Error::custom)?;
68
69        Ok(Self {
70            chain,
71            from,
72            to,
73            token,
74            amount,
75        })
76    }
77}