Verified contractExactBUSL-1.1
ReserveAccrualCreditLib
Address0xc95ba330adf273f7bcfdbf644c3c360b655dce41
- Balance
0 ETH
- Transactions
0
- 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,486,042
- Creation transaction
- 0x798ea906d3…b3e4ccc0e2
93 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/libraries/ReserveAccrualCreditLib.solmain16.47 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.sol22.46 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.sol3.99 KB
- src/libraries/ReserveAccrualLib.sol23.2 KB
- src/libraries/ReserveAccrualLifecycleLib.sol15.91 KB
- src/libraries/ReserveAccrualServiceLib.sol3.15 KB
- src/libraries/ReserveAccrualStorageLib.sol5.04 KB
- src/libraries/ReserveAccrualViewsLib.sol4.94 KB
- src/libraries/ReserveCascadeLib.sol12.63 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/ReserveAccrualCreditLib.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;
import {ReserveManager} from "../ReserveManager.sol";
import {ClaimBridge} from "../ClaimBridge.sol";
import {ICollateralRegistry} from "../interfaces/ICollateralRegistry.sol";
import {IAccrualBridge, IAccrualRegistry, IAccrualRisk} from "../interfaces/IAccrualLifecycle.sol";
import {AccrualBook} from "./AccrualBook.sol";
import {AccrualLoans} from "./AccrualLoans.sol";
import {AccrualCeiling} from "./AccrualCeiling.sol";
import {ReserveAccrualLib} from "./ReserveAccrualLib.sol";
import {ReserveAccrualStorageLib} from "./ReserveAccrualStorageLib.sol";
import {ReserveStorageLib} from "./ReserveStorageLib.sol";
import {BridgeMixedInterestLib} from "./BridgeMixedInterestLib.sol";
/// @title ReserveAccrualCreditLib
/// @notice Authenticated fixed-rate loan lifecycle and native receivable posting in the reserve.
/// @dev Public methods execute by delegatecall in the reserve. Its host supplies nonReentrant;
/// these methods additionally authenticate the immutable module and close callback admission.
library ReserveAccrualCreditLib {
using AccrualBook for AccrualBook.Book;
using AccrualLoans for AccrualLoans.State;
using ReserveStorageLib for ReserveManager.ReserveStorage;
uint256 internal constant MAX_EXPOSURE = type(uint256).max / 10_000;
/// @notice This event is restricted to the immutable module that proves its economic facts.
error AccrualCredit_WrongCaller(address expected, address caller);
/// @notice The funded bridge and measured native receivable disagree.
error AccrualCredit_InvalidFunding(uint256 facilityId);
/// @notice The first release supports fixed Actual/360 and Actual/365 cash, and fixed Actual/360 PIK.
error AccrualCredit_UnsupportedTerms(uint256 facilityId);
/// @notice A known funded identifier cannot be registered or bound a second time.
error AccrualCredit_AlreadyKnown(uint256 facilityId);
/// @notice An event cannot address an omitted or unfunded receivable.
error AccrualCredit_Unknown(uint256 facilityId);
/// @notice All funded future ceilings and present pending exposure must fit the registry domain.
error AccrualCredit_ExposureCapacity();
/// @notice Past-due baseline installation requires existing growth to be posted first.
error AccrualCredit_UnpostedRisk(uint256 facilityId);
/// @notice The exposure key discriminator must be total=0, class=1, borrower=2 or state=3.
error AccrualCredit_InvalidExposureKind(uint8 kind);
/// @notice A measured receipt or native face failed the exact contractual accounting identity.
error AccrualCredit_ReceiptMismatch(uint256 facilityId);
/// @notice Native loss reductions cannot leave a running contractual clock or exceed stopped debt.
error AccrualCredit_InvalidWriteDown(uint256 facilityId);
/// @notice An accrued facility must use its authenticated lifecycle rather than a legacy upward/receipt setter.
error AccrualCredit_UseAccrualLifecycle(uint256 facilityId);
/// @notice Gates legacy funding ledger writes to the one bound waterfall before registration.
function requireUnregisteredFunding(uint256 id) internal view {
ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
if (!s.enabled) return;
_caller(s.modules.waterfall);
if (s.identities[id].known) revert AccrualCredit_UseAccrualLifecycle(id);
}
/// @notice Legacy PIK/receipt ledgers cannot bypass continuous recognition after activation.
function requireLegacy(uint256 id) internal view {
if (ReserveAccrualStorageLib.state().enabled) revert AccrualCredit_UseAccrualLifecycle(id);
}
/// @notice Continuous recognition began on authenticated funded terms, from the funding block.
event AccruingLoanRegistered(
uint256 indexed facilityId,
address indexed asset,
uint256 indexed classId,
uint256 principal,
uint256 balanceCeiling,
uint16 rateBps,
uint32 yearSeconds,
uint64 fundedAt,
bool pik
);
event MixedAccruingLoanRegistered(
uint256 indexed facilityId,
uint16 cashRateBps,
uint16 pikRateBps,
uint64 nextCashPaymentDue,
uint64 nextPikCapitalization
);
/// @notice Existing earned interest moved from the virtual carrier to recorded face, without minting.
event AccruedLoanPosted(uint256 indexed facilityId, address indexed asset, uint256 amount, uint256 faceAfter);
/// @notice A keeper completed a chronological technical or signed contractual boundary.
event AccrualBoundaryProcessed(
uint256 indexed facilityId, uint64 indexed at, uint256 capitalized, uint64 nextDue, bool stopped
);
/// @notice The signed old curve closed before applying a forward amendment or declaration.
event AccrualLoanAligned(
uint256 indexed facilityId,
uint64 indexed closureNonce,
uint64 at,
uint256 positiveCorrection,
uint256 roundingLoss,
bool stopped
);
/// @notice Marked cohort membership changed without changing contractual interest entitlement.
event AccrualPastDueSet(uint256 indexed facilityId, bool marked);
/// @notice The remaining maximum future face reservation changed after a proved lifecycle event.
event AccrualCeilingReserved(uint256 indexed facilityId, uint256 previousCeiling, uint256 nextCeiling);
/// @notice Registers exactly the just-funded Active bridge facility; no caller supplies its terms.
function register(ReserveManager.ReserveStorage storage native, uint256 id) public {
ReserveAccrualStorageLib.State storage s = _fresh();
_caller(s.modules.waterfall);
if (s.identities[id].known) revert AccrualCredit_AlreadyKnown(id);
ClaimBridge.Facility memory f = ClaimBridge(s.modules.bridge).facility(id);
address asset = address(native.usdcToken);
if (f.state != ClaimBridge.LoanState.Active || asset == address(0) || native.deployed[id] != f.principal) {
revert AccrualCredit_InvalidFunding(id);
}
uint256 scale = ReserveStorageLib.USDC_SCALE;
uint64 at = ReserveAccrualStorageLib.now64();
bool mixed = s.mixedInterestEnabled && ClaimBridge(s.modules.bridge).isMixedInterest(id);
(AccrualLoans.Funding memory funded, uint16 cashRateBps, uint64 nextCashPaymentDue) =
_funding(s.modules.bridge, id, f, scale, at, mixed);
uint256 ceiling = ReserveAccrualLib.loanCeiling(
AccrualCeiling.Terms({
face: f.principal,
frozenBasis: f.principal,
scale: scale,
yearSeconds: funded.yearSeconds,
rateBps: f.interestRateBps,
at: at,
nextCapitalization: funded.nextPaymentDue,
paymentInterval: funded.paymentInterval,
maturity: f.maturity
}),
f.pik
);
_admitAdditional(s, ceiling - f.principal);
s.busy = true;
funded.balanceCeiling = ceiling;
if (mixed) s.loans.fundMixed(id, AccrualLoans.MixedFunding({pikTerms: funded, cashRateBps: cashRateBps}));
else s.loans.fund(id, funded);
s.identities[id] = ReserveAccrualStorageLib.Identity({
asset: asset,
classId: f.classId,
borrowerId: f.borrowerId,
stateId: f.stateId,
reservedCeiling: ceiling,
known: true
});
s.recordedFace += f.principal;
s.reservedCeilings += ceiling;
s.busy = false;
_emitRegistered(id, asset, f.classId, funded, f.interestRateBps);
if (mixed) {
emit MixedAccruingLoanRegistered(id, cashRateBps, funded.rateBps, nextCashPaymentDue, funded.nextPaymentDue);
}
}
/// @notice Bounded permissionless maintenance; does not mint or transfer every facility's face.
function checkpoint(ReserveManager.ReserveStorage storage native, uint256 maximum)
public
returns (uint256 processed, bool fresh)
{
ReserveAccrualLib.requireIdle();
ReserveAccrualStorageLib.State storage s = _enabled();
s.busy = true;
AccrualLoans.LifecycleWork[] memory work;
(work, processed, fresh) = s.loans.checkpoint(ReserveAccrualStorageLib.now64(), maximum);
for (uint256 i; i < processed; ++i) {
AccrualLoans.LifecycleWork memory item = work[i];
// A migrated legacy receipt can have advanced the native servicing date ahead
// of this coupon. Keep the bridge monotone while the canonical coupon clock catches up.
AccrualLoans.Loan storage loan = s.loans.loans[item.facilityId];
if (loan.mixed && item.previousDue != 0) {
IAccrualBridge(s.modules.bridge).setAccruedPaymentDue(item.facilityId, item.nextDue);
} else if (
item.nextDue != 0
&& item.nextDue > ClaimBridge(s.modules.bridge).facility(item.facilityId).nextPaymentDue
) {
IAccrualBridge(s.modules.bridge).setAccruedPaymentDue(item.facilityId, item.nextDue);
}
if (item.stopped) {
_replaceCeiling(
s,
item.facilityId,
native.deployed[item.facilityId] + ReserveAccrualStorageLib.facilityUnposted(item.facilityId)
);
}
emit AccrualBoundaryProcessed(item.facilityId, item.at, item.capitalized, item.nextDue, item.stopped);
}
s.busy = false;
}
/// @notice Neutral posting for a single facility before impairment or another native ledger act.
function post(ReserveManager.ReserveStorage storage native, uint256 id) public returns (uint256 amount) {
ReserveAccrualStorageLib.State storage s = _fresh();
_identity(s, id);
s.busy = true;
amount = s.loans.post(id, ReserveAccrualStorageLib.now64());
_post(native, s, id, amount);
s.busy = false;
}
/// @notice Constant-time earned additions to registry exposure; zero state has no jurisdiction book.
function exposure(uint8 kind, bytes32 key) public view returns (uint256 amount) {
if (kind > 3) revert AccrualCredit_InvalidExposureKind(kind);
if (kind == 0) return ReserveAccrualStorageLib.unposted();
ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
if (!s.enabled || (kind == 3 && key == bytes32(0))) return 0;
return s.loans.book.groupUnposted(keccak256(abi.encode(kind, key)), ReserveAccrualStorageLib.now64());
}
/// @notice Future face already reserved, additional to the current registry's effective exposure.
function reservedExposure() public view returns (uint256 amount) {
ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
if (!s.enabled) return 0;
uint256 effectiveFace = s.recordedFace + ReserveAccrualStorageLib.unposted();
return s.reservedCeilings > effectiveFace ? s.reservedCeilings - effectiveFace : 0;
}
/// @notice Growing marked-cohort face not yet included in the default manager's recorded mark.
function pastDue(uint256 classId) public view returns (uint256 amount) {
ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
if (!s.enabled) return 0;
return s.loans.book.pastDueInterest(keccak256(abi.encode(uint8(1), classId)), ReserveAccrualStorageLib.now64());
}
function _emitRegistered(
uint256 id,
address asset,
uint256 classId,
AccrualLoans.Funding memory funded,
uint16 aggregateRateBps
) private {
emit AccruingLoanRegistered(
id,
asset,
classId,
funded.principal,
funded.balanceCeiling,
aggregateRateBps,
uint32(funded.yearSeconds),
funded.fundedAt,
funded.pik
);
}
function _funding(
address bridgeAddress,
uint256 id,
ClaimBridge.Facility memory f,
uint256 scale,
uint64 at,
bool mixed
) private view returns (AccrualLoans.Funding memory funded, uint16 cashRateBps, uint64 nextCashPaymentDue) {
uint16 accrualRate = f.interestRateBps;
uint64 nextCapitalization = f.nextPaymentDue;
uint64 capitalizationInterval = f.paymentInterval;
if (mixed) {
BridgeMixedInterestLib.InterestLegTerms memory legs = ClaimBridge(bridgeAddress).mixedInterestTerms(id);
cashRateBps = legs.cashRateBps;
accrualRate = legs.pikRateBps;
nextCapitalization = legs.nextPikCapitalization;
capitalizationInterval = legs.pikCapitalizationInterval;
nextCashPaymentDue = legs.nextCashPaymentDue;
}
funded = AccrualLoans.Funding({
principal: f.principal,
balanceCeiling: 0,
scale: scale,
yearSeconds: _year(id, f.rateType, f.dayCountConvention, f.pik),
rateBps: accrualRate,
fundedAt: at,
nextPaymentDue: nextCapitalization,
paymentInterval: capitalizationInterval,
maturity: f.maturity,
pik: f.pik,
keys: ReserveAccrualStorageLib.keys(f.classId, f.borrowerId, f.stateId),
frozenPikBasis: 0
});
}
function _post(
ReserveManager.ReserveStorage storage native,
ReserveAccrualStorageLib.State storage s,
uint256 id,
uint256 amount
) private {
if (amount == 0) return;
ReserveAccrualStorageLib.Identity storage identity = s.identities[id];
native.deployed[id] += amount;
native.totalDeployedPrincipal += amount;
s.recordedFace += amount;
IAccrualRegistry(s.modules.registry).recordAccruedExposure(
identity.classId, identity.borrowerId, identity.stateId, amount
);
if (s.loans.book.entries[id].pastDue) IAccrualRisk(s.modules.defaultManager).onAccrualPosted(id, amount);
emit AccruedLoanPosted(id, identity.asset, amount, native.deployed[id]);
}
function _admitAdditional(ReserveAccrualStorageLib.State storage s, uint256 additional) private view {
uint256 effective = ICollateralRegistry(s.modules.registry).totalBookExposure();
uint256 future = reservedExposure();
if (
effective > MAX_EXPOSURE || future > MAX_EXPOSURE - effective
|| additional > MAX_EXPOSURE - effective - future
) {
revert AccrualCredit_ExposureCapacity();
}
}
function _replaceCeiling(ReserveAccrualStorageLib.State storage s, uint256 id, uint256 next) private {
uint256 previous = s.identities[id].reservedCeiling;
if (next > previous) _admitAdditional(s, next - previous);
s.reservedCeilings = s.reservedCeilings - previous + next;
s.identities[id].reservedCeiling = next;
emit AccrualCeilingReserved(id, previous, next);
}
function _year(uint256 id, ClaimBridge.RateType rate, ClaimBridge.DayCountConvention convention, bool pik)
private
pure
returns (uint32)
{
if (rate != ClaimBridge.RateType.Fixed) revert AccrualCredit_UnsupportedTerms(id);
if (convention == ClaimBridge.DayCountConvention.Actual360) return uint32(360 days);
if (!pik && convention == ClaimBridge.DayCountConvention.Actual365) return uint32(365 days);
revert AccrualCredit_UnsupportedTerms(id);
}
function _fresh() private view returns (ReserveAccrualStorageLib.State storage s) {
ReserveAccrualLib.requireFresh();
return _enabled();
}
function _enabled() private view returns (ReserveAccrualStorageLib.State storage s) {
s = ReserveAccrualStorageLib.state();
if (!s.enabled) revert ReserveAccrualLib.ReserveAccrual_NotEnabled();
}
function _identity(ReserveAccrualStorageLib.State storage s, uint256 id) private view {
if (!s.identities[id].known) revert AccrualCredit_Unknown(id);
}
function _caller(address expected) private view {
if (msg.sender != expected) revert AccrualCredit_WrongCaller(expected, msg.sender);
}
}
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.