tycho_execution/encoding/
models.rs

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