chore: small improvements
- Easier less convoluted way to decode address from string - No more tokio test (not necessary) - zero_for_one -> zero_to_one for consistency
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use alloy::hex::decode;
|
|
||||||
use alloy_primitives::Address;
|
use alloy_primitives::Address;
|
||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use tycho_core::Bytes;
|
|
||||||
|
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
errors::EncodingError,
|
errors::EncodingError,
|
||||||
@@ -37,28 +35,17 @@ impl SwapEncoder for UniswapV2SwapEncoder {
|
|||||||
let token_in_address = bytes_to_address(&swap.token_in)?;
|
let token_in_address = bytes_to_address(&swap.token_in)?;
|
||||||
let token_out_address = bytes_to_address(&swap.token_out)?;
|
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 zero_to_one = Self::get_zero_to_one(token_in_address, token_out_address);
|
||||||
let component_id = Bytes::from(
|
let component_id = Address::from_str(&swap.component.id)
|
||||||
decode(
|
.map_err(|_| EncodingError::FatalError("Invalid USV2 component id".to_string()))?;
|
||||||
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,
|
// 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
|
// since no optimizations are performed that send from one pool to the next
|
||||||
let args = (
|
let args = (
|
||||||
token_in_address,
|
token_in_address,
|
||||||
bytes_to_address(&component_id)?,
|
component_id,
|
||||||
bytes_to_address(&encoding_context.receiver)?,
|
bytes_to_address(&encoding_context.receiver)?,
|
||||||
zero_for_one,
|
zero_to_one,
|
||||||
encoding_context.exact_out,
|
encoding_context.exact_out,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -93,20 +80,9 @@ impl SwapEncoder for UniswapV3SwapEncoder {
|
|||||||
let token_in_address = bytes_to_address(&swap.token_in)?;
|
let token_in_address = bytes_to_address(&swap.token_in)?;
|
||||||
let token_out_address = bytes_to_address(&swap.token_out)?;
|
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 zero_to_one = Self::get_zero_to_one(token_in_address, token_out_address);
|
||||||
let component_id = Bytes::from(
|
let component_id = Address::from_str(&swap.component.id)
|
||||||
decode(
|
.map_err(|_| EncodingError::FatalError("Invalid USV3 component id".to_string()))?;
|
||||||
swap.component
|
|
||||||
.id
|
|
||||||
.trim_start_matches("0x"),
|
|
||||||
)
|
|
||||||
.map_err(|_| {
|
|
||||||
EncodingError::FatalError(format!(
|
|
||||||
"Failed to parse component id for Uniswap v3: {}",
|
|
||||||
swap.component.id
|
|
||||||
))
|
|
||||||
})?,
|
|
||||||
);
|
|
||||||
let mut pool_fee_bytes = swap
|
let mut pool_fee_bytes = swap
|
||||||
.component
|
.component
|
||||||
.static_attributes
|
.static_attributes
|
||||||
@@ -135,8 +111,8 @@ impl SwapEncoder for UniswapV3SwapEncoder {
|
|||||||
token_out_address,
|
token_out_address,
|
||||||
pool_fee_u24,
|
pool_fee_u24,
|
||||||
bytes_to_address(&encoding_context.receiver)?,
|
bytes_to_address(&encoding_context.receiver)?,
|
||||||
bytes_to_address(&component_id)?,
|
component_id,
|
||||||
zero_for_one,
|
zero_to_one,
|
||||||
encoding_context.exact_out,
|
encoding_context.exact_out,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -239,8 +215,8 @@ mod tests {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn test_encode_uniswap_v3() {
|
fn test_encode_uniswap_v3() {
|
||||||
let encoded_pool_fee: [u8; 4] = 500u32.to_le_bytes();
|
let encoded_pool_fee: [u8; 4] = 500u32.to_le_bytes();
|
||||||
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
|
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
|
||||||
static_attributes.insert("pool_fee".into(), Bytes::from(encoded_pool_fee[..3].to_vec()));
|
static_attributes.insert("pool_fee".into(), Bytes::from(encoded_pool_fee[..3].to_vec()));
|
||||||
|
|||||||
Reference in New Issue
Block a user