Skip to content

[executor] gas_cost_usd_cents missing Chainlink 8-decimal normalisation — cost off by 10^6 #183

@obchain

Description

@obchain

Summary

gas_cost_usd_cents(units, max_fee, native_price, native_decimals) converts wei-denominated gas cost to integer USD cents. The correct formula is:

cost_cents = units x max_fee_wei x native_price_chainlink x 100
/ (10^native_decimals x 10^chainlink_decimals)
= units x max_fee_wei x native_price x 100
/ (10^18 x 10^8)

Chainlink BNB/USD returns 8-decimal prices (e.g. ~60_000_000_000 for $600 BNB). If the implementation uses native_decimals (10^18) as the only divisor and omits the Chainlink decimal divisor (10^8) and the cents multiplier (x100), the result is wrong by a factor of 100 / 10^8 = 10^-6.

Concrete example: gas cost of 0.0009 BNB (300k gas at 3 gwei) = ~$0.54 = 54 cents.

  • Missing x100: result is 0 (u64 truncation) — all opportunities appear gas-free, no cost deducted
  • Missing /10^8 Chainlink: result is 54,000,000 cents ($540,000) — all opportunities appear unprofitable

Either direction breaks the profit gate.

File

crates/charon-executor/src/gas.rsgas_cost_usd_cents()

Fix

Define CHAINLINK_DECIMALS: u32 = 8 as a constant. The full checked arithmetic:

const CHAINLINK_DECIMALS: u32 = 8;

let divisor = U256::from(10u64)
    .pow(U256::from(u32::from(native_decimals) + CHAINLINK_DECIMALS));
let cost_cents = U256::from(units)
    .checked_mul(max_fee)
    .and_then(|v| v.checked_mul(native_price))
    .and_then(|v| v.checked_mul(U256::from(100u64)))
    .and_then(|v| v.checked_div(divisor))
    .ok_or(GasError::Overflow)?;

Audit the existing unit test's expected value against this formula with a real BNB/USD price.

Refs #43

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