Skip to content

feat: add validate_ratios and BASIS_POINTS_TOTAL for invoice ratio in… - #542

Merged
Kingsman-99 merged 2 commits into
Stellar-split:mainfrom
Keengfk:feat/ratio-validation
Jul 28, 2026
Merged

feat: add validate_ratios and BASIS_POINTS_TOTAL for invoice ratio in…#542
Kingsman-99 merged 2 commits into
Stellar-split:mainfrom
Keengfk:feat/ratio-validation

Conversation

@Keengfk

@Keengfk Keengfk commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Enforce ratio integrity at invoice creation time

Closes #[issue number]

Summary

Accepting split ratios that don't sum to exactly 10 000 basis points silently
causes under- or over-distribution of funds. This PR adds server-side
validation that rejects invalid ratios before any storage is written.

Changes

types.rs

  • Added BASIS_POINTS_TOTAL = 10_000u32 constant as the single source of truth
    for the 100% threshold
  • Added ratios: Vec field to InvoiceOptions — empty means "no ratio
    constraint, use amounts directly"

error.rs

  • Added InvalidRatioSum = 33 — ratios provided but don't sum to 10 000
  • Added EmptyRecipientList = 34 — ratios vec is non-null but empty

lib.rs

  • Added validate_ratios(ratios: &Vec) -> Result<(), ContractError> private
    helper
  • Called from create_invoice via env.panic_with_error() before any storage
    write

test.rs / fuzz/src/lib.rs

  • Added ratios field to all default_options helpers (empty vec,
    backward-compatible)
  • Added 7 unit tests covering all acceptance criteria

Acceptance criteria

┌───────────┬──────┐
│ Criterion │ Test │
├────────────────────────────────┼──────┤
│ Sum < 10 000 → InvalidRatioSum │ test_validate_ratios_under_sum_rejected │
├────────────────────────────────┼─────────────────────────────────────────┤
│ Sum > 10 000 → InvalidRatioSum │ test_validate_ratios_over_sum_rejected │
│ Sum > 10 000 → InvalidRatioSum │ test_validate_ratios_over_sum_rejected │
├──────────────────────────────────┼────────────────────────────────────────┤
│ Single entry = 10 000 → accepted │ test_validate_ratios_exact_sum_accepte │
│ │ d │
├──────────────────────────────────┼────────────────────────────────────────┤
│ Empty vec → EmptyRecipientList │ test_validate_ratios_empty_rejected │
├──────────────────────────────────┼────────────────────────────────────────┤
│ Validation before storage writes │ call site is before │
│ │ _create_invoice_inner │
│ │ _create_invoice_inner │
├─────────────────────────────────────┼─────────────────────────────────────┤
│ Valid ratios on create_invoice → │
│ succeeds │ cepted │
├─────────────────────────────────────┼─────────────────────────────────────┤
│ Invalid ratios on create_invoice → │
│ panics │ panics │
└─────────────────────────────────────┴─────────────────────────────────────┘

Migration note

InvoiceOptions gained a new ratios field. All existing callers that use struct
update syntax (..default_options(env)) are unaffected. Callers constructing
InvoiceOptions in full must add ratios: Vec::new(env) to preserve the existing
no-constraint behaviour.
closes #478

…tegrity

- Add BASIS_POINTS_TOTAL = 10_000u32 constant to types.rs
- Add ratios: Vec<u32> field to InvoiceOptions (empty = no constraint)
- Add InvalidRatioSum = 33 and EmptyRecipientList = 34 to ContractError
- Add pub(crate) validate_ratios() private helper: rejects empty vecs
  with EmptyRecipientList and non-10000 sums with InvalidRatioSum
- Call validate_ratios from create_invoice before any storage writes,
  using env.panic_with_error() to surface the typed error on-chain
- Update default_options() in test.rs and fuzz/src/lib.rs with ratios field
- Add 7 unit tests covering all acceptance criteria
@drips-wave

drips-wave Bot commented Jul 27, 2026

Copy link
Copy Markdown

@Keengfk Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Kingsman-99
Kingsman-99 merged commit b845bd3 into Stellar-split:main Jul 28, 2026
0 of 2 checks passed
@grantfox-oss grantfox-oss Bot mentioned this pull request Jul 28, 2026
6 tasks
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.

Split Ratio Sum Validation

2 participants