Перейти к содержимому
Проверенный контрактТочноеBUSL-1.1

LossProtectionLib

Адрес0xa9b71284bc0184cd4ca230f7faefc5e8edd2e68f
Баланс

0 ETH

 

Транзакций

1

 

Компилятор

solc 0.8.30

 

Тот же код

Только этот контракт

 

Подробности

Компилятор
solc 0.8.30+commit.73712a01
Язык
solidity
Оптимизация
Да, 100 прогонов
Версия EVM
cancun
Лицензия
BUSL-1.1
Совпадение
Точноекод создания: да, код в сети: да
Проверен на Sourcify
Создан в блоке
26 459 377
Транзакция создания
0xe28b8951e1…2fd6ff6460

48 файлов

Исходный код большой: показывается один файл. Выберите файл в списке.

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);
    }
}
Настройки компилятораПоказать
{
  "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/"
  ]
}

Исходный код и метаданные: Sourcify.

Исходный код принадлежит авторам контракта.