feat: Add estimated_amount_in to Swap. Add SwapBuilder

Use SwapBuilder everywhere inside crate
Integration tests will be done later

--- don't change below this line ---
ENG-4696 Took 1 hour 56 minutes

Took 4 minutes
This commit is contained in:
Diana Carvalho
2025-08-13 18:00:46 +01:00
parent 616b588fc8
commit 5eb9973dbd
15 changed files with 647 additions and 763 deletions

View File

@@ -87,7 +87,7 @@ mod tests {
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
use crate::encoding::models::Swap;
use crate::encoding::models::SwapBuilder;
fn weth() -> Bytes {
Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec())
@@ -105,41 +105,26 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swap_weth_wbtc = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: wbtc.clone(),
// This represents the remaining 50%, but to avoid any rounding errors we set this to
// 0 to signify "the remainder of the WETH value". It should still be very close to 50%
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_wbtc_usdc = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: wbtc.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_usdc_dai = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: usdc.clone(),
token_out: dai.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_weth_wbtc = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
weth.clone(),
wbtc.clone(),
)
.build();
let swap_wbtc_usdc = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
wbtc.clone(),
usdc.clone(),
)
.build();
let swap_usdc_dai = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v2".to_string(), ..Default::default() },
usdc.clone(),
dai.clone(),
)
.build();
let swaps = vec![swap_weth_wbtc.clone(), swap_wbtc_usdc.clone(), swap_usdc_dai.clone()];
let grouped_swaps = group_swaps(&swaps);
@@ -179,52 +164,34 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swap_wbtc_weth = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: wbtc.clone(),
token_out: weth.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_weth_usdc = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0.5f64,
user_data: None,
protocol_state: None,
};
let swap_weth_dai = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
// This represents the remaining 50%, but to avoid any rounding errors we set this to
// 0 to signify "the remainder of the WETH value". It should still be very close to 50%
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_dai_usdc = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: dai.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_wbtc_weth = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
wbtc.clone(),
weth.clone(),
)
.build();
let swap_weth_usdc = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
weth.clone(),
usdc.clone(),
)
.split(0.5f64)
.build();
let swap_weth_dai = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
weth.clone(),
dai.clone(),
)
.build();
// Split 0 represents the remaining 50%, but to avoid any rounding errors we set this to
// 0 to signify "the remainder of the WETH value". It should still be very close to 50%
let swap_dai_usdc = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
dai.clone(),
usdc.clone(),
)
.build();
let swaps = vec![
swap_wbtc_weth.clone(),
swap_weth_usdc.clone(),
@@ -275,52 +242,38 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swap_weth_wbtc = Swap {
component: ProtocolComponent {
let swap_weth_wbtc = SwapBuilder::new(
ProtocolComponent {
protocol_system: "vm:balancer_v3".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: wbtc.clone(),
split: 0.5f64,
user_data: None,
protocol_state: None,
};
let swap_wbtc_usdc = Swap {
component: ProtocolComponent {
weth.clone(),
wbtc.clone(),
)
.split(0.5f64)
.build();
let swap_wbtc_usdc = SwapBuilder::new(
ProtocolComponent {
protocol_system: "vm:balancer_v3".to_string(),
..Default::default()
},
token_in: wbtc.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_weth_dai = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
// This represents the remaining 50%, but to avoid any rounding errors we set this to
// 0 to signify "the remainder of the WETH value". It should still be very close to 50%
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_dai_usdc = Swap {
component: ProtocolComponent {
protocol_system: "uniswap_v4".to_string(),
..Default::default()
},
token_in: dai.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
wbtc.clone(),
usdc.clone(),
)
.build();
let swap_weth_dai = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
weth.clone(),
dai.clone(),
)
.build();
let swap_dai_usdc = SwapBuilder::new(
ProtocolComponent { protocol_system: "uniswap_v4".to_string(), ..Default::default() },
dai.clone(),
usdc.clone(),
)
.build();
let swaps = vec![
swap_weth_wbtc.clone(),

View File

@@ -518,7 +518,6 @@ mod tests {
};
use super::*;
use crate::encoding::models::Swap;
fn eth_chain() -> Chain {
Chain::Ethereum
@@ -539,8 +538,8 @@ mod tests {
}
mod single {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_single_swap_strategy_encoder() {
// Performs a single swap from WETH to DAI on a USV2 pool, with no grouping
@@ -549,18 +548,16 @@ mod tests {
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth.clone(),
dai.clone(),
)
.build();
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SingleSwapStrategyEncoder::new(
eth_chain(),
@@ -611,18 +608,16 @@ mod tests {
let checked_amount = BigUint::from_str("1_640_000000000000000000").unwrap();
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth.clone(),
dai.clone(),
)
.build();
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SingleSwapStrategyEncoder::new(
eth_chain(),
@@ -672,6 +667,7 @@ mod tests {
mod sequential {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_sequential_swap_strategy_encoder_no_permit2() {
@@ -683,30 +679,26 @@ mod tests {
let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let swap_weth_wbtc = Swap {
component: ProtocolComponent {
let swap_weth_wbtc = SwapBuilder::new(
ProtocolComponent {
id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: wbtc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap_wbtc_usdc = Swap {
component: ProtocolComponent {
weth.clone(),
wbtc.clone(),
)
.build();
let swap_wbtc_usdc = SwapBuilder::new(
ProtocolComponent {
id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: wbtc.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
wbtc.clone(),
usdc.clone(),
)
.build();
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SequentialSwapStrategyEncoder::new(
eth_chain(),
@@ -764,6 +756,7 @@ mod tests {
mod split {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_split_input_cyclic_swap() {
@@ -779,8 +772,8 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
// USDC -> WETH (Pool 1) - 60% of input
let swap_usdc_weth_pool1 = Swap {
component: ProtocolComponent {
let swap_usdc_weth_pool1 = SwapBuilder::new(
ProtocolComponent {
id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
* Pool 1 */
protocol_system: "uniswap_v3".to_string(),
@@ -794,16 +787,15 @@ mod tests {
},
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0.6f64, // 60% of input
user_data: None,
protocol_state: None,
};
usdc.clone(),
weth.clone(),
)
.split(0.6f64)
.build();
// USDC -> WETH (Pool 2) - 40% of input (remaining)
let swap_usdc_weth_pool2 = Swap {
component: ProtocolComponent {
let swap_usdc_weth_pool2 = SwapBuilder::new(
ProtocolComponent {
id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
* Pool 2 */
protocol_system: "uniswap_v3".to_string(),
@@ -817,16 +809,14 @@ mod tests {
},
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0f64,
user_data: None, // Remaining 40%
protocol_state: None,
};
usdc.clone(),
weth.clone(),
)
.build();
// WETH -> USDC (Pool 2)
let swap_weth_usdc_pool2 = Swap {
component: ProtocolComponent {
let swap_weth_usdc_pool2 = SwapBuilder::new(
ProtocolComponent {
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), /* USDC-WETH USV2
* Pool 2 */
protocol_system: "uniswap_v2".to_string(),
@@ -840,13 +830,10 @@ mod tests {
},
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0.0f64,
user_data: None,
protocol_state: None,
};
weth.clone(),
usdc.clone(),
)
.build();
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(
eth_chain(),
@@ -935,8 +922,8 @@ mod tests {
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let swap_usdc_weth_v2 = Swap {
component: ProtocolComponent {
let swap_usdc_weth_v2 = SwapBuilder::new(
ProtocolComponent {
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), // USDC-WETH USV2
protocol_system: "uniswap_v2".to_string(),
static_attributes: {
@@ -949,15 +936,13 @@ mod tests {
},
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0.0f64,
user_data: None,
protocol_state: None,
};
usdc.clone(),
weth.clone(),
)
.build();
let swap_weth_usdc_v3_pool1 = Swap {
component: ProtocolComponent {
let swap_weth_usdc_v3_pool1 = SwapBuilder::new(
ProtocolComponent {
id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
* Pool 1 */
protocol_system: "uniswap_v3".to_string(),
@@ -971,17 +956,16 @@ mod tests {
},
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0.6f64,
user_data: None,
protocol_state: None,
};
weth.clone(),
usdc.clone(),
)
.split(0.6f64)
.build();
let swap_weth_usdc_v3_pool2 = Swap {
component: ProtocolComponent {
let swap_weth_usdc_v3_pool2 = SwapBuilder::new(
ProtocolComponent {
id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
* Pool 2 */
* Pool 1 */
protocol_system: "uniswap_v3".to_string(),
static_attributes: {
let mut attrs = HashMap::new();
@@ -993,12 +977,10 @@ mod tests {
},
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0.0f64,
user_data: None,
protocol_state: None,
};
weth.clone(),
usdc.clone(),
)
.build();
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(

View File

@@ -197,7 +197,7 @@ mod tests {
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
use crate::encoding::models::Swap;
use crate::encoding::models::{Swap, SwapBuilder};
#[test]
fn test_validate_path_single_swap() {
@@ -205,18 +205,16 @@ mod tests {
let eth = Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap();
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swaps = vec![Swap {
component: ProtocolComponent {
let swaps = vec![SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}];
weth.clone(),
dai.clone(),
)
.build()];
let result = validator.validate_swap_path(&swaps, &weth, &dai, &None, &eth, &weth);
assert_eq!(result, Ok(()));
}
@@ -229,30 +227,27 @@ mod tests {
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.5f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.5f64)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
},
dai.clone(),
usdc.clone(),
)
.build(),
];
let result = validator.validate_swap_path(&swaps, &weth, &usdc, &None, &eth, &weth);
assert_eq!(result, Ok(()));
@@ -268,31 +263,28 @@ mod tests {
let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
let disconnected_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.5,
user_data: None,
protocol_state: None,
},
weth.clone(),
dai.clone(),
)
.split(0.5f64)
.build(),
// This swap is disconnected from the WETH->DAI path
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: wbtc.clone(),
token_out: usdc.clone(),
split: 0.0,
user_data: None,
protocol_state: None,
},
wbtc.clone(),
usdc.clone(),
)
.build(),
];
let result =
validator.validate_swap_path(&disconnected_swaps, &weth, &usdc, &None, &eth, &weth);
@@ -310,30 +302,26 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let cyclic_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
usdc.clone(),
weth.clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
},
weth.clone(),
usdc.clone(),
)
.build(),
];
// Test with USDC as both given token and checked token
@@ -349,18 +337,17 @@ mod tests {
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let unreachable_swaps = vec![Swap {
component: ProtocolComponent {
let unreachable_swaps = vec![SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 1.0,
user_data: None,
protocol_state: None,
}];
weth.clone(),
dai.clone(),
)
.split(1.0)
.build()];
let result =
validator.validate_swap_path(&unreachable_swaps, &weth, &usdc, &None, &eth, &weth);
assert!(matches!(
@@ -389,18 +376,16 @@ mod tests {
let validator = SplitSwapValidator;
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swaps = vec![Swap {
component: ProtocolComponent {
let swaps = vec![SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}];
weth.clone(),
dai.clone(),
)
.build()];
let result = validator.validate_split_percentages(&swaps);
assert_eq!(result, Ok(()));
}
@@ -413,42 +398,38 @@ mod tests {
// Valid case: Multiple swaps with proper splits (50%, 30%, remainder)
let valid_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.5,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.5)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.3,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.3)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool3".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.0, // Remainder (20%)
user_data: None,
protocol_state: None,
},
weth.clone(),
dai.clone(),
)
.build(),
];
assert!(validator
.validate_split_percentages(&valid_swaps)
@@ -462,30 +443,28 @@ mod tests {
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let invalid_total_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.7,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.7)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.3,
user_data: None,
protocol_state: None,
},
weth.clone(),
dai.clone(),
)
.split(0.3)
.build(),
];
assert!(matches!(
validator.validate_split_percentages(&invalid_total_swaps),
@@ -500,30 +479,27 @@ mod tests {
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let invalid_zero_position_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.0,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.5,
user_data: None,
protocol_state: None,
},
weth.clone(),
dai.clone(),
)
.split(0.5)
.build(),
];
assert!(matches!(
validator.validate_split_percentages(&invalid_zero_position_swaps),
@@ -538,42 +514,38 @@ mod tests {
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let invalid_overflow_swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.6,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.6)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool2".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.5,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth.clone(),
dai.clone(),
)
.split(0.5)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "pool3".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0.0,
user_data: None,
protocol_state: None,
},
weth.clone(),
dai.clone(),
)
.build(),
];
assert!(matches!(
validator.validate_split_percentages(&invalid_overflow_swaps),
@@ -588,18 +560,16 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let weth = Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap();
let swaps = vec![Swap {
component: ProtocolComponent {
let swaps = vec![SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: usdc.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}];
weth.clone(),
usdc.clone(),
)
.build()];
let result = validator.validate_swap_path(
&swaps,
@@ -619,18 +589,16 @@ mod tests {
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
let weth = Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap();
let swaps = vec![Swap {
component: ProtocolComponent {
let swaps = vec![SwapBuilder::new(
ProtocolComponent {
id: "pool1".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}];
usdc.clone(),
weth.clone(),
)
.build()];
let result = validator.validate_swap_path(
&swaps,

View File

@@ -121,7 +121,7 @@ mod tests {
use tycho_common::models::protocol::ProtocolComponent;
use super::*;
use crate::encoding::models::Swap;
use crate::encoding::models::SwapBuilder;
fn weth() -> Bytes {
Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec())
@@ -174,18 +174,16 @@ mod tests {
#[case] expected_transfer: TransferType,
) {
// The swap token is the same as the given token, which is not the native token
let swaps = vec![Swap {
component: ProtocolComponent {
let swaps = vec![SwapBuilder::new(
ProtocolComponent {
protocol_system: "uniswap_v2".to_string(),
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
..Default::default()
},
token_in: swap_token_in.clone(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
}];
swap_token_in.clone(),
dai(),
)
.build()];
let swap = SwapGroup {
protocol_system: protocol,
token_in: swap_token_in,
@@ -240,18 +238,16 @@ mod tests {
token_in: usdc(),
token_out: dai(),
split: 0f64,
swaps: vec![Swap {
component: ProtocolComponent {
swaps: vec![SwapBuilder::new(
ProtocolComponent {
protocol_system: protocol.unwrap().to_string(),
id: component_id().to_string(),
..Default::default()
},
token_in: usdc(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
}],
usdc(),
dai(),
)
.build()],
})
};

View File

@@ -651,10 +651,14 @@ mod tests {
};
use super::*;
use crate::encoding::{evm::utils::write_calldata_to_file, models::TransferType};
use crate::encoding::{
evm::utils::write_calldata_to_file,
models::{SwapBuilder, TransferType},
};
mod uniswap_v2 {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_encode_uniswap_v2() {
let usv2_pool = ProtocolComponent {
@@ -664,14 +668,7 @@ mod tests {
let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f");
let swap = Swap {
component: usv2_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(usv2_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
receiver: Bytes::from("0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e"), // BOB
exact_out: false,
@@ -711,6 +708,7 @@ mod tests {
mod uniswap_v3 {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_encode_uniswap_v3() {
let fee = BigInt::from(500);
@@ -725,14 +723,7 @@ mod tests {
};
let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f");
let swap = Swap {
component: usv3_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(usv3_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
exact_out: false,
@@ -775,6 +766,7 @@ mod tests {
mod balancer_v2 {
use super::*;
use crate::encoding::models::SwapBuilder;
#[test]
fn test_encode_balancer_v2() {
@@ -787,14 +779,7 @@ mod tests {
};
let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
let token_out = Bytes::from("0xba100000625a3754423978a60c9317c58a424e3D");
let swap = Swap {
component: balancer_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(balancer_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
@@ -841,7 +826,6 @@ mod tests {
mod uniswap_v4 {
use super::*;
use crate::encoding::evm::utils::write_calldata_to_file;
#[test]
fn test_encode_uniswap_v4_simple_swap() {
@@ -861,14 +845,7 @@ mod tests {
static_attributes,
..Default::default()
};
let swap = Swap {
component: usv4_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(usv4_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver is ALICE to match the solidity tests
receiver: Bytes::from("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2"),
@@ -935,14 +912,7 @@ mod tests {
..Default::default()
};
let swap = Swap {
component: usv4_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(usv4_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
@@ -1033,23 +1003,12 @@ mod tests {
..Default::default()
};
let initial_swap = Swap {
component: usde_usdt_component,
token_in: usde_address.clone(),
token_out: usdt_address.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let second_swap = Swap {
component: usdt_wbtc_component,
token_in: usdt_address,
token_out: wbtc_address.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let initial_swap =
SwapBuilder::new(usde_usdt_component, usde_address.clone(), usdt_address.clone())
.build();
let second_swap =
SwapBuilder::new(usdt_wbtc_component, usdt_address.clone(), wbtc_address.clone())
.build();
let encoder = UniswapV4SwapEncoder::new(
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
@@ -1100,7 +1059,6 @@ mod tests {
}
mod ekubo {
use super::*;
use crate::encoding::evm::utils::write_calldata_to_file;
const RECEIVER: &str = "ca4f73fe97d0b987a0d12b39bbd562c779bab6f6"; // Random address
@@ -1120,14 +1078,7 @@ mod tests {
let component = ProtocolComponent { static_attributes, ..Default::default() };
let swap = Swap {
component,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(component, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
receiver: RECEIVER.into(),
@@ -1180,8 +1131,8 @@ mod tests {
transfer_type: TransferType::Transfer,
};
let first_swap = Swap {
component: ProtocolComponent {
let first_swap = SwapBuilder::new(
ProtocolComponent {
static_attributes: HashMap::from([
("fee".to_string(), Bytes::from(0_u64)),
("tick_spacing".to_string(), Bytes::from(0_u32)),
@@ -1192,15 +1143,13 @@ mod tests {
]),
..Default::default()
},
token_in: group_token_in.clone(),
token_out: intermediary_token.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
group_token_in.clone(),
intermediary_token.clone(),
)
.build();
let second_swap = Swap {
component: ProtocolComponent {
let second_swap = SwapBuilder::new(
ProtocolComponent {
// 0.0025% fee & 0.005% base pool
static_attributes: HashMap::from([
("fee".to_string(), Bytes::from(461168601842738_u64)),
@@ -1209,12 +1158,10 @@ mod tests {
]),
..Default::default()
},
token_in: intermediary_token.clone(),
token_out: group_token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
intermediary_token.clone(),
group_token_out.clone(),
)
.build();
let first_encoded_swap = encoder
.encode_swap(&first_swap, &encoding_context)
@@ -1323,19 +1270,18 @@ mod tests {
) {
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
static_attributes.insert("coins".into(), Bytes::from_str(coins).unwrap());
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "pool-id".into(),
protocol_system: String::from("vm:curve"),
static_attributes,
..Default::default()
},
token_in: Bytes::from(token_in),
token_out: Bytes::from(token_out),
split: 0f64,
user_data: None,
protocol_state: None,
};
Bytes::from(token_in),
Bytes::from(token_out),
)
.build();
let encoder =
CurveSwapEncoder::new(String::default(), Chain::Ethereum, curve_config()).unwrap();
let (i, j) = encoder
@@ -1369,14 +1315,9 @@ mod tests {
};
let token_in = Bytes::from("0x6B175474E89094C44Da98b954EedeAC495271d0F");
let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
let swap = Swap {
component: curve_tri_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap =
SwapBuilder::new(curve_tri_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
@@ -1442,14 +1383,7 @@ mod tests {
};
let token_in = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
let token_out = Bytes::from("0x4c9EDD5852cd905f086C759E8383e09bff1E68B3");
let swap = Swap {
component: curve_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(curve_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
@@ -1516,14 +1450,7 @@ mod tests {
};
let token_in = Bytes::from("0x0000000000000000000000000000000000000000");
let token_out = Bytes::from("0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84");
let swap = Swap {
component: curve_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(curve_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
@@ -1591,14 +1518,7 @@ mod tests {
};
let token_in = Bytes::from("0x7bc3485026ac48b6cf9baf0a377477fff5703af8");
let token_out = Bytes::from("0xc71ea051a5f82c67adcf634c36ffe6334793d24c");
let swap = Swap {
component: balancer_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(balancer_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
@@ -1650,14 +1570,7 @@ mod tests {
};
let token_in = Bytes::from("0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f");
let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
let swap = Swap {
component: maverick_pool,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
let swap = SwapBuilder::new(maverick_pool, token_in.clone(), token_out.clone()).build();
let encoding_context = EncodingContext {
// The receiver was generated with `makeAddr("bob") using forge`
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),

View File

@@ -393,7 +393,7 @@ mod tests {
use tycho_common::models::{protocol::ProtocolComponent, Chain};
use super::*;
use crate::encoding::models::Swap;
use crate::encoding::models::{Swap, SwapBuilder};
fn dai() -> Bytes {
Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
@@ -428,20 +428,18 @@ mod tests {
let mut static_attributes_usdc_eth: HashMap<String, Bytes> = HashMap::new();
static_attributes_usdc_eth.insert("key_lp_fee".into(), pool_fee_usdc_eth);
static_attributes_usdc_eth.insert("tick_spacing".into(), tick_spacing_usdc_eth);
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xdce6394339af00981949f5f3baf27e3610c76326a700af57e4b3e3ae4977f78d"
.to_string(),
protocol_system: "uniswap_v4".to_string(),
static_attributes: static_attributes_usdc_eth,
..Default::default()
},
token_in: usdc().clone(),
token_out: eth().clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}
usdc().clone(),
eth().clone(),
)
.build()
}
fn swap_eth_pepe_univ4() -> Swap<'static> {
@@ -450,20 +448,18 @@ mod tests {
let mut static_attributes_eth_pepe: HashMap<String, Bytes> = HashMap::new();
static_attributes_eth_pepe.insert("key_lp_fee".into(), pool_fee_eth_pepe);
static_attributes_eth_pepe.insert("tick_spacing".into(), tick_spacing_eth_pepe);
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xecd73ecbf77219f21f129c8836d5d686bbc27d264742ddad620500e3e548e2c9"
.to_string(),
protocol_system: "uniswap_v4".to_string(),
static_attributes: static_attributes_eth_pepe,
..Default::default()
},
token_in: eth().clone(),
token_out: pepe().clone(),
split: 0f64,
user_data: None,
protocol_state: None,
}
eth().clone(),
pepe().clone(),
)
.build()
}
fn router_address() -> Bytes {
@@ -501,18 +497,16 @@ mod tests {
fn test_encode_router_calldata_single_swap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let eth_amount_in = BigUint::from(1000u32);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth().clone(),
dai().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -567,31 +561,27 @@ mod tests {
fn test_encode_router_calldata_sequential_swap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let eth_amount_in = BigUint::from(1000u32);
let swap_weth_dai = Swap {
component: ProtocolComponent {
let swap_weth_dai = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth().clone(),
dai().clone(),
)
.build();
let swap_dai_usdc = Swap {
component: ProtocolComponent {
let swap_dai_usdc = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: usdc(),
split: 0f64,
user_data: None,
protocol_state: None,
};
dai().clone(),
usdc().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -661,18 +651,16 @@ mod tests {
#[test]
fn test_validate_passes_for_wrap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth().clone(),
dai().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -691,18 +679,16 @@ mod tests {
#[test]
fn test_validate_fails_for_wrap_wrong_input() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
};
weth().clone(),
dai().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -726,18 +712,16 @@ mod tests {
#[test]
fn test_validate_fails_for_wrap_wrong_first_swap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: eth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
};
eth().clone(),
dai().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -781,18 +765,16 @@ mod tests {
#[test]
fn test_validate_passes_for_unwrap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
};
dai().clone(),
weth().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -810,18 +792,16 @@ mod tests {
#[test]
fn test_validate_fails_for_unwrap_wrong_output() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
};
dai().clone(),
weth().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -846,18 +826,16 @@ mod tests {
#[test]
fn test_validate_fails_for_unwrap_wrong_last_swap() {
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: eth(),
split: 0f64,
user_data: None,
protocol_state: None,
};
dai().clone(),
eth().clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -887,42 +865,36 @@ mod tests {
// (some of the pool addresses in this test are fake)
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0.5f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
dai().clone(),
weth().clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
dai().clone(),
weth().clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
},
weth().clone(),
dai().clone(),
)
.build(),
];
let solution = Solution {
@@ -945,54 +917,46 @@ mod tests {
// (some of the pool addresses in this test are fake)
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
dai().clone(),
weth().clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: usdc(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth().clone(),
usdc().clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: usdc(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
usdc().clone(),
dai().clone(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: wbtc(),
split: 0f64,
user_data: None,
protocol_state: None,
},
dai().clone(),
wbtc().clone(),
)
.build(),
];
let solution = Solution {
@@ -1022,42 +986,37 @@ mod tests {
// (some of the pool addresses in this test are fake)
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
weth(),
dai(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0.5f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
dai(),
weth(),
)
.split(0.5)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
},
dai(),
weth(),
)
.build(),
];
let solution = Solution {
@@ -1080,30 +1039,26 @@ mod tests {
// (some of the pool addresses in this test are fake)
let encoder = get_tycho_router_encoder(UserTransferType::TransferFrom);
let swaps = vec![
Swap {
component: ProtocolComponent {
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth(),
token_out: dai(),
split: 0f64,
user_data: None,
protocol_state: None,
},
Swap {
component: ProtocolComponent {
id: "0x0000000000000000000000000000000000000000".to_string(),
weth(),
dai(),
)
.build(),
SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: dai(),
token_out: weth(),
split: 0f64,
user_data: None,
protocol_state: None,
},
dai(),
weth(),
)
.build(),
];
let solution = Solution {
@@ -1137,7 +1092,7 @@ mod tests {
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
use crate::encoding::models::{Solution, Swap};
use crate::encoding::models::Solution;
#[test]
fn test_executor_encoder_encode() {
@@ -1147,18 +1102,16 @@ mod tests {
let token_in = weth();
let token_out = dai();
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
token_in.clone(),
token_out.clone(),
)
.build();
let solution = Solution {
exact_out: false,
@@ -1209,17 +1162,16 @@ mod tests {
let token_in = weth();
let token_out = dai();
let swap = Swap {
component: ProtocolComponent {
let swap = SwapBuilder::new(
ProtocolComponent {
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
user_data: None,
protocol_state: None,
};
token_in.clone(),
token_out.clone(),
)
.build();
let solution = Solution {
exact_out: false,

View File

@@ -89,6 +89,9 @@ pub struct Swap<'a> {
/// Optional protocol state used to perform the swap.
#[serde(skip)]
pub protocol_state: Option<&'a dyn ProtocolSim>,
/// Optional estimated amount in for this Swap. This is necessary for RFQ protocols. This value
/// is used to request the quote
pub estimated_amount_in: Option<BigUint>,
}
impl<'a> Swap<'a> {
@@ -99,8 +102,17 @@ impl<'a> Swap<'a> {
split: f64,
user_data: Option<Bytes>,
protocol_state: Option<&'a dyn ProtocolSim>,
estimated_amount_in: Option<BigUint>,
) -> Self {
Self { component: component.into(), token_in, token_out, split, user_data, protocol_state }
Self {
component: component.into(),
token_in,
token_out,
split,
user_data,
protocol_state,
estimated_amount_in,
}
}
}
@@ -115,6 +127,66 @@ impl<'a> PartialEq for Swap<'a> {
}
}
pub struct SwapBuilder<'a> {
component: ProtocolComponent,
token_in: Bytes,
token_out: Bytes,
split: f64,
user_data: Option<Bytes>,
protocol_state: Option<&'a dyn ProtocolSim>,
estimated_amount_in: Option<BigUint>,
}
impl<'a> SwapBuilder<'a> {
pub fn new<T: Into<ProtocolComponent>>(
component: T,
token_in: Bytes,
token_out: Bytes,
) -> Self {
Self {
component: component.into(),
token_in,
token_out,
split: 0.0,
user_data: None,
protocol_state: None,
estimated_amount_in: None,
}
}
pub fn split(mut self, split: f64) -> Self {
self.split = split;
self
}
pub fn user_data(mut self, user_data: Bytes) -> Self {
self.user_data = Some(user_data);
self
}
pub fn protocol_state(mut self, protocol_state: &'a dyn ProtocolSim) -> Self {
self.protocol_state = Some(protocol_state);
self
}
pub fn estimated_amount_in(mut self, estimated_amount_in: BigUint) -> Self {
self.estimated_amount_in = Some(estimated_amount_in);
self
}
pub fn build(self) -> Swap<'a> {
Swap {
component: self.component,
token_in: self.token_in,
token_out: self.token_out,
split: self.split,
user_data: self.user_data,
protocol_state: self.protocol_state,
estimated_amount_in: self.estimated_amount_in,
}
}
}
/// Represents a transaction to be executed.
///
/// # Fields
@@ -262,6 +334,7 @@ mod tests {
0.5,
user_data.clone(),
None,
None,
);
assert_eq!(swap.token_in, Bytes::from("0x12"));
assert_eq!(swap.token_out, Bytes::from("0x34"));