feat: Remove router_address from TychoEncoder

Make the router address mandatory for the Solution attribute instead

--- don't change below this line ---
ENG-4088 Took 30 minutes
This commit is contained in:
Diana Carvalho
2025-02-07 14:58:09 +00:00
parent d7f20aa74f
commit a234ff701f
8 changed files with 42 additions and 93 deletions

View File

@@ -11,6 +11,7 @@ designed to be safe, straightforward, and quick to set up, so anyone can start t
### Installation
First, build and install the binary:
```bash
# Build the project
cargo build --release
@@ -19,12 +20,11 @@ cargo build --release
cargo install --path .
```
After installation, the `tycho-encode` command will be available to use from any directory in your terminal. The command accepts the following options:
After installation, the `tycho-encode` command will be available to use from any directory in your terminal. The command
accepts the following options:
- `-c`: Path to the executor addresses configuration file (defaults to `src/encoding/config/executor_addresses.json`)
- `-p`: Private key for signing approvals (required when direct_execution is false)
- `ROUTER_ADDRESS`: Router contract address (defaults to `0xaa820C29648D5EA543d712cC928377Bd7206a0E7`)
### Encoding Transactions
@@ -36,9 +36,6 @@ echo '<solution_payload>' | tycho-encode
# Using custom config path
echo '<solution_payload>' | tycho-encode -c /path/to/your/config.json
# Using custom router address and config path
echo '<solution_payload>' | tycho-encode -c /path/to/your/config.json 0x1234...5678
```
#### Example
@@ -49,7 +46,6 @@ Here's a complete example that encodes a swap from WETH to DAI using Uniswap V2:
echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","check_amount":"990000000000000000","router_address":"0xaa820C29648D5EA543d712cC928377Bd7206a0E7","swaps":[{"component":{"id":"0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640","protocol_system":"uniswap_v2","protocol_type_name":"UniswapV2Pool","chain":"ethereum","tokens":["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"],"contract_ids":["0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"],"static_attributes":{"factory":"0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"},"change":"Update","creation_tx":"0x0000000000000000000000000000000000000000000000000000000000000000","created_at":"2024-02-28T12:00:00"},"token_in":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","token_out":"0x6B175474E89094C44Da98b954EedeAC495271d0F","split":1.0}],"direct_execution":true}' | tycho-encode
```
#### JSON Payload Structure: Solution struct
The `Solution` struct is composed of the following fields:
@@ -64,10 +60,10 @@ The `Solution` struct is composed of the following fields:
- `expected_amount`: The expected output amount
- `check_amount`: The minimum acceptable output amount (accounting for slippage)
- `swaps`: Array of swap steps, each containing:
- `component`: Details about the DEX/protocol being used
- `token_in`: Input token address for this step
- `token_out`: Output token address for this step
- `split`: Proportion of tokens to route through this step (1.0 = 100%)
- `component`: Details about the DEX/protocol being used
- `token_in`: Input token address for this step
- `token_out`: Output token address for this step
- `split`: Proportion of tokens to route through this step (1.0 = 100%)
- `router_address`: The address of the protocol's router contract
- `direct_execution`: Boolean indicating if the transaction should be executed directly

View File

@@ -17,7 +17,8 @@ use tycho_execution::encoding::{
fn main() {
// Setup variables
let router_address = "0x1234567890abcdef1234567890abcdef12345678".to_string();
let router_address = Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678")
.expect("Failed to create router address");
let signer_pk =
Some("0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string());
let user_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
@@ -27,9 +28,8 @@ fn main() {
let strategy_encoder_registry =
EVMStrategyEncoderRegistry::new(TychoCoreChain::Ethereum, None, signer_pk.clone())
.expect("Failed to create strategy encoder registry");
let encoder =
EVMTychoEncoder::new(strategy_encoder_registry, router_address, TychoCoreChain::Ethereum)
.expect("Failed to create encoder");
let encoder = EVMTychoEncoder::new(strategy_encoder_registry, TychoCoreChain::Ethereum)
.expect("Failed to create encoder");
// ------------------- Encode a simple swap -------------------
@@ -64,6 +64,7 @@ fn main() {
exact_out: false, // it's an exact in solution
check_amount: None, // the amount out will not be checked in execution
swaps: vec![simple_swap],
router_address,
..Default::default()
};

View File

@@ -1,5 +1,4 @@
pub use clap::Parser;
pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206a0E7";
#[derive(Parser)]
/// Encode swap transactions for the Tycho router
@@ -35,10 +34,6 @@ pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206
/// }
/// ```
pub struct Cli {
/// Router contract address to use for encoding transactions
#[arg(default_value = DEFAULT_ROUTER_ADDRESS)]
pub router_address: String,
/// Private key for signing approvals (required when direct_execution is false)
#[arg(short)]
pub private_key: Option<String>,

View File

@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
// Encode the solution
let encoded = encode_swaps(&buffer, &cli.router_address, cli.config_path, cli.private_key)?;
let encoded = encode_swaps(&buffer, cli.config_path, cli.private_key)?;
// Output the encoded result as JSON to stdout
println!(
@@ -47,7 +47,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn encode_swaps(
input: &str,
router_address: &str,
config_path: Option<String>,
private_key: Option<String>,
) -> Result<Value, Box<dyn std::error::Error>> {
@@ -55,7 +54,7 @@ fn encode_swaps(
let chain = Chain::Ethereum;
let strategy_selector = EVMStrategyEncoderRegistry::new(chain, config_path, private_key)?;
let encoder = EVMTychoEncoder::new(strategy_selector, router_address.to_string(), chain)?;
let encoder = EVMTychoEncoder::new(strategy_selector, chain)?;
let transactions = encoder.encode_router_calldata(vec![solution])?;
Ok(serde_json::json!({

View File

@@ -261,11 +261,7 @@ impl SplitSwapStrategyEncoder {
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
impl StrategyEncoder for SplitSwapStrategyEncoder {
fn encode_strategy(
&self,
solution: Solution,
router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
self.validate_split_percentages(&solution.swaps)?;
self.validate_swap_path(
&solution.swaps,
@@ -274,7 +270,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
&solution.native_action,
)?;
let (permit, signature) = self.permit2.get_permit(
&router_address,
&solution.router_address,
&solution.sender,
&solution.given_token,
&solution.given_amount,
@@ -346,9 +342,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
})?;
let encoding_context = EncodingContext {
receiver: router_address.clone(),
receiver: solution.router_address.clone(),
exact_out: solution.exact_out,
router_address: router_address.clone(),
router_address: solution.router_address.clone(),
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
let swap_data = self.encode_swap_header(
@@ -399,7 +395,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
.abi_encode();
let contract_interaction = encode_input(&self.selector, method_calldata);
Ok((contract_interaction, router_address))
Ok((contract_interaction, solution.router_address))
}
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
@@ -429,17 +425,7 @@ impl ExecutorStrategyEncoder {
}
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
impl StrategyEncoder for ExecutorStrategyEncoder {
fn encode_strategy(
&self,
solution: Solution,
_router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
let router_address = solution.router_address.ok_or_else(|| {
EncodingError::InvalidInput(
"Router address is required for straight-to-executor solutions".to_string(),
)
})?;
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
let swap = solution
.swaps
.first()
@@ -457,7 +443,7 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
let encoding_context = EncodingContext {
receiver: solution.receiver,
exact_out: solution.exact_out,
router_address,
router_address: solution.router_address,
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
@@ -540,13 +526,13 @@ mod tests {
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
swaps: vec![swap],
direct_execution: true,
router_address: Some(Bytes::zero(20)),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
slippage: None,
native_action: None,
};
let (protocol_data, executor_address) = encoder
.encode_strategy(solution, Bytes::zero(20))
.encode_strategy(solution)
.unwrap();
let hex_protocol_data = encode(&protocol_data);
assert_eq!(
@@ -631,13 +617,13 @@ mod tests {
check_amount,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
let expected_input = [
@@ -731,14 +717,14 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let hex_calldata = encode(&calldata);
@@ -779,14 +765,14 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Unwrap),
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let hex_calldata = encode(&calldata);
@@ -868,13 +854,13 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc],
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let _hex_calldata = encode(&calldata);

View File

@@ -1,5 +1,3 @@
use std::str::FromStr;
use num_bigint::BigUint;
use tycho_core::Bytes;
@@ -10,31 +8,22 @@ use crate::encoding::{
tycho_encoder::TychoEncoder,
};
/// Represents an encoder for a swap through the given router address using any strategy supported
/// by the strategy registry.
/// Represents an encoder for a swap using any strategy supported by the strategy registry.
///
/// # Fields
/// * `strategy_registry`: S, the strategy registry to use to select the best strategy to encode a
/// solution, based on its supported strategies and the solution attributes.
/// * `router_address`: Bytes, the address of the router to use to execute the swaps.
/// * `native_address`: Address of the chain's native token
/// * `wrapped_address`: Address of the chain's wrapped native token
#[derive(Clone)]
pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
strategy_registry: S,
router_address: Bytes,
native_address: Bytes,
wrapped_address: Bytes,
}
impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
pub fn new(
strategy_registry: S,
router_address: String,
chain: tycho_core::dto::Chain,
) -> Result<Self, EncodingError> {
let router_address = Bytes::from_str(&router_address)
.map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?;
pub fn new(strategy_registry: S, chain: tycho_core::dto::Chain) -> Result<Self, EncodingError> {
let chain: Chain = Chain::from(chain);
if chain.name != *"ethereum" {
return Err(EncodingError::InvalidInput(
@@ -43,7 +32,6 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
}
Ok(EVMTychoEncoder {
strategy_registry,
router_address,
native_address: chain.native_token()?,
wrapped_address: chain.wrapped_token()?,
})
@@ -113,16 +101,11 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
for solution in solutions.iter() {
self.validate_solution(solution)?;
let router_address = solution
.router_address
.clone()
.unwrap_or(self.router_address.clone());
let strategy = self
.strategy_registry
.get_encoder(solution)?;
let (contract_interaction, target_address) =
strategy.encode_strategy(solution.clone(), router_address)?;
strategy.encode_strategy(solution.clone())?;
let value = match solution.native_action.as_ref() {
Some(NativeAction::Wrap) => solution.given_amount.clone(),
@@ -141,6 +124,8 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tycho_core::dto::{Chain as TychoCoreChain, ProtocolComponent};
use super::*;
@@ -185,11 +170,7 @@ mod tests {
struct MockStrategy;
impl StrategyEncoder for MockStrategy {
fn encode_strategy(
&self,
_solution: Solution,
_router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
Ok((
Bytes::from_str("0x1234")
.unwrap()
@@ -209,12 +190,7 @@ mod tests {
fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategyRegistry> {
let strategy_registry =
MockStrategyRegistry::new(TychoCoreChain::Ethereum, None, None).unwrap();
EVMTychoEncoder::new(
strategy_registry,
"0x1234567890abcdef1234567890abcdef12345678".to_string(),
TychoCoreChain::Ethereum,
)
.unwrap()
EVMTychoEncoder::new(strategy_registry, TychoCoreChain::Ethereum).unwrap()
}
#[test]
@@ -236,7 +212,7 @@ mod tests {
exact_out: false,
given_amount: eth_amount_in.clone(),
given_token: eth(),
router_address: None,
router_address: Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()

View File

@@ -41,8 +41,8 @@ pub struct Solution {
pub check_amount: Option<BigUint>,
/// List of swaps to fulfill the solution.
pub swaps: Vec<Swap>,
/// If not set, then the Tycho Router will be used
pub router_address: Option<Bytes>,
/// Address of the router contract to be used for the swaps.
pub router_address: Bytes,
/// If set, the corresponding native action will be executed.
pub native_action: Option<NativeAction>,
/// If set to true, the solution will be encoded to be sent directly to the Executor and

View File

@@ -4,11 +4,7 @@ use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::Swa
/// Encodes a solution using a specific strategy.
pub trait StrategyEncoder {
fn encode_strategy(
&self,
to_encode: Solution,
router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError>;
fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec<u8>, Bytes), EncodingError>;
#[allow(clippy::borrowed_box)]
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;