feat: smart collateral liquidation & BNB provider for fixed-term markets#172
Open
ricklista wants to merge 14 commits into
Open
Conversation
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>
…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>
… functions - liquidateSmartCollateral: return actual seized (collAmount) and repaid (loanToken balance diff) instead of placeholder values - flashLiquidateSmartCollateral: capture repaidAssets from onMoolahLiquidate callback via _lastRepaidAssets storage
…ation Remove partial interest tracking logic and always reset interestRepaid and lastRepaidTime after liquidation deduction.
Amount now clears interest first, then principal. New fixed position principal <= amount. Also fix constructor args after rebase and add exact-full and excess-capped conversion tests.
- [L03] Block smart collateral markets from being liquidated via normal liquidate() by adding _isSmartCollateral() check that detects StableSwapLPCollateral via minter() -> TOKEN() chain - [I01] Add sellBNB() function for selling native BNB received from smart collateral redemptions, mirroring Liquidator.sol - [I05] Add zero address check in batchSetSmartProviders() - [I07] Fix incorrect comment on BOT role constant
…ixed-term markets - BrokerLiquidator: add liquidateSmartCollateral, flashLiquidateSmartCollateral, redeemSmartCollateral, withdrawETH, receive(), and smart provider whitelist - MarketFactory: auto-set BNBProvider for WBNB fixed-term markets, route smart provider config to brokerLiquidator for fixed-term vs liquidator/publicLiquidator for common markets - IBroker: expose ORACLE() view - Tests: add coverage for BNB provider and smart provider in fixed-term market creation
20f1bf6 to
4567528
Compare
2e7613d to
3d3d63c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add smart collateral liquidation support to
BrokerLiquidatorfor fixed-term broker markets, and extendMarketFactoryto auto-configureBNBProviderfor WBNB-based fixed-term markets and route smart provider whitelisting tobrokerLiquidator(vsliquidator/publicLiquidatorfor common markets).Change type
Contracts changed
src/liquidator/BrokerLiquidator.solsrc/liquidator/IBrokerLiquidator.solsrc/moolah/MarketFactory.solsrc/broker/interfaces/IBroker.solscript/broker/deploy_brokerLiquidator.s.soltest/broker/LendingBroker.t.soltest/moolah/MarketFactoryTest.solInterface changes
IBrokerLiquidator — new functions:
withdrawETH(uint256 amount) externalliquidateSmartCollateral(bytes32, address, address, uint256, uint256, bytes) external returns (uint256, uint256)flashLiquidateSmartCollateral(bytes32, address, address, uint256, address, address, bytes, bytes, bytes) external returns (uint256, uint256)smartProviders(address) external view returns (bool)batchSetSmartProviders(address[], bool) externalBrokerLiquidator — new functions/features:
receive() external payable— accept native BNBwithdrawETH(uint256)— MANAGER roleliquidateSmartCollateral(...)— BOT role, seize + redeem LP via SmartProviderflashLiquidateSmartCollateral(...)— BOT role, flash liquidation with LP redemption + token swaps in callbackredeemSmartCollateral(...)— BOT role, standalone LP redemptionbatchSetSmartProviders(...)— MANAGER roleSmartProvidersChanged,SmartLiquidationIBrokerLiquidator.MoolahLiquidateData struct — new fields appended:
swapSmartCollateral,smartProvider,minToken0Amt,minToken1Amt,token0Pair,token1Pair,swapToken0Data,swapToken1DataIBroker — new function:
ORACLE() external view returns (IOracle)MarketFactory — signature changes:
createFixedTermMarket(FixedTermMarketParams, bool)— addedliquidatorSmartProviderparambatchCreateFixedTermMarkets(FixedTermMarketParams[], bool[])— addedliquidatorSmartProvidersparamStorage layout
BrokerLiquidator (upgrade): Two new state variables appended after existing storage:
mapping(address => bool) public smartProviders(slot afterbrokerToMarketId)uint256 internal _lastRepaidAssets(next slot)No variables reordered or removed. No
__gapin this contract. New variables are strictly appended — no storage collision risk.MarketFactory: No new state variables. All new dependencies (
brokerLiquidator,BNBProvider, etc.) areimmutable(set in constructor, stored in bytecode). No storage impact.Access control
BrokerLiquidator:
BOT-gated functions:liquidateSmartCollateral,flashLiquidateSmartCollateral,redeemSmartCollateralMANAGER-gated functions:withdrawETH,batchSetSmartProvidersMarketFactory: Unchanged —
createFixedTermMarketandbatchCreateFixedTermMarketsremainOPERATOR-gated.Risk assessment
receive()+withdrawETHadded — MANAGER-gated withdrawal mitigates risk.onMoolahLiquidatecallback handles smart collateral swaps with external calls to whitelisted pairs._lastRepaidAssetsused as transient state across callback boundaryflashLiquidateSmartCollateralcallback makes external calls totoken0Pair/token1Pairwith user-supplied calldata — mitigated bypairWhitelistcheck. Native BNB sent viacall{value}to whitelisted pairsDeployment
script/broker/deploy_brokerLiquidator.s.sol(updated forpayablecast)BrokerLiquidatorimplementation, upgrade proxy via timelock. Deploy newMarketFactoryimplementation (constructor args includebrokerLiquidatoraddress), upgrade proxy via timelock.Test plan
forge testpasses (9/9 MarketFactoryTest)testCreateFixedTermMarketWithBNBLoan— BNBProvider auto-set for WBNB loan marketstestCreateFixedTermMarketWithBNBCollateral— BNBProvider auto-set for WBNB collateral marketstestCreateFixedTermMarketWithSmartProvider— smart provider config routes to brokerLiquidatortestBatchCreateFixedTermMarkets— batch creation with mixed BNB + smart provider markets