tycho_execution/encoding/tycho_encoder.rs
1use crate::encoding::{
2 errors::EncodingError,
3 models::{EncodedSolution, Solution, Transaction},
4};
5
6/// A high-level interface for encoding solutions into Tycho-compatible transactions or raw call
7/// data.
8///
9/// This trait is designed to abstract the encoding logic required to prepare swap transactions for
10/// the Tycho Router. It enables modular and customizable construction of transactions, allowing
11/// integrators to maintain full control over the execution constraints.
12///
13/// # User Responsibility
14///
15/// While this trait provides convenience methods, it is **strongly recommended** that users favor
16/// [`encode_solutions`] over [`encode_calldata`]. This is because:
17///
18/// - `encode_solutions` returns raw [`EncodedSolution`] objects, which include Tycho’s swap path
19/// encoding, but leave **function argument encoding entirely in the user’s hands**.
20/// - The function arguments to the router (e.g., `minAmountOut`, `receiver`, `unwrap`, `permit2`,
21/// etc.) are used as **guardrails** to ensure safe on-chain execution.
22/// - Automatically constructing full transactions via [`encode_calldata`] can obscure these
23/// important safeguards and may result in unexpected behavior or vulnerability to MEV.
24///
25/// Tycho is only responsible for generating the internal swap plan. **The user must encode the
26/// outer function call arguments themselves** and verify that they enforce correct and secure
27/// behavior.
28pub trait TychoEncoder: Send + Sync {
29 /// Encodes a list of [`Solution`]s into [`EncodedSolution`]s, which include the function
30 /// signature and internal swap call data.
31 ///
32 /// This method gives users maximum flexibility and control. It **does not** produce full
33 /// transaction objects. Users are responsible for:
34 /// - Constructing the full calldata using their own encoding logic.
35 /// - Managing execution-critical parameters like `minAmountOut`.
36 ///
37 /// # Returns
38 /// A vector of encoded solutions, each containing:
39 /// - The Tycho method function signature
40 /// - The encoded swap path
41 /// - Additional metadata (e.g., permit2 information)
42 ///
43 /// # Recommendation
44 /// Use this method if you care about execution safety and want to avoid surprises.
45 fn encode_solutions(
46 &self,
47 solutions: Vec<Solution>,
48 ) -> Result<Vec<EncodedSolution>, EncodingError>;
49
50 /// Encodes a list of [`Solution`]s directly into executable transactions for the Tycho router.
51 ///
52 /// This method wraps around Tycho’s example encoding logic (see [`encode_tycho_router_call`])
53 /// and should only be used for **prototyping or development**.
54 ///
55 /// # Warning
56 /// This implementation uses default logic to construct the outer calldata (e.g., for setting
57 /// `minAmountOut`). This might not be optimal or safe for production use.
58 ///
59 /// To ensure correctness, **users should implement their own encoding pipeline** using
60 /// [`encode_solutions`].
61 ///
62 /// # Returns
63 /// A vector of fully constructed [`Transaction`]s that can be submitted to a node or bundler.
64 #[deprecated(note = "Please use `encode_solutions` instead")]
65 fn encode_full_calldata(
66 &self,
67 solutions: Vec<Solution>,
68 ) -> Result<Vec<Transaction>, EncodingError>;
69
70 /// Performs solution-level validation and sanity checks.
71 ///
72 /// This function can be used to verify whether a proposed solution is structurally sound and
73 /// ready for encoding.
74 ///
75 /// # Returns
76 /// - `Ok(())` if the solution is valid.
77 /// - `Err(EncodingError)` if the solution is malformed or unsupported.
78 fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError>;
79}