Verified contractExactUNLICENSED
FLAMMPairQuoteLib
Address0xa0c8489d5a572eaeaeeddf48cb888c20a950c3e3
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.35
- Same code
Only this contract
Details
- Compiler
- solc 0.8.35+commit.47b9dedd
- Language
- solidity
- Optimization
- Yes, 100 runs
- EVM version
- cancun
- Via IR
- yes
- License
- UNLICENSED
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,446,213
- Creation transaction
- 0xfa81f667d2…6fc9da24fe
- Libraries
- src/core/mm/MMRouterLib.sol:MMRouterLib0xa474…d2b4e5src/core/flamm/FLAMMLane.sol:FLAMMLane0x82a0…7f67dfsrc/core/flamm/FLAMMRoute.sol:FLAMMRoute0x312e…0fdaa9src/periphery/FLAMMMaxima.sol:FLAMMMaxima0x4a99…d6259csrc/core/flamm/FLAMMLawLib.sol:FLAMMLawLib0x5d47…27cc62src/core/flamm/FLAMMOpsLib.sol:FLAMMOpsLib0x3bd2…f79a69src/core/mm/AaveV3ReadsLib.sol:AaveV3ReadsLib0xd53f…ed5983src/core/flamm/FLAMMFlowLib.sol:FLAMMFlowLib0xac61…6a909dsrc/core/flamm/FLAMMGateLib.sol:FLAMMGateLib0x2291…64f98csrc/core/flamm/FLAMMKeptLib.sol:FLAMMKeptLib0x7a11…58517bsrc/core/mm/CTokenMarketLib.sol:CTokenMarketLib0xfd87…6df060src/core/flamm/FLAMMAdmitLib.sol:FLAMMAdmitLib0x4aac…e1aed6src/core/flamm/FLAMMBlindLib.sol:FLAMMBlindLib0x9e50…7eeb41src/core/flamm/FLAMMClaimLib.sol:FLAMMClaimLib0xc6c9…da2afesrc/core/flamm/FLAMMFenceLib.sol:FLAMMFenceLib0x739d…5a629fsrc/core/flamm/FLAMMFloatLib.sol:FLAMMFloatLib0xf128…b16b70src/core/flamm/FLAMMQuoteLib.sol:FLAMMQuoteLib0xf284…efd526src/core/flamm/FLAMMTopUpLib.sol:FLAMMTopUpLib0xd561…60c5a5src/core/mm/CTokenRewardsLib.sol:CTokenRewardsLib0xcda6…a4dcf7src/core/mm/MMRouterAdminLib.sol:MMRouterAdminLib0x8423…7adc96src/core/flamm/FLAMMPolicyLib.sol:FLAMMPolicyLib0x6fe6…678097src/core/flamm/FLAMMSettleLib.sol:FLAMMSettleLib0x1834…56e060src/core/flamm/FLAMMSharedLib.sol:FLAMMSharedLib0xfaae…2511basrc/periphery/FLAMMExitMaxima.sol:FLAMMExitMaxima0x480f…4e0a81src/hooks/everlong/LeverageLib.sol:LeverageLib0x1b1d…40ad11src/core/flamm/FLAMMMaintainLib.sol:FLAMMMaintainLib0x5691…f4d380src/core/flamm/FLAMMPairQuoteLib.sol:FLAMMPairQuoteLib0xa0c8…50c3e3src/hooks/everlong/EverlongOpsLib.sol:EverlongOpsLib0xabe4…7426f7
53 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/core/flamm/FLAMMPairQuoteLib.solmain21.6 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.sol55.76 KB
- src/core/flamm/FLAMMAdmitLib.sol37.35 KB
- src/core/flamm/FLAMMBlindLib.sol44.11 KB
- src/core/flamm/FLAMMClaimLib.sol49.63 KB
- src/core/flamm/FLAMMClaimVault.sol1.65 KB
- src/core/flamm/FLAMMFenceLib.sol72.81 KB
- src/core/flamm/FLAMMFloatLib.sol58.79 KB
- src/core/flamm/FLAMMFlowLib.sol54.32 KB
- src/core/flamm/FLAMMGateLib.sol90.43 KB
- src/core/flamm/FLAMMKeptLib.sol45.86 KB
- src/core/flamm/FLAMMLane.sol70.53 KB
- src/core/flamm/FLAMMLawLib.sol60.67 KB
- src/core/flamm/FLAMMMaintainLib.sol42.7 KB
- src/core/flamm/FLAMMOpsLib.sol32.2 KB
- src/core/flamm/FLAMMPolicyLib.sol39.79 KB
- src/core/flamm/FLAMMQuoteLib.sol17 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.51 KB
- src/core/flamm/FLAMMSwapLib.sol55.76 KB
- src/core/flamm/FLAMMTopUpLib.sol50.7 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/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/propamm/libraries/PropAMMLaneLib.sol2.31 KB
src/core/flamm/FLAMMPairQuoteLib.sol
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2025-2026 Everlong Labs Limited
pragma solidity ^0.8.26;
import {FLAMMStore, InvalidAmount} from "src/core/flamm/FLAMMStore.sol";
import {FLAMMGateLib} from "src/core/flamm/FLAMMGateLib.sol";
import {FLAMMSwapLib} from "src/core/flamm/FLAMMSwapLib.sol";
import {FLAMMSettleLib} from "src/core/flamm/FLAMMSettleLib.sol";
import {FLAMMAdmitLib} from "src/core/flamm/FLAMMAdmitLib.sol";
import {FLAMMMaintainLib} from "src/core/flamm/FLAMMMaintainLib.sol";
import {FLAMMFloatLib} from "src/core/flamm/FLAMMFloatLib.sol";
import {FLAMMSharedLib} from "src/core/flamm/FLAMMSharedLib.sol";
import {FLAMMTopUpLib} from "src/core/flamm/FLAMMTopUpLib.sol";
import {IMMRouter} from "src/interfaces/core/mm/IMMRouter.sol";
import {ReentrancyGuardLib} from "src/libraries/ReentrancyGuardLib.sol";
import {
PairRequest,
PAIR_MODE_COVER,
PAIR_MODE_BORROW,
PAIR_MODE_CROSS,
PAIR_MODE_ROTATE,
PAIR_MODE_HELD_ONLY
} from "src/interfaces/core/flamm/IPairHook.sol";
/// @notice The swap's quotes and its request, apart from the swap: each takes the swap's branch and plan on the same
/// state (`FLAMMSwapLib`), asks the hook's quote with the swap's request, and checks the fill as the swap does.
/// A quote is re-asked at the input the fill used, rounded to native units, so a swap of the quoted input fills
/// the quote. A payout that draws past what the Router can fund for it now, or a cover's that leans on a
/// release a venue past its threshold refuses, reverts as the swap would (`IMMRouter.InsufficientLiquidity`,
/// or `IMMRouter.InsufficientCollateral` at a strict release), rather than quoting a smaller fill the swap
/// would not make. Each entry has the pool's own selector: `FLAMMFront`'s fallback routes the pool's call here
/// as it stands.
library FLAMMPairQuoteLib {
/// @dev How many times a quote is re-asked at the input it used.
uint256 internal constant REQUOTES = 2;
/// @notice `IFLAMM.previewSwap`, any pair: the fill a swap of `amountIn` of `tokenIn` for `tokenOut` makes now.
function previewSwap(address tokenIn, address tokenOut, uint256 amountIn)
public
view
returns (uint256 amountInUsed, uint256 amountOut, uint256 feeWad)
{
ReentrancyGuardLib._readGuard();
return quote(FLAMMStore.s(), tokenIn, tokenOut, amountIn);
}
/// @notice `IFLAMM.pairRequest`: the request a swap of `amountIn` hands the swap hook now, on the swap's branch and
/// plan; `sender` is the caller. The swap's own request words (`FLAMMSwapLib.requestAt`), past its
/// selector, kept as the answer, so the two cannot differ.
function pairRequest(address tokenIn, address tokenOut, uint256 amountIn) public view returns (PairRequest memory r) {
ReentrancyGuardLib._readGuard();
if (amountIn == 0) revert InvalidAmount();
FLAMMGateLib.Book memory none;
(FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b) =
_plan(FLAMMStore.s(), tokenIn, tokenOut, amountIn, none);
uint256 m = FLAMMSwapLib.requestAt(p, b, bytes4(0));
// the 26 words past the selector, kept: the free pointer moves past the request's 836 bytes
assembly ("memory-safe") {
r := add(m, 4)
mstore(0x40, add(m, 864))
}
}
/// @notice The quote: the swap's plan, the hook's quote at it and the swap's checks; re-asked at the input it used
/// while that is under the one asked, on the first full pass's book and prices, as no read can move within
/// the call (each pass still takes its own branch at its input); then, where the payout draws past the
/// cash, the Router's funding of what it draws, which must cover it, where it rotates, the Router's funding
/// of the rotation's draw, each after the swap's loss check at its pair (`VenueReportsLoss`, on a pool
/// that lists a venue kind reporting losses), and where a cover's payout or a cash payout passes the
/// output's custody and lent
/// supply, the collateral the venues can release for it now.
function quote(FLAMMStore.S storage $, address tokenIn, address tokenOut, uint256 amountIn)
internal
view
returns (uint256 used, uint256 out, uint256 feeWad)
{
if (amountIn == 0) revert InvalidAmount();
FLAMMSwapLib.Plan memory p;
FLAMMGateLib.Book memory b;
// the book a later pass on the full branch reads again: none until a pass has read it
FLAMMGateLib.Book memory full = b;
for (uint256 k; k <= REQUOTES; ++k) {
(p, b) = _plan($, tokenIn, tokenOut, amountIn, full);
FLAMMSwapLib.quoteCall(p, b);
FLAMMSwapLib.validate(p, b, 0);
if (p.usedNative >= amountIn) break;
amountIn = p.usedNative;
if (p.mode & PAIR_MODE_HELD_ONLY == 0) full = b;
}
_funded($, p, b);
_rotationFunded($, p, b);
_released($, p, b);
return (p.usedNative, p.netNative, p.fill.feeWad);
}
/// @dev The swap's branch and plan on the state as it stands: custody alone where it takes that branch, else the
/// book as the law reads it, `full` where an earlier pass of the quote read it (its prices kept: the plan
/// prices only a pair it holds unread, and the law re-reads no price it holds).
function _plan(
FLAMMStore.S storage $,
address tokenIn,
address tokenOut,
uint256 amountIn,
FLAMMGateLib.Book memory full
) private view returns (FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b) {
(uint8 x, uint8 y) = FLAMMSwapLib.route($, tokenIn, tokenOut);
if (FLAMMSwapLib.heldOnlyCandidate($, x, y, amountIn)) {
b = FLAMMGateLib.custodyBook($);
p = FLAMMSwapLib.planHeld($, b, x, y, tokenIn, tokenOut, amountIn);
if (p.ceiling != 0) return (p, b);
}
b = full.legs.length != 0 ? full : FLAMMGateLib.bookOf($);
p = FLAMMSwapLib.plan($, b, x, y, tokenIn, tokenOut, amountIn);
}
/// @dev Where the fill draws (its net, protocol part and operator's share past the custody and the collateral the
/// Router would reclaim now, on a plan that may borrow; at least the settlement's `MIN_DRAW` where the draw is
/// past the output's supply, as the settlement floors it), what the Router can fund of the draw now on the
/// payout pair (`IMMRouter.fundingCeiling`: the output's supply withdrawn, then each venue's slice at its
/// cash, caps, rate ceiling and pin), posting the backing token's custody, its lent supply as far as its
/// lenders can return it now (the plan's `lent`, what the settlement's withdrawal gets) and the input where it
/// is that token, less what the input repays of its own debt first, which is read as repaid where it clears
/// it: reverts `VenueReportsLoss` first where the payout pair has a venue that borrows reporting a loss or
/// taking a fee on its collateral ({FLAMMSettleLib.requireNoLossAt}, the swap's check, at the swap's point:
/// where the payout passes custody and what its releases pay, a payout the pair's supply would pay too);
/// then `InsufficientLiquidity` by what it lacks, as the swap's `fund` would.
/// - The swap reaches its draw where the releases it makes fall short of what the payout needs: the quote
/// reads those releases as the Router's `reclaimable` says, so where a venue releases less than it reports,
/// the swap reverts `VenueReportsLoss` where the quote fills.
/// - A crossing whose input owes at a venue that refuses repays (`FLAMMGateLib.refusedDebt`) clears nothing
/// there: it is read as the swap runs it, the input still owed: its custody, the larger of what the Router
/// frees now and what the repay frees at the other venues ({_freed}), then the Router's funding with no debt
/// assumed repaid, so its drawn set is read before the repay. The quote may refuse such a crossing that
/// would fill, never the reverse.
/// - Where venues sharing one reserve's cash post the output (`FLAMMGateLib.sharedFit`), the Router's reclaim,
/// which counts that cash at each, counts it once per reserve, and where the draw's venues share a reserve
/// the releases draw on too, the funding, read before the releases, must fund the draw and what those
/// releases take of that reserve's cash (`shared`), posting the collateral that much more needs at the pin:
/// the quote may refuse there too, within `shared` of a limit only the other venues meet.
/// - The plan passes over a slice that would leave a venue holding a minimum position under it
/// (`MMRouterLib.buildPlan`), as the swap's does on the same state; but the settlement plans after it
/// withdraws the lent backing, which can free post room under the posting market's supply cap, and after
/// its releases out of a reserve the draw's venue shares, which matters where that venue is held under the
/// reserve's cash by its debt cap, its collateral or its rate ceiling. Each can move a slice across such a
/// venue's minimum between the two plans, so a quoted payout can revert `InsufficientLiquidity` by the slice
/// the swap's plan passes over, under one minimum position, and a payout the quote refuses can fill
/// (`IFinancingAccount` ADMISSION); no swap fills under its quote.
function _funded(FLAMMStore.S storage $, FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b) private view {
// a pool whose venues are all idle or retired still plans the draw, which its `fund` then refuses: so does this
if (p.mode & PAIR_MODE_BORROW == 0) return;
FLAMMGateLib.Leg memory Ly = b.legs[p.y];
// the payout, the protocol's part and the operator's share, which the settlement funds together (`keep`'s high
// half, `FLAMMSwapLib.validate`): where custody holds them, it calls no venue for them, so nothing past custody
// is read
uint256 out = p.netNative + (p.keep >> 128);
if (out <= Ly.held) return;
// a crossing whose input owes at a venue that refuses repays clears nothing there: read as the swap runs it,
// the input still owed, so custody pays where it can and the Router refuses where its `fund` would
bool crossing = p.mode & PAIR_MODE_CROSS != 0;
bool cross = crossing && FLAMMGateLib.refusedDebt($, p.x) == 0;
// the custody, then the collateral the settlement reclaims before it draws: what the Router frees of it now,
// after a crossing's repay all its venues can release (the draw's rule leaves `y` backing no other debt), and
// past a venue that refuses repays at least what the repay frees at the others
uint256 liquid = Ly.held;
uint256 shared;
if (Ly.posted != 0) {
(uint256 sum, uint256 mine) = (0, 0);
if ($.sharedVenues & ~uint256($.retiredVenues) != 0) {
(shared, sum, mine) = FLAMMGateLib.sharedFit($, p.y, $.pairs[p.borrowPair].venues);
}
uint256 got;
if (cross) {
got = FLAMMGateLib.postedReleasable($, p.y);
} else {
got = $.router.reclaimable(address(this), p.y, b.usd);
// the Router counts one reserve's cash at each venue sharing it
uint256 over = sum - shared;
got = got > over ? got - over : 0;
}
if (crossing && !cross) {
uint256 f = _freed($, p, b, type(uint256).max);
if (f > got) got = f;
}
// two or more legs at one reserve each redeem a whole unit at least: a unit's payout per leg comes off what
// its releases pay, and onto what they take of its cash
uint256 u = _units($, p.y);
got = got > u ? got - u : 0;
liquid += got;
// what those releases may take of the cash of a reserve the draw's venues share
if (shared > got) shared = got;
if (shared > mine) shared = mine;
if (shared != 0) shared += u;
}
if (out <= liquid) return;
// the swap's draw check, on the same pair and before the funding, as {FLAMMSettleLib}'s `_borrow` runs it
FLAMMSettleLib.requireNoLossAt($, p.borrowPair);
uint256 need = out - liquid;
if (need < FLAMMSettleLib.MIN_DRAW && need > Ly.supplied) need = FLAMMSettleLib.MIN_DRAW;
uint8 pair = p.borrowPair;
uint256 a = $.pairs[pair].coll;
FLAMMGateLib.Leg memory La = b.legs[a];
uint256 coll = La.held;
if (a == p.x) {
// the input, less what its repay takes first, as the swap's `_repayInput` sizes it
coll += p.usedNative;
uint256 pay = La.owed < coll ? La.owed : coll;
if (
pay > p.usedNative && pay < La.owed && (La.partner == FLAMMStore.MANY || FLAMMGateLib.split(b, a))
) pay = p.usedNative;
coll -= pay;
}
coll += p.lent;
uint16 assume = cross ? uint16(1 << p.x) : 0;
// the releases before the draw take `shared` of a reserve's cash, which the funding reads whole: it must fund
// the draw and that much more, which only the venues of that reserve lose, borrowed notionally at the pin
uint256 ask = need;
uint256 pad;
if (shared != 0) {
ask += shared;
uint256 frame = FLAMMGateLib.frameOf(b, a, p.y);
if (frame != 0) pad = FLAMMGateLib.requiredPosted(shared, $.pairs[pair].ltvWad, frame);
}
uint256 got = $.router.fundingCeiling(address(this), pair, ask, coll + pad, b.usd, assume);
if (got < ask) revert IMMRouter.InsufficientLiquidity(ask - got);
}
/// @dev Where the fill rotates (its net, protocol part and operator's share past the output's custody less what
/// the rotation keeps for the law), what the Router can fund now of the rotation's draw at `rotIn`: the sum
/// of the repays its release takes at `rotOut` (`FLAMMTopUpLib.rotationNeed`), posting the input's custody
/// and the input: reverts `VenueReportsLoss` where `rotIn` has a venue that borrows reporting a loss or
/// taking a fee on its collateral (the swap's check at its rotation, {FLAMMSettleLib.requireNoLossAt}), then
/// `InsufficientLiquidity` by what it lacks, as the rotation's `fund` would.
function _rotationFunded(FLAMMStore.S storage $, FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b)
private
view
{
if (p.mode & PAIR_MODE_ROTATE == 0) return;
// `keep`'s low half: its high half carries the fill's protocol part and the operator's share
// (`FLAMMSwapLib.validate`), counted next
uint256 cash = b.legs[p.y].held - uint128(p.keep);
uint256 out = p.netNative + (p.keep >> 128);
if (out <= cash) return;
// the swap's check at the rotation's draw pair, as {FLAMMSettleLib}'s `_rotate` runs it
FLAMMSettleLib.requireNoLossAt($, p.rotIn);
uint256 need = FLAMMTopUpLib.rotationNeed(p.rotOut, out - cash);
uint256 coll = b.legs[p.x].held + p.usedNative;
uint256 got = $.router.fundingCeiling(address(this), p.rotIn, need, coll, b.usd, 0);
if (got < need) revert IMMRouter.InsufficientLiquidity(need - got);
}
/// @dev Where a cover pays past the output's custody and what its lenders can return, the rest is the output's
/// posted collateral the swap frees ({_freed}). Only that counts: reverts by what the rest lacks as the swap
/// would, `InsufficientCollateral` at its strict release where `y` is no pair's debt, else
/// `InsufficientLiquidity` at its withdraw-only funding. A repay that clears the input's debt frees all of it
/// on a pool none of whose venues can refuse a release; elsewhere each venue counts what it can release now
/// (`FLAMMGateLib.releasableOf`), and a venue the input owes at that refuses repays keeps its debt. What the
/// swap reclaims above a venue's pin first, a partial release a venue past its threshold might still make,
/// and what an indebted venue releases down to its pin are not counted, so the quote may refuse a swap that
/// would fill, never the reverse: a kind that may take less of a repay than offered (a minimum position)
/// declares it (`CAP_HELD_REPAY`), and a partial repay's release counts there only where the repay leaves the
/// debt over that minimum (`FLAMMGateLib.holdsRepay`), as the venue then takes it whole. A payout that neither
/// covers, draws nor rotates (the cash path, `y` backing nothing) is bounded the same way past its custody and
/// what its lenders return: by what `y`'s venues can release now.
function _released(FLAMMStore.S storage $, FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b) private view {
uint256 m = p.mode & (PAIR_MODE_COVER | PAIR_MODE_BORROW | PAIR_MODE_ROTATE);
FLAMMGateLib.Leg memory Ly = b.legs[p.y];
if (m == 0) {
// the cash path: what pays past custody and the supply its lenders return is the posted collateral of a
// `y` that backs nothing, every venue of it debt-free: only what each can release now counts
if (Ly.posted == 0 || FLAMMGateLib.backsDebt(b, p.y)) return;
} else {
if (m != PAIR_MODE_COVER) return;
FLAMMGateLib.Leg memory Lx = b.legs[p.x];
// the repay clears the debt: every venue releases all of it, where none can refuse a release
if (Lx.held + p.usedNative >= Lx.owed && $.gatedVenues & ~$.retiredVenues == 0) return;
}
// the payout, the protocol's part and the operator's share, which the settlement funds together: where custody
// holds them, it calls no venue for them, and a supply of nothing returns nothing
uint256 need = p.netNative + (p.keep >> 128);
if (need <= Ly.held) return;
uint256 liquid = Ly.held + (Ly.supplied == 0 ? 0 : FLAMMGateLib.withdrawable($, p.y, Ly.supplied));
if (need <= liquid) return;
need -= liquid;
uint256 got;
if (m == 0) {
got = FLAMMGateLib.postedReleasable($, p.y);
// a unit's payout per leg where two or more share one reserve's cash ({_freed})
uint256 u = _units($, p.y);
got = got > u ? got - u : 0;
} else {
got = _freed($, p, b, need);
}
if (got >= need) return;
need -= got;
// the swap's last step: a strict release where `y` is no pair's debt, else the withdraw-only funding
if (Ly.partner == FLAMMStore.NO_PARTNER) revert IMMRouter.InsufficientCollateral(need);
revert IMMRouter.InsufficientLiquidity(need);
}
/// @dev What the input's repay frees of the output for the swap's reclaim and release, counted up to `cap`
/// (`FLAMMGateLib.coverReleases`): the repay all of the input's custody with the input, the input alone where
/// it is owed at two or more pairs, or has two or more live pairs, and leaves some of its debt, as the
/// swap's `_repayInput` sizes it. Where two or more of the output's legs share one reserve's cash, each
/// redeems a whole unit at least, past the face its budget counts: a unit's payout per leg comes off
/// (`FLAMMSharedLib.sharedUnits`), as the Router's releases, each at most once and each re-read at the cash
/// the earlier ones left, then pay all of the rest.
function _freed(FLAMMStore.S storage $, FLAMMSwapLib.Plan memory p, FLAMMGateLib.Book memory b, uint256 cap)
private
view
returns (uint256)
{
FLAMMGateLib.Leg memory Lx = b.legs[p.x];
bool many = Lx.partner == FLAMMStore.MANY;
uint256 left = Lx.held + p.usedNative;
if (left < Lx.owed && (many || FLAMMGateLib.split(b, p.x))) left = p.usedNative;
// the cover pair's venues in index order only where the book owes the input at two or more pairs, as the
// swap's `_repayInput` walks them; else the Router's repay order
uint256 freed = FLAMMGateLib.coverReleases($, p.y, p.coverPair, many, left, cap);
uint256 u = _units($, p.y);
return freed > u ? freed - u : 0;
}
/// @dev `FLAMMSharedLib.sharedUnits(y)`, read only where two or more live venues share a reserve's cash (the set
/// it walks): a venue holds at most one leg of a token (its supply in its debt token, its collateral in its
/// collateral token, and a pair's two tokens differ), and the units count only where a token has two legs
/// there, so with one such venue or none they are zero, at no library call.
function _units(FLAMMStore.S storage $, uint256 y) private view returns (uint256) {
uint256 m = $.sharedVenues & ~uint256($.retiredVenues | $.idleVenues);
return m == 0 || m & (m - 1) == 0 ? 0 : FLAMMSharedLib.sharedUnits(y);
}
}
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": "0x82a048459DF53186031Fd66507B260EeaC7F67df"
},
"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": "0x3Bd2B976e70C0D6ea7eb041e36ad525F66F79A69"
},
"src/core/mm/AaveV3ReadsLib.sol": {
"AaveV3ReadsLib": "0xD53F6170226A74D453e84414Ffc2FFcE5ded5983"
},
"src/core/flamm/FLAMMFlowLib.sol": {
"FLAMMFlowLib": "0xAC619698656d47651EF67a050859578e176A909D"
},
"src/core/flamm/FLAMMGateLib.sol": {
"FLAMMGateLib": "0x2291F5e012B0740468E2e2E9AeA37F7Fea64F98c"
},
"src/core/flamm/FLAMMKeptLib.sol": {
"FLAMMKeptLib": "0x7a11899232dd9F17c28bF062Ec2ee2267158517b"
},
"src/core/mm/CTokenMarketLib.sol": {
"CTokenMarketLib": "0xfD87dcDDdc9b4eCe3F2413A858B265D8Ec6dF060"
},
"src/core/flamm/FLAMMAdmitLib.sol": {
"FLAMMAdmitLib": "0x4AAcA346B9430656eB1316094cea0Df0f2e1AED6"
},
"src/core/flamm/FLAMMBlindLib.sol": {
"FLAMMBlindLib": "0x9E5010D8393EB299b6FEce8EB160b3826f7eEb41"
},
"src/core/flamm/FLAMMClaimLib.sol": {
"FLAMMClaimLib": "0xC6C94A120AC43f689BD8D8444dFf300eb9Da2aFe"
},
"src/core/flamm/FLAMMFenceLib.sol": {
"FLAMMFenceLib": "0x739D84c7662030d5Ed8C6BA7606152977D5A629f"
},
"src/core/flamm/FLAMMFloatLib.sol": {
"FLAMMFloatLib": "0xf128B7AdB5bF43814D595E3d1fC95a1f2bB16b70"
},
"src/core/flamm/FLAMMQuoteLib.sol": {
"FLAMMQuoteLib": "0xF284B0271EA829F3a15f005BB588b7Df4bEFd526"
},
"src/core/flamm/FLAMMTopUpLib.sol": {
"FLAMMTopUpLib": "0xD56127118C7619e4a13713Fb4280Cf66D560C5A5"
},
"src/core/mm/CTokenRewardsLib.sol": {
"CTokenRewardsLib": "0xcda63d7eCCF838F2732Bff676706BDE5abA4DcF7"
},
"src/core/mm/MMRouterAdminLib.sol": {
"MMRouterAdminLib": "0x8423744b3F304ED0B43043d7ad4E7d8A247aDc96"
},
"src/core/flamm/FLAMMPolicyLib.sol": {
"FLAMMPolicyLib": "0x6FE69Ed6659A547da6100225Cd0616CfDB678097"
},
"src/core/flamm/FLAMMSettleLib.sol": {
"FLAMMSettleLib": "0x1834d367BD65AF7CCf41D2025758bB69Af56e060"
},
"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": "0x5691dc81338DFdF5e4E76Fccb79e38540ff4D380"
},
"src/core/flamm/FLAMMPairQuoteLib.sol": {
"FLAMMPairQuoteLib": "0xa0c8489D5a572eAeAeEDDf48cb888c20A950c3E3"
},
"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.