odos_sdk/
transfer.rs

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