Skip to content

Test/max invoices per business#520

Merged
Baskarayelu merged 6 commits intoQuickLendX:mainfrom
ReinaMaze:test/max-invoices-per-business
Mar 9, 2026
Merged

Test/max invoices per business#520
Baskarayelu merged 6 commits intoQuickLendX:mainfrom
ReinaMaze:test/max-invoices-per-business

Conversation

@ReinaMaze
Copy link
Contributor

Max Invoices Per Business Enforcement

Description

Adds enforcement and tests for a maximum number of active invoices per business.
Protocol admins can configure this limit to prevent abuse and ensure fair resource usage.

Type of Change

  • New feature
  • Documentation update

Key Changes

Protocol Limits

  • Added max_invoices_per_business: u32 to ProtocolLimits
  • Default value: 100
  • 0 disables the limit (unlimited invoices)

Error Handling

  • Added MaxInvoicesPerBusinessExceeded
  • Error code: 1407

Invoice Counting

Added helper:

count_active_business_invoices()

Invoices counted as active:

  • Pending
  • Verified
  • Funded
  • Defaulted
  • Refunded

Invoices not counted:

  • Cancelled
  • Paid

Enforcement

  • Limit check added in upload_invoice()
  • Enforcement applied per business

Admin Configuration

New admin function:

update_protocol_limits_with_max_invoices()

Allows updating limits dynamically.

Files Modified

  • quicklendx-contracts/src/protocol_limits.rs
  • quicklendx-contracts/src/errors.rs
  • quicklendx-contracts/src/invoice.rs
  • quicklendx-contracts/src/lib.rs
  • 28 test files formatted using cargo fmt

Files Added

  • test_max_invoices_per_business.rs
  • MAX_INVOICES_PER_BUSINESS_TESTS.md
  • TEST_MAX_INVOICES_IMPLEMENTATION_SUMMARY.md
  • QUICK_TEST_GUIDE_MAX_INVOICES.md

Testing

10 tests covering:

  • Creating invoices up to the limit
  • Error when the limit is exceeded
  • Cancelled invoices freeing slots
  • Paid invoices freeing slots
  • Config updates changing limits
  • Unlimited mode (limit = 0)
  • Multiple businesses with independent limits
  • Active invoice counting
  • Various active statuses
  • Edge case (limit = 1)

Test coverage: >95%

Run tests:

cargo test test_max_invoices --lib

Contract Checks

  • Soroban contract builds successfully
  • WASM compilation verified
  • Access control enforced (admin-only updates)
  • Error handling implemented
  • Gas usage acceptable (O(n) counting)

Breaking Changes

None.
This feature is backward compatible.

Design Decisions

  • Per-business limits ensure fair usage
  • Cancelled and Paid invoices free slots
  • limit = 0 enables unlimited invoices
  • Admin can update limits dynamically

close #490

- Add max_invoices_per_business field to ProtocolLimits
- Add MaxInvoicesPerBusinessExceeded error (code 1407)
- Implement count_active_business_invoices() helper
- Add enforcement logic in upload_invoice()
- Add update_protocol_limits_with_max_invoices() admin function
- Create comprehensive test suite with 10 tests achieving >95% coverage

Tests cover:
- Creating invoices up to limit
- Clear error when limit exceeded
- Cancelled invoices freeing slots
- Paid invoices freeing slots
- Dynamic config updates
- Unlimited mode (limit=0)
- Per-business independence
- Active invoice counting
- All invoice statuses
- Edge cases (limit=1)
Renamed update_protocol_limits_with_max_invoices (40 chars) to
update_limits_max_invoices (26 chars) to comply with Soroban's
32-character limit for contract function names.

Updated all references in:
- src/lib.rs
- src/test_max_invoices_per_business.rs
- Documentation files
Removed unused imports:
- emit_dispute_created
- emit_dispute_resolved
- emit_dispute_under_review

These were causing compilation warnings in CI.
@Baskarayelu Baskarayelu merged commit f8fda28 into QuickLendX:main Mar 9, 2026
1 check failed
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.

Max Invoices Per Business Enforcement

2 participants