Add encoder tests
This commit is contained in:
@@ -263,11 +263,11 @@ impl SwapEncoder for BalancerV2SwapEncoder {
|
|||||||
/// # Fields
|
/// # Fields
|
||||||
/// * `executor_address` - The address of the executor contract that will perform the swap.
|
/// * `executor_address` - The address of the executor contract that will perform the swap.
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct EkuboEncoder {
|
pub struct EkuboSwapEncoder {
|
||||||
executor_address: String,
|
executor_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SwapEncoder for EkuboEncoder {
|
impl SwapEncoder for EkuboSwapEncoder {
|
||||||
fn new(executor_address: String) -> Self {
|
fn new(executor_address: String) -> Self {
|
||||||
Self { executor_address }
|
Self { executor_address }
|
||||||
}
|
}
|
||||||
@@ -694,4 +694,137 @@ mod tests {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod ekubo {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encode_swap_simple() {
|
||||||
|
let token_in = Bytes::from(Address::ZERO.as_slice());
|
||||||
|
let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
|
||||||
|
|
||||||
|
let static_attributes = HashMap::from([
|
||||||
|
("fee".to_string(), Bytes::from(0_u64)),
|
||||||
|
("tick_spacing".to_string(), Bytes::from(0_u32)),
|
||||||
|
("extension".to_string(), Bytes::from("0x51d02a5948496a67827242eabc5725531342527c")), // Oracle
|
||||||
|
]);
|
||||||
|
|
||||||
|
let component = ProtocolComponent {
|
||||||
|
static_attributes,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let swap = Swap {
|
||||||
|
component,
|
||||||
|
token_in: token_in.clone(),
|
||||||
|
token_out: token_out.clone(),
|
||||||
|
split: 0f64,
|
||||||
|
};
|
||||||
|
|
||||||
|
let encoding_context = EncodingContext {
|
||||||
|
receiver: "0xcA4F73Fe97D0B987a0D12B39BBD562c779BAb6f6".into(), // Random address
|
||||||
|
group_token_in: token_in.clone(),
|
||||||
|
group_token_out: token_out.clone(),
|
||||||
|
exact_out: false,
|
||||||
|
router_address: Bytes::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let encoder = EkuboSwapEncoder::new(String::default());
|
||||||
|
|
||||||
|
let encoded_swap = encoder
|
||||||
|
.encode_swap(swap, encoding_context)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let hex_swap = encode(&encoded_swap);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
hex_swap,
|
||||||
|
String::from(concat!(
|
||||||
|
// receiver
|
||||||
|
"ca4f73fe97d0b987a0d12b39bbd562c779bab6f6",
|
||||||
|
// group token in
|
||||||
|
"0000000000000000000000000000000000000000",
|
||||||
|
// token out 1st swap
|
||||||
|
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
||||||
|
// pool config 1st swap
|
||||||
|
"51d02a5948496a67827242eabc5725531342527c000000000000000000000000",
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encode_swap_multi() {
|
||||||
|
let group_token_in = Bytes::from(Address::ZERO.as_slice());
|
||||||
|
let group_token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT
|
||||||
|
let intermediary_token = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
|
||||||
|
|
||||||
|
let encoder = EkuboSwapEncoder::new(String::default());
|
||||||
|
|
||||||
|
let encoding_context = EncodingContext {
|
||||||
|
receiver: "0xcA4F73Fe97D0B987a0D12B39BBD562c779BAb6f6".into(), // Random address
|
||||||
|
group_token_in: group_token_in.clone(),
|
||||||
|
group_token_out: group_token_out.clone(),
|
||||||
|
exact_out: false,
|
||||||
|
router_address: Bytes::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let first_swap = Swap {
|
||||||
|
component: ProtocolComponent {
|
||||||
|
static_attributes: HashMap::from([
|
||||||
|
("fee".to_string(), Bytes::from(0_u64)),
|
||||||
|
("tick_spacing".to_string(), Bytes::from(0_u32)),
|
||||||
|
("extension".to_string(), Bytes::from("0x51d02a5948496a67827242eabc5725531342527c")), // Oracle
|
||||||
|
]),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
token_in: group_token_in.clone(),
|
||||||
|
token_out: intermediary_token.clone(),
|
||||||
|
split: 0f64,
|
||||||
|
};
|
||||||
|
|
||||||
|
let second_swap = Swap {
|
||||||
|
component: ProtocolComponent {
|
||||||
|
// 0.0025% fee & 0.005% base pool
|
||||||
|
static_attributes: HashMap::from([
|
||||||
|
("fee".to_string(), Bytes::from(461168601842738_u64)),
|
||||||
|
("tick_spacing".to_string(), Bytes::from(50_u32)),
|
||||||
|
("extension".to_string(), Bytes::zero(20)),
|
||||||
|
]),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
token_in: intermediary_token.clone(),
|
||||||
|
token_out: group_token_out.clone(),
|
||||||
|
split: 0f64,
|
||||||
|
};
|
||||||
|
|
||||||
|
let first_encoded_swap = encoder
|
||||||
|
.encode_swap(first_swap, encoding_context.clone())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let second_encoded_swap = encoder
|
||||||
|
.encode_swap(second_swap, encoding_context)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let combined_hex = format!("{}{}", encode(first_encoded_swap), encode(second_encoded_swap));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
combined_hex,
|
||||||
|
String::from(concat!(
|
||||||
|
// receiver
|
||||||
|
"ca4f73fe97d0b987a0d12b39bbd562c779bab6f6",
|
||||||
|
// group token in
|
||||||
|
"0000000000000000000000000000000000000000",
|
||||||
|
// token out 1st swap
|
||||||
|
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
||||||
|
// pool config 1st swap
|
||||||
|
"51d02a5948496a67827242eabc5725531342527c000000000000000000000000",
|
||||||
|
// token out 2nd swap
|
||||||
|
"dac17f958d2ee523a2206206994597c13d831ec7",
|
||||||
|
// pool config 2nd swap
|
||||||
|
"00000000000000000000000000000000000000000001a36e2eb1c43200000032",
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user