fix(contracts): approve_loan honours borrower-requested term#34
Conversation
ogazboiz
left a comment
There was a problem hiding this comment.
the logic is correct and ci is green. issue #5: approve_loan now honors the borrower's stored term (term_ledgers = if loan.term_ledgers == 0 { read_default_term } else { loan.term_ledgers }) instead of overwriting it with the default, and i confirmed request_loan does store term_ledgers and due_date derives from it at approve time, so it's coherent end to end. issue #3: calc_assets_to_redeem no longer expect()-panics, it returns typed PoolError (InvalidAmount / InsufficientLiquidity) with the view callers using unwrap_or(0). good.
the blocker is a collision with #33, not a bug in your code:
-
#33 and your pr edit the same code in incompatible ways, both rewrite calc_assets_to_redeem into a Result-returning fn and both gate the loan term by loan.term_ledgers. they can't both merge as-is. your changes are actually complementary (yours = approval-time term, theirs = accrual-time term plus the donation guard and redeem hardening), so i'm landing #33 first as the lead. once it's in, please rebase on main and keep just your approval-time term change, adopting #33's calc_assets_to_redeem signature so the two line up. one small divergence to reconcile while you're there: you map total_assets <= 0 to InsufficientLiquidity, #33 maps only < 0 to InvalidAmount, yours is the more informative mapping for a drained pool so call that out in the rebase if you keep it.
-
you'll need the rebase regardless because #31 just merged and reworked the redeem path in lending_pool/src/lib.rs, so your green ci predates the current main.
separately, not for this pr: neither this nor main enforces the configured min_term/max_term inside request_loan, it only rejects term == 0. since approval now faithfully uses the borrower term, that gap is reachable, might be worth a follow-up issue.
i'll re-review as soon as #33 is in and you've rebased.
if you want to keep contributing, join us on Telegram: https://t.me/+DOylgFv1jyJlNzM0
LoanManager.approve_loan now uses the loan's stored term_ledgers (falling back to read_default_term() only when 0 / unset), instead of overwriting the borrower-requested term with the contract-wide default. The min_term/max_term limits checked at request time now actually govern the approved due_date. Regression test asserts that requesting a non-default term and approving produces a due_date derived from the requested term. Scope narrowed: the redeem-math hardening (LabsCrypt#3) has been dropped in favour of LabsCrypt#33's version, which the maintainer is landing as the lead. This PR keeps only the approve-time term fix from LabsCrypt#5; the redeem hardening will arrive via LabsCrypt#33 and is no longer duplicated here. cargo test -p lending_pool -p loan_manager green (50 + 89). Closes LabsCrypt#5
52dbf92 to
0f373be
Compare
ogazboiz
left a comment
There was a problem hiding this comment.
exactly the focused change i was hoping for, you dropped the calc_assets_to_redeem overlap and kept just the approval-term fix, so it no longer collides with #33. approve_loan now honors the borrower's stored term_ledgers (falling back to the default only when it's 0 for legacy loans) instead of overwriting it with the contract default, which is the issue #5 fix. ci is green, and your change is in approve_loan while #33's is in the accrual path, so they don't overlap. merging.
if you want to keep contributing, join us on Telegram: https://t.me/+DOylgFv1jyJlNzM0
Summary
Narrowed-scope re-spin per review: only the #5 approve-time term fix remains in this PR. The redeem-math hardening I had under #3 has been dropped in favour of #33's version, which the maintainer is landing as the lead.
approve_loanoverrode the borrower-requested term:loan_manager/src/lib.rsnow honours the loan's storedterm_ledgers(falling back toread_default_term()only when 0 / unset), so themin_term/max_termlimits checked at request time actually govern the approveddue_date. New regression test asserts that requesting a non-default term and approving produces adue_datederived from the requested term.Test plan
cargo test -p loan_manager— 89 / 89 pass (88 existing + 1 new for [Contracts] LoanManager approve_loan overrides the borrower-requested term with the default term #5).cargo test -p lending_pool— 50 / 50 pass (unmodified).upstream/main.Closes #5