feat: (WIP) Support selection of transfer into router
- For protocols like Balancer and Curve, which expect funds to be in the router at the time of swap, we must support also transferring funds from the user into the router. Doing this in the router would mean we are dealing with transfers in two different places: in the router main methods and in the executors. To avoid this, we are now performing transfers just in the executors, and two transfer types have been added to support transfers into the router. TODO: - Add this for Balancer and Curve (only added for USV4 atm). - TODO consider renaming TRANSFER_FROM and TRANSFER_PERMIT2 to include "pool" in the name
This commit is contained in:
committed by
Diana Carvalho
parent
59a80dc392
commit
a301a1cef3
@@ -1,26 +1,47 @@
|
||||
use tycho_common::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
evm::constants::IN_TRANSFER_OPTIMIZABLE_PROTOCOLS,
|
||||
evm::constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, PROTOCOLS_EXPECTING_FUNDS_IN_ROUTER},
|
||||
models::{Swap, TransferType},
|
||||
};
|
||||
|
||||
/// A trait that defines how the tokens will be transferred into the given pool given the solution.
|
||||
pub trait TransferOptimization {
|
||||
/// Returns the transfer method that should be used for the given swap and solution.
|
||||
///
|
||||
/// If the swap is for the in token of the solution and the protocol supports transferring
|
||||
/// straight from the user, it will return `TransferType::Permit2Transfer` or
|
||||
/// `TransferType::TransferFrom`.
|
||||
fn get_transfer_method(&self, swap: Swap, given_token: Bytes, permit2: bool) -> TransferType {
|
||||
let optimize_in_transfer =
|
||||
fn get_transfer_method(
|
||||
&self,
|
||||
swap: Swap,
|
||||
given_token: Bytes,
|
||||
native_token: Bytes,
|
||||
permit2: bool,
|
||||
) -> TransferType {
|
||||
let send_funds_to_pool: bool =
|
||||
IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&swap.component.protocol_system.as_str());
|
||||
if (swap.token_in == given_token) && optimize_in_transfer {
|
||||
if permit2 {
|
||||
let funds_expected_in_router: bool =
|
||||
PROTOCOLS_EXPECTING_FUNDS_IN_ROUTER.contains(&swap.component.protocol_system.as_str());
|
||||
|
||||
if (swap.token_in == given_token) && send_funds_to_pool {
|
||||
if swap.token_in == native_token {
|
||||
// Funds are already in router. Transfer from router to pool.
|
||||
TransferType::Transfer
|
||||
} else if permit2 {
|
||||
// Transfer from swapper to pool using permit2.
|
||||
TransferType::Permit2Transfer
|
||||
} else {
|
||||
// Transfer from swapper to pool.
|
||||
TransferType::TransferFrom
|
||||
}
|
||||
} else if (swap.token_in == given_token) && funds_expected_in_router {
|
||||
if swap.token_in == native_token {
|
||||
// Funds already in router. Do nothing.
|
||||
TransferType::None
|
||||
} else if permit2 {
|
||||
// Transfer from swapper to router using permit2.
|
||||
TransferType::Permit2TransferToRouter
|
||||
} else {
|
||||
// Transfer from swapper to router.
|
||||
TransferType::TransferToRouter
|
||||
}
|
||||
} else {
|
||||
TransferType::Transfer
|
||||
}
|
||||
@@ -41,6 +62,10 @@ mod tests {
|
||||
Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec())
|
||||
}
|
||||
|
||||
fn eth() -> Bytes {
|
||||
Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec())
|
||||
}
|
||||
|
||||
fn dai() -> Bytes {
|
||||
Bytes::from(hex!("6b175474e89094c44da98b954eedeac495271d0f").to_vec())
|
||||
}
|
||||
@@ -57,7 +82,7 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), true);
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), eth(), true);
|
||||
assert_eq!(transfer_method, TransferType::Permit2Transfer);
|
||||
}
|
||||
|
||||
@@ -73,7 +98,7 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), false);
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), eth(), false);
|
||||
assert_eq!(transfer_method, TransferType::TransferFrom);
|
||||
}
|
||||
|
||||
@@ -89,7 +114,7 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), dai(), false);
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), dai(), eth(), false);
|
||||
assert_eq!(transfer_method, TransferType::Transfer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user