Verified contractExactBUSL-1.1
ReserveCreditLib
Address0x7809bb6bfd961e855b8f5e445a475e9f4c3bb466
- 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,304
- Creation transaction
- 0x07710ce426…b1a77b410e
79 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/libraries/ReserveCreditLib.solmain10.73 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/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
- src/ClaimBridge.sol51.43 KB
- src/ReserveManager.sol46.83 KB
- src/interfaces/IAccrualExposure.sol2.49 KB
- src/interfaces/IAccrualLifecycle.sol6.35 KB
- src/interfaces/IAccrualMigration.sol2.22 KB
- src/interfaces/IAttestationOracle.sol11.8 KB
- src/interfaces/ICascadeBackstop.sol3.47 KB
- src/interfaces/ICollateralRegistry.sol20.52 KB
- src/interfaces/IComplianceRegistry.sol2.52 KB
- src/interfaces/IContinuousAccrual.sol4.39 KB
- src/interfaces/ICuratorModule.sol21.74 KB
- src/interfaces/ILMVReleaseHistoryMonitor.sol1.06 KB
- src/interfaces/IMintRedeemController.sol42.91 KB
- src/interfaces/IReserveLossAbsorber.sol2.11 KB
- src/interfaces/IReserveLossGovernance.sol574 B
- src/interfaces/IReserveManager.sol26.93 KB
- src/interfaces/IWaterfallEngine.sol16.65 KB
- src/interfaces/IsUSDfr.sol12.92 KB
- src/libraries/AccrualBook.sol23.61 KB
- src/libraries/AccrualCeiling.sol4.34 KB
- src/libraries/AccrualLoans.sol48.22 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/BridgeMixedInterestLib.sol29.67 KB
- src/libraries/Config.sol23.1 KB
- src/libraries/LossEventIds.sol1.35 KB
- src/libraries/ReleaseHistoryMonitorLib.sol5.08 KB
- src/libraries/ReserveAccrualCreditLib.sol16.47 KB
- src/libraries/ReserveAccrualLib.sol22.9 KB
- src/libraries/ReserveAccrualLifecycleLib.sol14.71 KB
- src/libraries/ReserveAccrualServiceLib.sol3.15 KB
- src/libraries/ReserveAccrualStorageLib.sol5.04 KB
- src/libraries/ReserveAccrualViewsLib.sol3.69 KB
- src/libraries/ReserveCascadeLib.sol12.35 KB
- src/libraries/ReserveIncidentLib.sol12.48 KB
- src/libraries/ReserveMigrationLib.sol18.51 KB
- src/libraries/ReserveMigrationViewLib.sol1.14 KB
- src/libraries/ReserveRoundingLib.sol11.72 KB
- src/libraries/ReserveStorageLib.sol5.53 KB
- src/libraries/ReserveWiringLib.sol8.77 KB
- src/libraries/Roles.sol4.2 KB
src/libraries/ReserveCreditLib.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IReserveManager} from "../interfaces/IReserveManager.sol";
import {ReserveManager} from "../ReserveManager.sol";
import {ReserveStorageLib} from "./ReserveStorageLib.sol";
import {BorrowerPayoutLib} from "./BorrowerPayoutLib.sol";
import {ReleaseHistoryMonitorLib} from "./ReleaseHistoryMonitorLib.sol";
/// @title ReserveCreditLib — the ReserveManager's credit-path bodies
///
/// @notice EXTRACTED 2026-09-09 FOR EIP-170, mirroring BSC commit `9438be1`. The live mainnet
/// `ReserveManager` shipped with 9 bytes of runtime margin, so it was frozen against any
/// new function; adding `recordPikCapitalization` put it 184 bytes over the 24,576 limit
/// even after the fee and PIK twins were made to share one private body.
///
/// @dev EVERY FUNCTION HERE IS `public` AND THAT IS THE WHOLE POINT. A `public` library function is
/// deployed as its own contract and reached by delegatecall, so its bytecode leaves the
/// caller's runtime; an `internal` one is inlined and saves nothing. The library therefore
/// needs LINKING at deploy time, which is new for this repository.
///
/// @dev THE CALLER KEEPS THE GUARDS. `ReserveManager` retains every `onlyRole`, `nonReentrant` and
/// `whenNotPaused` modifier on the external entry points, because a delegatecall runs in the
/// proxy's context and a library cannot see the caller's modifiers. Do not call anything here
/// from a path that does not carry them.
///
/// @dev DELEGATECALL MEANS `address(this)` IS THE PROXY, which is what keeps `safeTransfer`,
/// `balanceOf(address(this))` and the self-deployment guard meaning exactly what they meant
/// when these bodies lived in the contract.
library ReserveCreditLib {
/// @notice Principal and previously recognized interest discharged by measured canonical USDC.
event AccruedPaymentReceived(
uint256 indexed facilityId,
address indexed asset,
address indexed payer,
uint256 nativeUnits,
uint256 principalReduction,
uint256 interestReduction
);
using SafeERC20 for IERC20;
using ReserveStorageLib for ReserveManager.ReserveStorage;
/// @dev Body of `ReserveManager.recordDeployment`.
function deploy(ReserveManager.ReserveStorage storage $, uint256 facilityId, address to, uint256 usdcAmount)
public
{
if (to == address(0)) revert IReserveManager.ReserveManager_ZeroAddress();
if (to == address(this)) revert IReserveManager.ReserveManager_SelfDeployment();
if (usdcAmount == 0) revert IReserveManager.ReserveManager_ZeroAmount();
BorrowerPayoutLib.requireAllowedInternal(to);
// MA-1/R4-01: a live custody shortfall freezes the facility-funding USDC out-door.
$.requireIdleFullyCustodied();
uint256 value = ReserveStorageLib.normalize(usdcAmount);
uint256 idleValue = ReserveStorageLib.normalize($.idleUSDCUnits);
if (value > idleValue) revert IReserveManager.ReserveManager_InsufficientIdleValue(value, idleValue);
$.idleUSDCUnits -= usdcAmount;
$.deployed[facilityId] += value;
$.totalDeployedPrincipal += value;
ReleaseHistoryMonitorLib.recordOutflow(value);
$.usdcToken.safeTransfer(to, usdcAmount);
emit IReserveManager.PrincipalDeployed(facilityId, value);
}
/// @notice The unchanged legacy release body, moved here to retain EIP-170 headroom.
/// @dev The ReserveManager caller retains its role, pause and reentrancy guards.
function release(ReserveManager.ReserveStorage storage $, address to, uint256 amount) public {
if (to == address(0)) revert IReserveManager.ReserveManager_ZeroAddress();
if (to == address(this)) revert IReserveManager.ReserveManager_SelfDeployment();
if (amount == 0) revert IReserveManager.ReserveManager_ZeroAmount();
$.requireIdleFullyCustodied();
uint256 value = ReserveStorageLib.normalize(amount);
uint256 idleValue = ReserveStorageLib.normalize($.idleUSDCUnits);
if (value > idleValue) revert IReserveManager.ReserveManager_InsufficientIdleValue(value, idleValue);
$.idleUSDCUnits -= amount;
ReleaseHistoryMonitorLib.recordOutflow(value);
$.usdcToken.safeTransfer(to, amount);
emit IReserveManager.USDCReleased(to, amount);
}
/// @dev Shared body of `recordFeeCapitalization` and `recordPikCapitalization`. `requireIdle`
/// is the fee twin's solvency bound; PIK has no cash behind it and passes false.
///
/// The fee twin is sound because the fee's cash NEVER LEFT: `fund` deploys
/// `principal - fee`, so backing rises against RETAINED CASH and bounding it by idle is
/// meaningful. There is no retained cash behind a PIK capitalisation; what stands behind
/// it is the borrower's obligation to repay a larger balance and, beneath that, the
/// curator first-loss layer. Bounding a receivable by idle cash would refuse a legitimate
/// capitalisation on a drawn-down book and admit one on a flush book.
///
/// EVERY OTHER BOUND ON PIK LIVES IN `WaterfallEngine.capitalizePik`.
function capitalize(ReserveManager.ReserveStorage storage $, uint256 facilityId, uint256 amount, bool requireIdle)
public
{
if (amount == 0) revert IReserveManager.ReserveManager_ZeroAmount();
uint256 units = ReserveStorageLib.denormalize(amount);
if (ReserveStorageLib.normalize(units) != amount) {
revert IReserveManager.ReserveManager_ValueNotUSDCExact(amount);
}
if (requireIdle) {
uint256 idleValue = ReserveStorageLib.normalize($.idleUSDCUnits);
if (amount > idleValue) revert IReserveManager.ReserveManager_InsufficientIdleValue(amount, idleValue);
}
// The borrower owes the full face amount while the OID cash stays in the treasury (fee
// path) or while the interest compounds into principal (PIK path). Both are additional
// receivable; only the fee path has retained cash standing behind it.
$.deployed[facilityId] += amount;
$.totalDeployedPrincipal += amount;
if (requireIdle) {
emit IReserveManager.FeeCapitalized(facilityId, amount);
} else {
emit IReserveManager.PikCapitalized(facilityId, amount, $.deployed[facilityId]);
}
}
/// @dev Body of `ReserveManager.recordPayment`.
function pay(
ReserveManager.ReserveStorage storage native,
uint256 facilityId,
address payer,
uint256 usdcAmount,
uint256 principal
) public returns (uint256 receivedValue) {
receivedValue = _pay(native, facilityId, payer, usdcAmount, principal);
emit IReserveManager.PaymentReceived(facilityId, payer, usdcAmount, principal);
}
/// @notice A measured receipt replaces already recognized cash or PIK receivable, without new income.
function payAccrued(
ReserveManager.ReserveStorage storage native,
uint256 facilityId,
address payer,
uint256 usdcAmount,
uint256 principalReduction,
uint256 interestReduction
) public returns (uint256 receivedValue) {
receivedValue = _pay(native, facilityId, payer, usdcAmount, principalReduction + interestReduction);
emit AccruedPaymentReceived(
facilityId, address(native.usdcToken), payer, usdcAmount, principalReduction, interestReduction
);
}
function _pay(
ReserveManager.ReserveStorage storage $,
uint256 facilityId,
address payer,
uint256 usdcAmount,
uint256 principal
) private returns (uint256 receivedValue) {
if (payer == address(0)) revert IReserveManager.ReserveManager_ZeroAddress();
if (usdcAmount == 0) revert IReserveManager.ReserveManager_ZeroAmount();
uint256 beforeBalance = $.usdcToken.balanceOf(address(this));
$.usdcToken.safeTransferFrom(payer, address(this), usdcAmount);
uint256 received = $.usdcToken.balanceOf(address(this)) - beforeBalance;
if (received != usdcAmount) {
revert IReserveManager.ReserveManager_UnexpectedUSDCReceipt(usdcAmount, received);
}
receivedValue = ReserveStorageLib.normalize(received);
if (principal > receivedValue) {
revert IReserveManager.ReserveManager_PrincipalExceedsPayment(principal, receivedValue);
}
uint256 deployedFace = $.deployed[facilityId];
if (principal > deployedFace) {
revert IReserveManager.ReserveManager_InsufficientDeployedPrincipal(facilityId, principal, deployedFace);
}
$.idleUSDCUnits += received;
if (principal != 0) {
uint256 remainingFace = deployedFace - principal;
$.deployed[facilityId] = remainingFace;
$.totalDeployedPrincipal -= principal;
// H-1: cash collection does not prove that a governance-adjudicated impairment
// was recovered. Preserve the mark unless the smaller remaining face can no longer
// support it; only that arithmetically unavoidable excess is released. Treating
// every repayment dollar as recovery made backing depend on repayment/write-down
// ordering and could silently reopen par exits against the still-impaired claim.
$.clampImpairmentToRemainingFace(facilityId, remainingFace);
}
}
/// @dev Body of `ReserveManager.recordPrincipalWritedown`. Extracted with the credit path
/// because it shares the impairment helpers with `pay`.
function writeDown(ReserveManager.ReserveStorage storage $, uint256 facilityId, uint256 amount) public {
if (amount == 0) revert IReserveManager.ReserveManager_ZeroAmount();
uint256 deployedFace = $.deployed[facilityId];
if (amount > deployedFace) {
revert IReserveManager.ReserveManager_InsufficientDeployedPrincipal(facilityId, amount, deployedFace);
}
$.deployed[facilityId] = deployedFace - amount;
$.totalDeployedPrincipal -= amount;
// A write-down is the realization of loss, so the mark already carried against that
// loss must be consumed to avoid counting the same dollar twice.
$.realizeImpairmentOnWriteDown(facilityId, amount);
emit IReserveManager.PrincipalWrittenDown(facilityId, amount);
}
}
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.