Skip to content

D6: Partial-unwind liquidation protection#230

Open
NnamdiCyber wants to merge 1 commit into
Dgetsylver:mainfrom
NnamdiCyber:d6-partial-unwind-liquidation-protection
Open

D6: Partial-unwind liquidation protection#230
NnamdiCyber wants to merge 1 commit into
Dgetsylver:mainfrom
NnamdiCyber:d6-partial-unwind-liquidation-protection

Conversation

@NnamdiCyber
Copy link
Copy Markdown
Contributor

Summary

Implements orange-zone partial unwind: when HF drops below a configurable orange_hf threshold, unwind the minimal amount needed to restore target_hf rather than forcing a full close.

Changes

storage.rs — Two new Config fields:

  • orange_hf — trigger threshold (partial unwind fires when HF drops below this)
  • target_hf — HF to restore to after unwind

leverage.rs — Replaced compute_unwind_loops with compute_partial_unwind:
Closed-form solution for the minimal repay amount x:

x = ceil((target_hf × debt_value − supply_value × c_factor) / (target_hf − c_factor))

Returns (0, 0) if already at target or no debt.

blend_pool.rs — New submit_partial_unwind(repay, withdraw, config): single atomic [WithdrawCollateral, Repay] submit with exact amounts.

lib.rs — Constructor accepts orange_hf (arg 8) and target_hf (arg 9). rebalance() triggers on hf < orange_hf, permissionless (keeper- or user-callable).

Tests

  • test_partial_unwind_already_at_target — returns (0, 0)
  • test_partial_unwind_no_debt — returns (0, 0)
  • test_partial_unwind_single_loop — correct amount, minimality verified
  • test_partial_unwind_max_loops — partial (not full) close restores HF
  • test_partial_unwind_unhealthy_position — HF restored to target

Closes #47

- Add orange_hf (trigger) and target_hf (restore-to) fields to Config
- Replace compute_unwind_loops with compute_partial_unwind: closed-form
  minimal repay/withdraw to restore HF to target_hf in one atomic step
- Add submit_partial_unwind to blend_pool: single [Withdraw, Repay] submit
- rebalance() now triggers on HF < orange_hf and executes partial unwind
- Tests: already_at_target, no_debt, single_loop (minimality), max_loops,
  unhealthy_position
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.

D6: Partial-unwind liquidation protection

1 participant