tycho_execution/encoding/
models.rs

1use clap::ValueEnum;
2use num_bigint::BigUint;
3use serde::{Deserialize, Serialize};
4use tycho_common::{
5    models::protocol::ProtocolComponent, simulation::protocol_sim::ProtocolSim, Bytes,
6};
7
8use crate::encoding::serde_primitives::biguint_string;
9
10/// Specifies the method for transferring user funds into Tycho execution.
11///
12/// Options:
13///
14/// - `TransferFromPermit2`: Use Permit2 for token transfer.
15///     - You must manually approve the Permit2 contract and sign the permit object externally
16///       (outside `tycho-execution`).
17///
18/// - `TransferFrom`: Use standard ERC-20 approval and `transferFrom`.
19///     - You must approve the Tycho Router contract to spend your tokens via standard `approve()`
20///       calls.
21///
22/// - `None`: No transfer will be performed.
23///     - Assumes the tokens are already present in the Tycho Router.
24///     - **Warning**: This is an advanced mode. Ensure your logic guarantees that the tokens are
25///       already in the router at the time of execution.
26///     - The Tycho router is **not** designed to safely hold tokens. If tokens are not transferred
27///       and used in the **same transaction**, they will be permanently lost.
28#[derive(Clone, Debug, PartialEq, ValueEnum)]
29pub enum UserTransferType {
30    TransferFromPermit2,
31    TransferFrom,
32    None,
33}
34
35/// Represents a solution containing details describing an order, and  instructions for filling
36/// the order.
37#[derive(Clone, Default, Debug, Deserialize, Serialize)]
38pub struct Solution<'a> {
39    /// Address of the sender.
40    pub sender: Bytes,
41    /// Address of the receiver.
42    pub receiver: Bytes,
43    /// The token being sold (exact in) or bought (exact out).
44    pub given_token: Bytes,
45    /// Amount of the given token.
46    #[serde(with = "biguint_string")]
47    pub given_amount: BigUint,
48    /// The token being bought (exact in) or sold (exact out).
49    pub checked_token: Bytes,
50    /// False if the solution is an exact input solution. Currently only exact input solutions are
51    /// supported.
52    #[serde(default)]
53    pub exact_out: bool,
54    /// Minimum amount to be checked for the solution to be valid.
55    #[serde(with = "biguint_string")]
56    pub checked_amount: BigUint,
57    /// List of swaps to fulfill the solution.
58    pub swaps: Vec<Swap<'a>>,
59    /// If set, the corresponding native action will be executed.
60    pub native_action: Option<NativeAction>,
61}
62
63/// Represents an action to be performed on the native token either before or after the swap.
64///
65/// `Wrap` means that the native token will be wrapped before the first swap, and `Unwrap`
66/// means that the native token will be unwrapped after the last swap, before being sent to the
67/// receiver.
68#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
69#[serde(rename_all = "snake_case")]
70pub enum NativeAction {
71    Wrap,
72    Unwrap,
73}
74
75/// Represents a swap operation to be performed on a pool.
76#[derive(Clone, Debug, Deserialize, Serialize)]
77pub struct Swap<'a> {
78    /// Protocol component from tycho indexer
79    pub component: ProtocolComponent,
80    /// Token being input into the pool.
81    pub token_in: Bytes,
82    /// Token being output from the pool.
83    pub token_out: Bytes,
84    /// Decimal of the amount to be swapped in this operation (for example, 0.5 means 50%)
85    #[serde(default)]
86    pub split: f64,
87    /// Optional user data to be passed to encoding.
88    pub user_data: Option<Bytes>,
89    /// Optional protocol state used to perform the swap.
90    #[serde(skip)]
91    pub protocol_state: Option<&'a dyn ProtocolSim>,
92    /// Optional estimated amount in for this Swap. This is necessary for RFQ protocols. This value
93    /// is used to request the quote
94    pub estimated_amount_in: Option<BigUint>,
95}
96
97impl<'a> Swap<'a> {
98    pub fn new<T: Into<ProtocolComponent>>(
99        component: T,
100        token_in: Bytes,
101        token_out: Bytes,
102        split: f64,
103        user_data: Option<Bytes>,
104        protocol_state: Option<&'a dyn ProtocolSim>,
105        estimated_amount_in: Option<BigUint>,
106    ) -> Self {
107        Self {
108            component: component.into(),
109            token_in,
110            token_out,
111            split,
112            user_data,
113            protocol_state,
114            estimated_amount_in,
115        }
116    }
117}
118
119impl<'a> PartialEq for Swap<'a> {
120    fn eq(&self, other: &Self) -> bool {
121        self.component == other.component &&
122            self.token_in == other.token_in &&
123            self.token_out == other.token_out &&
124            self.split == other.split &&
125            self.user_data == other.user_data
126        // Skip protocol_state comparison since trait objects don't implement PartialEq
127    }
128}
129
130pub struct SwapBuilder<'a> {
131    component: ProtocolComponent,
132    token_in: Bytes,
133    token_out: Bytes,
134    split: f64,
135    user_data: Option<Bytes>,
136    protocol_state: Option<&'a dyn ProtocolSim>,
137    estimated_amount_in: Option<BigUint>,
138}
139
140impl<'a> SwapBuilder<'a> {
141    pub fn new<T: Into<ProtocolComponent>>(
142        component: T,
143        token_in: Bytes,
144        token_out: Bytes,
145    ) -> Self {
146        Self {
147            component: component.into(),
148            token_in,
149            token_out,
150            split: 0.0,
151            user_data: None,
152            protocol_state: None,
153            estimated_amount_in: None,
154        }
155    }
156
157    pub fn split(mut self, split: f64) -> Self {
158        self.split = split;
159        self
160    }
161
162    pub fn user_data(mut self, user_data: Bytes) -> Self {
163        self.user_data = Some(user_data);
164        self
165    }
166
167    pub fn protocol_state(mut self, protocol_state: &'a dyn ProtocolSim) -> Self {
168        self.protocol_state = Some(protocol_state);
169        self
170    }
171
172    pub fn estimated_amount_in(mut self, estimated_amount_in: BigUint) -> Self {
173        self.estimated_amount_in = Some(estimated_amount_in);
174        self
175    }
176
177    pub fn build(self) -> Swap<'a> {
178        Swap {
179            component: self.component,
180            token_in: self.token_in,
181            token_out: self.token_out,
182            split: self.split,
183            user_data: self.user_data,
184            protocol_state: self.protocol_state,
185            estimated_amount_in: self.estimated_amount_in,
186        }
187    }
188}
189
190/// Represents a transaction to be executed.
191///
192/// # Fields
193/// * `to`: Address of the contract to call with the calldata
194/// * `value`: Native token value to be sent with the transaction.
195/// * `data`: Encoded calldata for the transaction.
196#[derive(Clone, Debug)]
197pub struct Transaction {
198    pub to: Bytes,
199    pub value: BigUint,
200    pub data: Vec<u8>,
201}
202
203/// Represents a solution that has been encoded for execution.
204///
205/// # Fields
206/// * `swaps`: Encoded swaps to be executed.
207/// * `interacting_with`: Address of the contract to be called.
208/// * `function_signature`: The signature of the function to be called.
209/// * `n_tokens`: Number of tokens in the swap.
210/// * `permit`: Optional permit for the swap (if permit2 is enabled).
211#[derive(Clone, Debug)]
212pub struct EncodedSolution {
213    pub swaps: Vec<u8>,
214    pub interacting_with: Bytes,
215    pub function_signature: String,
216    pub n_tokens: usize,
217    pub permit: Option<PermitSingle>,
218}
219
220/// Represents a single permit for permit2.
221///
222/// # Fields
223/// * `details`: The details of the permit, such as token, amount, expiration, and nonce.
224/// * `spender`: The address authorized to spend the tokens.
225/// * `sig_deadline`: The deadline (as a timestamp) for the permit signature
226#[derive(Debug, Clone)]
227pub struct PermitSingle {
228    pub details: PermitDetails,
229    pub spender: Bytes,
230    pub sig_deadline: BigUint,
231}
232
233/// Details of a permit.
234///
235/// # Fields
236/// * `token`: The token address for which the permit is granted.
237/// * `amount`: The amount of tokens approved for spending.
238/// * `expiration`: The expiration time (as a timestamp) for the permit.
239/// * `nonce`: The unique nonce to prevent replay attacks.
240#[derive(Debug, Clone)]
241pub struct PermitDetails {
242    pub token: Bytes,
243    pub amount: BigUint,
244    pub expiration: BigUint,
245    pub nonce: BigUint,
246}
247
248impl PartialEq for PermitSingle {
249    fn eq(&self, other: &Self) -> bool {
250        self.details == other.details && self.spender == other.spender
251        // sig_deadline is intentionally ignored
252    }
253}
254
255impl PartialEq for PermitDetails {
256    fn eq(&self, other: &Self) -> bool {
257        self.token == other.token && self.amount == other.amount && self.nonce == other.nonce
258        // expiration is intentionally ignored
259    }
260}
261
262/// Represents necessary attributes for encoding an order.
263///
264/// # Fields
265///
266/// * `receiver`: Address of the receiver of the out token after the swaps are completed.
267/// * `exact_out`: true if the solution is a buy order, false if it is a sell order.
268/// * `router_address`: Address of the router contract to be used for the swaps. Zero address if
269///   solution does not require router address.
270/// * `group_token_in`: Token to be used as the input for the group swap.
271/// * `group_token_out`: Token to be used as the output for the group swap.
272/// * `transfer`: Type of transfer to be performed. See `TransferType` for more details.
273#[derive(Clone, Debug)]
274pub struct EncodingContext {
275    pub receiver: Bytes,
276    pub exact_out: bool,
277    pub router_address: Option<Bytes>,
278    pub group_token_in: Bytes,
279    pub group_token_out: Bytes,
280    pub transfer_type: TransferType,
281}
282
283/// Represents the type of transfer to be performed into the pool.
284///
285/// # Fields
286///
287/// * `TransferFrom`: Transfer the token from the sender to the protocol/router.
288/// * `Transfer`: Transfer the token from the router into the protocol.
289/// * `None`: No transfer is needed. Tokens are already in the pool.
290#[repr(u8)]
291#[derive(Copy, Clone, Debug, PartialEq)]
292pub enum TransferType {
293    TransferFrom = 0,
294    Transfer = 1,
295    None = 2,
296}
297
298mod tests {
299    use super::*;
300
301    struct MockProtocolComponent {
302        id: String,
303        protocol_system: String,
304    }
305
306    impl From<MockProtocolComponent> for ProtocolComponent {
307        fn from(component: MockProtocolComponent) -> Self {
308            ProtocolComponent {
309                id: component.id,
310                protocol_system: component.protocol_system,
311                tokens: vec![],
312                protocol_type_name: "".to_string(),
313                chain: Default::default(),
314                contract_addresses: vec![],
315                static_attributes: Default::default(),
316                change: Default::default(),
317                creation_tx: Default::default(),
318                created_at: Default::default(),
319            }
320        }
321    }
322
323    #[test]
324    fn test_swap_new() {
325        let component = MockProtocolComponent {
326            id: "i-am-an-id".to_string(),
327            protocol_system: "uniswap_v2".to_string(),
328        };
329        let user_data = Some(Bytes::from("0x1234"));
330        let swap = Swap::new(
331            component,
332            Bytes::from("0x12"),
333            Bytes::from("34"),
334            0.5,
335            user_data.clone(),
336            None,
337            None,
338        );
339        assert_eq!(swap.token_in, Bytes::from("0x12"));
340        assert_eq!(swap.token_out, Bytes::from("0x34"));
341        assert_eq!(swap.component.protocol_system, "uniswap_v2");
342        assert_eq!(swap.component.id, "i-am-an-id");
343        assert_eq!(swap.split, 0.5);
344        assert_eq!(swap.user_data, user_data);
345    }
346}