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 pub component: ProtocolComponent,
82 /// Token being input into the pool.
83 pub token_in: Bytes,
84 /// Token being output from the pool.
85 pub token_out: Bytes,
86 /// Decimal of the amount to be swapped in this operation (for example, 0.5 means 50%)
87 #[serde(default)]
88 pub split: f64,
89 /// Optional user data to be passed to encoding.
90 pub user_data: Option<Bytes>,
91 /// Optional protocol state used to perform the swap.
92 #[serde(skip)]
93 pub 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 pub estimated_amount_in: Option<BigUint>,
97}
98
99impl Swap {
100 pub fn new<T: Into<ProtocolComponent>>(
101 component: T,
102 token_in: Bytes,
103 token_out: Bytes,
104 split: f64,
105 user_data: Option<Bytes>,
106 protocol_state: Option<Arc<dyn ProtocolSim>>,
107 estimated_amount_in: Option<BigUint>,
108 ) -> Self {
109 Self {
110 component: component.into(),
111 token_in,
112 token_out,
113 split,
114 user_data,
115 protocol_state,
116 estimated_amount_in,
117 }
118 }
119}
120
121impl PartialEq for Swap {
122 fn eq(&self, other: &Self) -> bool {
123 self.component == other.component &&
124 self.token_in == other.token_in &&
125 self.token_out == other.token_out &&
126 self.split == other.split &&
127 self.user_data == other.user_data &&
128 self.estimated_amount_in == other.estimated_amount_in
129 // Skip protocol_state comparison since trait objects don't implement PartialEq
130 }
131}
132
133pub struct SwapBuilder {
134 component: ProtocolComponent,
135 token_in: Bytes,
136 token_out: Bytes,
137 split: f64,
138 user_data: Option<Bytes>,
139 protocol_state: Option<Arc<dyn ProtocolSim>>,
140 estimated_amount_in: Option<BigUint>,
141}
142
143impl SwapBuilder {
144 pub fn new<T: Into<ProtocolComponent>>(
145 component: T,
146 token_in: Bytes,
147 token_out: Bytes,
148 ) -> Self {
149 Self {
150 component: component.into(),
151 token_in,
152 token_out,
153 split: 0.0,
154 user_data: None,
155 protocol_state: None,
156 estimated_amount_in: None,
157 }
158 }
159
160 pub fn split(mut self, split: f64) -> Self {
161 self.split = split;
162 self
163 }
164
165 pub fn user_data(mut self, user_data: Bytes) -> Self {
166 self.user_data = Some(user_data);
167 self
168 }
169
170 pub fn protocol_state(mut self, protocol_state: Arc<dyn ProtocolSim>) -> Self {
171 self.protocol_state = Some(protocol_state);
172 self
173 }
174
175 pub fn estimated_amount_in(mut self, estimated_amount_in: BigUint) -> Self {
176 self.estimated_amount_in = Some(estimated_amount_in);
177 self
178 }
179
180 pub fn build(self) -> Swap {
181 Swap {
182 component: self.component,
183 token_in: self.token_in,
184 token_out: self.token_out,
185 split: self.split,
186 user_data: self.user_data,
187 protocol_state: self.protocol_state,
188 estimated_amount_in: self.estimated_amount_in,
189 }
190 }
191}
192
193/// Represents a transaction to be executed.
194///
195/// # Fields
196/// * `to`: Address of the contract to call with the calldata
197/// * `value`: Native token value to be sent with the transaction.
198/// * `data`: Encoded calldata for the transaction.
199#[derive(Clone, Debug)]
200pub struct Transaction {
201 pub to: Bytes,
202 pub value: BigUint,
203 pub data: Vec<u8>,
204}
205
206/// Represents a solution that has been encoded for execution.
207///
208/// # Fields
209/// * `swaps`: Encoded swaps to be executed.
210/// * `interacting_with`: Address of the contract to be called.
211/// * `function_signature`: The signature of the function to be called.
212/// * `n_tokens`: Number of tokens in the swap.
213/// * `permit`: Optional permit for the swap (if permit2 is enabled).
214#[derive(Clone, Debug)]
215pub struct EncodedSolution {
216 pub swaps: Vec<u8>,
217 pub interacting_with: Bytes,
218 pub function_signature: String,
219 pub n_tokens: usize,
220 pub permit: Option<PermitSingle>,
221}
222
223/// Represents a single permit for permit2.
224///
225/// # Fields
226/// * `details`: The details of the permit, such as token, amount, expiration, and nonce.
227/// * `spender`: The address authorized to spend the tokens.
228/// * `sig_deadline`: The deadline (as a timestamp) for the permit signature
229#[derive(Debug, Clone)]
230pub struct PermitSingle {
231 pub details: PermitDetails,
232 pub spender: Bytes,
233 pub sig_deadline: BigUint,
234}
235
236/// Details of a permit.
237///
238/// # Fields
239/// * `token`: The token address for which the permit is granted.
240/// * `amount`: The amount of tokens approved for spending.
241/// * `expiration`: The expiration time (as a timestamp) for the permit.
242/// * `nonce`: The unique nonce to prevent replay attacks.
243#[derive(Debug, Clone)]
244pub struct PermitDetails {
245 pub token: Bytes,
246 pub amount: BigUint,
247 pub expiration: BigUint,
248 pub nonce: BigUint,
249}
250
251impl PartialEq for PermitSingle {
252 fn eq(&self, other: &Self) -> bool {
253 self.details == other.details && self.spender == other.spender
254 // sig_deadline is intentionally ignored
255 }
256}
257
258impl PartialEq for PermitDetails {
259 fn eq(&self, other: &Self) -> bool {
260 self.token == other.token && self.amount == other.amount && self.nonce == other.nonce
261 // expiration is intentionally ignored
262 }
263}
264
265/// Represents necessary attributes for encoding an order.
266///
267/// # Fields
268///
269/// * `receiver`: Address of the receiver of the out token after the swaps are completed.
270/// * `exact_out`: true if the solution is a buy order, false if it is a sell order.
271/// * `router_address`: Address of the router contract to be used for the swaps. Zero address if
272/// solution does not require router address.
273/// * `group_token_in`: Token to be used as the input for the group swap.
274/// * `group_token_out`: Token to be used as the output for the group swap.
275/// * `transfer`: Type of transfer to be performed. See `TransferType` for more details.
276/// * `historical_trade`: Whether the swap is to be done in the current block or in an historical
277/// one. This is relevant for checking token approvals in some protocols (like Balancer v2).
278#[derive(Clone, Debug)]
279pub struct EncodingContext {
280 pub receiver: Bytes,
281 pub exact_out: bool,
282 pub router_address: Option<Bytes>,
283 pub group_token_in: Bytes,
284 pub group_token_out: Bytes,
285 pub transfer_type: TransferType,
286 pub historical_trade: bool,
287}
288
289/// Represents the type of transfer to be performed into the pool.
290///
291/// # Fields
292///
293/// * `TransferFrom`: Transfer the token from the sender to the protocol/router.
294/// * `Transfer`: Transfer the token from the router into the protocol.
295/// * `None`: No transfer is needed. Tokens are already in the pool.
296#[repr(u8)]
297#[derive(Copy, Clone, Debug, PartialEq)]
298pub enum TransferType {
299 TransferFrom = 0,
300 Transfer = 1,
301 None = 2,
302}
303
304mod tests {
305 use super::*;
306
307 struct MockProtocolComponent {
308 id: String,
309 protocol_system: String,
310 }
311
312 impl From<MockProtocolComponent> for ProtocolComponent {
313 fn from(component: MockProtocolComponent) -> Self {
314 ProtocolComponent {
315 id: component.id,
316 protocol_system: component.protocol_system,
317 tokens: vec![],
318 protocol_type_name: "".to_string(),
319 chain: Default::default(),
320 contract_addresses: vec![],
321 static_attributes: Default::default(),
322 change: Default::default(),
323 creation_tx: Default::default(),
324 created_at: Default::default(),
325 }
326 }
327 }
328
329 #[test]
330 fn test_swap_new() {
331 let component = MockProtocolComponent {
332 id: "i-am-an-id".to_string(),
333 protocol_system: "uniswap_v2".to_string(),
334 };
335 let user_data = Some(Bytes::from("0x1234"));
336 let swap = Swap::new(
337 component,
338 Bytes::from("0x12"),
339 Bytes::from("34"),
340 0.5,
341 user_data.clone(),
342 None,
343 None,
344 );
345 assert_eq!(swap.token_in, Bytes::from("0x12"));
346 assert_eq!(swap.token_out, Bytes::from("0x34"));
347 assert_eq!(swap.component.protocol_system, "uniswap_v2");
348 assert_eq!(swap.component.id, "i-am-an-id");
349 assert_eq!(swap.split, 0.5);
350 assert_eq!(swap.user_data, user_data);
351 }
352}