Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dex(router): internally split execution #2851

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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