feat(tycho-router-encoder): Select strategy depending on the solution

- The tycho router address default is set at the EncoderBuilder level (not inside the strategies)
- Rename TychoCoreChain to TychoCommonChain
- Only take TychoCommonChain as an argument at the outermost level: EncoderBuilder. Everywhere else we use the execution Chain right away

--- don't change below this line ---
ENG-4332 Took 1 hour 10 minutes
This commit is contained in:
Diana Carvalho
2025-04-08 15:55:55 +01:00
parent 6fd0ab54c1
commit f5e712e0ff
6 changed files with 150 additions and 147 deletions

View File

@@ -1,7 +1,4 @@
use std::{
collections::{HashMap, HashSet},
str::FromStr,
};
use std::{collections::HashSet, str::FromStr};
use alloy_primitives::{aliases::U24, U256, U8};
use alloy_sol_types::SolValue;
@@ -11,7 +8,6 @@ use crate::encoding::{
errors::EncodingError,
evm::{
approvals::permit2::Permit2,
constants::DEFAULT_ROUTERS_JSON,
group_swaps::group_swaps,
strategy_encoder::strategy_validators::{
SequentialSwapValidator, SplitSwapValidator, SwapValidator,
@@ -47,12 +43,11 @@ pub struct SingleSwapStrategyEncoder {
impl SingleSwapStrategyEncoder {
pub fn new(
blockchain: tycho_common::models::Chain,
chain: Chain,
swap_encoder_registry: SwapEncoderRegistry,
swapper_pk: Option<String>,
router_address: Bytes,
) -> Result<Self, EncodingError> {
let chain = Chain::from(blockchain);
let (permit2, selector) = if let Some(swapper_pk) = swapper_pk {
(Some(Permit2::new(swapper_pk, chain.clone())?), "singleSwapPermit2(uint256,address,address,uint256,bool,bool,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string())
} else {
@@ -206,12 +201,11 @@ pub struct SequentialSwapStrategyEncoder {
impl SequentialSwapStrategyEncoder {
pub fn new(
blockchain: tycho_common::models::Chain,
chain: Chain,
swap_encoder_registry: SwapEncoderRegistry,
swapper_pk: Option<String>,
router_address: Bytes,
) -> Result<Self, EncodingError> {
let chain = Chain::from(blockchain);
let (permit2, selector) = if let Some(swapper_pk) = swapper_pk {
(Some(Permit2::new(swapper_pk, chain.clone())?), "sequentialSwapPermit2(uint256,address,address,uint256,bool,bool,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string())
} else {
@@ -375,12 +369,11 @@ pub struct SplitSwapStrategyEncoder {
impl SplitSwapStrategyEncoder {
pub fn new(
blockchain: tycho_common::models::Chain,
chain: Chain,
swap_encoder_registry: SwapEncoderRegistry,
swapper_pk: Option<String>,
router_address: Option<Bytes>,
router_address: Bytes,
) -> Result<Self, EncodingError> {
let chain = Chain::from(blockchain);
let (permit2, selector) = if let Some(swapper_pk) = swapper_pk {
(Some(Permit2::new(swapper_pk, chain.clone())?), "splitSwapPermit2(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string())
} else {
@@ -391,20 +384,6 @@ impl SplitSwapStrategyEncoder {
)
};
let tycho_router_address;
if let Some(address) = router_address {
tycho_router_address = address;
} else {
let default_routers: HashMap<String, Bytes> =
serde_json::from_str(DEFAULT_ROUTERS_JSON)?;
tycho_router_address = default_routers
.get(&chain.name)
.ok_or(EncodingError::FatalError(
"No default router address found for chain".to_string(),
))?
.to_owned();
}
Ok(Self {
permit2,
selector,
@@ -412,7 +391,7 @@ impl SplitSwapStrategyEncoder {
native_address: chain.native_token()?,
wrapped_address: chain.wrapped_token()?,
split_swap_validator: SplitSwapValidator,
router_address: tycho_router_address,
router_address,
})
}
@@ -601,15 +580,15 @@ mod tests {
use num_bigint::{BigInt, BigUint};
use rstest::rstest;
use tycho_common::{
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
models::{protocol::ProtocolComponent, Chain as TychoCommonChain},
Bytes,
};
use super::*;
use crate::encoding::models::Swap;
fn eth_chain() -> TychoCoreChain {
TychoCoreChain::Ethereum
fn eth_chain() -> Chain {
TychoCommonChain::Ethereum.into()
}
fn eth() -> Bytes {
@@ -675,7 +654,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -882,7 +861,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -934,7 +913,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap()),
Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
)
.unwrap();
let solution = Solution {
@@ -986,7 +965,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -1079,7 +1058,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -1291,7 +1270,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -1411,7 +1390,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
None,
Some(Bytes::from_str("0x1d1499e622D69689cdf9004d05Ec547d650Ff211").unwrap()),
Bytes::from_str("0x1d1499e622D69689cdf9004d05Ec547d650Ff211").unwrap(),
)
.unwrap();
@@ -1544,7 +1523,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
None,
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
let solution = Solution {
@@ -1637,7 +1616,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
@@ -1704,7 +1683,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
@@ -1791,7 +1770,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
@@ -1943,7 +1922,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key.clone()),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();
@@ -2101,7 +2080,7 @@ mod tests {
eth_chain(),
swap_encoder_registry,
Some(private_key.clone()),
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395"),
)
.unwrap();