tycho_execution/encoding/tycho_encoder.rs
1use crate::encoding::{
2 errors::EncodingError,
3 models::{EncodedSolution, Solution},
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()`](Self::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 important
23/// 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 /// Performs solution-level validation and sanity checks.
51 ///
52 /// This function can be used to verify whether a proposed solution is structurally sound and
53 /// ready for encoding.
54 ///
55 /// # Returns
56 /// - `Ok(())` if the solution is valid.
57 /// - `Err(EncodingError)` if the solution is malformed or unsupported.
58 fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError>;
59}