From 153f3b6607e268f0c93e51138a1a97ada644bd97 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Thu, 24 Nov 2022 16:03:51 +0100 Subject: [PATCH 1/4] Update ExecutionPlan for Interactions --- src/models/batch_auction_model.rs | 62 +++++++++---------------------- src/solve.rs | 24 ++++++++---- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/src/models/batch_auction_model.rs b/src/models/batch_auction_model.rs index cf92825..22b2188 100644 --- a/src/models/batch_auction_model.rs +++ b/src/models/batch_auction_model.rs @@ -149,7 +149,7 @@ pub struct InteractionData { #[derivative(Debug(format_with = "debug_bytes"))] #[serde(with = "bytes_hex")] pub call_data: Vec, - pub exec_plan: Option, + pub exec_plan: ExecutionPlan, #[serde(default)] /// The input amounts into the AMM interaction - i.e. the amount of tokens @@ -181,42 +181,17 @@ pub fn debug_bytes( formatter.write_fmt(format_args!("0x{}", hex::encode(bytes.as_ref()))) } -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] -#[serde(untagged)] -pub enum ExecutionPlan { - /// The interaction should **not** be included in the settlement as - /// internal buffers will be used instead. - #[serde(with = "execution_plan_internal")] - Internal, +#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)] +pub struct ExecutionPlan { + #[serde(flatten)] + pub coordinates: ExecutionPlanCoordinatesModel, + pub internal: bool, } -/// A module for implementing `serde` (de)serialization for the execution plan -/// enum. -/// -/// This is a work-around for untagged enum serialization not supporting empty -/// variants . -mod execution_plan_internal { - use super::*; - - #[derive(Deserialize, Serialize)] - enum Kind { - #[serde(rename = "internal")] - Internal, - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result<(), D::Error> - where - D: serde::Deserializer<'de>, - { - Kind::deserialize(deserializer)?; - Ok(()) - } - pub fn serialize(serializer: S) -> Result - where - S: serde::Serializer, - { - Kind::serialize(&Kind::Internal, serializer) - } +#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, PartialOrd, Ord, Serialize)] +pub struct ExecutionPlanCoordinatesModel { + pub sequence: u32, + pub position: u32, } #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] @@ -435,12 +410,6 @@ impl UpdatedAmmModel { } } -#[derive(Clone, Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord, Serialize)] -pub struct ExecutionPlanCoordinatesModel { - pub sequence: u32, - pub position: u32, -} - #[cfg(test)] mod tests { use super::*; @@ -455,7 +424,10 @@ mod tests { target: "ffffffffffffffffffffffffffffffffffffffff".parse().unwrap(), value: U256::from_dec_str("1").unwrap(), call_data: vec![1, 2], - exec_plan: Some(ExecutionPlan::Internal), + exec_plan: ExecutionPlan { + coordinates: Default::default(), + internal: true, + }, inputs: vec![TokenAmount { token: H160([1; 20]), amount: 9999.into(), @@ -471,7 +443,7 @@ mod tests { }, ], }; - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":"internal","inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":"0","sequence":"0","internal":"true"},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string @@ -480,8 +452,8 @@ mod tests { serde_json::from_str::(expected_string).unwrap(), interaction_data ); - interaction_data.exec_plan = None; - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":null,"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + interaction_data.exec_plan = Default::default(); + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":"0","sequence":"0","internal":"false"},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string diff --git a/src/solve.rs b/src/solve.rs index f000114..ed4b9cd 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -3,7 +3,6 @@ mod solver_utils; pub mod zeroex_solver; use crate::models::batch_auction_model::ApprovalModel; use crate::models::batch_auction_model::ExecutedOrderModel; -use crate::models::batch_auction_model::ExecutionPlan; use crate::models::batch_auction_model::InteractionData; use crate::models::batch_auction_model::OrderModel; use crate::models::batch_auction_model::SettledBatchAuctionModel; @@ -191,6 +190,13 @@ pub async fn solve( }) } + // 4a step: Update execution plan coordinates once we have all plans prepared + let mut position = 0; + for plan in &mut solution.interaction_data { + plan.exec_plan.coordinates.position = position; + position += 1; + } + // 5th step: Insert traded orders into settlement for (i, order) in matched_orders { solution.orders.insert( @@ -238,7 +244,7 @@ fn build_payload_for_swap( target: swap.to, value: swap.value, call_data: swap.data.0.clone(), - exec_plan: None, + exec_plan: Default::default(), inputs: vec![TokenAmount { token: query.sell_token, amount: swap.sell_amount, @@ -254,7 +260,7 @@ fn build_payload_for_swap( && swap_tokens_are_tradable_buffer_tokens(query, tradable_buffer_token_list) { // Trade against internal buffer - swap_interaction_data.exec_plan = Some(ExecutionPlan::Internal); + swap_interaction_data.exec_plan.internal = true; // Adjust buffer balances if let Some(mut token_info) = tokens.get_mut(&query.buy_token) { @@ -657,6 +663,7 @@ fn overwrite_eth_with_weth_token(token: H160) -> H160 { mod tests { use super::*; use crate::models::batch_auction_model::CostModel; + use crate::models::batch_auction_model::ExecutionPlan; use crate::models::batch_auction_model::FeeModel; use hex_literal::hex; use maplit::hashmap; @@ -789,7 +796,10 @@ mod tests { token: query.sell_token, amount: swap.sell_amount, }], - exec_plan: Some(ExecutionPlan::Internal), + exec_plan: ExecutionPlan { + coordinates: Default::default(), + internal: true, + }, }; assert_eq!(swap_interaction_data, expected_swap_interaction_data); @@ -806,7 +816,7 @@ mod tests { target: H160::zero(), value: U256::zero(), call_data: vec![0u8], - exec_plan: None, + exec_plan: Default::default(), outputs: vec![TokenAmount { token: query.buy_token, amount: swap.buy_amount, @@ -830,7 +840,7 @@ mod tests { target: H160::zero(), value: U256::zero(), call_data: vec![0u8], - exec_plan: None, + exec_plan: Default::default(), outputs: vec![TokenAmount { token: query.buy_token, amount: swap.buy_amount, @@ -874,7 +884,7 @@ mod tests { target: H160::zero(), value: U256::zero(), call_data: vec![0u8], - exec_plan: None, + exec_plan: Default::default(), outputs: vec![TokenAmount { token: query.buy_token, amount: swap.buy_amount, From 5d491e875586f4f814c4b212a5d218f1c8d6b974 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Thu, 24 Nov 2022 16:19:54 +0100 Subject: [PATCH 2/4] fix clippy and tests --- src/models/batch_auction_model.rs | 4 ++-- src/solve.rs | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/models/batch_auction_model.rs b/src/models/batch_auction_model.rs index 22b2188..9194119 100644 --- a/src/models/batch_auction_model.rs +++ b/src/models/batch_auction_model.rs @@ -443,7 +443,7 @@ mod tests { }, ], }; - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":"0","sequence":"0","internal":"true"},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":0,"sequence":0,"internal":true},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string @@ -453,7 +453,7 @@ mod tests { interaction_data ); interaction_data.exec_plan = Default::default(); - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":"0","sequence":"0","internal":"false"},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":0,"sequence":0,"internal":false},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string diff --git a/src/solve.rs b/src/solve.rs index ed4b9cd..457f0ad 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -191,10 +191,8 @@ pub async fn solve( } // 4a step: Update execution plan coordinates once we have all plans prepared - let mut position = 0; - for plan in &mut solution.interaction_data { - plan.exec_plan.coordinates.position = position; - position += 1; + for (position, plan) in solution.interaction_data.iter_mut().enumerate() { + plan.exec_plan.coordinates.position = position as u32; } // 5th step: Insert traded orders into settlement From 0467369dc09a40cf4ddca26182c0e2f0ba8edc3e Mon Sep 17 00:00:00 2001 From: sunce86 Date: Thu, 24 Nov 2022 16:28:16 +0100 Subject: [PATCH 3/4] fix ut --- src/models/batch_auction_model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/batch_auction_model.rs b/src/models/batch_auction_model.rs index 9194119..ba0bf5a 100644 --- a/src/models/batch_auction_model.rs +++ b/src/models/batch_auction_model.rs @@ -443,7 +443,7 @@ mod tests { }, ], }; - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":0,"sequence":0,"internal":true},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"sequence":0,"position":0,"internal":true},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string @@ -453,7 +453,7 @@ mod tests { interaction_data ); interaction_data.exec_plan = Default::default(); - let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"position":0,"sequence":0,"internal":false},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; + let expected_string = r#"{"target":"0xffffffffffffffffffffffffffffffffffffffff","value":"0x1","call_data":"0x0102","exec_plan":{"sequence":0,"position":0,"internal":false},"inputs":[{"amount":"9999","token":"0x0101010101010101010101010101010101010101"}],"outputs":[{"amount":"2000","token":"0x0202020202020202020202020202020202020202"},{"amount":"3000","token":"0x0303030303030303030303030303030303030303"}]}"#; assert_eq!( serde_json::to_string(&interaction_data).unwrap(), expected_string From 166d9a0bb163a1d0eada608e83e605d1add49b65 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Fri, 25 Nov 2022 11:48:09 +0100 Subject: [PATCH 4/4] update comments --- src/solve.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/solve.rs b/src/solve.rs index 457f0ad..4f7bdcf 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -190,12 +190,12 @@ pub async fn solve( }) } - // 4a step: Update execution plan coordinates once we have all plans prepared + // 5th step: Update execution plan coordinates once we have all plans prepared for (position, plan) in solution.interaction_data.iter_mut().enumerate() { plan.exec_plan.coordinates.position = position as u32; } - // 5th step: Insert traded orders into settlement + // 6th step: Insert traded orders into settlement for (i, order) in matched_orders { solution.orders.insert( i,