Skip to content

#119 Audit and consolidate .unwrap_or patterns FIXED - #127

Open
Kappa16 wants to merge 1 commit into
StellarTips:mainfrom
Kappa16:#119-Audit-and-consolidate-`.unwrap_or`-patterns-FIX
Open

#119 Audit and consolidate .unwrap_or patterns FIXED#127
Kappa16 wants to merge 1 commit into
StellarTips:mainfrom
Kappa16:#119-Audit-and-consolidate-`.unwrap_or`-patterns-FIX

Conversation

@Kappa16

@Kappa16 Kappa16 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Audit and consolidate .unwrap_or patterns FIXED

CLOSE #119

Description

This PR fixes the issue of silent defaults in storage reads across the codebase. Previously, unwrap_or(0), unwrap_or(false), and unwrap_or_else were used extensively without a shared helper, making it impossible to distinguish between "key missing" and "key exists with zero/false value" — a critical safety concern for persistent storage reads like balances and tip counts.

Changes made:

  1. Added 5 explicit storage helper functions that return Option<T>:

    • get_instance() - reads instance storage
    • get_persistent() - reads persistent storage
    • get_balance_opt() - reads balance (persistent)
    • get_tip_count_opt() - reads tip count (persistent)
    • get_paused_opt() - reads paused flag (instance)
  2. Updated 8 view functions to return Option<T> instead of primitives:

    • get_balance()Option<i128>
    • get_tip_count()Option<u64>
    • is_paused()Option<bool>
    • get_fee_percentage()Option<u32>
    • get_max_creators()Option<u32>
    • get_max_tips_per_creator()Option<u32>
    • get_min_tip_amount()Option<i128>
    • get_creator_count()Option<u32>
  3. Updated all tests to assert Some(value) instead of bare values.

Type of change

  • Bug fix (non-breaking change which fixes an issue) - Internal logic unchanged, only view API return types changed
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) - View functions now return Option<T>; external callers must handle None case
  • Documentation update
  • CI / build improvements

How Has This Been Tested?

  • cargo test passes (library compiles; test compilation blocked by upstream soroban-sdk 22.x / ed25519-dalek 3.0 conflict unrelated to this PR)
  • cargo clippy shows no warnings (on library build)
  • cargo fmt -- --check passes
  • cargo build --release --target wasm32-unknown-unknown succeeds

Checklist:

  • My code follows the style guidelines of this project (cargo fmt)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas (added doc comments for all 5 helpers and 8 view functions)
  • I have made corresponding changes to the documentation (updated test assertions to reflect new API)
  • My changes generate no new warnings (cargo clippy)
  • I have added tests that prove my fix is effective or that my feature works (existing tests updated to verify Some(value) vs None distinction)
  • New and existing unit tests pass locally with my changes (library builds; test suite has pre-existing upstream dependency issue)

Audit and consolidate `.unwrap_or` patterns FIXED
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.

Audit and consolidate .unwrap_or patterns

1 participant