Skip to content

fix: persist extra interest overflow in deductFixedPositionDebt#167

Open
ricklista wants to merge 12 commits into
feature/brokerLiquidatorSupportSmartProviderfrom
fix/fixed-position-extra-interest-overflow
Open

fix: persist extra interest overflow in deductFixedPositionDebt#167
ricklista wants to merge 12 commits into
feature/brokerLiquidatorSupportSmartProviderfrom
fix/fixed-position-extra-interest-overflow

Conversation

@ricklista

Copy link
Copy Markdown
Contributor

Summary

  • When liquidation reduces principal significantly, the interest formula (based on reduced principal) can no longer represent the full outstanding interest. Previously this overflow at BrokerMath.deductFixedPositionDebt L567 was silently lost.
  • Added fixedPositionExtraInterest mapping (address => uint256 => uint256) to LendingBroker to persist the overflow, and threaded it through all debt/interest calculation paths: getTotalDebt, peek, previewRepayFixedLoanPosition, refinanceMaturedFixedPositions, repay, and onMoolahLiquidate.
  • Cleanup of fixedPositionExtraInterest entries on position removal and bad debt clearing.

Test plan

  • All 12 BrokerMathDeductFixedTest tests pass (updated for new 4-param/4-return signature)
  • All 64 LendingBroker tests pass

qingyang-lista and others added 12 commits March 31, 2026 22:10
…al liquidation

When partial liquidation deducts both interest and principal from a fixed
position, the old code unconditionally reset interestRepaid=0 and
lastRepaidTime=now. This erased any unpaid interest from position tracking,
causing getUserTotalDebt() to under-report debt.

The fix introduces three branches:
1. All interest covered -> safe to reset (unchanged behavior)
2. Partial interest + formula can represent outstanding -> adjust interestRepaid
   precisely so outstanding = accruedInterest - paidInterest (exact)
3. Partial interest + formula too small (most principal repaid) -> set
   interestRepaid=0 without resetting lastRepaidTime to maximize preserved
   outstanding

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add `redeemSmartCollateral` to allow BOT role to redeem smart collateral
LP tokens via whitelisted SmartProviders. Add `batchSetSmartProviders` for
MANAGER role to manage the provider whitelist.
…r shared impl

Move RELAYER and ORACLE from constructor immutables to storage variables
(appended at end of layout to preserve upgrade compatibility). This allows
all LendingBroker proxies to share a single implementation deployment.

- Constructor now only takes moolah address
- initialize() accepts relayer and oracle as additional params
- Added setRelayer() and setOracle() manager-only setters
- Updated deploy scripts and all tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…-interest-tracking

fix: preserve unpaid interest in deductFixedPositionDebt during partial liquidation
- Add RelayerSet/OracleSet event emissions in LendingBroker migration setters (#7)
- Rename withdrawERC20 to withdraw with native BNB support in BrokerLiquidator (#1)
- Add NatSpec documenting custody requirement for redeemSmartCollateral (#2)
… functions

- liquidateSmartCollateral: return actual seized (collAmount) and
  repaid (loanToken balance diff) instead of placeholder values
- flashLiquidateSmartCollateral: capture repaidAssets from
  onMoolahLiquidate callback via _lastRepaidAssets storage
…event silent loss

When liquidation reduces principal significantly, the interest formula
(based on reduced principal) can no longer represent the full outstanding
interest. Previously this overflow was silently lost. Now it is persisted
in a fixedPositionExtraInterest mapping and included in all debt/interest
calculations.
…xedPositionDebt

When partial liquidation deducts only interest (no principal), extraInterest
was preserved unchanged, causing interestRepaid to exceed formula-based accrued
interest and triggering arithmetic underflow on subsequent reads.
…on on full liquidation

- repay(): consume fixedPositionExtraInterest before increasing interestRepaid,
  preventing interestRepaid from exceeding formula-based accrued interest
- _deductFixedPositionsDebt(): keep position alive when principalRepaid >= principal
  but extraInterest > 0, preventing silent loss of unpaid interest debt
- Add unit tests for BrokerMath extraInterest edge cases
- Add integration tests verifying extraInterest creation via partial liquidation,
  interest-only repay consumption, and position cleanup
@ricklista ricklista force-pushed the feature/brokerLiquidatorSupportSmartProvider branch from c5205c0 to f4a2e4a Compare April 16, 2026 04:03
@ricklista ricklista force-pushed the feature/brokerLiquidatorSupportSmartProvider branch from 2e7613d to 3d3d63c Compare June 16, 2026 09:29
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.

4 participants