Skip to content

[profit] calculate_profit returns anyhow::Error — structured ProfitError thiserror enum required #149

@obchain

Description

@obchain

Refs #40

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

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

Problem:
calculate_profit() returns anyhow::Result. The executor consuming this function must distinguish:

  • BelowMinThreshold: opportunity rejected on economics; log and skip to next entry.
  • InvalidInputs / Overflow / InvalidBps: programming or data error; abort cycle and alert.

With anyhow::Error both cases collapse to a string. The executor cannot match on error variant to decide safe recovery behavior. This is the same defect flagged as p1 in the PR #39 review (FlashLoanProvider) and the pattern established in the PR #28 review (LendingProtocol trait).

Additionally, thiserror is not in [workspace.dependencies] or charon-core/Cargo.toml. Adding it is a required Cargo.toml change not present in this PR.

Fix:

  1. Add thiserror = "1" to [workspace.dependencies] in root Cargo.toml.
  2. Add thiserror to charon-core/Cargo.toml [dependencies].
  3. Define:
    #[derive(Debug, thiserror::Error)]
    #[non_exhaustive]
    pub enum ProfitError {
    #[error("net {net_cents} cents below minimum {min_cents} cents")]
    BelowMinThreshold { net_cents: u64, min_cents: u64 },
    #[error("invalid bps {bps}: must be <= 10_000")]
    InvalidBps { bps: u32 },
    #[error("arithmetic overflow")]
    Overflow,
    #[error("invalid price input: {reason}")]
    InvalidPrice { reason: &'static str },
    }
  4. Change calculate_profit signature to -> Result<NetProfit, ProfitError>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglayer:rustRust crates (core / scanner / protocols / executor / cli)pr-reviewFindings from PR review processpriority:p1-coreCore MVP scopestatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions