Skip to content

Commit

Permalink
dex(router): internally split execution
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Jul 24, 2023
1 parent bfda3a8 commit b3bf749
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions crates/core/component/dex/src/component/router/route_and_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
router::{FillRoute, PathSearch, RoutingParams},
PositionManager, StateWriteExt,
},
lp::position::MAX_RESERVE_AMOUNT,
BatchSwapOutputData, SwapExecution, TradingPair,
};

Expand Down Expand Up @@ -121,26 +122,29 @@ impl<T: PositionManager> HandleBatchSwaps for T {}
/// Lower-level trait that ties together the routing and filling logic.
#[async_trait]
pub trait RouteAndFill: StateWrite + Sized {
#[instrument(skip(self, asset_1, asset_2, delta_1, params))]
#[instrument(skip(self, asset_1, asset_2, input, params))]
async fn route_and_fill(
self: &mut Arc<Self>,
asset_1: asset::Id,
asset_2: asset::Id,
delta_1: Amount,
input: Amount,
params: RoutingParams,
) -> Result<SwapExecution>
where
Self: 'static,
{
tracing::debug!(?delta_1, ?asset_1, ?asset_2, "starting route_and_fill");
tracing::debug!(?input, ?asset_1, ?asset_2, "starting route_and_fill");

// Unfilled output of asset 1
let mut total_unfilled_1 = input;
// Output of asset 2
let mut total_output_2 = 0u64.into();
// Unfilled output of asset 1
let mut total_unfilled_1 = delta_1;

// All traces of trades that were executed.
// An ordered list of execution traces that were used to fill the trade.
let mut traces: Vec<Vec<Value>> = Vec::new();

let max_delta_1: Amount = MAX_RESERVE_AMOUNT.into();

// Termination conditions:
// 1. We have no more delta_1 remaining
// 2. A path can no longer be found
Expand All @@ -163,7 +167,7 @@ pub trait RouteAndFill: StateWrite + Sized {
}

let delta_1 = Value {
amount: total_unfilled_1,
amount: total_unfilled_1.min(max_delta_1),
asset_id: asset_1,
};

Expand Down Expand Up @@ -212,7 +216,10 @@ pub trait RouteAndFill: StateWrite + Sized {
// Append the traces from this execution to the outer traces.
traces.append(&mut execution.traces.clone());

(total_output_2 + lambda_2.amount, unfilled_1.amount)
(
total_output_2 + lambda_2.amount,
total_unfilled_1 - delta_1.amount + unfilled_1.amount,
)
};

if total_unfilled_1.value() == 0 {
Expand Down Expand Up @@ -243,7 +250,7 @@ pub trait RouteAndFill: StateWrite + Sized {
traces,
input: Value {
asset_id: asset_1,
amount: delta_1 - total_unfilled_1,
amount: input - total_unfilled_1,
},
output: Value {
asset_id: asset_2,
Expand Down

0 comments on commit b3bf749

Please sign in to comment.