Skip to content

Commit ecb2766

Browse files
author
ChainBid Developer
committed
refactor: extract basis-points range validation into assert_valid_bps (#704)
1 parent f08bf62 commit ecb2766

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

contracts/split/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ mod storage_keys;
7171

7272
mod migrations;
7373

74-
mod validation;
75-
7674
use error::ContractError;
7775
use validation::assert_valid_bps;
7876
use soroban_sdk::crypto::bls12_381::{Fr, G1Affine};

contracts/split/src/validation.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ pub fn assert_bps_total(total: u32) -> Result<(), ContractError> {
9797
Ok(())
9898
}
9999

100+
/// Issue #704: Validate that a single basis-point value is within the legal
101+
/// range `[0, BASIS_POINTS_TOTAL]` (i.e. `0..=10_000`).
102+
///
103+
/// Per-invoice options such as `penalty_bps`, `tax_bps`, and
104+
/// `insurance_premium_bps` are stored as `u32` basis points and must never
105+
/// exceed 100%. Callers invoke this guard before writing storage so an
106+
/// out-of-range value is rejected atomically. The three existing call sites
107+
/// use `.expect("… must be ≤ 10000")`, so this returns a `Result` and lets the
108+
/// caller choose how to surface the failure.
109+
///
110+
/// # Errors
111+
/// Returns `Err(ContractError::InvalidRatio)` when `bps > BASIS_POINTS_TOTAL`.
112+
pub fn assert_valid_bps(bps: u32) -> Result<(), ContractError> {
113+
if bps > BASIS_POINTS_TOTAL {
114+
return Err(ContractError::InvalidRatio);
115+
}
116+
Ok(())
117+
}
118+
100119
#[cfg(test)]
101120
mod tests {
102121
use super::*;

0 commit comments

Comments
 (0)