Skip to content

[profit] Profit formula incorrect: gross = repay x bonus drops collateral/debt price ratio #147

@obchain

Description

@obchain

Refs #40

File: crates/charon-core/src/profit.rs

PR branch: feat/15-profit-calc-and-queue

Problem:
The formula:
gross = repay_amount_cents * liquidation_bonus_bps / 10_000

produces a collateral-seizure value in USD cents only when the seized collateral and repaid debt have equal USD prices. The correct Venus accounting is:
collateral_seized_usd = repay_usd * (1 + incentive)

which holds in USD terms and is correct. The issue is that gross here equals repay_usd * incentive_fraction (the profit margin), not the full collateral seizure. Then slippage and swap costs must be applied to the full swap, not to gross. Mixing repay_amount_cents (debt priced in USD) as the base without isolating whether it is truly the debt USD value or collateral USD value produces a formula that is only correct for stable:stable pairs where both tokens trade at .

For real Venus liquidations (BNB collateral, BTCB debt, or any non-stable combination) this formula will produce systematically wrong profit estimates — either overstating profit (executes losing liquidations) or understating it (skips profitable ones) depending on price ratio direction.

PRD clause: PRD off-chain profit gate — must correctly estimate net profit after all costs.

Impact: Wrong profit estimates for all non-stable-to-stable liquidations. Core bot function is incorrect.

Fix: Add a pub fn from_opportunity(opp, collateral_price_usd_1e8, debt_price_usd_1e8, collateral_decimals, debt_decimals) -> Result<ProfitInputs, ProfitError> constructor that computes repay_amount_cents and expected_collateral_out_cents separately using the correct price for each token. Document the formula derivation in rustdoc with explicit unit tracking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglayer:rustRust crates (core / scanner / protocols / executor / cli)pr-reviewFindings from PR review processpriority:p0-blockerBlocks the critical pathstatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions