Skip to content

Clear phantom debt/collateral types after exact zeroing#199

Merged
nialexsan merged 2 commits intonialexsan/multi-debtfrom
lionel/fix-phantom-balance-types
Mar 4, 2026
Merged

Clear phantom debt/collateral types after exact zeroing#199
nialexsan merged 2 commits intonialexsan/multi-debtfrom
lionel/fix-phantom-balance-types

Conversation

@liobrasil
Copy link
Contributor

Summary

Fix phantom debt/collateral type retention after exact zeroing so one-type guardrails in PR #184 are released correctly when a type balance is fully cleared.

Previous behavior (bug example)

Example position lifecycle:

  • Collateral: MOET
  • Debt: FLOW

Action sequence:

  1. Borrow 300 FLOW (debt type becomes FLOW).
  2. Repay exactly 300 FLOW (debt balance reaches 0).
  3. Try to borrow 100 MOET.

Before this fix:

  • The debt-type list still included FLOW even at zero balance.
  • validateDebtType treated position as still locked to FLOW debt.
  • Borrowing MOET incorrectly failed due to a phantom/stale debt type.

Equivalent issue also existed for collateral:

  • After exact full withdrawal of the only collateral token, the collateral-type list could still keep that token type at zero balance, incorrectly blocking a new collateral type deposit.

Why this fix is needed

PR #184 intentionally adds temporary one-collateral/one-debt constraints. Those constraints must track active non-zero balances, not stale zero balances. Otherwise users can get permanently constrained by phantom types after valid exact repay/withdraw flows.

What changed

  • Updated type list helpers in FlowALPModels.cdc:
    • getDebtTypes() now includes only types with scaledBalance > 0.0.
    • getCollateralTypes() now includes only types with scaledBalance > 0.0.
  • This ensures type constraints reflect real active exposure and clear automatically after exact zeroing.

Tests

  • Added regression tests in cadence/tests/debt_type_constraint_three_token_test.cdc:
    • testExactRepayClearsDebtTypeConstraint
    • testExactFullWithdrawClearsCollateralTypeConstraint
  • Ran:
    • flow test cadence/tests/debt_type_constraint_three_token_test.cdc
    • flow test cadence/tests/single_token_constraint_test.cdc

@liobrasil liobrasil requested a review from a team as a code owner March 4, 2026 22:22
@nialexsan nialexsan merged commit 3641469 into nialexsan/multi-debt Mar 4, 2026
@nialexsan nialexsan deleted the lionel/fix-phantom-balance-types branch March 4, 2026 23:06
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.

2 participants