Skip to content

Commit

Permalink
Merge pull request #40 from gnosis/add-execution-plan-for-interactions
Browse files Browse the repository at this point in the history
Update ExecutionPlan for Interactions
  • Loading branch information
sunce86 authored Nov 25, 2022
2 parents cbc083d + 166d9a0 commit 12ad71c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 53 deletions.
62 changes: 17 additions & 45 deletions src/models/batch_auction_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct InteractionData {
#[derivative(Debug(format_with = "debug_bytes"))]
#[serde(with = "bytes_hex")]
pub call_data: Vec<u8>,
pub exec_plan: Option<ExecutionPlan>,
pub exec_plan: ExecutionPlan,
#[serde(default)]

/// The input amounts into the AMM interaction - i.e. the amount of tokens
Expand Down Expand Up @@ -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 <https://github.com/serde-rs/serde/issues/1560>.
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<S>(serializer: S) -> Result<S::Ok, S::Error>
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)]
Expand Down Expand Up @@ -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::*;
Expand All @@ -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(),
Expand All @@ -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":{"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
Expand All @@ -480,8 +452,8 @@ mod tests {
serde_json::from_str::<InteractionData>(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":{"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
Expand Down
24 changes: 16 additions & 8 deletions src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -191,7 +190,12 @@ pub async fn solve(
})
}

// 5th step: Insert traded orders into settlement
// 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;
}

// 6th step: Insert traded orders into settlement
for (i, order) in matched_orders {
solution.orders.insert(
i,
Expand Down Expand Up @@ -238,7 +242,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,
Expand All @@ -254,7 +258,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) {
Expand Down Expand Up @@ -657,6 +661,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;
Expand Down Expand Up @@ -789,7 +794,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);

Expand All @@ -806,7 +814,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,
Expand All @@ -830,7 +838,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,
Expand Down Expand Up @@ -874,7 +882,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,
Expand Down

0 comments on commit 12ad71c

Please sign in to comment.