docs: add docs for TychoEncoder, StrategyEncoder and SwapEncoder traits

This commit is contained in:
royvardhan
2025-02-20 23:23:18 +05:30
parent 30a7d5bbe4
commit 4b20a1164b
3 changed files with 53 additions and 9 deletions

View File

@@ -3,26 +3,40 @@ use crate::encoding::{
models::{EncodingContext, Swap},
};
/// This trait must be implemented in order to encode a single swap for a specific protocol.
/// A trait for protocol-specific swap encoding, where each implementation should handle the
/// encoding logic for swaps on a specific protocol.
pub trait SwapEncoder: Sync + Send {
/// Creates a new swap encoder for a specific protocol.
///
/// # Arguments
/// * `executor_address` - The address of the contract that will execute the swap
fn new(executor_address: String) -> Self
where
Self: Sized;
/// Encodes a swap and its relevant context information into call data for a specific protocol.
/// Encodes a swap for execution on the protocol.
///
/// # Arguments
/// * `swap` - The swap details including the protocol component, token in, token out, and split
/// * `encoding_context` - Additional context needed for encoding (receiver of the tokens,
/// router address, etc.)
///
/// # Returns
/// The encoded swap data as bytes, directly executable on the executor contract
fn encode_swap(
&self,
swap: Swap,
encoding_context: EncodingContext,
) -> Result<Vec<u8>, EncodingError>;
/// The address of the executor that will be used to swap through a specific protocol.
/// Returns the address of the protocol-specific executor contract.
fn executor_address(&self) -> &str;
/// The selector of the executor function that will be called in order to perform a swap.
/// Returns the function selector used to execute the swap on the protocol.
fn swap_selector(&self) -> &str;
/// Clones the swap encoder as a trait object.
/// Creates a cloned instance of the swap encoder.
///
/// This allows the encoder to be cloned when it is being used as a `Box<dyn SwapEncoder>`.
fn clone_box(&self) -> Box<dyn SwapEncoder>;
}