feat: add validate_ratios and BASIS_POINTS_TOTAL for invoice ratio in… - #542
Merged
Merged
Conversation
…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
|
@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! 🚀 |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
for the 100% threshold
constraint, use amounts directly"
error.rs
lib.rs
helper
write
test.rs / fuzz/src/lib.rs
backward-compatible)
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