Skip to content

Liquidation Phase 2 (DEX): stacked on #41#43

Merged
kgrgpg merged 15 commits intofeature/liquidation-mechanismfrom
feature/liquidation-phase2-merged
Sep 3, 2025
Merged

Liquidation Phase 2 (DEX): stacked on #41#43
kgrgpg merged 15 commits intofeature/liquidation-mechanismfrom
feature/liquidation-phase2-merged

Conversation

@kgrgpg
Copy link
Contributor

@kgrgpg kgrgpg commented Sep 2, 2025

Stacked on #41 (base: feature/liquidation-mechanism).

What

  • Introduces DEX-based liquidation path in cadence/contracts/TidalProtocol.cdc: liquidateViaDex(...) and LiquidationExecutedViaDex event.
  • Adds DEX guards and config with getters/governance:
    • allowedSwapperTypes allowlist (by Type)
    • dexOracleDeviationBps (max DEX-implied vs oracle deviation)
    • dexMaxSlippageBps, dexMaxRouteHops
    • getDexLiquidationConfig() and setDexLiquidationConfig(...)
  • Adds resource-scoped helpers: internalSeize, internalRepay.
  • Adds transactions/scripts:
    • pool-management/liquidate_via_dex.cdc
    • pool-management/liquidate_via_mock_dex.cdc (test-only convenience)
    • pool-governance/set_dex_liquidation_config.cdc
    • scripts/tidal-protocol/get_dex_liquidation_config.cdc
  • Adds contracts/mocks/MockDexSwapper.cdc for testing; updates flow.json testing alias.
  • Tests: liquidation_phase2_dex_test.cdc + updates to test_helpers.cdc, liquidation_phase1_test.cdc.

How it works

  1. Quote internal seize amount and cap by maxSeizeAmount.
  2. Validate swapper is allowlisted and matches seizeType/debtType.
  3. Compute DEX-implied price from quote and compare to oracle; require deviation ≤ dexOracleDeviationBps.
  4. Seize collateral (internalSeize) and swap via DeFiActions.Swapper.
  5. Optional slippage guard when a quote is provided: enforce slippageBps ≤ dexMaxSlippageBps.
  6. Repay debt (internalRepay) and emit LiquidationExecutedViaDex.

Defaults

  • allowedSwapperTypes = {} (must be allowlisted by governance)
  • dexOracleDeviationBps = 300 (3%)
  • dexMaxSlippageBps = 100 (1%)
  • dexMaxRouteHops = 3

Diff vs base (#41)

  • Modified: cadence/contracts/TidalProtocol.cdc, cadence/tests/liquidation_phase1_test.cdc, cadence/tests/test_helpers.cdc, flow.json
  • Added: cadence/contracts/mocks/MockDexSwapper.cdc, cadence/tests/liquidation_phase2_dex_test.cdc, cadence/scripts/tidal-protocol/get_dex_liquidation_config.cdc, cadence/transactions/tidal-protocol/pool-governance/set_dex_liquidation_config.cdc, cadence/transactions/tidal-protocol/pool-management/liquidate_via_dex.cdc, cadence/transactions/tidal-protocol/pool-management/liquidate_via_mock_dex.cdc
  • Stats: +456 / -1 across 10 files

Testing notes

  • The DEX test deploys and allowlists MockDexSwapper, sets priceRatio to satisfy the oracle deviation guard, funds the MOET vault for swap output, and liquidates via the DEX path.

Risks and considerations

  • Governance must allowlist the intended swapper types before use.
  • Tune dexOracleDeviationBps and dexMaxSlippageBps to operational preferences.
  • MockDexSwapper is test-only and should not be used in production.

Stacking

…n- Add allowedSwapperTypes + dexOracleDeviationBps with getters/governance setter\n- Implement liquidateViaDex with oracle dev guard + slippage minRepay\n- Add get_dex_liquidation_config script\n- Add set_dex_liquidation_config governance tx\n- Add liquidate_via_dex management tx
… + separate DEX test\n- Pool.liquidateViaDex takes Swapper, adds oracle/slippage/hops guards\n- Add Pool.internalSeize/internalRepay (resource)\n- Add MockDexSwapper + liquidate_via_mock_dex tx\n- Move DEX test into liquidation_phase2_dex_test.cdc; remove from phase1
Tests: add snapshot-based safe reset and handle zero-quote insolvency/rounding edges. Docs: clarify repay-all redemption, partial-to-above-one, and insolvency scenarios. Contract: quoteLiquidation rounds up and avoids HF-worsening keeper steps with zero-quote/full-seize fallback.
…allback in quoteLiquidation

Tests: add insolvency_redemption_test.cdc; update repay_and_close_position.cdc to repay then withdraw.\nContract: discrete bounded-search fallback (~16 steps) in quoteLiquidation when direct solve would worsen HF; preserves determinism and caps.
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Sep 3, 2025

Merged 'feature/liquidation-phase2-merged' into base 'feature/liquidation-mechanism'.

Reason: common Phase 1 liquidation edits were necessary in the base (HF-maximizing fallback, borrower insolvency redemption test, and related helpers). To minimize cherry-picking and keep history coherent, we merged the changes directly into the base branch.

Status: base updated and tests pass locally (one pre-existing rounding-guard assertion remains unchanged). Phase 2 work stays on this branch as-is.

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.

1 participant

Comments