Verified contractExactBUSL-1.1
LossProtectionLib
Address0xa9b71284bc0184cd4ca230f7faefc5e8edd2e68f
- 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,377
- Creation transaction
- 0xe28b8951e1…2fd6ff6460
48 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/libraries/LossProtectionLib.solmain5.39 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/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/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/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/solady/src/utils/FixedPointMathLib.sol55.69 KB
- src/interfaces/IComplianceRegistry.sol2.52 KB
- src/interfaces/IContinuousAccrual.sol4.69 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/IRevisionedImpairmentSource.sol2.79 KB
- src/interfaces/IsUSDfr.sol14.06 KB
- src/libraries/Config.sol23.1 KB
- src/libraries/PointsHookGas.sol2.73 KB
- src/libraries/Roles.sol4.2 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/LossProtectionLib.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;
import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {IDefaultManager} from "../interfaces/IDefaultManager.sol";
import {ILossProtection} from "../interfaces/ILossProtection.sol";
import {IsUSDfr} from "../interfaces/IsUSDfr.sol";
import {SUSDfr} from "../sUSDfr.sol";
import {Roles} from "./Roles.sol";
/// @notice Records consumed loan-loss protection and carries it into the vault's fee basis.
/// @dev Delegated by DefaultManager and sUSDfr in their separate proxy contexts. Hosts retain
/// authorization and reentrancy guards. Operational hosts never expose an arbitrary-amount writer;
/// the manager has one separate, governed historical-basis initialization.
library LossProtectionLib {
/// @custom:storage-location erc7201:forestroad.storage.LossProtection
struct State {
uint256 recorded;
mapping(address source => uint256 amount) seen;
bool historicalInitialized;
}
bytes32 private constant LOSS_PROTECTION_STORAGE_LOCATION =
0xf64a7ee24f8409b017a7c1aebfd9f66f7a715ea2eb41b4abdf03dba6bd218a00;
/// @notice The loss-accounting namespace belonging to the calling proxy.
function _state() private pure returns (State storage $) {
assembly {
$.slot := LOSS_PROTECTION_STORAGE_LOCATION
}
}
/// @notice Historical protection can be credited only once by the governed host.
error LossProtection_HistoricalAlreadyInitialized();
/// @notice Marks a fresh zero history or adds one evidenced legacy capital adjustment.
/// @dev Adds to any already recorded prospective loss rather than overwriting it. The
/// host authenticates governance; the vault subsequently pulls only the counter delta.
function initializeHistorical(uint256 capital, bytes32 evidence) public {
State storage s = _state();
if (s.historicalInitialized) revert LossProtection_HistoricalAlreadyInitialized();
s.historicalInitialized = true;
s.recorded += capital;
emit IDefaultManager.HistoricalProtectedCapitalInitialized(capital, evidence);
}
/// @notice Whether the manager's one-time historical adjustment has been consumed.
function historicalInitialized() public view returns (bool) {
return _state().historicalInitialized;
}
/// @notice Records only protection already consumed in an atomic facility write-off.
function record(uint256 capital) public {
_state().recorded += capital;
}
/// @notice Exposes the host's monotonically increasing record without caller-supplied data.
function cumulative() public view returns (uint256) {
return _state().recorded;
}
/// @notice Merges a bound reserve's actual fractional-loss capital into the manager record.
/// @dev Called only after the manager's existing reserve-only continuation authenticates
/// the source. Pulling a monotonic record supplies no caller-controlled financial amount.
function importFrom(address source) public returns (uint256 capital) {
State storage s = _state();
uint256 total = ILossProtection(source).cumulativeProtectedCapital();
uint256 previous = s.seen[source];
if (total < previous) revert IsUSDfr.SUSDfr_LossProtectionCounterDecreased(source, previous, total);
s.seen[source] = total;
capital = total - previous;
s.recorded += capital;
}
/// @notice Pulls the authenticated loss actor's cumulative record and credits only its delta.
/// @dev The vault additionally requires FEE_ACCOUNTING_ROLE, an idle accounting state and its
/// reentrancy guard. A fee actor alone cannot invent a controller: that controller must
/// be an actual underlying-token minter, and the actor must hold its LOSS_BURNER_ROLE.
/// Empty-supply losses consume the record without gifting a hurdle to future investors.
/// Rounding up can only exclude a sub-share-unit residue from performance fees.
function sync(
SUSDfr.SUSDfrStorage storage $,
address token,
address controller,
uint256 supply,
uint256 virtualShares,
uint256 shareUnit
) public {
if (
!IAccessControl(token).hasRole(Roles.MINTER_ROLE, controller)
|| !IAccessControl(controller).hasRole(Roles.LOSS_BURNER_ROLE, msg.sender)
) revert IsUSDfr.SUSDfr_InvalidLossProtectionSource(msg.sender, controller);
uint256 total = ILossProtection(msg.sender).cumulativeProtectedCapital();
State storage s = _state();
uint256 previous = s.seen[msg.sender];
if (total < previous) revert IsUSDfr.SUSDfr_LossProtectionCounterDecreased(msg.sender, previous, total);
s.seen[msg.sender] = total;
uint256 credit = total - previous;
uint256 adjusted = $.highWaterMark;
if (supply != 0) adjusted += Math.mulDiv(credit, shareUnit, supply + virtualShares, Math.Rounding.Ceil);
if (adjusted != $.highWaterMark) {
emit IsUSDfr.HighWaterMarkAdjusted($.highWaterMark, adjusted);
$.highWaterMark = adjusted;
}
emit IsUSDfr.LossProtectionBasisCredited(msg.sender, credit, adjusted);
}
}
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.