Skip to content

fix(campaign): use typed Error::Overflow in calculate_refund_amount#71

Open
Bug-Hunter-X wants to merge 1 commit into
OrbitChainLabs:mainfrom
Bug-Hunter-X:fix/calculate-refund-amount-typed-errors-33
Open

fix(campaign): use typed Error::Overflow in calculate_refund_amount#71
Bug-Hunter-X wants to merge 1 commit into
OrbitChainLabs:mainfrom
Bug-Hunter-X:fix/calculate-refund-amount-typed-errors-33

Conversation

@Bug-Hunter-X

Copy link
Copy Markdown

Overview

This PR fixes calculate_refund_amount in the campaign contract so refund arithmetic failures use the contract's typed Error::Overflow panic path instead of debug_assert! and .expect(). This ensures zero-denominator and overflow checks remain active in release/WASM builds and that off-chain clients can match failures against stable error discriminants.

Related Issue

Closes #33

Changes

🛡️ Typed Error Handling in Refund Math

  • [MODIFY] campaign/src/lib.rs
  • Replaced debug_assert!(refund_denominator > 0) with a runtime panic_with_error(env, Error::Overflow) check.
  • Replaced .expect("overflow in refund numerator") with checked_mul(...).unwrap_or_else(|| panic_with_error(env, Error::Overflow)).
  • Plumbed &Env through calculate_refund_amount and its claim_refund call site.
  • Preserved the PR Integer division precision loss zeroes out small donor refunds #21 anti-dust floor behavior and comment.

🧪 Tests

  • [MODIFY] campaign/src/test/claim_refund_tests.rs

  • Added unit tests asserting Error(Contract, #17) on zero denominator, negative denominator, and integer overflow.

  • Added integration tests through claim_refund for overflow and zero-denominator paths.

  • Added anti-dust floor regression test.

  • [ADD] campaign/test_snapshots/test/claim_refund_tests/*.json

  • Added Soroban test snapshots for the new negative-path and anti-dust tests.

Verification Results

cargo test (campaign) ✅ passed (148/148)
Acceptance Criteria Status
debug_assert! removed; replaced with typed panic_with_error
.expect() replaced with unwrap_or_else + panic_with_error
calculate_refund_amount accepts &Env
Negative-path tests for overflow and zero denominator
PR #21 anti-dust floor comment preserved
All existing refund and invariant tests pass

Replace debug_assert! and .expect() with panic_with_error so refund math
failures surface stable Error::Overflow on-chain in release builds.

Closes OrbitChainLabs#33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MEDIUM] calculate_refund_amount uses debug_assert! and .expect() instead of typed errors

1 participant