Merge pull request #16 from propeller-heads/encoding/tnl/ENG-4066-usv2-swap

feat: UniswapV2 Swap Encoder
This commit is contained in:
Tamara
2025-01-22 09:57:09 -05:00
committed by GitHub
3 changed files with 84 additions and 9 deletions

View File

@@ -65,7 +65,7 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder {
let encoding_context = EncodingContext {
receiver: solution.receiver,
exact_out: solution.exact_out,
address_for_approvals: router_address,
router_address,
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
// TODO: here we need to pass also the address of the executor to be used

View File

@@ -1,7 +1,9 @@
use std::str::FromStr;
use alloy::hex::decode;
use alloy_primitives::Address;
use alloy_sol_types::SolValue;
use tycho_core::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -16,17 +18,51 @@ pub struct UniswapV2SwapEncoder {
executor_address: String,
}
impl UniswapV2SwapEncoder {}
impl UniswapV2SwapEncoder {
fn get_zero_to_one(sell_token_address: Address, buy_token_address: Address) -> bool {
sell_token_address < buy_token_address
}
}
impl SwapEncoder for UniswapV2SwapEncoder {
fn new(executor_address: String) -> Self {
Self { executor_address }
}
fn encode_swap(
&self,
_swap: Swap,
_encoding_context: EncodingContext,
swap: Swap,
encoding_context: EncodingContext,
) -> Result<Vec<u8>, EncodingError> {
todo!()
let token_in_address = bytes_to_address(&swap.token_in)?;
let token_out_address = bytes_to_address(&swap.token_out)?;
let zero_for_one = Self::get_zero_to_one(token_in_address, token_out_address);
let component_id = Bytes::from(
decode(
swap.component
.id
.trim_start_matches("0x"),
)
.map_err(|_| {
EncodingError::FatalError(format!(
"Failed to parse component id for Uniswap v2: {}",
swap.component.id
))
})?,
);
// Token in address is always needed to perform a manual transfer from the router,
// since no optimizations are performed that send from one pool to the next
let args = (
token_in_address,
bytes_to_address(&component_id)?,
bytes_to_address(&encoding_context.receiver)?,
zero_for_one,
encoding_context.exact_out,
);
Ok(args.abi_encode_packed())
}
fn executor_address(&self) -> &str {
@@ -53,7 +89,7 @@ impl SwapEncoder for BalancerV2SwapEncoder {
) -> Result<Vec<u8>, EncodingError> {
let token_approvals_manager = ProtocolApprovalsManager::new()?;
let token = bytes_to_address(&swap.token_in)?;
let router_address = bytes_to_address(&encoding_context.address_for_approvals)?;
let router_address = bytes_to_address(&encoding_context.router_address)?;
let approval_needed = token_approvals_manager.approval_needed(
token,
router_address,
@@ -80,8 +116,47 @@ impl SwapEncoder for BalancerV2SwapEncoder {
#[cfg(test)]
mod tests {
use alloy::hex::encode;
use tycho_core::{dto::ProtocolComponent, Bytes};
use super::*;
#[tokio::test]
async fn test_encode_swap() {
// Dummy test to make CI pass. Please implement me.
async fn test_encode_uniswap_v2() {
let usv2_pool = ProtocolComponent {
id: String::from("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
..Default::default()
};
let swap = Swap {
component: usv2_pool,
token_in: Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"),
token_out: Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"),
split: 0f64,
};
let encoding_context = EncodingContext {
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
exact_out: false,
router_address: Bytes::zero(20),
};
let encoder = UniswapV2SwapEncoder::new(String::from("0x"));
let encoded_swap = encoder
.encode_swap(swap, encoding_context)
.unwrap();
let hex_swap = encode(&encoded_swap);
assert_eq!(
hex_swap,
String::from(concat!(
// in token
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
// component id
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
// receiver
"0000000000000000000000000000000000000001",
// zero for one
"00",
// exact out
"00",
))
);
}
}

View File

@@ -66,5 +66,5 @@ pub struct Transaction {
pub struct EncodingContext {
pub receiver: Bytes,
pub exact_out: bool,
pub address_for_approvals: Bytes,
pub router_address: Bytes,
}