Skip to content

[flashloan] FlashLoanRouter sort lacks available_liquidity tiebreaker for equal-fee providers #145

@obchain

Description

@obchain

Refs #39

File: crates/charon-flashloan/src/router.rs — sort logic

Problem

The router sorts providers by fee_rate_bps ascending. When two providers share the same fee, Rust's stable sort preserves insertion order, which is determined by registration order — not by operational preference. When two equal-fee providers are available, the one with greater available_liquidity is strictly better: more liquidity reduces the probability of the flash loan failing mid-execution.

At v0.1 with a single Aave adapter this is dormant. The router is designed for multi-provider use (BalancerV2 and UniswapV3 variants exist in FlashLoanSource) and this ordering bug is latent from day one.

Fix

providers.sort_by(|a, b| {
    a.fee_rate_bps
        .cmp(&b.fee_rate_bps)
        .then_with(|| b.available_liquidity.cmp(&a.available_liquidity))
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlayer:rustRust crates (core / scanner / protocols / executor / cli)priority:p2-polishNice-to-have / polish

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions