Skip to content

[profit] Missing wei-to-USD conversion: ProfitInputs cannot be populated from LiquidationOpportunity #148

@obchain

Description

@obchain

Refs #40

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

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

Problem:
LiquidationOpportunity.debt_to_repay is U256 (token wei). Token decimals on BSC/Venus vary: BNB=18, BTCB=8, USDT=18. Chainlink prices are 8-decimal fixed-point USD. ProfitInputs.repay_amount_cents is u64.

To populate repay_amount_cents from real data:

  1. Fetch token decimal exponent for the debt token.
  2. Read Chainlink latest answer (i256, 8 decimal places).
  3. Compute: (debt_wei / 10^decimals) * price_usd_1e8 / 1e8 * 100 -> u64

This conversion is not implemented, not tested, and not documented anywhere in the PR. The 5 unit tests pass hardcoded cent values and never exercise a wei->cents path.

The same gap applies to gas_cost_cents (gas in wei * BNB price -> cents) and flash_fee (fee in wei * debt price -> cents). Every ProfitInputs field requires this conversion.

PRD clause: PRD requirement — profit calculation uses live Chainlink prices and is denominated correctly.

Impact: PR is functionally incomplete. No existing crate can safely populate ProfitInputs from real LiquidationOpportunity data without independently implementing this conversion, risking divergent rounding behavior across callers.

Fix: Add pub fn ProfitInputs::from_opportunity(opp: &LiquidationOpportunity, collateral_price: Price, debt_price: Price, gas_price_wei: U256, bnb_price: Price) -> Result<Self, ProfitError> where Price holds (answer: i256, decimals: u8). Include tests with realistic BSC values (1 BNB at current price, 0.001 BTCB repay, etc.).

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