Verified contractExactUNLICENSED
FLAMMClaimLib
Address0x5dc3bb30c60390ce9522b7e3ca315dd334aa7e91
- 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,480,473
- Creation transaction
- 0x6f8c24c9d0…8f2aedee1b
- Libraries
- src/core/mm/MMRouterLib.sol:MMRouterLib0x95b3…48ef2csrc/core/flamm/FLAMMLane.sol:FLAMMLane0x5d81…90de37src/core/flamm/FLAMMRoute.sol:FLAMMRoute0xb1ff…7fb0fesrc/periphery/FLAMMMaxima.sol:FLAMMMaxima0x6854…90a91bsrc/core/flamm/FLAMMLawLib.sol:FLAMMLawLib0xf55c…49cf45src/core/flamm/FLAMMOpsLib.sol:FLAMMOpsLib0x0be7…5ecc47src/core/mm/AaveV3ReadsLib.sol:AaveV3ReadsLib0xa5db…72803esrc/core/flamm/FLAMMFlowLib.sol:FLAMMFlowLib0x2795…4cfb77src/core/flamm/FLAMMGateLib.sol:FLAMMGateLib0x3a05…434368src/core/flamm/FLAMMKeptLib.sol:FLAMMKeptLib0x70ca…60bee4src/core/mm/CTokenMarketLib.sol:CTokenMarketLib0x60e4…314010src/core/flamm/FLAMMAdmitLib.sol:FLAMMAdmitLib0x0b10…1ce950src/core/flamm/FLAMMBlindLib.sol:FLAMMBlindLib0x634d…298fcdsrc/core/flamm/FLAMMClaimLib.sol:FLAMMClaimLib0x5dc3…aa7e91src/core/flamm/FLAMMFenceLib.sol:FLAMMFenceLib0x9dbb…798f1asrc/core/flamm/FLAMMFloatLib.sol:FLAMMFloatLib0x6b2b…518935src/core/flamm/FLAMMQuoteLib.sol:FLAMMQuoteLib0x99de…929869src/core/flamm/FLAMMTopUpLib.sol:FLAMMTopUpLib0xe4be…cfac2esrc/core/mm/CTokenRewardsLib.sol:CTokenRewardsLib0x93ab…262861src/core/mm/MMRouterAdminLib.sol:MMRouterAdminLib0x6abb…60ea7esrc/core/flamm/FLAMMPolicyLib.sol:FLAMMPolicyLib0x3858…f91f06src/core/flamm/FLAMMSettleLib.sol:FLAMMSettleLib0x4551…33b301src/core/flamm/FLAMMSharedLib.sol:FLAMMSharedLib0xb852…b71005src/periphery/FLAMMExitMaxima.sol:FLAMMExitMaxima0x6a6a…798659src/hooks/everlong/LeverageLib.sol:LeverageLib0x8039…16d055src/core/flamm/FLAMMMaintainLib.sol:FLAMMMaintainLib0xaa54…b56417src/core/flamm/FLAMMPairQuoteLib.sol:FLAMMPairQuoteLib0x3bc5…2055ddsrc/hooks/everlong/EverlongOpsLib.sol:EverlongOpsLib0xf6d5…9dfeef
52 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/core/flamm/FLAMMClaimLib.solmain49.63 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.93 KB
- src/core/flamm/FLAMMAdmitLib.sol38.79 KB
- src/core/flamm/FLAMMBlindLib.sol60.72 KB
- src/core/flamm/FLAMMClaimVault.sol1.65 KB
- src/core/flamm/FLAMMFenceLib.sol72.81 KB
- src/core/flamm/FLAMMFloatLib.sol62.2 KB
- src/core/flamm/FLAMMFlowLib.sol54.32 KB
- src/core/flamm/FLAMMGateLib.sol93.55 KB
- src/core/flamm/FLAMMKeptLib.sol45.86 KB
- src/core/flamm/FLAMMLane.sol70.59 KB
- src/core/flamm/FLAMMLawLib.sol61.53 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.sol43.2 KB
- src/core/flamm/FLAMMSharedLib.sol13.11 KB
- src/core/flamm/FLAMMStore.sol137.17 KB
- src/core/flamm/FLAMMSwapLib.sol55.86 KB
- src/core/flamm/FLAMMTopUpLib.sol52.56 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.sol3.04 KB
- src/interfaces/core/mm/IFinancingAccount.sol47.32 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/FLAMMClaimLib.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 {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
FLAMMStore, InvalidConfig, CustodyMismatch, ExitWorsensLedger, NotFenced, NothingToClaim, VenueRefusedLeg,
ClaimTransferred, ClaimSettled, FenceUnwound, ProceedsParked, ParkedDrawn, WAD, InvalidAmount
} from "src/core/flamm/FLAMMStore.sol";
import {FLAMMGateLib} from "src/core/flamm/FLAMMGateLib.sol";
import {FLAMMLawLib} from "src/core/flamm/FLAMMLawLib.sol";
import {FLAMMFenceLib} from "src/core/flamm/FLAMMFenceLib.sol";
import {FLAMMKeptLib} from "src/core/flamm/FLAMMKeptLib.sol";
import {FLAMMClaimVault} from "src/core/flamm/FLAMMClaimVault.sol";
import {FLAMMQuoteLib} from "src/core/flamm/FLAMMQuoteLib.sol";
import {IMMRouter} from "src/interfaces/core/mm/IMMRouter.sol";
import {MMRouterLib} from "src/core/mm/MMRouterLib.sol";
import {ReentrancyGuardLib} from "src/libraries/ReentrancyGuardLib.sol";
/// @notice The fences' claims (`FLAMMFenceLib`): an exit leaves its share of each fence that holds a pool part as its
/// receiver's claim (`FLAMMFenceLib.recordClaims`), a part of the fence's position that settles in kind
/// against it whenever its lender takes the legs, by its holder or by anyone for it.
/// @dev - A holder moves a claim; a token claim moves with the claims' share to its key's vault at the token's lift,
/// and what a venue claim's holder cannot take at a settlement by anyone else waits in its key's vault for the
/// holder.
/// - The pool's own part leaves a venue fence by its unwind into custody.
/// - The pool's fallback hands this library the calls its dispatch table routes here (`FLAMM.route`), so its
/// entries take the pool's storage from its own namespace, never as an argument; each checks its own caller and
/// holds the lock.
/// - Frames: the venues at a frame's account (`FLAMMFenceLib.mates`) share one position at the lender, which holds
/// every posted part behind every part's debt, and their fence took them together, so their records hold the
/// same fractions and each exit's claim the same part of each ({_records}). A claim there settles and moves with
/// the holder's claims on the frame's other records, the same part of each, and the pool's unwind takes the same
/// part of every record, each in one call that repays every debt share before it releases any collateral
/// ({_frameLegs}): no settlement, move or unwind takes one venue's slice of the frame without its slices of the
/// others, so none moves a venue's deficit between the pool and the claims, or between holders.
library FLAMMClaimLib {
/// @dev How a settlement's shares are cut ({shares}): a claim's (plain or senior), or a junior part's, its debt
/// share repaid from custody, which its exit paid, while its record holds it
/// (`FLAMMStore.JUNIOR_DEBT_SHIFT`). {JUNIOR_PAYS} is cut as a junior part, but its debt share is pulled
/// from its caller, as a claim's: the void's (`FLAMMFenceLib.VOID`, a given-up kept share, whose debt share
/// is the lender's), and every junior part once the curator ended custody's hold (`FLAMMFenceLib.abandonFence`).
uint256 private constant CUT = 1;
uint256 private constant JUNIOR = 2;
uint256 private constant JUNIOR_PAYS = 3;
// ------------------------------------------------------------------ claims
/// @notice Settles `part` of `holder`'s claim on fence `key`, anyone's: a caller other than the holder pays the
/// debt share and the proceeds go to the holder, and the holder names any `to` but zero or the pool itself
/// (`InvalidAmount`, as the swap refuses its `to`), so no proceeds land past custody for the next caller.
/// @dev - At a venue ({_legs}) the claim takes its shares of the position as the venue holds it now, which bear the
/// interest and any liquidation since the exit: the supply share is withdrawn first, the payer brings what
/// it does not cover of the debt share, and the collateral share is released by the proportional rule, at
/// no price.
/// - A leg the venue takes short refuses (`VenueRefusedLeg`), as a repay held at a minimum position does: the
/// payer settles another part or waits. A repay short of the debt share passes where it clears the debt, as
/// a kind's debt read can stand over what the repay finds ({_repayLeg}), and what the payer brought past it
/// stays in the pool.
/// - A caller other than the holder pays the venue's proceeds to the holder by a try each: what the holder
/// cannot take (a token that blocks it) waits in the key's vault, the holder's to draw to any address but
/// zero or the pool ({drawParked}), and the claim leaves the fence all the same, so no holder a token blocks
/// holds a venue's lift.
/// - At a token's custody the claim takes its share of the balance outside custody, less the protocol's fee
/// while the custody is out; after the token's lift, its part of what the key's vault holds (`vaulted`), the
/// last part all of it.
/// - A caller other than the holder settles a token claim whole, and is refused (`NothingToClaim`) one that
/// pays nothing or less than the claim's part at the key's floor (`FLAMMStore.S.strangerFloor`: the custody
/// the fence moved out, net of the protocol's fee, per `ONE` of part; from the lift, the vault's pot per
/// `ONE` of the claims then open): under it only the holder settles, so no claim is closed at a balance an
/// issuer's burn or seizure depressed, and it keeps its share of a later restore. After a burn the issuer
/// never restores, only the holders settle until the lift, which moves what the open claims hold to the
/// key's vault and resets their floor to that pot; on a token that admits listed holders only, whose issuer
/// never lists the vault, the lift waits on the holders.
/// - Where the venue's kind reports a haircut, the shares are cut by it ({shares}): a settlement by anyone
/// realizes the venue's reported loss for the holder at that moment, the value NAV and the holder's own
/// settlement read then; at a venue whose cut is a redemption fee the fee goes to the lender's treasury.
/// The cut is the larger of the lender's ratio and the venue's loss mark, which the settlement reads first
/// (`FLAMMKeptLib.recordLossMarks`: a ratio of zero clears the mark at once). Where that cut, on either
/// side, is `FLAMMStore.LOSS_MOVE_FLOOR` or more (`FLAMMKeptLib.lossAtFloor`) a caller other than the
/// holder is refused (`NothingToClaim`): the lender's ratio is a spot figure that a withdrawal of supply
/// parked there raises for one call, and a mark may stand over it after a cover, so no co-holder of the
/// fence closes another's claim at a cut of its choosing and keeps what the cut leaves in the position. The
/// holder settles at the cut in force whenever it chooses; under the floor anyone does, at a cut under a
/// hundredth of a percent of the shares. At a venue whose cut is a redemption fee only the holder settles
/// while any fee stands (`FLAMMStore.LOSS_LEG_FEES`): the fee is paid for good, so the holder chooses
/// whether to pay it or wait for it to end, and the venue's lift waits on the holders meanwhile. The void's
/// claim (`FLAMMFenceLib.VOID`, a part an abandon gave up) has no holder to choose: anyone settles it under
/// a fee, which comes out of what goes to the void, so no fee holds the venue's lift on it.
/// - A venue claim on a record that has closed (the venue retired, its slot reused) settles for nothing, by
/// anyone, so a worthless claim never holds a venue's lift.
/// - At a frame's account ({_records}) the claim settles with the holder's claims on the frame's other
/// records, `part` of each (`NothingToClaim` where one holds less), in one call ({_frameLegs}), and each
/// record's settlement is announced.
function settleClaim(uint32 key, address holder, uint256 part, address to) public {
FLAMMStore.S storage $ = FLAMMStore.s();
ReentrancyGuardLib._guard();
bool stranger = msg.sender != holder;
if (stranger) to = holder;
else if (to == address(0) || to == address(this)) revert InvalidAmount();
uint256 index = key & 63;
(uint256 word, uint256 have, uint256 cut) = _have($, key, index, holder);
// a cleared kept share's claim draws its pots' share, whole, the holder's own call
// (`FLAMMKeptLib.drawKept`), its senior and junior parts apart as stored
if (index < 32 && $.vaulted[uint256(key) | 2 << 32] != 0) {
word = $.claims[key][holder];
if (stranger || word == 0) revert NothingToClaim();
delete $.claims[key][holder];
FLAMMKeptLib.drawKept(key, word, holder, to);
ReentrancyGuardLib._unlockGuard();
return;
}
// a stranger settles a token claim whole: no grinding of a holder by parts
if (part == 0 || part > have || stranger && index >= 32 && part != have) revert NothingToClaim();
FLAMMStore.Fence storage f = $.fences[index];
if (index < 32 && f.key == key) {
uint256 m = _records($, index, $.fenceWord);
if ($.features & FLAMMStore.LOSS_MARKS != 0) {
// the claim's shares are cut by its venues' loss marks ({shares}): read first, so a lender's ratio that
// reads zero clears its mark; and where the cut in force is the rule's floor or more only the holder
// settles, so no one else closes a claim at a ratio raised for the call or left by a cover; any fee
// too, but on the void's claim, which no holder could settle
FLAMMKeptLib.recordLossMarks();
uint256 leg = holder == FLAMMFenceLib.VOID
? FLAMMStore.LOSS_LEG_ANY
: FLAMMStore.LOSS_LEG_ANY | FLAMMStore.LOSS_LEG_FEES;
if (stranger && FLAMMKeptLib.lossAtFloor(m, leg)) revert NothingToClaim();
}
if (m & (m - 1) != 0) {
_settleFrame($, m, holder, part, to, stranger);
FLAMMStore.reconcile($);
ReentrancyGuardLib._unlockGuard();
return;
}
}
$.claims[key][holder] = word - (cut >= JUNIOR ? part << 128 : part);
uint256 c;
uint256 s;
uint256 d;
if (f.key == key) {
(uint256 live, uint256 claimed) = (f.live, f.claimed);
f.claimed = uint128(claimed - part);
if (index < 32) {
(c, s, d) = _legs($, uint16(index), part, live + claimed, false, to, stranger ? key : 0, 0, cut);
// a senior's part leaves the seniors' after its shares are read at their ratios
if (cut < JUNIOR && f.fee & FLAMMStore.SENIORS != 0) f.fee -= part;
if (cut == JUNIOR) _juniorsDone($, f, key, index);
FLAMMStore.reconcile($);
} else {
c = _tokenShare($, index - 32, f.fee, part, live + claimed, to);
}
} else if (index >= 32) {
// the token's lift moved the claims' share to the key's vault: this claim's part of what it holds
uint256 v = $.vaulted[key];
if (v != 0) {
$.vaulted[key] = v - part;
address vault = $.vaults[key];
address token = FLAMMStore.tokenOf($, index - 32);
c = Math.mulDiv(IERC20(token).balanceOf(vault), part, v);
if (c != 0) FLAMMClaimVault(vault).pay(token, to, c);
}
}
// a stranger's settlement of a token claim pays something, and the claim's part at the key's floor at least:
// under it the holder alone settles, and the claim keeps its share of a later restore
if (stranger && index >= 32 && (c == 0 || c < Math.mulDiv($.strangerFloor[key], part, FLAMMFenceLib.ONE))) {
revert NothingToClaim();
}
emit ClaimSettled(key, holder, to, part, c, s, d);
ReentrancyGuardLib._unlockGuard();
}
/// @dev `holder`'s claim word on fence `key` at index `index`, the part it settles now and how its shares are cut
/// ({shares}): the low part (a plain or senior claim) cut by the venue's haircut; a junior part (past bit
/// 128, an exit's slice of a kept share, `FLAMMFenceLib.recordClaims`) waits while the kept share stands,
/// for its clear, and once the record holds none (an abandon gave it up) settles first, whole, as {JUNIOR}.
function _have(FLAMMStore.S storage $, uint32 key, uint256 index, address holder)
private
view
returns (uint256 word, uint256 have, uint256 cut)
{
word = $.claims[key][holder];
(have, cut) = (uint128(word), CUT);
if (word >> 128 != 0 && ($.fenceWord >> (FLAMMStore.KEPT_SHIFT + index)) & 1 == 0) {
bool held = ($.fenceWord >> (FLAMMStore.JUNIOR_DEBT_SHIFT + index)) & 1 != 0;
(have, cut) = (word >> 128, held && holder != FLAMMFenceLib.VOID ? JUNIOR : JUNIOR_PAYS);
}
}
/// @dev Record `j` (key `key`) of a given-up kept share once a junior part settled: where no junior claim stands
/// (all its claims the senior claims' and the void's), its junior debt bit clears
/// (`FLAMMStore.JUNIOR_DEBT_SHIFT`), so NAV reads its venue no more.
function _juniorsDone(FLAMMStore.S storage $, FLAMMStore.Fence storage g, uint32 key, uint256 j) private {
uint256 o = $.claims[key][FLAMMFenceLib.VOID];
if (g.claimed == (g.fee & FLAMMStore.SENIORS) + uint128(o) + (o >> 128)) {
$.fenceWord &= ~(uint256(1) << (FLAMMStore.JUNIOR_DEBT_SHIFT + j));
}
}
/// @notice Moves the caller's whole claim on fence `key` to `to`, neither zero, the pool itself nor the void
/// (`NothingToClaim`): a claim the pool held would settle, by anyone, past custody. A claim moves whole:
/// none splits into pieces, each of which the lift would wait on for a settlement of its own. At a frame's
/// account ({_records}) the caller's claims on the frame's other records move with it, so no holder keeps
/// one venue's slice of the frame and hands on another's.
function transferClaim(uint32 key, address to) public {
FLAMMStore.S storage $ = FLAMMStore.s();
ReentrancyGuardLib._guard();
if (_noClaimTarget(to) || _moveClaim($, key, to) == 0) revert NothingToClaim();
uint256 index = key & 63;
uint256 m =
index < 32 && $.fences[index].key == key ? _records($, index, $.fenceWord) & ~(uint256(1) << index) : 0;
for (; m != 0; m &= m - 1) {
_moveClaim($, $.fences[FLAMMFenceLib.low(m)].key, to);
}
ReentrancyGuardLib._unlockGuard();
}
/// @notice Moves every claim the caller holds on an open record to `to`, neither zero, the pool itself nor
/// the void (`NothingToClaim`, as {transferClaim}), in one call: a contract that exits for its user (as the
/// receiver, as the pool records every claim to an exit's receiver) passes them on.
function transferClaims(address to) public {
FLAMMStore.S storage $ = FLAMMStore.s();
ReentrancyGuardLib._guard();
if (_noClaimTarget(to)) revert NothingToClaim();
uint256 w = $.fenceWord;
uint256 open = (w & FLAMMStore.FENCE_VENUES) | (((w >> FLAMMStore.FENCED_TOKENS_SHIFT) & 0xffff) << 32);
for (; open != 0; open &= open - 1) {
_moveClaim($, $.fences[FLAMMFenceLib.low(open)].key, to);
}
ReentrancyGuardLib._unlockGuard();
}
/// @dev Whether `to` may hold no claim: zero, the pool itself, or the void, which holds the parts given up and
/// whose own part {FLAMMKeptLib.keptLegs} reads as the pool's.
function _noClaimTarget(address to) private view returns (bool) {
return to == address(0) || to == address(this) || to == FLAMMFenceLib.VOID;
}
/// @dev Moves the caller's whole claim on fence `key` to `to`: its part, zero where it holds none, which moves
/// nothing.
function _moveClaim(FLAMMStore.S storage $, uint32 key, address to) private returns (uint256 part) {
part = $.claims[key][msg.sender];
if (part == 0) return 0;
delete $.claims[key][msg.sender];
$.claims[key][to] += part;
emit ClaimTransferred(key, msg.sender, to, part);
}
/// @notice Unwinds `part` of the pool's own part of venue fence `key` into custody, the maintainer's (the keeper,
/// the curator or the controller hook), and a kept share's clear anyone's: its collateral and supply shares
/// come in, and its debt share is repaid from the custody the pool owns of the debt token, after the supply
/// share (`CustodyMismatch` where that falls short: no bridge through another venue). Not at a venue a
/// token of whose pair has its custody out (`InvalidConfig`).
/// @dev - Gated as an entry's legs are: at the anchor's prices, where they check and every token's price reads, the
/// credit law, else no owed token's ratio to its backing rising ({FLAMMQuoteLib.entryGate}); on a feed that
/// does not read, that price-free monotone law alone, so the unwind never waits on a price.
/// - The monotone law holds a repay of a debt the law does not see, from custody it counts, to no rise of
/// another pair's net debt in that token; the credit law takes the venue's slice at its price, so an unwind
/// that leaves the book within its pins lands where custody of the debt token beside a live venue's debt (a
/// repay held at a minimum) lowers the live book's ratio.
/// - A `part` past the pool's part unwinds all of it (`type(uint256).max`: all), so an exit landing first in
/// the block, which takes a slice of the part, cannot fail the unwind that returns the rest.
/// - Once the pool's whole part is unwound the slot's idle bit and the fence's unwritten bit clear: exits
/// record nothing there, and NAV, the entries and the rises reopen where no other fence holds a part.
/// After a whole unwind, or a kept share's clear, at a reported loss the cut stays at the fenced venue,
/// outside NAV, with the entries, mints and rises open, until the lift returns it.
/// - At a venue that keeps a loss mark ({shares}; not one whose cut is a redemption fee), while a claim is
/// open and a loss ratio reads above zero at the records' venues (`FLAMMKeptLib.atFace`), the unwind takes
/// the pool's whole part less its share at the largest such ratio, rounded up, at face, its shares uncut,
/// and leaves that share in the pool's part: the claims keep exactly their part of the position, and the
/// pool keeps what the cut would have left in it, the cut's size and no more, as NAV cut the venue by it
/// already. That share stands as the kept share (`FLAMMStore.KEPT_SHIFT`) behind the claims open then,
/// which are senior (`FLAMMKeptLib.seniorWord`); the entries, rises and fee mints wait until its clear,
/// the lift or its abandon. With no claim open the unwind takes the shares cut and leaves the cut in the
/// position, which the lift returns to the pool.
/// - A kept share's unwind, its clear, is anyone's (`FLAMMKeptLib.clearKept`): a stranger's lands at once
/// where the lender reads no loss, within `FLAMMStore.KEPT_CLEAR_WAIT` of the unwind that left the share
/// only while the loss ratios stand within `FLAMMStore.LOSS_MOVE_FLOOR` of what they read after it
/// (`FLAMMKeptLib.markKept`), and past it at the lender's ratios held within that floor of those, so no
/// one moves the reading for the call by more than the floor; the curator's is unguarded.
/// - The clear repays the record's whole debt from custody, the senior claims' share included, as the junior
/// claims' exits paid theirs into it (`CustodyMismatch` where custody holds less of the debt token): a buy
/// into the pool, a flash loan's included, refills it in the clear's transaction.
/// - The maintainer sends the unwind privately, or bundles it behind a buy that refills the debt token's
/// custody: a sale that pays that token out first spends the custody the unwind repays from. Its gas turns
/// on the lender and the book, so the maintainer sizes it by `eth_estimateGas` plus 20 %, never a fixed
/// limit.
/// - At a frame's account ({_records}) it unwinds `part` of the pool's part of every record of the frame, in
/// one call ({_frameLegs}), under one gate, each record's unwind announced: their fence took them together
/// and the lender holds every posted part behind every part's debt.
function unwindFence(uint32 key, uint256 part) public {
FLAMMStore.S storage $ = FLAMMStore.s();
uint256 id = key & 63;
uint256 w = $.fenceWord;
// a kept share's unwind is anyone's: no keeper's absence holds the entries it closes
bool kept = id < 32 && (w >> (FLAMMStore.KEPT_SHIFT + id)) & 1 != 0;
if (!kept) FLAMMStore.requireKeeperOrCurator($, true);
ReentrancyGuardLib._guard();
FLAMMStore.Fence storage f = $.fences[id];
if (id >= 32 || f.key != key) revert NotFenced();
(uint256 live, uint256 claimed) = (f.live, f.claimed);
if (part > live) part = live;
if (part == 0) revert NothingToClaim();
// the part's shares are cut by its venues' loss marks ({shares}): read first, as a claim's settlement reads them
bool marks = $.features & FLAMMStore.LOSS_MARKS != 0;
if (marks) FLAMMKeptLib.recordLossMarks();
FLAMMGateLib.Book memory b = FLAMMGateLib.priced($);
(uint256[] memory u0, uint256[] memory g0, uint256[] memory d0, uint256[] memory o0) = FLAMMLawLib.anchorOf(b);
uint256 m = _records($, id, w);
// with a claim open at a reported loss the pool takes its whole part at face less the cut's share, which stays
// its own, the kept share, behind the claims open now
uint256 take = part;
if (claimed != 0 && marks && !kept) {
take = FLAMMKeptLib.atFace(m, live);
if (take != live) part = live;
else take = part;
}
uint256 c;
uint256 s;
uint256 d;
if (kept) {
// every claim there and the pool's own part leave together, in kind (`FLAMMKeptLib.clearKept`), the
// claims' shares into their keys' vaults
// and no token of a record's pair may have its custody out, as the pool's own unwind refuses ({_legs}): the
// clear would bring the token back into custody while its fence holds it out
uint256 out = w >> FLAMMStore.OUT_TOKENS_SHIFT;
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
IMMRouter.VenueView memory v = $.router.venue(address(this), uint16(j));
if (((out >> v.coll) | (out >> v.debt)) & 1 != 0) revert InvalidConfig();
FLAMMStore.Fence storage g = $.fences[j];
if (g.claimed != 0) _vaultOf($, g.key);
}
FLAMMKeptLib.clearKept(m, b.usd, msg.sender != $.core.owner());
} else if (m & (m - 1) != 0) {
_unwindFrame($, m, part, take);
} else {
uint256 sw = take != part ? FLAMMKeptLib.seniorWord(id, claimed) : 0;
if (take != 0) {
uint256 cut = take == part ? CUT : 0;
(c, s, d) = _legs($, uint16(id), take, live + claimed, true, address(this), 0, w, cut);
}
f.live = uint128(live - take);
if (live == part) _whole($, f, id, sw);
}
// a kept share left: its loss reading after the legs, which a stranger's clear stays within
if (take != part) FLAMMKeptLib.markKept(m);
// the credit law first, at the anchor's prices, where every price it may charge at reads; else the price-free
// monotone law alone. No repay leaves its share owing (each leg refuses one), no forfeit and no dust: its legs
// move exact amounts
if (FLAMMGateLib.checkedAt(b) && _pricesRead(b)) {
FLAMMQuoteLib.entryGate($, b.usd, u0, g0, d0, o0, 0, ExitWorsensLedger.selector);
} else {
FLAMMQuoteLib.gateExitAtRead($, u0, g0, d0, o0, new uint256[](0), 0, ExitWorsensLedger.selector, 0);
}
FLAMMStore.reconcile($);
if (!kept && m & (m - 1) == 0) emit FenceUnwound(key, take, live - take, c, s, d);
ReentrancyGuardLib._unlockGuard();
}
/// @dev Record `j`'s whole part is unwound: its slot leaves the idle set, and its kept and unwritten bits clear;
/// but where the unwind left a kept share (`sw`, its senior word, not zero) its kept bit is set and the word
/// written, its unwritten bit standing as it was, so a write-off and an abandon reach it as a pool part.
function _whole(FLAMMStore.S storage $, FLAMMStore.Fence storage g, uint256 j, uint256 sw) private {
$.idleVenues &= ~(uint32(1) << j);
uint256 k = uint256(1) << (FLAMMStore.KEPT_SHIFT + j);
uint256 w = $.fenceWord & ~k;
$.fenceWord = sw != 0 ? w | k : w & ~(uint256(1) << (FLAMMStore.UNWRITTEN_SHIFT + j));
if (sw != 0) g.fee = sw;
}
/// @dev Whether every token's USD price reads at `b`: the credit law charges a debt at its pair's price, which a
/// token holding no leg (a live venue's debt left with no collateral) still enters, and
/// `FLAMMGateLib.checkedAt` reads only the tokens that hold one.
function _pricesRead(FLAMMGateLib.Book memory b) private pure returns (bool) {
for (uint256 t; t < b.usd.length; ++t) {
if (b.usd[t] == 0) return false;
}
return true;
}
/// @dev Moves `part` of `whole` of venue `id`'s position ({shares}): the supply share withdrawn first, then the
/// debt share repaid, its rest past the supply share from the pool's own custody (`own`) or pulled from the
/// caller, then the collateral share released by the proportional rule at the pool's zero price vector. A leg
/// the venue takes short refuses (`VenueRefusedLeg`), but a repay short of its share passes where it clears
/// the debt ({_repayLeg}): `d` is then what the venue took, custody moves by that, and what a caller brought
/// past it stays in the pool. With `own` the shares stay in custody; else they go to `to`, by a try where
/// `park` names the fence's key (a settlement for the holder `to`, {_payOut}). With `own`, `w` is the fence
/// word. `cut` as {shares}'; the pool's unwind at face takes them uncut. A {JUNIOR} part's debt share is
/// repaid from the pool's own custody, which its exit paid, and its supply share goes to `to` whole.
function _legs(
FLAMMStore.S storage $,
uint16 id,
uint256 part,
uint256 whole,
bool own,
address to,
uint32 park,
uint256 w,
uint256 cut
) private returns (uint256 c, uint256 s, uint256 d) {
IMMRouter r = $.router;
IMMRouter.VenueView memory v;
(c, s, d, v) = shares($, id, part, whole, cut);
if (own) {
uint256 out = w >> FLAMMStore.OUT_TOKENS_SHIFT;
if (((out >> v.coll) | (out >> v.debt)) & 1 != 0) revert InvalidConfig();
}
address dt = FLAMMStore.tokenOf($, v.debt);
uint256 got;
if (s != 0) {
got = r.withdrawSupplied(id, s, address(this));
if (got < s) revert VenueRefusedLeg();
}
bool paid = own || cut == JUNIOR;
if (d != 0) {
if (paid) {
if (FLAMMStore.ownHeld($, v.debt) + (own ? got : 0) < d) revert CustodyMismatch();
} else if (d > got) {
FLAMMStore.pull(dt, d - got);
}
d = _repayLeg($, r, id, dt, d);
}
if (c != 0 && r.withdrawCollateral(id, c, new uint256[]($.tokenCount), true) != c) revert VenueRefusedLeg();
if (own) {
if (got != d) FLAMMStore.setHeld($, v.debt, FLAMMStore.heldOf($, v.debt) + got - d);
if (c != 0) FLAMMStore.setHeld($, v.coll, FLAMMStore.heldOf($, v.coll) + c);
} else {
if (c != 0) _payOut($, park, FLAMMStore.tokenOf($, v.coll), to, c);
if (paid) {
if (d != 0) FLAMMStore.setHeld($, v.debt, FLAMMStore.heldOf($, v.debt) - d);
if (got != 0) _payOut($, park, dt, to, got);
} else if (got > d) {
_payOut($, park, dt, to, got - d);
}
}
}
/// @dev The records a claim, or the pool's part, on venue fence `index` moves with: `index`'s and, at a frame's
/// account (`FLAMMFenceLib.matesOf`), every other open venue record there holding a share of a position
/// (`live` or `claimed` not zero). Their fence opened them together, and each exit, settlement, move and
/// unwind since took the same part of each, so they hold the same fractions. The account is read only where
/// another venue's fence is open in the fence word `w`: `index`'s alone otherwise, as at a pair's own account.
function _records(FLAMMStore.S storage $, uint256 index, uint256 w) private view returns (uint256 m) {
m = uint256(1) << index;
uint256 open = w & FLAMMStore.FENCE_VENUES & ~m;
if (open == 0) return m;
for (uint256 k = FLAMMFenceLib.matesOf($, uint16(index)) & open; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
FLAMMStore.Fence storage g = $.fences[j];
if (g.live | g.claimed != 0) m |= uint256(1) << j;
}
}
/// @dev {settleClaim} at a frame's records `m` ({_records}): `part` of `holder`'s claim on each (`NothingToClaim`
/// where one holds less), the legs in one call ({_frameLegs}).
function _settleFrame(FLAMMStore.S storage $, uint256 m, address holder, uint256 part, address to, bool stranger)
private
{
uint256[32] memory whole;
uint256 cut;
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
FLAMMStore.Fence storage g = $.fences[j];
uint32 key = g.key;
uint256 word;
uint256 have;
(word, have, cut) = _have($, key, j, holder);
if (part > have) revert NothingToClaim();
$.claims[key][holder] = word - (cut >= JUNIOR ? part << 128 : part);
(uint256 live, uint256 claimed) = (g.live, g.claimed);
whole[j] = live + claimed;
g.claimed = uint128(claimed - part);
}
_frameLegs($, m, part, whole, false, holder, to, stranger, cut);
}
/// @dev {unwindFence} at a frame's records `m` ({_records}): `part` of the pool's part of each (`NothingToClaim`
/// where one holds less), `take` of it moved, the legs in one call ({_frameLegs}), uncut where `take` is short
/// of `part` (the unwind at face, `FLAMMKeptLib.atFace`); a record whose whole part it unwinds leaves the
/// idle set and the unwritten bits, as one venue's does.
function _unwindFrame(FLAMMStore.S storage $, uint256 m, uint256 part, uint256 take) private {
uint256[32] memory whole;
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
FLAMMStore.Fence storage g = $.fences[j];
(uint256 live, uint256 claimed) = (g.live, g.claimed);
if (part > live) revert NothingToClaim();
whole[j] = live + claimed;
g.live = uint128(live - take);
if (live == part) _whole($, g, j, take != part ? FLAMMKeptLib.seniorWord(j, claimed) : 0);
}
if (take != 0) _frameLegs($, m, take, whole, true, address(0), address(this), false, take == part ? CUT : 0);
}
/// @dev {_legs} over a frame's records `m` (two or more, {_records}): `part` of each one's `whole` of its venue's
/// position ({shares}), every share read before any leg, the legs in passes, so the lender, which holds every
/// posted part behind every part's debt, takes each while the frame is no less healthy than it was (neither
/// frame kind counts a supply as collateral: Aave keeps its flag off, Venus lends from a credit sub-account
/// outside every market): every supply share withdrawn, then every debt share repaid (its rest past its own
/// venue's supply share from the pool's own custody (`own`) or pulled from the caller), then every collateral
/// share released by the proportional rule at the pool's zero price vector. A leg the venue takes short
/// refuses (`VenueRefusedLeg`), but a repay short of its share passes where it clears the venue's debt
/// ({_repayLeg}): a part of a shared loan market reads rounded up, so after a mate's whole repay the next one
/// finds less than was read before any leg. Its share is then what the venue took, custody moves by that, and
/// what a caller brought past it stays in the pool. With `own` the shares stay in custody and each record's
/// unwind is announced; else they go to `to`, by a try into each record's key's vault where `park` (a
/// settlement for the holder by someone else, {_payOut}), and each record's settlement for `holder` is
/// announced. Not at a venue a token of whose pair has its custody out, with `own` (`InvalidConfig`). `cut` as
/// {_legs}'.
function _frameLegs(
FLAMMStore.S storage $,
uint256 m,
uint256 part,
uint256[32] memory whole,
bool own,
address holder,
address to,
bool park,
uint256 cut
) private {
IMMRouter r = $.router;
uint256[32] memory c;
uint256[32] memory s;
uint256[32] memory d;
uint256[32] memory got;
// each venue's collateral token, and its debt token past the low byte
uint256[32] memory tk;
uint256 out = $.fenceWord >> FLAMMStore.OUT_TOKENS_SHIFT;
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
IMMRouter.VenueView memory v;
(c[j], s[j], d[j], v) = shares($, uint16(j), part, whole[j], cut);
if (own && ((out >> v.coll) | (out >> v.debt)) & 1 != 0) revert InvalidConfig();
tk[j] = uint256(v.coll) | uint256(v.debt) << 8;
}
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
if (s[j] == 0) continue;
got[j] = r.withdrawSupplied(uint16(j), s[j], address(this));
if (got[j] < s[j]) revert VenueRefusedLeg();
if (own) FLAMMStore.setHeld($, tk[j] >> 8, FLAMMStore.heldOf($, tk[j] >> 8) + got[j]);
}
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
uint256 dj = d[j];
if (dj == 0) continue;
uint256 t = tk[j] >> 8;
address dt = FLAMMStore.tokenOf($, t);
bool fromCustody = own || cut == JUNIOR;
if (fromCustody) {
if (FLAMMStore.ownHeld($, t) < dj) revert CustodyMismatch();
FLAMMStore.setHeld($, t, FLAMMStore.heldOf($, t) - dj);
} else if (dj > got[j]) {
FLAMMStore.pull(dt, dj - got[j]);
}
uint256 paid = _repayLeg($, r, uint16(j), dt, dj);
if (paid != dj) {
d[j] = paid;
if (fromCustody) FLAMMStore.setHeld($, t, FLAMMStore.heldOf($, t) + dj - paid);
}
}
uint256[] memory usd = new uint256[]($.tokenCount);
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
uint256 cj = c[j];
if (cj == 0) continue;
if (r.withdrawCollateral(uint16(j), cj, usd, true) != cj) revert VenueRefusedLeg();
if (own) FLAMMStore.setHeld($, tk[j] & 0xff, FLAMMStore.heldOf($, tk[j] & 0xff) + cj);
}
for (uint256 k = m; k != 0; k &= k - 1) {
uint256 j = FLAMMFenceLib.low(k);
FLAMMStore.Fence storage g = $.fences[j];
if (own) {
emit FenceUnwound(g.key, part, g.live, c[j], s[j], d[j]);
continue;
}
// a senior's part leaves the seniors' after its shares are read at their ratios
if (cut < JUNIOR && g.fee & FLAMMStore.SENIORS != 0) g.fee -= part;
if (cut == JUNIOR) _juniorsDone($, g, g.key, j);
uint32 key = park ? g.key : 0;
if (c[j] != 0) _payOut($, key, FLAMMStore.tokenOf($, tk[j] & 0xff), to, c[j]);
// a junior part's debt share was the pool's own: its supply share goes whole
uint256 back = cut == JUNIOR ? got[j] : got[j] > d[j] ? got[j] - d[j] : 0;
if (back != 0) _payOut($, key, FLAMMStore.tokenOf($, tk[j] >> 8), to, back);
emit ClaimSettled(g.key, holder, to, part, c[j], s[j], d[j]);
}
}
/// @dev Repays `d` of `dt` at venue `id` and returns what it took: short of `d` only where that cleared the venue's
/// debt, as a kind's debt read can stand over what the repay finds: a frame venue's part of a shared loan
/// market, rounded up so the parts sum past the debt, once a mate's repay took its own
/// (`PooledAccount._owedPart`), and `CTokenAccount`'s upper bound, once someone else's repay or a liquidation
/// rewrote the borrow record, until its market's next accrual, which the pool cannot run first: the Router
/// accrues no fenced venue. Else `VenueRefusedLeg`: a repay held at a minimum position, or capped at a
/// market's total borrows, which leaves a sole borrower's remainder owing and the collateral behind it posted
/// until another account borrows there (`CTokenAccount`'s notes). What the Router did not pull leaves no grant
/// behind.
function _repayLeg(FLAMMStore.S storage $, IMMRouter r, uint16 id, address dt, uint256 d)
private
returns (uint256 paid)
{
FLAMMStore.grant($, dt, d);
paid = r.repay(id, d);
if (paid == d) return d;
(,,,, uint256 left) = r.venuePosition(address(this), id);
if (left != 0) revert VenueRefusedLeg();
FLAMMStore.revoke($, dt);
}
/// @dev Pays `amount` of `token` to `to`. Where `park` names a fence's key (a settlement for the holder `to` by
/// someone else), by a try: what `to` cannot take moves to the key's vault (`FLAMMClaimVault`, deployed at
/// `FLAMMFenceLib.claimVault(park)` by the first such payout of the key), recorded as `to`'s to draw
/// ({drawParked}). A transfer to the vault that refuses reverts the settlement.
function _payOut(FLAMMStore.S storage $, uint32 park, address token, address to, uint256 amount) private {
if (park == 0) {
FLAMMStore.safeTransfer(token, to, amount);
return;
}
if (FLAMMStore.tryTransfer(token, to, amount)) return;
$.parked[park][to][token] += amount;
FLAMMStore.safeTransfer(token, _vaultOf($, park), amount);
emit ProceedsParked(park, to, token, amount);
}
/// @dev Fence `key`'s vault (`FLAMMClaimVault`), deployed at `FLAMMFenceLib.claimVault(key)` by its first use.
function _vaultOf(FLAMMStore.S storage $, uint32 key) private returns (address vault) {
vault = $.vaults[key];
if (vault == address(0)) {
vault = address(new FLAMMClaimVault{salt: bytes32(uint256(key))}());
$.vaults[key] = vault;
}
}
/// @notice Pays the caller's proceeds in `token` that a settlement by someone else parked in fence `key`'s vault
/// (the caller could not take them then), all of them, to `to`: the holder's draw, to any address but zero
/// or the pool itself (`InvalidAmount`, as {settleClaim}). `NothingToClaim` where none is parked.
function drawParked(uint32 key, address token, address to) public {
FLAMMStore.S storage $ = FLAMMStore.s();
ReentrancyGuardLib._guard();
if (to == address(0) || to == address(this)) revert InvalidAmount();
uint256 amount = $.parked[key][msg.sender][token];
if (amount == 0) revert NothingToClaim();
delete $.parked[key][msg.sender][token];
FLAMMClaimVault($.vaults[key]).pay(token, to, amount);
emit ParkedDrawn(key, msg.sender, token, to, amount);
ReentrancyGuardLib._unlockGuard();
}
/// @notice `part` of `whole` of venue `id`'s position as the venue holds it now: the recognized collateral and
/// supply shares, rounded down and cut by the venue's haircut where its kind reports one, each ratio the
/// larger of the lender's now and the venue's loss mark (`FLAMMKeptLib.largerLoss`; the cut stays in
/// the position, for everyone left in the fence), and the debt share, rounded up and the lenders' rounding
/// more (`MMRouterLib.RELEASE_SLACK`), at most the debt, as an exit's. Uncut where `cut` is zero: the
/// pool's unwind at face ({unwindFence}). {CUT}: a claim's, a senior one's at its ratios
/// (`FLAMMKeptLib.claimLoss`); {JUNIOR}: a junior part's, what stays past the senior claims' due at the
/// ratios in force, its share of all but theirs (`FLAMMKeptLib.juniorLegs`).
function shares(FLAMMStore.S storage $, uint16 id, uint256 part, uint256 whole, uint256 cut)
internal
view
returns (uint256 c, uint256 s, uint256 d, IMMRouter.VenueView memory v)
{
IMMRouter r = $.router;
v = r.venue(address(this), id);
uint256 debt;
(, c,, s, debt) = r.venuePosition(address(this), id);
if (debt != 0) {
d = Math.mulDiv(debt, part, whole, Math.Rounding.Up) + MMRouterLib.RELEASE_SLACK;
if (d > debt) d = debt;
}
bool lossy = ($.lossVenues >> id) & 1 != 0;
if (cut >= JUNIOR && lossy) {
// a junior part takes what stays past the senior claims' due, as the clear shares it
(c, s) = FLAMMKeptLib.juniorLegs(id, v.account, v.id, c, s, whole, part);
} else {
(c, s) = (Math.mulDiv(c, part, whole), Math.mulDiv(s, part, whole));
if (cut == CUT && lossy) {
// a senior claim of a kept share's record: the kept share bears the loss first
(uint256 cl, uint256 sl) = FLAMMKeptLib.claimLoss(id, v.account, v.id, whole);
(c, s) = (c - Math.mulDiv(c, cl, WAD, Math.Rounding.Up), s - Math.mulDiv(s, sl, WAD, Math.Rounding.Up));
}
}
}
/// @dev {_tokenAmount}, paid to `to`.
function _tokenShare(FLAMMStore.S storage $, uint256 t, uint256 fee, uint256 part, uint256 whole, address to)
private
returns (uint256 amount)
{
amount = _tokenAmount($, t, fee, part, whole);
if (amount == 0) return 0;
address token = FLAMMStore.tokenOf($, t);
FLAMMStore.safeTransfer(token, to, amount);
if (FLAMMStore.balanceOfThis(token) < FLAMMStore.heldOf($, t)) revert CustodyMismatch();
}
/// @dev `part` of `whole` of token `t`'s balance outside custody, less `fee` (the protocol's while the custody is
/// out), rounded down.
function _tokenAmount(FLAMMStore.S storage $, uint256 t, uint256 fee, uint256 part, uint256 whole)
private
view
returns (uint256)
{
uint256 kept = FLAMMStore.heldOf($, t) + fee;
uint256 bal = FLAMMStore.balanceOfThis(FLAMMStore.tokenOf($, t));
return bal > kept ? Math.mulDiv(bal - kept, part, whole) : 0;
}
/// @notice `holder`'s part of fence `key`, of `FLAMMFenceLib.ONE`: its senior and junior parts summed
/// (`FLAMMStore.SENIORS`).
function claimOf(uint32 key, address holder) public view returns (uint256) {
uint256 w = FLAMMStore.s().claims[key][holder];
return uint128(w) + (w >> 128);
}
/// @notice `holder`'s proceeds in `token` parked in fence `key`'s vault ({drawParked}).
function parkedOf(uint32 key, address holder, address token) public view returns (uint256) {
return FLAMMStore.s().parked[key][holder][token];
}
/// @notice What settling `holder`'s whole claim on fence `key` would move now: at a venue its collateral and supply
/// shares out, cut by the venue's haircut, and its debt share in; at a token's custody the token out
/// (`collateralOut`), from the key's vault once the token's lift has run. Zero for a venue claim on a
/// record that has closed. At a frame's account this is the one record's: a settlement moves the frame's
/// other records with it (`FLAMMFenceLib.mates`), so a holder sums this over their keys.
function claimAmounts(uint32 key, address holder)
public
view
returns (uint256 collateralOut, uint256 supplyOut, uint256 debtIn)
{
ReentrancyGuardLib._readGuard();
FLAMMStore.S storage $ = FLAMMStore.s();
uint256 index = key & 63;
FLAMMStore.Fence storage f = $.fences[index];
(, uint256 part, uint256 cut) = _have($, key, index, holder);
if (part == 0) return (0, 0, 0);
if (f.key != key) {
uint256 v = $.vaulted[key];
if (index >= 32 && v != 0) {
collateralOut =
Math.mulDiv(IERC20(FLAMMStore.tokenOf($, index - 32)).balanceOf($.vaults[key]), part, v);
}
return (collateralOut, 0, 0);
}
uint256 whole = uint256(f.live) + f.claimed;
if (whole == 0) return (0, 0, 0);
if (index < 32) {
(collateralOut, supplyOut, debtIn,) = shares($, uint16(index), part, whole, cut);
if (cut == JUNIOR) debtIn = 0;
} else {
collateralOut = _tokenAmount($, index - 32, f.fee, part, whole);
}
}
/// @notice The part an exit of `shares` at the pool's supply now would leave at each fence that holds a pool part
/// (`FLAMMFenceLib.recordClaims`), by key; a skim's fee shares before the exit lower each. On a token's
/// record, less what the exit pays from that claim of a kept share's debt owed in the token
/// (`FLAMMKeptLib.fromRecord`). A part under `FLAMMFenceLib.DUST`, or a token fence that holds no
/// pool part, lists as zero.
function previewClaims(uint256 shares_) public view returns (uint32[] memory keys, uint256[] memory parts) {
ReentrancyGuardLib._readGuard();
FLAMMStore.S storage $ = FLAMMStore.s();
uint256 supply = IERC20(address(this)).totalSupply();
uint256 w = $.fenceWord;
uint256 m = FLAMMFenceLib.parts($, w);
uint256 n;
for (uint256 k = m; k != 0; k &= k - 1) {
++n;
}
(keys, parts) = (new uint32[](n), new uint256[](n));
// a kept share's debt owed in a token whose custody is out, which the exit pays from its claim on the token's
// record first, as the exit does (`FLAMMFlowLib.proRata`)
uint256[] memory kept;
if (supply != 0 && w >> FLAMMStore.KEPT_SHIFT != 0 && m >> 32 != 0) {
(,,, kept) = FLAMMKeptLib.keptLegs($.tokenCount, true);
}
for (uint256 j; m != 0; m &= m - 1) {
uint256 i = FLAMMFenceLib.low(m);
FLAMMStore.Fence storage f = $.fences[i];
uint256 part = supply == 0 ? 0 : Math.mulDiv(f.live, shares_, supply);
if (i >= 32 && kept.length != 0) {
uint256 t = i - 32;
uint256 neg = Math.mulDiv(kept[t], shares_, supply, Math.Rounding.Up);
uint256 pos = Math.mulDiv(FLAMMStore.heldOf($, t), shares_, supply);
if (neg > pos) {
(uint256 c,) = FLAMMKeptLib.fromRecord($, t, neg - pos, shares_, supply);
part = part > c ? part - c : 0;
}
}
(keys[j], parts[j]) = (f.key, part < FLAMMFenceLib.DUST ? 0 : part);
++j;
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {
"src/core/mm/MMRouterLib.sol": {
"MMRouterLib": "0x95b386d242c061ab2551a0d294c94dfe3948ef2c"
},
"src/core/flamm/FLAMMLane.sol": {
"FLAMMLane": "0x5d81d4a7cfd40c3dfe9e009212545608be90de37"
},
"src/core/flamm/FLAMMRoute.sol": {
"FLAMMRoute": "0xb1ffeae12c53f876aa0e495e9c00c829f47fb0fe"
},
"src/periphery/FLAMMMaxima.sol": {
"FLAMMMaxima": "0x68546ce65986354c5394e4bc2f28027ef490a91b"
},
"src/core/flamm/FLAMMLawLib.sol": {
"FLAMMLawLib": "0xf55c5807b6fbe5555443fd6ddbbd52079849cf45"
},
"src/core/flamm/FLAMMOpsLib.sol": {
"FLAMMOpsLib": "0x0be7fdb3d611b05df90a61778a644654735ecc47"
},
"src/core/mm/AaveV3ReadsLib.sol": {
"AaveV3ReadsLib": "0xa5db415d1af2158dd0a4a3c9c1eada277472803e"
},
"src/core/flamm/FLAMMFlowLib.sol": {
"FLAMMFlowLib": "0x27957f4ddf818de39a1c9dba0bf273a5994cfb77"
},
"src/core/flamm/FLAMMGateLib.sol": {
"FLAMMGateLib": "0x3a05779218510235badcbddcdd78732a16434368"
},
"src/core/flamm/FLAMMKeptLib.sol": {
"FLAMMKeptLib": "0x70cace9076ac8dd6a691ced2f9aa62a5c360bee4"
},
"src/core/mm/CTokenMarketLib.sol": {
"CTokenMarketLib": "0x60e4ab6b6ba72325c58924ee7753b14b37314010"
},
"src/core/flamm/FLAMMAdmitLib.sol": {
"FLAMMAdmitLib": "0x0b107b1f0c64f3716554ccc83b53e492e51ce950"
},
"src/core/flamm/FLAMMBlindLib.sol": {
"FLAMMBlindLib": "0x634d5a14c42fb5bec82a7aea32bf636a8d298fcd"
},
"src/core/flamm/FLAMMClaimLib.sol": {
"FLAMMClaimLib": "0x5dc3bb30c60390ce9522b7e3ca315dd334aa7e91"
},
"src/core/flamm/FLAMMFenceLib.sol": {
"FLAMMFenceLib": "0x9dbba0cbd9d7883f4eedd1c8e08780fac4798f1a"
},
"src/core/flamm/FLAMMFloatLib.sol": {
"FLAMMFloatLib": "0x6b2b25ebba12e78afea697b9ac6d2167a6518935"
},
"src/core/flamm/FLAMMQuoteLib.sol": {
"FLAMMQuoteLib": "0x99de54a108c64ebe23ac7766901c199a91929869"
},
"src/core/flamm/FLAMMTopUpLib.sol": {
"FLAMMTopUpLib": "0xe4be161081d1c9aa91951ca347cc99da35cfac2e"
},
"src/core/mm/CTokenRewardsLib.sol": {
"CTokenRewardsLib": "0x93ab647215a9f9f1050302a76506afcbc0262861"
},
"src/core/mm/MMRouterAdminLib.sol": {
"MMRouterAdminLib": "0x6abb06b1e9cba80888992d366913bb9b7760ea7e"
},
"src/core/flamm/FLAMMPolicyLib.sol": {
"FLAMMPolicyLib": "0x3858e2202cebcb6f12f36a596260dd2ed8f91f06"
},
"src/core/flamm/FLAMMSettleLib.sol": {
"FLAMMSettleLib": "0x4551cddb1de95f5452f6a0f836e047e01833b301"
},
"src/core/flamm/FLAMMSharedLib.sol": {
"FLAMMSharedLib": "0xb852d9cbdda85cbed7da0885bca2714ba1b71005"
},
"src/periphery/FLAMMExitMaxima.sol": {
"FLAMMExitMaxima": "0x6a6a3ff1e834dbbf07dee472e07d1ca977798659"
},
"src/hooks/everlong/LeverageLib.sol": {
"LeverageLib": "0x80396f9f894cc3060dfaa7468c4f576e8b16d055"
},
"src/core/flamm/FLAMMMaintainLib.sol": {
"FLAMMMaintainLib": "0xaa5434e13eb54f37c577229efc981cceceb56417"
},
"src/core/flamm/FLAMMPairQuoteLib.sol": {
"FLAMMPairQuoteLib": "0x3bc57bc05cc9dd1fe18106add8ffc060882055dd"
},
"src/hooks/everlong/EverlongOpsLib.sol": {
"EverlongOpsLib": "0xf6d56f4ed6a7c441fd96c53a327026bac19dfeef"
}
},
"optimizer": {
"runs": 100,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable-compat/contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts-compat/contracts/",
"forge-std/=lib/forge-std/src/",
"solady/=lib/solady/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.