Verified contractPartialUNLICENSED
FLAMMExitMaxima
Address0x6a6a3ff1e834dbbf07dee472e07d1ca977798659
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.35
- Same code
2 more contracts
Details
- Compiler
- solc 0.8.35+commit.47b9dedd
- Language
- solidity
- Optimization
- Yes, 100 runs
- EVM version
- cancun
- Via IR
- yes
- License
- UNLICENSED
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,480,505
- Creation transaction
- 0xb100ff8e74…ae47669e96
- Main copy (most transactions)
- 0x480f5186…234e0a81
- Libraries
- src/core/mm/MMRouterLib.sol:MMRouterLib0xa474…d2b4e5src/core/flamm/FLAMMLane.sol:FLAMMLane0x0988…d22e98src/core/flamm/FLAMMRoute.sol:FLAMMRoute0x312e…0fdaa9src/periphery/FLAMMMaxima.sol:FLAMMMaxima0x4a99…d6259csrc/core/flamm/FLAMMLawLib.sol:FLAMMLawLib0x5d47…27cc62src/core/flamm/FLAMMOpsLib.sol:FLAMMOpsLib0x0bfe…043753src/core/mm/AaveV3ReadsLib.sol:AaveV3ReadsLib0xd53f…ed5983src/core/flamm/FLAMMFlowLib.sol:FLAMMFlowLib0x2577…4e3473src/core/flamm/FLAMMGateLib.sol:FLAMMGateLib0xf0be…2b9555src/core/flamm/FLAMMKeptLib.sol:FLAMMKeptLib0x7a11…58517bsrc/core/mm/CTokenMarketLib.sol:CTokenMarketLib0x18c5…b2071csrc/core/flamm/FLAMMAdmitLib.sol:FLAMMAdmitLib0xa6db…bdcbcbsrc/core/flamm/FLAMMBlindLib.sol:FLAMMBlindLib0x0b8d…2e7e5csrc/core/flamm/FLAMMClaimLib.sol:FLAMMClaimLib0x02b4…731ccesrc/core/flamm/FLAMMFenceLib.sol:FLAMMFenceLib0xa16d…a201e8src/core/flamm/FLAMMFloatLib.sol:FLAMMFloatLib0x539e…a79737src/core/flamm/FLAMMQuoteLib.sol:FLAMMQuoteLib0xa4d3…d5ce2esrc/core/flamm/FLAMMTopUpLib.sol:FLAMMTopUpLib0x7c55…d233dfsrc/core/mm/CTokenRewardsLib.sol:CTokenRewardsLib0xcda6…a4dcf7src/core/mm/MMRouterAdminLib.sol:MMRouterAdminLib0x8423…7adc96src/core/flamm/FLAMMPolicyLib.sol:FLAMMPolicyLib0xa6b4…0c67f9src/core/flamm/FLAMMSettleLib.sol:FLAMMSettleLib0xb489…d3c7cbsrc/core/flamm/FLAMMSharedLib.sol:FLAMMSharedLib0xfaae…2511basrc/periphery/FLAMMExitMaxima.sol:FLAMMExitMaxima0x480f…4e0a81src/hooks/everlong/LeverageLib.sol:LeverageLib0x1b1d…40ad11src/core/flamm/FLAMMMaintainLib.sol:FLAMMMaintainLib0x9499…409cc2src/core/flamm/FLAMMPairQuoteLib.sol:FLAMMPairQuoteLib0x0775…d94cbesrc/hooks/everlong/EverlongOpsLib.sol:EverlongOpsLib0xabe4…7426f7
55 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/periphery/FLAMMExitMaxima.solmain23.75 KB
- lib/openzeppelin-contracts-compat/contracts/interfaces/draft-IERC6093.sol6.54 KB
- lib/openzeppelin-contracts-compat/contracts/token/ERC20/IERC20.sol2.77 KB
- lib/openzeppelin-contracts-compat/contracts/token/ERC20/extensions/IERC20Metadata.sol683 B
- lib/openzeppelin-contracts-compat/contracts/utils/math/Math.sol12.51 KB
- lib/openzeppelin-contracts-compat/contracts/utils/math/SafeCast.sol35.08 KB
- lib/openzeppelin-contracts-upgradeable-compat/contracts/proxy/utils/Initializable.sol8.91 KB
- lib/openzeppelin-contracts-upgradeable-compat/contracts/token/ERC20/ERC20Upgradeable.sol12.23 KB
- lib/openzeppelin-contracts-upgradeable-compat/contracts/utils/ContextUpgradeable.sol1.19 KB
- src/core/PriceFeed.sol19.12 KB
- src/core/flamm/FLAMM.sol54.32 KB
- src/core/flamm/FLAMMAdmitLib.sol37.26 KB
- src/core/flamm/FLAMMBlindLib.sol41.3 KB
- src/core/flamm/FLAMMClaimLib.sol49.42 KB
- src/core/flamm/FLAMMClaimVault.sol1.65 KB
- src/core/flamm/FLAMMFenceLib.sol72.83 KB
- src/core/flamm/FLAMMFloatLib.sol57.91 KB
- src/core/flamm/FLAMMFlowLib.sol55.24 KB
- src/core/flamm/FLAMMGateLib.sol87.39 KB
- src/core/flamm/FLAMMKeptLib.sol45.59 KB
- src/core/flamm/FLAMMLane.sol70.27 KB
- src/core/flamm/FLAMMLawLib.sol60.67 KB
- src/core/flamm/FLAMMMaintainLib.sol41.67 KB
- src/core/flamm/FLAMMOpsLib.sol32.2 KB
- src/core/flamm/FLAMMPolicyLib.sol39.49 KB
- src/core/flamm/FLAMMQuoteLib.sol16.89 KB
- src/core/flamm/FLAMMRoute.sol16.45 KB
- src/core/flamm/FLAMMSettleLib.sol42.92 KB
- src/core/flamm/FLAMMSharedLib.sol13.11 KB
- src/core/flamm/FLAMMStore.sol135.42 KB
- src/core/flamm/FLAMMSwapLib.sol55.65 KB
- src/core/flamm/FLAMMTopUpLib.sol48.97 KB
- src/core/mm/MMRouterAdminLib.sol24.07 KB
- src/core/mm/MMRouterLib.sol72.71 KB
- src/core/mm/VenueAccountBase.sol15.61 KB
- src/core/mm/VenueReads.sol2.05 KB
- src/interfaces/core/IDepositAllowlist.sol1.06 KB
- src/interfaces/core/IEverlongCore.sol889 B
- src/interfaces/core/IPriceFeed.sol3.71 KB
- src/interfaces/core/flamm/IFLAMM.sol52.68 KB
- src/interfaces/core/flamm/IFLAMMFence.sol17.71 KB
- src/interfaces/core/flamm/IFLAMMHooks.sol8.36 KB
- src/interfaces/core/flamm/IHookCapabilities.sol2.63 KB
- src/interfaces/core/flamm/IPairHook.sol14.14 KB
- src/interfaces/core/mm/IAccountDeployer.sol2.99 KB
- src/interfaces/core/mm/IFinancingAccount.sol46.04 KB
- src/interfaces/core/mm/IHeldRepay.sol2.61 KB
- src/interfaces/core/mm/IMMRouter.sol70.37 KB
- src/interfaces/utils/integrations/IAggregatorV3Interface.sol818 B
- src/libraries/Constants.sol2.11 KB
- src/libraries/DelayLib.sol11.22 KB
- src/libraries/ReentrancyGuardLib.sol2.52 KB
- src/libraries/SafeTransferLib.sol5.11 KB
- src/periphery/FLAMMMaxima.sol44.38 KB
- src/propamm/libraries/PropAMMLaneLib.sol2.31 KB
src/periphery/FLAMMExitMaxima.sol
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2025-2026 Everlong Labs Limited
pragma solidity ^0.8.26;
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {FLAMMStore, WAD} from "src/core/flamm/FLAMMStore.sol";
import {FLAMMGateLib} from "src/core/flamm/FLAMMGateLib.sol";
import {IMMRouter} from "src/interfaces/core/mm/IMMRouter.sol";
import {MMRouterLib} from "src/core/mm/MMRouterLib.sol";
import {IFinancingAccount, CAN_REPAY, CAN_WITHDRAW_SUPPLY} from "src/interfaces/core/mm/IFinancingAccount.sol";
import {IHeldRepay, IRepayable} from "src/interfaces/core/mm/IHeldRepay.sol";
import {FLAMMMaxima} from "src/periphery/FLAMMMaxima.sol";
/// @notice The exit maximum of any pool, outside it: the largest `redeemProRata` a holder may make now, which
/// `FLAMMMaxima.maxRedeemProRata` runs, on the facts and the pool book `FLAMMMaxima` reads, as its entry maxima
/// do. A library apart, linked into `FLAMMMaxima`, so each fits a contract; read through `FLAMMMaxima`.
library FLAMMExitMaxima {
/// @dev One venue's exit legs as the exit maximum sizes them ({_exitFits}):
/// - Its tokens; its collateral and supply, net of a redemption fee; what it releases and withdraws now.
/// - Its market's unit payouts where it shares one reserve's cash (`FLAMMSharedLib.sharedUnits`').
/// - Whether its supply may leave through custody under the batch (`FLAMMFlowLib.proRata`: no supply cut by a
/// loss, and an action it allows).
/// - Whether it releases none while it owes under a redemption fee, custody paying its collateral slice.
/// - Where the exit's repay there may be held short, its debt (`hd`), and how: at a kind with a minimum
/// position (`CAP_HELD_REPAY`) owing past the lenders' rounding, the most a held repay leaves it owing
/// (`hk`: its minimum position, the lenders' rounding and two units more; all of its debt where the minimum
/// cannot be read), and at a lender that takes repays only up to a bound a debt can pass (`IRepayable`: a
/// Venus market's total borrows, which a sole borrower's remainder passes), that bound (`hr`, else
/// `type(uint256).max`) and its market (`hm`, zero for none), whose one bound every venue borrowing there
/// spends.
/// - What it may release while a held repay leaves it owing (`hf`: at a kind whose collateral accrues all but
/// the last whole units `FLAMMAdmitLib.heldRelease` keeps posted, else `type(uint256).max`), and there the
/// dust the exit may leave posted where custody cannot pay that slice (`hu`: those units at
/// `max(lltv - ltv, gap, 1 %)` and the lenders' rounding; zero elsewhere), and the least such a release
/// takes (`hn`: one unit's assets and one, under which it releases none, as a release of less would burn a
/// whole unit, and the share it does not release joins that dust).
/// - Whether it reads past its threshold owing more than the lenders' rounding (`sick`).
struct ExitLeg {
uint8 coll;
uint8 debt;
bool shared;
bool batchable;
bool fromCustody;
uint256 c;
uint256 s;
uint256 rel;
uint256 wd;
uint256 cu;
uint256 su;
uint256 hd;
uint256 hk;
uint256 hr;
uint256 hf;
uint256 hu;
uint256 hm;
uint256 hn;
bool sick;
}
/// @notice The largest `redeemProRata` of `owner`'s shares now, at the supply the exit burns at (after its fee
/// skim), with every slice rounded down as the exit takes it ({_exitFits}).
/// @dev The bounds:
/// - Each supply slice lies within what its venue pays now, the venues sharing one reserve's cash together
/// within the least any reports, less a whole unit's payout per leg where two or more legs of one token draw
/// on it (`FLAMMSharedLib.sharedUnits`' legs), unless the slice is under its token's batch and custody covers
/// it, which then pays it.
/// - Each collateral slice is released as far as its venue releases now (those sharing one reserve out of the
/// cash their supply legs leave), the rest paid from the pool's custody of that token, which pays too the
/// exiter's own custody slice and the supply legs it moves, all within it, and where the token is owed only
/// while what custody and its supply leave still cover its debt.
/// - Zero under the lock, at or under the dead shares, while an indebted venue refuses a repay, or below the
/// count whose debt legs clear an unhealthy venue outright: a venue whose threshold equals its LTV reads
/// unhealthy once the next accrual adds interest (`IFinancingAccount.healthy`), so there it reads zero below
/// that count, though smaller exits may execute.
/// - A frozen venue owing no more than a lender's rounding bounds nothing: the exit forfeits there.
/// - A venue that owes under a redemption fee releases nothing: custody pays its collateral slice.
/// - A venue that may take the exit's repay short releases in proportion to what it takes, read where its
/// kind reports the hold: at a minimum position (`CAP_HELD_REPAY`, `IHeldRepay.minimumDebt`: the repay down
/// to it, the lenders' rounding and two units over it; none where it cannot be read), or at the bound its
/// lender takes repays up to (`IRepayable.repayable`: a Venus market's total borrows, which a sole
/// borrower's remainder passes), the venues borrowing from one market spending its one bound in the exit's
/// order; while the repay leaves it owing it keeps its last whole units posted at a kind whose collateral
/// accrues, and custody pays the rest of its collateral slice, as the exit does
/// (`FLAMMFlowLib._heldRepay`): a share under one unit's assets and one releases nothing, custody pays the
/// slice and what it cannot of that share may stay posted beside the dust, while a slice that small stays
/// posted whole, held or not (`FLAMMGateLib.nearUnitsAt`, which the preview reads).
/// - So no exit it advertises refuses for a hold its kind reports; past a venue's threshold, a repay of its
/// whole debt the lender takes short leaves a remainder that can keep the release in proportion past what
/// the lender allows, so the maximum reads zero there, though such an exit executes where the last whole
/// units the venue keeps carry the remainder.
/// - A repay that leaves the venue owing nothing holds nothing (`FLAMMAdmitLib.heldRelease`: an Aave repay of
/// the whole debt taken a scaled unit short, a debt read a unit over after another account's repay on the
/// venue's behalf).
/// - An exit it advertises may pay short of its preview by the dust a held venue may leave posted where
/// custody cannot pay its slice; floors taken from the preview, less that dust, hold.
function maxRedeemProRata(address pool, address owner) public view returns (uint256 maxShares) {
FLAMMMaxima.Ctx memory c = FLAMMMaxima._ctx(pool);
if (c.locked || c.supply <= FLAMMStore.MINIMUM_SHARES) return 0;
(FLAMMGateLib.Book memory b,) = FLAMMMaxima._book(c);
(uint256 lo, uint256 hi) = FLAMMMaxima._pendingFeeShares(c, b);
uint256 supply = c.supply + lo;
maxShares = Math.min(c.p.balanceOf(owner), supply - FLAMMStore.MINIMUM_SHARES);
ExitLeg[] memory legs = new ExitLeg[](c.n);
uint256 clearing;
uint256 slack = MMRouterLib.RELEASE_SLACK;
for (uint16 i; i < c.n; ++i) {
if ((c.retired >> i) & 1 != 0) continue;
(bool refused, uint256 debt) = _exitLeg(c, i, legs[i]);
if (refused) return 0;
if (debt <= slack || c.r.venueHealthy(pool, i)) continue;
legs[i].sick = true;
clearing = Math.max(clearing, Math.mulDiv(debt - slack - 1, c.supply + hi, debt) + 1);
}
maxShares = _exitFit(c, b, legs, supply, maxShares);
if (maxShares < clearing) return 0;
}
/// @dev Venue `i`'s exit legs into `e` ({ExitLeg}); `refused` where it owes and refuses a repay, which no exit
/// passes; its debt, zero where it holds nothing or is a frozen venue owing no more than a lender's rounding,
/// where the exit forfeits and it bounds nothing.
function _exitLeg(FLAMMMaxima.Ctx memory c, uint16 i, ExitLeg memory e)
private
view
returns (bool refused, uint256 debt)
{
uint256 coll;
uint256 sup;
(, coll,, sup, debt) = c.r.venuePosition(address(c.p), i);
if ((sup | coll | debt) == 0) return (false, 0);
uint256 coll0 = coll;
IMMRouter.VenueView memory w = c.v[i];
IFinancingAccount.Capacity memory k = c.r.venueCapacity(address(c.p), i);
if (k.actions == 0 && (c.gated >> i) & 1 != 0 && debt <= MMRouterLib.RELEASE_SLACK) return (false, 0);
if (debt != 0 && k.actions & CAN_REPAY == 0) return (true, debt);
(e.coll, e.debt, e.rel, e.wd) = (w.coll, w.debt, k.collateralReleasable, k.supplyWithdrawable);
bool supplyCut;
if (((c.fee | c.lossy) >> i) & 1 != 0) {
(uint64 cl, uint64 sl) = IFinancingAccount(w.account).haircut(w.id);
if ((c.fee >> i) & 1 != 0) {
// a redemption fee: the exit's legs there leave net of it, and while it owes it releases none
(coll, sup) = (coll - Math.mulDiv(coll, cl, WAD), sup - Math.mulDiv(sup, sl, WAD));
e.fromCustody = cl != 0 && debt != 0;
} else {
// a loss on its supply keeps the exit's supply leg at the venue. The exit cuts by the larger of the
// lender's ratio and the pool's mark of it (`FLAMMKeptLib.largerLoss`), and it skims first, which clears
// the mark where the lender's ratio reads zero and raises it where it reads more: so after that skim
// the ratio it cuts by is above zero exactly where the lender's is, the one fact read here
supplyCut = sl != 0;
}
}
(e.c, e.s, e.hr, e.hf) = (coll, sup, type(uint256).max, type(uint256).max);
if ((c.held >> i) & 1 != 0) {
if (debt > MMRouterLib.RELEASE_SLACK) {
(bool ok, bytes memory ret) = w.account.staticcall(abi.encodeCall(IHeldRepay.minimumDebt, (w.id)));
(e.hd, e.hk) = (debt, debt);
if (ok && ret.length >= 64) {
(uint256 known, uint256 floor) = abi.decode(ret, (uint256, uint256));
if (known != 0) e.hk = floor + MMRouterLib.RELEASE_SLACK + 2;
}
}
} else if (debt != 0) {
// every indebted venue whose kind answers, as the venues borrowing from one market share its bound
(bool ok, bytes memory ret) = w.account.staticcall(abi.encodeCall(IRepayable.repayable, (w.id)));
if (ok && ret.length >= 64) (e.hr, e.hm) = abi.decode(ret, (uint256, uint256));
if (e.hm != 0) e.hd = debt;
}
if (e.hd != 0 && (c.accruing >> i) & 1 != 0) {
// what a release leaves posted while a held repay leaves the venue owing, and the dust past custody the
// exit allows there (`FLAMMAdmitLib.heldRelease`)
uint256 f = IFinancingAccount(w.account).collateralUnitsToAssets(w.id, 1) + 1;
uint256 most = f;
e.hn = f;
(uint256 lltv, uint256 pin) = (w.lltvWad, c.p.pairConfig(w.pair).ltvWad);
if (lltv > pin) {
uint256 gap = c.r.pair(address(c.p), w.pair).safetyGapWad;
if (gap < FLAMMStore.MIN_DUST_GAP_WAD) gap = FLAMMStore.MIN_DUST_GAP_WAD;
(f, most) = (
Math.mulDiv(f, lltv, lltv - pin, Math.Rounding.Up),
Math.mulDiv(f, lltv, lltv - pin < gap ? gap : lltv - pin, Math.Rounding.Up)
);
}
(e.hf, e.hu) = (coll0 > f ? coll0 - f : 0, most + MMRouterLib.RELEASE_SLACK);
}
e.batchable = !supplyCut && ((c.gated >> i) & 1 == 0 || k.actions & CAN_WITHDRAW_SUPPLY != 0);
if ((c.shared >> i) & 1 != 0) {
e.shared = true;
IFinancingAccount a = IFinancingAccount(w.account);
if (sup != 0) e.su = a.supplySharesToAssets(w.id, 1);
if (coll != 0 && !e.fromCustody) e.cu = a.collateralUnitsToAssets(w.id, 1);
}
}
/// @dev {ExitScratch}'s `dust` once a short release other than one held venue's slice joins a token's.
uint256 private constant NO_DUST = type(uint256).max;
/// @dev {_exitFits}' scratch, one word per token: custody's slice and the supply legs it moves (`held`), the short
/// releases it pays (`short`), the dust the exit may leave posted where the only one is the slice a venue
/// that holds its repay keeps (`dust`; {NO_DUST} once any other short release joins it, as custody pays them
/// in the exit's order), and at the venues sharing one reserve the supply and collateral slices, their unit
/// payouts, their count and the least cash any reports; one word per venue: its repay leg where it may take
/// it short and what it takes (`owed`, `paid`), and one slot per market the venues repay at, its bound less
/// what the repays before took (`mk`, `room`).
struct ExitScratch {
uint256[] held;
uint256[] short;
uint256[] dust;
uint256[] sup;
uint256[] col;
uint256[] units;
uint256[] count;
uint256[] least;
uint256[] owed;
uint256[] paid;
uint256[] mk;
uint256[] room;
}
/// @dev The largest exit, up to `most` shares of `supply`, whose legs the venues and custody pay ({_exitFits}):
/// `most` where it fits, else the largest that does, by bisection; each fits where every larger one does not,
/// as each leg only grows with the exit.
function _exitFit(
FLAMMMaxima.Ctx memory c,
FLAMMGateLib.Book memory b,
ExitLeg[] memory legs,
uint256 supply,
uint256 most
) private view returns (uint256) {
uint256 m = c.m;
for (uint256 i; i < legs.length; ++i) {
if (legs[i].s != 0 && legs[i].batchable) FLAMMMaxima._batch(c, legs[i].debt);
}
uint256 backs = _backs(b);
uint256 n = legs.length;
ExitScratch memory x = ExitScratch(
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](m),
new uint256[](n),
new uint256[](n),
new uint256[](n),
new uint256[](n)
);
if (_exitFits(b, legs, c.batch, backs, most, supply, x)) return most;
uint256 lo;
uint256 hi = most;
while (hi - lo > 1) {
uint256 mid = lo + (hi - lo) / 2;
if (_exitFits(b, legs, c.batch, backs, mid, supply, x)) lo = mid;
else hi = mid;
}
return lo;
}
/// @dev Whether an exit of `s` shares of `supply` executes as {maxRedeemProRata} reads it, each slice rounded down
/// as the exit takes it: in venue order, each supply slice under its token's batch (`batch`, one past it) that
/// custody covers beside what it already pays, of a token that backs no debt (`backs`), moves through custody
/// (`FLAMMFlowLib.proRata`); every other supply slice within what its venue pays now, or, at the venues
/// sharing one reserve, all of a token's together within the least cash any reports, less a unit's payout per
/// leg where two or more draw on it; each collateral slice released as far as its venue releases now, those
/// sharing one reserve out of the cash their supply legs leave, the rest short; and per token, custody's own
/// slice, the supply it moves and every short release within the custody the pool book holds
/// (`FLAMMFlowLib._fromOwnCustody`).
function _exitFits(
FLAMMGateLib.Book memory b,
ExitLeg[] memory legs,
uint256[] memory batch,
uint256 backs,
uint256 s,
uint256 supply,
ExitScratch memory x
) private pure returns (bool) {
uint256 m = b.legs.length;
for (uint256 t; t < m; ++t) {
(x.held[t], x.short[t], x.dust[t], x.sup[t], x.col[t], x.units[t], x.count[t], x.least[t]) =
(Math.mulDiv(b.legs[t].held, s, supply), 0, 0, 0, 0, 0, 0, type(uint256).max);
}
for (uint256 i; i < legs.length; ++i) {
ExitLeg memory e = legs[i];
if (e.s == 0) continue;
uint256 v = Math.mulDiv(e.s, s, supply);
if (v == 0) continue;
uint256 t = e.debt;
if (e.batchable && v + 1 < batch[t] && (backs >> t) & 1 == 0 && x.held[t] + v <= b.legs[t].held) {
x.held[t] += v;
} else if (e.shared) {
x.sup[t] += v;
x.units[t] += e.su;
++x.count[t];
if (e.wd < x.least[t]) x.least[t] = e.wd;
} else if (v > e.wd) {
return false;
}
}
// the repays a venue may take short, in the exit's order, venue by venue, every indebted one repaid whatever
// its collateral slice (`FLAMMFlowLib._exitLegs`): each down to its minimum, the lenders' rounding and two
// units over it (none where the minimum cannot be read), and at most what its lender takes now, the venues
// borrowing from one market each taking at most what the repays before it left of the market's one bound
for (uint256 i; i < legs.length; ++i) x.mk[i] = 0;
for (uint256 i; i < legs.length; ++i) {
ExitLeg memory e = legs[i];
if (e.hd == 0) continue;
uint256 owed = Math.mulDiv(e.hd, s, supply, Math.Rounding.Up) + MMRouterLib.RELEASE_SLACK;
// the exit that leaves only the dead shares repays the whole debt, as the pool sizes it
if (owed > e.hd || s + FLAMMStore.MINIMUM_SHARES == supply) owed = e.hd;
uint256 paid = owed < e.hd && e.hd - owed < e.hk ? (e.hd > e.hk ? e.hd - e.hk : 0) : owed;
if (e.hm != 0) {
uint256 j;
while (x.mk[j] != e.hm && x.mk[j] != 0) ++j;
if (x.mk[j] == 0) (x.mk[j], x.room[j]) = (e.hm, e.hr);
if (paid > x.room[j]) paid = x.room[j];
x.room[j] -= paid;
}
// past its threshold, a repay of the whole debt the lender takes short leaves a remainder that can keep
// the release in proportion past what the lender allows: no such exit is advertised
if (e.sick && owed == e.hd && paid < owed) return false;
(x.owed[i], x.paid[i]) = (owed, paid);
}
for (uint256 i; i < legs.length; ++i) {
ExitLeg memory e = legs[i];
if (e.c == 0) continue;
uint256 v = Math.mulDiv(e.c, s, supply);
if (v == 0) continue;
uint256 a = e.coll;
if (e.fromCustody) {
(x.short[a], x.dust[a]) = (x.short[a] + v, NO_DUST);
continue;
}
if (e.hd != 0) {
// a repay the venue takes short: it releases in proportion to what it takes, keeping its last whole
// units posted while it still owes at a kind whose collateral accrues, and custody pays the rest of the
// slice (`FLAMMFlowLib._heldRepay`)
(uint256 owed, uint256 paid) = (x.owed[i], x.paid[i]);
if (paid < owed) {
uint256 h = Math.min(Math.mulDiv(v, paid, owed), e.hf);
uint256 hu = e.hu;
if (h < e.hn) (hu, h) = (hu + h, 0);
if (h < v) {
// the dust this venue may leave posted, past custody, where its slice is the only short
x.dust[a] = x.short[a] != 0 ? NO_DUST : Math.min(v - h, hu);
(x.short[a], v) = (x.short[a] + v - h, h);
}
if (v == 0) continue;
}
}
if (e.shared) {
x.col[a] += v;
x.units[a] += e.cu;
++x.count[a];
if (e.rel < x.least[a]) x.least[a] = e.rel;
} else if (v > e.rel) {
(x.short[a], x.dust[a]) = (x.short[a] + v - e.rel, NO_DUST);
}
}
for (uint256 t; t < m; ++t) {
if (x.count[t] != 0) {
// the cash less a unit's payout per leg where two or more draw on it, and a unit per venue for what
// the exit's earlier legs on the reserve move its reads by
uint256 cash = x.least[t];
uint256 u = (x.count[t] < 2 ? 0 : x.units[t]) + legs.length;
cash = cash > u ? cash - u : 0;
if (x.sup[t] > cash) return false;
cash -= x.sup[t];
if (x.col[t] > cash) (x.short[t], x.dust[t]) = (x.short[t] + x.col[t] - cash, NO_DUST);
}
// custody pays every short release, all but the dust where a held venue's slice is the only one
uint256 dust = x.dust[t] == NO_DUST ? 0 : x.dust[t];
if (x.held[t] + x.short[t] > b.legs[t].held + dust) return false;
if (x.short[t] != 0 && !_netDebtFreeAfter(b.legs[t], x.held[t] + x.short[t], s, supply, legs.length)) {
return false;
}
}
return true;
}
/// @dev Whether token `L` stays net debt-free within the lenders' rounding after an exit of `s` shares of
/// `supply` whose custody payout of it is `paid`, short releases among it: custody that pays a short release
/// leaves past the token's pro-rata slice, so where the token is owed the exit's ledger gate refuses it
/// unless what custody and supply leave still covers what the exit leaves owed (`ExitWorsensLedger`). Read
/// low: the debt as repaid at its pro-rata floor, the supply as withdrawn at its pro-rata ceiling, a unit
/// per venue of rounding.
function _netDebtFreeAfter(FLAMMGateLib.Leg memory L, uint256 paid, uint256 s, uint256 supply, uint256 n)
private
pure
returns (bool)
{
if (L.owed == 0) return true;
uint256 owedAfter = L.owed - Math.mulDiv(L.owed, s, supply);
uint256 cover = L.held - paid + L.supplied - Math.mulDiv(L.supplied, s, supply, Math.Rounding.Up);
return owedAfter + n <= cover + MMRouterLib.RELEASE_SLACK;
}
/// @dev The tokens that back a debt, as the pool reads the pool book (`FLAMMGateLib.backsDebt`, on the partners and
/// backs of {FLAMMMaxima._book}): an exit's supply leg of such a token stays at its venue.
function _backs(FLAMMGateLib.Book memory b) private pure returns (uint256 backs) {
for (uint256 t; t < b.legs.length; ++t) {
if (FLAMMGateLib.backsDebt(b, t)) backs |= uint256(1) << t;
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {
"src/core/mm/MMRouterLib.sol": {
"MMRouterLib": "0xA4749766934833DfBeaF4Fc0d79c150A1Dd2B4E5"
},
"src/core/flamm/FLAMMLane.sol": {
"FLAMMLane": "0x0988077AB06F1efd8cFa3f08FC60CAf3e6d22e98"
},
"src/core/flamm/FLAMMRoute.sol": {
"FLAMMRoute": "0x312ee92ecd113F925dD513d450b062C8740fDaA9"
},
"src/periphery/FLAMMMaxima.sol": {
"FLAMMMaxima": "0x4A997343D91c981BF63274fc222AA96D32d6259c"
},
"src/core/flamm/FLAMMLawLib.sol": {
"FLAMMLawLib": "0x5D473B15E032756Cd81fc66B8F1e9BE2f427cc62"
},
"src/core/flamm/FLAMMOpsLib.sol": {
"FLAMMOpsLib": "0x0BFe60fB1f5223F1A2b7B02Ef18f5C33fb043753"
},
"src/core/mm/AaveV3ReadsLib.sol": {
"AaveV3ReadsLib": "0xD53F6170226A74D453e84414Ffc2FFcE5ded5983"
},
"src/core/flamm/FLAMMFlowLib.sol": {
"FLAMMFlowLib": "0x2577D707D41209489b2EF53101a4f6baDd4E3473"
},
"src/core/flamm/FLAMMGateLib.sol": {
"FLAMMGateLib": "0xF0BE115A2D6F27274F2e9EE50B89Ed42db2B9555"
},
"src/core/flamm/FLAMMKeptLib.sol": {
"FLAMMKeptLib": "0x7a11899232dd9F17c28bF062Ec2ee2267158517b"
},
"src/core/mm/CTokenMarketLib.sol": {
"CTokenMarketLib": "0x18c53F2aCAbcDEEdb575A136f49fa4C43EB2071c"
},
"src/core/flamm/FLAMMAdmitLib.sol": {
"FLAMMAdmitLib": "0xA6dBD8cD5Cd10F8a5857cbeD54a28405ffbDcBCb"
},
"src/core/flamm/FLAMMBlindLib.sol": {
"FLAMMBlindLib": "0x0b8d436F1a7AE25C5d16955ed4CDa781E42e7E5C"
},
"src/core/flamm/FLAMMClaimLib.sol": {
"FLAMMClaimLib": "0x02b4354a56630ab165A880B1198a603E62731cCE"
},
"src/core/flamm/FLAMMFenceLib.sol": {
"FLAMMFenceLib": "0xA16ddA83f47a42F875f141b6954595D584a201e8"
},
"src/core/flamm/FLAMMFloatLib.sol": {
"FLAMMFloatLib": "0x539EfC2b17cF0fdF254AAaB925b3A7c360A79737"
},
"src/core/flamm/FLAMMQuoteLib.sol": {
"FLAMMQuoteLib": "0xa4D34f4882a529574840881e755BCFae2bd5ce2e"
},
"src/core/flamm/FLAMMTopUpLib.sol": {
"FLAMMTopUpLib": "0x7C55a535Ce298b1e5e6d00F645088F777ad233DF"
},
"src/core/mm/CTokenRewardsLib.sol": {
"CTokenRewardsLib": "0xcda63d7eCCF838F2732Bff676706BDE5abA4DcF7"
},
"src/core/mm/MMRouterAdminLib.sol": {
"MMRouterAdminLib": "0x8423744b3F304ED0B43043d7ad4E7d8A247aDc96"
},
"src/core/flamm/FLAMMPolicyLib.sol": {
"FLAMMPolicyLib": "0xa6B495B2BdcDcf4F327844d9aaA0eD4C550c67F9"
},
"src/core/flamm/FLAMMSettleLib.sol": {
"FLAMMSettleLib": "0xB4890a27205505BDAA05717B6c3aB38b2dd3C7Cb"
},
"src/core/flamm/FLAMMSharedLib.sol": {
"FLAMMSharedLib": "0xFaAEd38962aEb6c282Ac36E0cdE9313AbA2511ba"
},
"src/periphery/FLAMMExitMaxima.sol": {
"FLAMMExitMaxima": "0x480f51866554e92649c9d3502bA31AA4234e0a81"
},
"src/hooks/everlong/LeverageLib.sol": {
"LeverageLib": "0x1B1D1C42d26c169dB59D8681d0FD4C9a2840Ad11"
},
"src/core/flamm/FLAMMMaintainLib.sol": {
"FLAMMMaintainLib": "0x94998f13fc5d5D2271c90b835c6a805dad409cC2"
},
"src/core/flamm/FLAMMPairQuoteLib.sol": {
"FLAMMPairQuoteLib": "0x077523D1427aE408105fe8592F907ECDa0D94cbE"
},
"src/hooks/everlong/EverlongOpsLib.sol": {
"EverlongOpsLib": "0xAbe4447C954E3C7a405f8E75116BF9f5787426F7"
}
},
"optimizer": {
"runs": 100,
"enabled": true
},
"viaSSACFG": false,
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts-compat/contracts/",
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable-compat/contracts/",
"solady/=lib/solady/src/",
"forge-std/=lib/forge-std/src/"
],
"experimental": false
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.