feat: Adapt SplitSwapStrategyEncoder to have optional permit2 logic
- If the signer_pk is not passed -> use the swap method that expects the tokens to be already in the Router - If it is passed -> compute permit2 and use swap method that does the token in transfer - Extend builder to have another shortcut - Add integration test with contract - Update bin (and simplified it) and quickstart --- don't change below this line --- ENG-4255 Took 1 hour 51 minutes Took 2 minutes Took 7 seconds
This commit is contained in:
15
README.md
15
README.md
@@ -32,7 +32,18 @@ The command lets you choose the encoding strategy to be used. The available stra
|
|||||||
|
|
||||||
#### Tycho Router
|
#### Tycho Router
|
||||||
|
|
||||||
`tycho-router`: Encodes a transaction using the Tycho Router encoding strategy. Requires a private key for signing
|
`tycho-router`: Encodes a transaction using the Tycho Router encoding strategy.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
echo '<solution_payload>' | tycho-encode tycho-router
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Tycho Router With Permit2
|
||||||
|
|
||||||
|
`tycho-router-with-permit2`: Encodes a transaction using the Tycho Router encoding strategy. Requires a private key for
|
||||||
|
signing
|
||||||
Permit2.
|
Permit2.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
@@ -64,7 +75,7 @@ The commands accept the following options:
|
|||||||
Here's a complete example that encodes a swap from WETH to DAI using Uniswap V2 and the Tycho Router strategy:
|
Here's a complete example that encodes a swap from WETH to DAI using Uniswap V2 and the Tycho Router strategy:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","checked_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":0.0}],"direct_execution":true}' | tycho-encode tycho-router --swapper-pk 0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234
|
echo '{"sender":"0x1234567890123456789012345678901234567890","receiver":"0x1234567890123456789012345678901234567890","given_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","given_amount":"1000000000000000000","checked_token":"0x6B175474E89094C44Da98b954EedeAC495271d0F","exact_out":false,"slippage":0.01,"expected_amount":"1000000000000000000","checked_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":0.0}],"direct_execution":true}' | tycho-encode tycho-router-permit2 --swapper-pk 0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234
|
||||||
```
|
```
|
||||||
|
|
||||||
#### JSON Payload Structure: Solution struct
|
#### JSON Payload Structure: Solution struct
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ fn main() {
|
|||||||
// Initialize the encoder
|
// Initialize the encoder
|
||||||
let encoder = EVMEncoderBuilder::new()
|
let encoder = EVMEncoderBuilder::new()
|
||||||
.chain(TychoCoreChain::Ethereum)
|
.chain(TychoCoreChain::Ethereum)
|
||||||
.tycho_router(swapper_pk, None)
|
.tycho_router_with_permit2(None, swapper_pk)
|
||||||
.expect("Failed to create encoder builder")
|
.expect("Failed to create encoder builder")
|
||||||
.build()
|
.build()
|
||||||
.expect("Failed to build encoder");
|
.expect("Failed to build encoder");
|
||||||
|
|||||||
@@ -710,7 +710,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||||
|
|
||||||
assertTrue(success, "Call Failed");
|
assertTrue(success, "Call Failed");
|
||||||
assertGt(balancerAfter - balancerBefore, 26173932);
|
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSingleSwapWithoutPermit2Integration() public {
|
function testSingleSwapWithoutPermit2Integration() public {
|
||||||
@@ -720,17 +720,18 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
// Tests swapping WETH -> DAI on a USV2 pool without permit2
|
// Tests swapping WETH -> DAI on a USV2 pool without permit2
|
||||||
deal(WETH_ADDR, tychoRouterAddr, 1 ether);
|
deal(WETH_ADDR, tychoRouterAddr, 1 ether);
|
||||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||||
|
// Encoded solution generated using `test_split_swap_strategy_encoder_simple_route_no_permit2`
|
||||||
|
// but manually replacing the executor address
|
||||||
|
// `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test
|
||||||
|
// `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f`
|
||||||
|
(bool success,) = tychoRouterAddr.call(
|
||||||
|
hex"0a83cb080000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000008f1d5c1cae37400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000"
|
||||||
|
);
|
||||||
|
|
||||||
// TODO replace this calldata once we have the encoder side
|
vm.stopPrank();
|
||||||
// (bool success,) = tychoRouterAddr.call(
|
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||||
// hex"d499aa880000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d481bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfbc3000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f2740fde9662d8bc1f8fe8e8fc29447c1832d625f06f4a56ee5103ad555c12323af5d50eb840f73d17873383ae3b7573956d5df7b2bf76bddba768c2837894a51b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000"
|
assertTrue(success, "Call Failed");
|
||||||
// );
|
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||||
//
|
|
||||||
// vm.stopPrank();
|
|
||||||
//
|
|
||||||
// uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
|
||||||
// assertTrue(success, "Call Failed");
|
|
||||||
// assertGt(balancerAfter - balancerBefore, 26173932);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testUSV4Integration() public {
|
function testUSV4Integration() public {
|
||||||
@@ -790,7 +791,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||||
|
|
||||||
assertTrue(success, "Call Failed");
|
assertTrue(success, "Call Failed");
|
||||||
assertGt(balancerAfter - balancerBefore, 26173932);
|
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSingleSwapWithUnwrapIntegration() public {
|
function testSingleSwapWithUnwrapIntegration() public {
|
||||||
@@ -819,7 +820,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
|||||||
uint256 balancerAfter = ALICE.balance;
|
uint256 balancerAfter = ALICE.balance;
|
||||||
|
|
||||||
assertTrue(success, "Call Failed");
|
assertTrue(success, "Call Failed");
|
||||||
assertGt(balancerAfter - balancerBefore, 26173932);
|
assertEq(balancerAfter - balancerBefore, 1120007305574805922);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSplitSwapIntegration() public {
|
function testSplitSwapIntegration() public {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use serde_json::Value;
|
|
||||||
use tycho_core::models::Chain;
|
use tycho_core::models::Chain;
|
||||||
use tycho_execution::encoding::{
|
use tycho_execution::encoding::{
|
||||||
errors::EncodingError, evm::encoder_builder::EVMEncoderBuilder, models::Solution,
|
evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder,
|
||||||
tycho_encoder::TychoEncoder,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -52,6 +50,11 @@ pub enum Commands {
|
|||||||
TychoRouter {
|
TychoRouter {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
config_path: Option<String>,
|
config_path: Option<String>,
|
||||||
|
},
|
||||||
|
/// Use the Tycho router encoding strategy with Permit2 approval and token in transfer
|
||||||
|
TychoRouterPermit2 {
|
||||||
|
#[arg(short, long)]
|
||||||
|
config_path: Option<String>,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
swapper_pk: String,
|
swapper_pk: String,
|
||||||
},
|
},
|
||||||
@@ -64,6 +67,7 @@ pub enum Commands {
|
|||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
let chain = Chain::Ethereum;
|
||||||
|
|
||||||
// Read from stdin until EOF
|
// Read from stdin until EOF
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
@@ -74,16 +78,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if buffer.trim().is_empty() {
|
if buffer.trim().is_empty() {
|
||||||
return Err("No input provided. Expected JSON input on stdin.".into());
|
return Err("No input provided. Expected JSON input on stdin.".into());
|
||||||
}
|
}
|
||||||
|
let solution: Solution = serde_json::from_str(&buffer)?;
|
||||||
|
|
||||||
// Encode the solution
|
let mut builder = EVMEncoderBuilder::new().chain(chain);
|
||||||
let encoded = match cli.command {
|
|
||||||
Commands::TychoRouter { config_path, swapper_pk } => {
|
builder = match cli.command {
|
||||||
encode_swaps(&buffer, config_path, Some(swapper_pk), true)?
|
Commands::TychoRouter { config_path } => builder.tycho_router(config_path)?,
|
||||||
}
|
Commands::TychoRouterPermit2 { config_path, swapper_pk } => {
|
||||||
Commands::DirectExecution { config_path } => {
|
builder.tycho_router_with_permit2(config_path, swapper_pk)?
|
||||||
encode_swaps(&buffer, config_path, None, false)?
|
|
||||||
}
|
}
|
||||||
|
Commands::DirectExecution { config_path } => builder.direct_execution(config_path)?,
|
||||||
};
|
};
|
||||||
|
let encoder = builder.build()?;
|
||||||
|
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||||
|
let encoded = serde_json::json!({
|
||||||
|
"to": format!("0x{}", hex::encode(&transactions[0].to)),
|
||||||
|
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),
|
||||||
|
"data": format!("0x{}", hex::encode(&transactions[0].data)),
|
||||||
|
});
|
||||||
// Output the encoded result as JSON to stdout
|
// Output the encoded result as JSON to stdout
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
@@ -93,32 +105,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_swaps(
|
|
||||||
input: &str,
|
|
||||||
config_path: Option<String>,
|
|
||||||
swapper_pk: Option<String>,
|
|
||||||
use_tycho_router: bool,
|
|
||||||
) -> Result<Value, EncodingError> {
|
|
||||||
let solution: Solution = serde_json::from_str(input)?;
|
|
||||||
let chain = Chain::Ethereum;
|
|
||||||
|
|
||||||
let mut builder = EVMEncoderBuilder::new().chain(chain);
|
|
||||||
builder = if use_tycho_router {
|
|
||||||
let private_key = swapper_pk.ok_or(EncodingError::FatalError(
|
|
||||||
"Swapper private key is required for tycho_router".to_string(),
|
|
||||||
))?;
|
|
||||||
builder.tycho_router(private_key, config_path)?
|
|
||||||
} else {
|
|
||||||
builder.direct_execution(config_path)?
|
|
||||||
};
|
|
||||||
let encoder = builder.build()?;
|
|
||||||
|
|
||||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
|
||||||
|
|
||||||
Ok(serde_json::json!({
|
|
||||||
"to": format!("0x{}", hex::encode(&transactions[0].to)),
|
|
||||||
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),
|
|
||||||
"data": format!("0x{}", hex::encode(&transactions[0].data)),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -42,17 +42,35 @@ impl EVMEncoderBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` with a given `swapper_pk`.
|
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` without any approval nor token in
|
||||||
/// **Note**: Should not be used at the same time as `strategy_encoder`.
|
/// transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
|
||||||
pub fn tycho_router(
|
pub fn tycho_router(self, executors_file_path: Option<String>) -> Result<Self, EncodingError> {
|
||||||
self,
|
|
||||||
swapper_pk: String,
|
|
||||||
executors_file_path: Option<String>,
|
|
||||||
) -> Result<Self, EncodingError> {
|
|
||||||
if let Some(chain) = self.chain {
|
if let Some(chain) = self.chain {
|
||||||
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
||||||
let strategy =
|
let strategy =
|
||||||
Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?);
|
Box::new(SplitSwapStrategyEncoder::new(chain, swap_encoder_registry, None)?);
|
||||||
|
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
|
||||||
|
} else {
|
||||||
|
Err(EncodingError::FatalError(
|
||||||
|
"Please set the chain before setting the tycho router".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` with Permit2 approval and token
|
||||||
|
/// in transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
|
||||||
|
pub fn tycho_router_with_permit2(
|
||||||
|
self,
|
||||||
|
executors_file_path: Option<String>,
|
||||||
|
swapper_pk: String,
|
||||||
|
) -> Result<Self, EncodingError> {
|
||||||
|
if let Some(chain) = self.chain {
|
||||||
|
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
||||||
|
let strategy = Box::new(SplitSwapStrategyEncoder::new(
|
||||||
|
chain,
|
||||||
|
swap_encoder_registry,
|
||||||
|
Some(swapper_pk),
|
||||||
|
)?);
|
||||||
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
|
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
|
||||||
} else {
|
} else {
|
||||||
Err(EncodingError::FatalError(
|
Err(EncodingError::FatalError(
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SplitSwapStrategyEncoder {
|
pub struct SplitSwapStrategyEncoder {
|
||||||
swap_encoder_registry: SwapEncoderRegistry,
|
swap_encoder_registry: SwapEncoderRegistry,
|
||||||
permit2: Permit2,
|
permit2: Option<Permit2>,
|
||||||
selector: String,
|
selector: String,
|
||||||
native_address: Bytes,
|
native_address: Bytes,
|
||||||
wrapped_address: Bytes,
|
wrapped_address: Bytes,
|
||||||
@@ -88,14 +88,21 @@ pub struct SplitSwapStrategyEncoder {
|
|||||||
|
|
||||||
impl SplitSwapStrategyEncoder {
|
impl SplitSwapStrategyEncoder {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
swapper_pk: String,
|
|
||||||
blockchain: tycho_core::models::Chain,
|
blockchain: tycho_core::models::Chain,
|
||||||
swap_encoder_registry: SwapEncoderRegistry,
|
swap_encoder_registry: SwapEncoderRegistry,
|
||||||
|
swapper_pk: Option<String>,
|
||||||
) -> Result<Self, EncodingError> {
|
) -> Result<Self, EncodingError> {
|
||||||
let chain = Chain::from(blockchain);
|
let chain = Chain::from(blockchain);
|
||||||
let selector = "swapPermit2(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string();
|
let (permit2, selector) = if let Some(swapper_pk) = swapper_pk {
|
||||||
|
(Some(Permit2::new(swapper_pk, chain.clone())?), "swapPermit2(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string())
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
None,
|
||||||
|
"swap(uint256,address,address,uint256,bool,bool,uint256,address,bytes)".to_string(),
|
||||||
|
)
|
||||||
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
permit2: Permit2::new(swapper_pk, chain.clone())?,
|
permit2,
|
||||||
selector,
|
selector,
|
||||||
swap_encoder_registry,
|
swap_encoder_registry,
|
||||||
native_address: chain.native_token()?,
|
native_address: chain.native_token()?,
|
||||||
@@ -122,12 +129,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
|||||||
&self.native_address,
|
&self.native_address,
|
||||||
&self.wrapped_address,
|
&self.wrapped_address,
|
||||||
)?;
|
)?;
|
||||||
let (permit, signature) = self.permit2.get_permit(
|
|
||||||
&solution.router_address,
|
|
||||||
&solution.sender,
|
|
||||||
&solution.given_token,
|
|
||||||
&solution.given_amount,
|
|
||||||
)?;
|
|
||||||
let min_amount_out = get_min_amount_for_solution(solution.clone());
|
let min_amount_out = get_min_amount_for_solution(solution.clone());
|
||||||
|
|
||||||
// The tokens array is composed of the given token, the checked token and all the
|
// The tokens array is composed of the given token, the checked token and all the
|
||||||
@@ -214,7 +216,14 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let encoded_swaps = self.ple_encode(swaps);
|
let encoded_swaps = self.ple_encode(swaps);
|
||||||
let method_calldata = (
|
let method_calldata = if let Some(permit2) = self.permit2.clone() {
|
||||||
|
let (permit, signature) = permit2.get_permit(
|
||||||
|
&solution.router_address,
|
||||||
|
&solution.sender,
|
||||||
|
&solution.given_token,
|
||||||
|
&solution.given_amount,
|
||||||
|
)?;
|
||||||
|
(
|
||||||
biguint_to_u256(&solution.given_amount),
|
biguint_to_u256(&solution.given_amount),
|
||||||
bytes_to_address(&solution.given_token)?,
|
bytes_to_address(&solution.given_token)?,
|
||||||
bytes_to_address(&solution.checked_token)?,
|
bytes_to_address(&solution.checked_token)?,
|
||||||
@@ -227,7 +236,21 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
|||||||
signature.as_bytes().to_vec(),
|
signature.as_bytes().to_vec(),
|
||||||
encoded_swaps,
|
encoded_swaps,
|
||||||
)
|
)
|
||||||
.abi_encode();
|
.abi_encode()
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
biguint_to_u256(&solution.given_amount),
|
||||||
|
bytes_to_address(&solution.given_token)?,
|
||||||
|
bytes_to_address(&solution.checked_token)?,
|
||||||
|
biguint_to_u256(&min_amount_out),
|
||||||
|
wrap,
|
||||||
|
unwrap,
|
||||||
|
U256::from(tokens.len()),
|
||||||
|
bytes_to_address(&solution.receiver)?,
|
||||||
|
encoded_swaps,
|
||||||
|
)
|
||||||
|
.abi_encode()
|
||||||
|
};
|
||||||
|
|
||||||
let contract_interaction = encode_input(&self.selector, method_calldata);
|
let contract_interaction = encode_input(&self.selector, method_calldata);
|
||||||
Ok((contract_interaction, solution.router_address, None))
|
Ok((contract_interaction, solution.router_address, None))
|
||||||
@@ -606,7 +629,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let swap_encoder_registry = get_swap_encoder_registry();
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
let encoder =
|
let encoder =
|
||||||
SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap();
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||||
|
.unwrap();
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
exact_out: false,
|
exact_out: false,
|
||||||
given_token: weth,
|
given_token: weth,
|
||||||
@@ -707,7 +731,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let swap_encoder_registry = get_swap_encoder_registry();
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
let encoder =
|
let encoder =
|
||||||
SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap();
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||||
|
.unwrap();
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
exact_out: false,
|
exact_out: false,
|
||||||
given_token: eth(),
|
given_token: eth(),
|
||||||
@@ -755,7 +780,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let swap_encoder_registry = get_swap_encoder_registry();
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
let encoder =
|
let encoder =
|
||||||
SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap();
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||||
|
.unwrap();
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
exact_out: false,
|
exact_out: false,
|
||||||
given_token: dai,
|
given_token: dai,
|
||||||
@@ -844,7 +870,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let swap_encoder_registry = get_swap_encoder_registry();
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
let encoder =
|
let encoder =
|
||||||
SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap();
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||||
|
.unwrap();
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
exact_out: false,
|
exact_out: false,
|
||||||
given_token: weth,
|
given_token: weth,
|
||||||
@@ -924,7 +951,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let swap_encoder_registry = get_swap_encoder_registry();
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
let encoder =
|
let encoder =
|
||||||
SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap();
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||||
|
.unwrap();
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
exact_out: false,
|
exact_out: false,
|
||||||
given_token: usdc,
|
given_token: usdc,
|
||||||
@@ -1010,4 +1038,84 @@ mod tests {
|
|||||||
assert_eq!(hex_calldata[..520], expected_input);
|
assert_eq!(hex_calldata[..520], expected_input);
|
||||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_split_swap_strategy_encoder_simple_route_no_permit2() {
|
||||||
|
// Performs a single swap from WETH to DAI on a USV2 pool, without permit2 and no grouping
|
||||||
|
// optimizations.
|
||||||
|
|
||||||
|
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
|
||||||
|
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
|
||||||
|
|
||||||
|
let expected_amount = Some(BigUint::from_str("2_650_000000000000000000").unwrap());
|
||||||
|
let slippage = Some(0.01f64);
|
||||||
|
let checked_amount = Some(BigUint::from_str("2_640_000000000000000000").unwrap());
|
||||||
|
let expected_min_amount = U256::from_str("2_640_000000000000000000").unwrap();
|
||||||
|
|
||||||
|
let swap = Swap {
|
||||||
|
component: ProtocolComponent {
|
||||||
|
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||||
|
protocol_system: "uniswap_v2".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
token_in: weth.clone(),
|
||||||
|
token_out: dai.clone(),
|
||||||
|
split: 0f64,
|
||||||
|
};
|
||||||
|
let swap_encoder_registry = get_swap_encoder_registry();
|
||||||
|
let encoder =
|
||||||
|
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, None).unwrap();
|
||||||
|
let solution = Solution {
|
||||||
|
exact_out: false,
|
||||||
|
given_token: weth,
|
||||||
|
given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
|
||||||
|
checked_token: dai,
|
||||||
|
expected_amount,
|
||||||
|
slippage,
|
||||||
|
checked_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 (calldata, _, _) = encoder
|
||||||
|
.encode_strategy(solution)
|
||||||
|
.unwrap();
|
||||||
|
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
|
||||||
|
let expected_input = [
|
||||||
|
"0a83cb08", // Function selector
|
||||||
|
"0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out
|
||||||
|
"000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||||
|
"0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out
|
||||||
|
&expected_min_amount_encoded, // min amount out
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000", // wrap
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||||
|
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000120", // offset of ple encoded swaps
|
||||||
|
"000000000000000000000000000000000000000000000000000000000000005c", // length of ple encoded swaps without padding
|
||||||
|
"005a", // ple encoded swaps
|
||||||
|
// Swap header
|
||||||
|
"00", // token in index
|
||||||
|
"01", // token out index
|
||||||
|
"000000", // split
|
||||||
|
// Swap data
|
||||||
|
"5c2f5a71f67c01775180adc06909288b4c329308", // executor address
|
||||||
|
"bd0625ab", // selector
|
||||||
|
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||||
|
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||||
|
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||||
|
"00", // zero2one
|
||||||
|
"00", // exact out
|
||||||
|
"000000", // padding
|
||||||
|
]
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
let hex_calldata = encode(&calldata);
|
||||||
|
|
||||||
|
assert_eq!(hex_calldata, expected_input);
|
||||||
|
println!("{}", hex_calldata);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user