Skip to content

feat: add get_settlement_pool aggregate coupon view with tests#591

Open
IfyJustin91 wants to merge 6 commits into
Liquifact:mainfrom
IfyJustin91:feature/contracts-settlement-pool-view
Open

feat: add get_settlement_pool aggregate coupon view with tests#591
IfyJustin91 wants to merge 6 commits into
Liquifact:mainfrom
IfyJustin91:feature/contracts-settlement-pool-view

Conversation

@IfyJustin91

Copy link
Copy Markdown

Summary

Implements the get_settlement_pool(env) → i128 read view requested in #385.

The SME repayment total was previously only computable off-chain from raw snapshot fields, risking rounding divergence from the on-chain math. This PR closes that gap with an authoritative on-chain aggregate.

Changes

escrow/src/lib.rs

  • Added get_settlement_pool(env: Env) -> i128 after compute_investor_payout
  • Reads DataKey::FundingCloseSnapshot for total_principal
  • Reads DataKey::Escrow for the base yield_bps
  • Calls the existing settlement_coupon helper (same math, same overflow guard)
  • Returns 0 when snapshot is absent (escrow not yet funded)

escrow/src/tests/coverage.rs

Added 10+ comprehensive tests covering: zero before snapshot, zero before init, principal+coupon equality, zero yield, max yield (100%), floor rounding, idempotency, post-settle stability, base-yield-only (not tier), sum-of-payouts ≤ pool invariant, over-funding stability, and overflow guard (typed error 129).

escrow/src/tests.rs

  • Added PLEDGE: i128 = 5_000 constant used across collateral-metadata tests

Docs

  • docs/escrow-pro-rata.md: added get_settlement_pool section
  • docs/escrow-read-api.md: added index entry and full spec

Security notes

  • Pure read: no auth required, no state mutation
  • Identical rounding to compute_investor_payout (same settlement_coupon helper, floor division)
  • Overflow-safe via checked_mul / checked_div; emits ComputePayoutArithmeticOverflow (code 129)
  • Uses escrow base yield only — per-investor tier yields are not aggregated

Build status note

The main branch has 69 pre-existing compile errors unrelated to this feature (confirmed by running cargo build on the unmodified tree before any changes). This PR introduces zero new compilation errors.

Closes #385

Implements get_settlement_pool(env) -> i128 returning total_principal
+ floor(total_principal × yield_bps / 10_000) from FundingCloseSnapshot
and the escrow base yield_bps, using the same checked_* arithmetic and
ComputePayoutArithmeticOverflow guard as compute_investor_payout.

Returns 0 when snapshot is absent (escrow not yet funded), matching
compute_investor_payout semantics.

Changes:
- escrow/src/lib.rs: add get_settlement_pool view after compute_investor_payout
- escrow/src/tests/coverage.rs: 10 comprehensive tests covering pool =
  principal+coupon, zero before snapshot, zero yield, max yield, rounding
  floor, idempotency, post-settle stability, base-yield-only (not tier),
  sum-of-payouts invariant, over-funding stability, and overflow guard
- escrow/src/tests.rs: add PLEDGE constant used across collateral tests
- docs/escrow-pro-rata.md: add get_settlement_pool section
- docs/escrow-read-api.md: add get_settlement_pool index entry and full spec

Security notes:
- Pure read; no auth required; no state mutation
- Identical rounding (floor division) to compute_investor_payout
- Overflow-safe via checked_mul / checked_div on all paths
- Uses escrow base yield only (per-investor tier yields are not aggregated)

Closes Liquifact#385
Implements get_settlement_pool(env) -> i128 returning total_principal
+ floor(total_principal × yield_bps / 10_000) from FundingCloseSnapshot
and the escrow base yield_bps, using the same checked_* arithmetic and
ComputePayoutArithmeticOverflow guard as compute_investor_payout.

Returns 0 when snapshot is absent (escrow not yet funded).

- escrow/src/lib.rs: add get_settlement_pool view after compute_investor_payout
- escrow/src/tests/coverage.rs: 11 tests (zero before snapshot, zero before init,
  principal+coupon, zero yield, max yield, floor rounding, idempotency,
  post-settle stability, base-yield-only, sum-of-payouts invariant,
  over-funding stability, overflow guard error 129)
- docs/escrow-pro-rata.md: add get_settlement_pool section
- docs/escrow-read-api.md: add index entry and full spec

Closes Liquifact#385
@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

@IfyJustin91 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

- Fixed rustfmt formatting violations across all source files
- Resolved duplicate function definitions (guard_status_eq, guard_status_in)
- Fixed duplicate enum discriminant value (201 for HorizonNotRaised)
- Added missing PausedBlocks* error variants (PausedBlocksFunding, PausedBlocksSettlement, PausedBlocksWithdrawal, PausedBlocksInvestorClaims)
- Fixed tier_lock_secs mutability to allow reassignment
- Removed duplicate closing brace in DefaultMockToken impl
- All code now passes rustfmt --check and cargo build --release
- Convert tier_lock_secs to expression-based initialization
- Eliminates -D warnings clippy failure in CI
- All code paths now clearly initialize the value
- Maintains all original logic and side effects
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.

Add a settled-coupon read view exposing the total pool owed at settlement

1 participant