Verified contractExactBUSL-1.1
ReserveCascadeLib
Address0x069fb670efb0604c3cc40dda8b77952c2f8d916b
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 100 runs
- EVM version
- cancun
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,459,471
- Creation transaction
- 0x9b9d22516f…9d2558bedd
93 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/libraries/ReserveCascadeLib.solmain12.63 KB
- lib/openzeppelin-contracts-upgradeable/contracts/access/AccessControlUpgradeable.sol8.33 KB
- lib/openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol9.28 KB
- lib/openzeppelin-contracts-upgradeable/contracts/proxy/utils/UUPSUpgradeable.sol6.57 KB
- lib/openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol12.13 KB
- lib/openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC4626Upgradeable.sol13.29 KB
- lib/openzeppelin-contracts-upgradeable/contracts/token/ERC721/ERC721Upgradeable.sol17.8 KB
- lib/openzeppelin-contracts-upgradeable/contracts/utils/ContextUpgradeable.sol1.19 KB
- lib/openzeppelin-contracts-upgradeable/contracts/utils/PausableUpgradeable.sol3.66 KB
- lib/openzeppelin-contracts-upgradeable/contracts/utils/ReentrancyGuardUpgradeable.sol4.51 KB
- lib/openzeppelin-contracts-upgradeable/contracts/utils/introspection/ERC165Upgradeable.sol1.15 KB
- lib/openzeppelin-contracts/contracts/access/IAccessControl.sol3.34 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol4.45 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol193 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC1967.sol637 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol182 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC4626.sol12.18 KB
- lib/openzeppelin-contracts/contracts/interfaces/draft-IERC1822.sol882 B
- lib/openzeppelin-contracts/contracts/interfaces/draft-IERC6093.sol6.54 KB
- lib/openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Utils.sol6.24 KB
- lib/openzeppelin-contracts/contracts/proxy/beacon/IBeacon.sol473 B
- lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol2.78 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol684 B
- lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol10.08 KB
- lib/openzeppelin-contracts/contracts/token/ERC721/IERC721.sol4.93 KB
- lib/openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol997 B
- lib/openzeppelin-contracts/contracts/token/ERC721/extensions/IERC721Metadata.sol792 B
- lib/openzeppelin-contracts/contracts/token/ERC721/utils/ERC721Utils.sol1.92 KB
- lib/openzeppelin-contracts/contracts/utils/Address.sol6 KB
- lib/openzeppelin-contracts/contracts/utils/Errors.sol896 B
- lib/openzeppelin-contracts/contracts/utils/Panic.sol1.97 KB
- lib/openzeppelin-contracts/contracts/utils/StorageSlot.sol4.18 KB
- lib/openzeppelin-contracts/contracts/utils/Strings.sol19.28 KB
- lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol871 B
- lib/openzeppelin-contracts/contracts/utils/math/Math.sol32.15 KB
- lib/openzeppelin-contracts/contracts/utils/math/SafeCast.sol34.94 KB
- lib/openzeppelin-contracts/contracts/utils/math/SignedMath.sol2.61 KB
- lib/solady/src/utils/FixedPointMathLib.sol55.69 KB
- src/ClaimBridge.sol50.08 KB
- src/ReserveManager.sol47.28 KB
- src/interfaces/IAccrualExposure.sol2.49 KB
- src/interfaces/IAccrualLifecycle.sol6.67 KB
- src/interfaces/IAccrualMigration.sol2.22 KB
- src/interfaces/IAttestationOracle.sol11.8 KB
- src/interfaces/ICascadeBackstop.sol3.47 KB
- src/interfaces/ICollateralRegistry.sol21.89 KB
- src/interfaces/IComplianceRegistry.sol2.52 KB
- src/interfaces/IContinuousAccrual.sol4.69 KB
- src/interfaces/ICuratorModule.sol21.74 KB
- src/interfaces/IDefaultManager.sol34.39 KB
- src/interfaces/IImpairmentSource.sol2.26 KB
- src/interfaces/ILossProtection.sol355 B
- src/interfaces/IMintRedeemController.sol42.91 KB
- src/interfaces/IPointsModule.sol2.43 KB
- src/interfaces/IReserveLossAbsorber.sol2.11 KB
- src/interfaces/IReserveLossGovernance.sol574 B
- src/interfaces/IReserveManager.sol31.28 KB
- src/interfaces/IRevisionedImpairmentSource.sol2.79 KB
- src/interfaces/IWaterfallEngine.sol16.65 KB
- src/interfaces/IsUSDfr.sol14.06 KB
- src/libraries/AccrualBook.sol23.61 KB
- src/libraries/AccrualCeiling.sol4.34 KB
- src/libraries/AccrualLoans.sol49.12 KB
- src/libraries/AccrualMath.sol8.76 KB
- src/libraries/AccrualSchedule.sol10.13 KB
- src/libraries/AccrualSegments.sol8.37 KB
- src/libraries/BorrowerPayoutLib.sol3.5 KB
- src/libraries/BridgeAccrualLib.sol5.25 KB
- src/libraries/BridgeFundingLib.sol14.58 KB
- src/libraries/BridgeMixedInterestLib.sol32.77 KB
- src/libraries/Config.sol23.1 KB
- src/libraries/LossEventIds.sol1.35 KB
- src/libraries/LossProtectionLib.sol5.39 KB
- src/libraries/PointsHookGas.sol2.73 KB
- src/libraries/ReserveAccrualCreditLib.sol16.47 KB
- src/libraries/ReserveAccrualLib.sol23.2 KB
- src/libraries/ReserveAccrualLifecycleLib.sol14.71 KB
- src/libraries/ReserveAccrualServiceLib.sol3.15 KB
- src/libraries/ReserveAccrualStorageLib.sol5.04 KB
- src/libraries/ReserveAccrualViewsLib.sol4.94 KB
- src/libraries/ReserveCreditLib.sol13.44 KB
- src/libraries/ReserveIncidentLib.sol12.64 KB
- src/libraries/ReserveMigrationLib.sol18.51 KB
- src/libraries/ReserveMigrationViewLib.sol1.14 KB
- src/libraries/ReserveRoundingLib.sol11.87 KB
- src/libraries/ReserveStorageLib.sol5.53 KB
- src/libraries/ReserveWiringLib.sol8.77 KB
- src/libraries/Roles.sol4.2 KB
- src/libraries/SettlementLiquidityLib.sol10.02 KB
- src/libraries/VaultAccrualLib.sol18.03 KB
- src/libraries/VaultFeeMath.sol9.97 KB
- src/libraries/VaultStreamLib.sol4.99 KB
- src/sUSDfr.sol70.36 KB
src/libraries/ReserveCascadeLib.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SettlementLiquidityLib} from "./SettlementLiquidityLib.sol";
import {ReserveStorageLib} from "./ReserveStorageLib.sol";
import {ReserveManager} from "../ReserveManager.sol";
import {IReserveManager} from "../interfaces/IReserveManager.sol";
import {IReserveLossAbsorber} from "../interfaces/IReserveLossAbsorber.sol";
import {IMintRedeemController} from "../interfaces/IMintRedeemController.sol";
import {ICuratorModule} from "../interfaces/ICuratorModule.sol";
import {ICascadeBackstop} from "../interfaces/ICascadeBackstop.sol";
import {LossEventIds} from "./LossEventIds.sol";
/// @title ReserveCascadeLib
/// @notice Ethereum's native custody-loss accounting, separated to leave room for continuous accrual.
/// @dev No storage is declared or located here. ReserveManager passes its original live namespace
/// pointer and retains all external authorization, arm/evidence and reentrancy checks. The
/// curator -> sGROVE -> senior order and the existing surplus/residual accounting are retained.
library ReserveCascadeLib {
/// @notice Records a custody loss against the exact native reserve state.
/// @dev The host authenticates the arm, evidence, loss approval and reentrancy guard.
function recognize(ReserveManager.ReserveStorage storage $, uint256 nativeUnits, uint256 backingReduction) public {
IMintRedeemController controller = $.lossController;
if (address(controller) == address(0)) revert IReserveManager.ReserveManager_InvalidLossController(address(0));
uint256 supplyBefore = controller.totalUSDfr();
uint256 backingBefore = controller.backingValue();
if (backingReduction > backingBefore) {
revert IReserveManager.ReserveManager_LossAllocationMismatch(0, 0, backingReduction, backingBefore);
}
uint256 surplusBefore = backingBefore > supplyBefore ? backingBefore - supplyBefore : 0;
uint256 surplusAbsorbed = backingReduction < surplusBefore ? backingReduction : surplusBefore;
uint256 supplyReductionRequired = backingReduction - surplusAbsorbed;
_retireCustodyPrepayment($, $.activeReserveLossIncidentId, surplusBefore, surplusAbsorbed);
// Recognition lowers backing first. Every subsequent burn is checked as a precise,
// non-worsening reduction of the now-visible deficit by MintRedeemController.
$.idleUSDCUnits -= nativeUnits;
SettlementLiquidityLib.outflow(
ReserveStorageLib.normalize(nativeUnits), ReserveStorageLib.normalize($.idleUSDCUnits)
);
emit IReserveManager.ReserveLossRecognized(
$.activeReserveLossIncidentId, backingReduction, surplusAbsorbed, supplyReductionRequired
);
if (supplyReductionRequired == 0) {
IReserveLossAbsorber.ReserveLossAllocation memory allocation;
allocation.surplusAbsorbed = surplusAbsorbed;
_emitReserveLossAllocated($.activeReserveLossIncidentId, backingReduction, allocation);
return;
}
$.recognizedBackingReduction += backingReduction;
$.recognizedSurplusAbsorbed += surplusAbsorbed;
$.recognizedSupplyReduction += supplyReductionRequired;
}
/// @dev Retires only credit whose surplus backing this custody loss consumed. Surplus
/// beyond the prepaid balance absorbs loss first. Retired credit cannot offset a later
/// facility loss after the capital supporting it has already been spent.
function _retireCustodyPrepayment(
ReserveManager.ReserveStorage storage $,
uint256 incidentId,
uint256 surplusBefore,
uint256 surplusAbsorbed
) private {
uint256 prepaid = $.exitPrepaidAbsorption;
uint256 freeSurplus = surplusBefore > prepaid ? surplusBefore - prepaid : 0;
if (surplusAbsorbed <= freeSurplus) return;
uint256 used = surplusAbsorbed - freeSurplus;
uint256 outstanding = prepaid - used;
$.exitPrepaidAbsorption = outstanding;
emit IReserveManager.ExitPrepaymentAbsorbedByCustody(incidentId, used, outstanding);
}
/// @notice Allocates the recognized loss through curator, sGROVE and senior assets.
/// @dev Executes in the reserve context; every transfer and supply/backing delta remains proved.
function absorb(ReserveManager.ReserveStorage storage $, uint256 incidentId) public {
if (!LossEventIds.isCustodyEvent(incidentId)) {
revert IReserveManager.ReserveManager_InvalidReserveLossIncident(incidentId);
}
uint256 active = $.activeReserveLossIncidentId;
if (active == 0) revert IReserveManager.ReserveManager_NoActiveIncident();
if (incidentId != active) revert IReserveManager.ReserveManager_IncidentMismatch(active, incidentId);
uint256 requiredSupplyReduction = $.recognizedSupplyReduction;
if (requiredSupplyReduction == 0) revert IReserveManager.ReserveManager_NoRecognizedReserveLoss();
IMintRedeemController controller = $.lossController;
if (
address($.lossCurator) == address(0) || address($.lossBackstop) == address(0)
|| address($.lossVault) == address(0) || address($.lossUSDfr) == address(0)
) revert IReserveManager.ReserveManager_InvalidLossAbsorber(address(0));
if (address(controller) == address(0)) revert IReserveManager.ReserveManager_InvalidLossController(address(0));
$.lossVault.accrueFees();
uint256 supplyBefore = controller.totalUSDfr();
// Recognition has already lowered backing by `recognizedBackingReduction`. Reconstruct
// the pre-loss level so the standing valuation hole and any previously latched cascade
// residual are carried through the new delta rather than charged or latched twice.
uint256 backingAfterRecognition = controller.backingValue();
uint256 backingBeforeLoss = backingAfterRecognition + $.recognizedBackingReduction;
uint256 deficitBefore = supplyBefore > backingBeforeLoss ? supplyBefore - backingBeforeLoss : 0;
IReserveLossAbsorber.ReserveLossAllocation memory allocation;
uint256 residual;
(allocation.curatorAbsorbed, allocation.backstopCovered, residual) =
_drawJuniorReserveLoss($.lossCurator, $.lossBackstop, $.lossUSDfr, incidentId, requiredSupplyReduction);
uint256 juniorBurn = allocation.curatorAbsorbed + allocation.backstopCovered;
if (juniorBurn != 0) controller.burnLoss(address(this), juniorBurn);
if (residual != 0) {
uint256 vaultAssets = $.lossVault.totalAssets();
allocation.seniorBurned = residual < vaultAssets ? residual : vaultAssets;
if (allocation.seniorBurned != 0) {
controller.burnLoss(address($.lossVault), allocation.seniorBurned);
residual -= allocation.seniorBurned;
}
}
allocation.residualDeficit = residual;
allocation.surplusAbsorbed = $.recognizedSurplusAbsorbed;
_finalizeReserveLoss($, incidentId, requiredSupplyReduction, supplyBefore, deficitBefore, allocation);
}
function _finalizeReserveLoss(
ReserveManager.ReserveStorage storage $,
uint256 incidentId,
uint256 requiredSupplyReduction,
uint256 supplyBefore,
uint256 deficitBefore,
IReserveLossAbsorber.ReserveLossAllocation memory allocation
) private {
uint256 observedDeficit =
_verifyReserveLoss($, requiredSupplyReduction, supplyBefore, deficitBefore, allocation);
uint256 backingReduction = $.recognizedBackingReduction;
$.recognizedBackingReduction = 0;
$.recognizedSurplusAbsorbed = 0;
$.recognizedSupplyReduction = 0;
// Only the portion of the pre-loss hole that was already a cascade residual is carried
// into the latch. A standing valuation mark is an output/price constraint, not a new
// custody shortfall; the current loss has already been offered unconditionally above.
uint256 previousDeficit = $.reserveDeficit;
uint256 carriedValuationDeficit = deficitBefore > previousDeficit ? deficitBefore - previousDeficit : 0;
uint256 nextDeficit = observedDeficit - carriedValuationDeficit;
if (nextDeficit != previousDeficit) {
$.reserveDeficit = nextDeficit;
emit IReserveManager.ReserveDeficitUpdated(incidentId, previousDeficit, nextDeficit);
}
_emitReserveLossAllocated(incidentId, backingReduction, allocation);
}
function _verifyReserveLoss(
ReserveManager.ReserveStorage storage $,
uint256 requiredSupplyReduction,
uint256 supplyBefore,
uint256 deficitBefore,
IReserveLossAbsorber.ReserveLossAllocation memory allocation
) private view returns (uint256 observedDeficit) {
IMintRedeemController controller = $.lossController;
uint256 supplyAfter = controller.totalUSDfr();
uint256 reportedBurn = allocation.curatorAbsorbed + allocation.backstopCovered + allocation.seniorBurned;
uint256 observedBurn = supplyAfter > supplyBefore ? 0 : supplyBefore - supplyAfter;
if (reportedBurn > requiredSupplyReduction || observedBurn != reportedBurn) {
revert IReserveManager.ReserveManager_LossAbsorberContractViolated(reportedBurn, observedBurn);
}
uint256 expectedAccounted = $.recognizedBackingReduction - $.recognizedSurplusAbsorbed;
uint256 reportedAccounted = reportedBurn + allocation.residualDeficit;
if (expectedAccounted != requiredSupplyReduction || reportedAccounted != expectedAccounted) {
revert IReserveManager.ReserveManager_LossAllocationMismatch(
$.recognizedSurplusAbsorbed, allocation.surplusAbsorbed, expectedAccounted, reportedAccounted
);
}
uint256 backingAfter = controller.backingValue();
observedDeficit = supplyAfter > backingAfter ? supplyAfter - backingAfter : 0;
uint256 expectedDeficit = deficitBefore + requiredSupplyReduction;
expectedDeficit = expectedDeficit > reportedBurn ? expectedDeficit - reportedBurn : 0;
if (observedDeficit != expectedDeficit) {
revert IReserveManager.ReserveManager_PostLossDeficitMismatch(expectedDeficit, observedDeficit);
}
}
function _drawJuniorReserveLoss(
ICuratorModule curator,
ICascadeBackstop backstop,
IERC20 usdfr,
uint256 incidentId,
uint256 requiredSupplyReduction
) private returns (uint256 curatorAbsorbed, uint256 backstopCovered, uint256 residual) {
uint256 balanceBefore = usdfr.balanceOf(address(this));
(curatorAbsorbed, residual) = curator.absorbGlobalLoss(requiredSupplyReduction);
if (curatorAbsorbed > requiredSupplyReduction || residual != requiredSupplyReduction - curatorAbsorbed) {
revert IReserveManager.ReserveManager_LossAllocationMismatch(
0, 0, requiredSupplyReduction, curatorAbsorbed + residual
);
}
uint256 balanceAfter = usdfr.balanceOf(address(this));
uint256 received = balanceAfter < balanceBefore ? 0 : balanceAfter - balanceBefore;
if (received != curatorAbsorbed) {
revert IReserveManager.ReserveManager_LossAbsorberContractViolated(curatorAbsorbed, received);
}
if (residual != 0) {
balanceBefore = balanceAfter;
backstopCovered = backstop.coverShortfall(incidentId, residual);
balanceAfter = usdfr.balanceOf(address(this));
received = balanceAfter < balanceBefore ? 0 : balanceAfter - balanceBefore;
if (backstopCovered > residual || received != backstopCovered) {
revert IReserveManager.ReserveManager_LossAbsorberContractViolated(backstopCovered, received);
}
residual -= backstopCovered;
}
}
function _emitReserveLossAllocated(
uint256 incidentId,
uint256 backingReduction,
IReserveLossAbsorber.ReserveLossAllocation memory allocation
) private {
emit IReserveManager.ReserveLossAllocated(
incidentId,
backingReduction,
allocation.surplusAbsorbed,
allocation.curatorAbsorbed,
allocation.backstopCovered,
allocation.seniorBurned,
allocation.residualDeficit
);
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 100,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/",
"halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
"openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"solady/=lib/solady/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.