Skip to content
Verified contractExactBUSL-1.1

ReserveRoundingLib

Address0x13cd7d9372c4c77aedc4cd8051f4c6abefa4db45
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,050
Creation transaction
0x597d06a236…3483e40c25

93 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/libraries/ReserveRoundingLib.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.30;

import {LossProtectionLib} from "./LossProtectionLib.sol";
import {ReserveManager} from "../ReserveManager.sol";

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ICuratorModule} from "../interfaces/ICuratorModule.sol";
import {IMintRedeemController} from "../interfaces/IMintRedeemController.sol";
import {IsUSDfr} from "../interfaces/IsUSDfr.sol";
import {IReserveManager} from "../interfaces/IReserveManager.sol";
import {IAccrualRoundingRisk, IAccrualRoundingRegistry} from "../interfaces/IAccrualLifecycle.sol";
import {AccrualBook} from "./AccrualBook.sol";
import {AccrualLoans} from "./AccrualLoans.sol";
import {ReserveAccrualStorageLib} from "./ReserveAccrualStorageLib.sol";
import {ReserveAccrualLib} from "./ReserveAccrualLib.sol";
import {ReserveStorageLib} from "./ReserveStorageLib.sol";
import {ReserveCreditLib} from "./ReserveCreditLib.sol";
import {ICascadeBackstop} from "../interfaces/ICascadeBackstop.sol";

/// @title ReserveRoundingLib
/// @notice Allocates a proved sub-native-unit contractual discrepancy through Ethereum's native three-layer loss order.
/// @dev Gross earned income and both earned fee claims are retained. This library owns no independent
///      write-off authority: the reserve supplies its just-derived, nonce-bound lifecycle work.
library ReserveRoundingLib {
    /// @dev One in-memory allocation; no independent accounting or storage namespace.
    struct Allocation {
        uint256 prepaid;
        uint256 curator;
        uint256 backstop;
        uint256 senior;
        uint256 unabsorbed;
        uint256 mark;
    }

    using AccrualBook for AccrualBook.Book;
    using ReserveStorageLib for ReserveManager.ReserveStorage;

    error AccrualRounding_InvalidContinuation();
    error AccrualRounding_DeltaMismatch(uint8 measurement, uint256 expected, uint256 actual);
    error AccrualRounding_InsufficientGas();

    /// @notice The exact discrepancy, its native loss allocation, and the explicitly unabsorbed remainder.
    event AccrualRoundingAllocated(
        uint256 indexed facilityId,
        uint64 indexed closureNonce,
        uint256 amount,
        uint256 prepaid,
        uint256 curator,
        uint256 backstop,
        uint256 senior,
        uint256 unabsorbed,
        uint256 markConsumed
    );

    /// @notice Reconciles interpolation and delivers the vault's pre-loss claims before closing the loan.
    /// @dev Preparation exposes a coherent pre-loss book; the reserve's outer operation guard remains
    ///      held. No receipt, basis change, face write-down or burn has occurred during these callbacks.
    ///      A separate fee recipient retains its earned claim for permissionless delivery. Its transfer
    ///      restrictions must not block repayment or senior loss allocation. If the vault also owns
    ///      the fee claim, both legs must be physical before its assets can fund a loss.
    function prepare(ReserveManager.ReserveStorage storage native, uint256 id) public {
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        AccrualLoans.Loan storage loan = s.loans.loans[id];
        if (loan.terms.scale == 1 || !s.loans.book.entries[id].scheduled) return;
        uint64 at = ReserveAccrualStorageLib.now64();
        s.loans.book.reconcile(id, at);
        uint256 recognized = s.loans.book.earned(id, at) - loan.segmentBookBase;
        (uint256 canonical,) = AccrualLoans.canonicalSegment(loan, at);
        if (recognized <= canonical) return;
        ReserveAccrualLib.prepareNativeLoss(native);
        IsUSDfr(s.modules.vault).accrueFees();
    }

    /// @notice Consumes one armed burn; all unrelated controller operations retain the busy gate.
    function consumeBurn(address caller, address from, uint256 amount) public returns (bool) {
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        if (msg.sender != s.modules.controller) revert AccrualRounding_InvalidContinuation();
        ReserveAccrualStorageLib.Rounding storage r = s.rounding;
        if (!r.active) return false;
        if (
            !s.busy || s.delivery.active || !r.ready || caller != address(this) || from != r.from || amount != r.amount
                || amount == 0
        ) revert AccrualRounding_InvalidContinuation();
        r.ready = false;
        return true;
    }

    /// @notice Uses prepaid marks, this facility's curator class, then sGROVE and clamped senior assets.
    function allocate(ReserveManager.ReserveStorage storage native, AccrualLoans.LifecycleWork memory work) public {
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        uint256 loss = work.roundingLoss;
        AccrualLoans.Loan storage loan = s.loans.loans[work.facilityId];
        if (
            !s.busy || s.delivery.active || s.rounding.active || loss == 0 || loss >= loan.terms.scale
                || work.closureNonce != loan.closureNonce || _unissuedVaultAssets(s, work.at) != 0
        ) revert AccrualRounding_InvalidContinuation();
        uint256[5] memory before_ = _measure(s, native);
        Allocation memory allocation;
        allocation.mark = native.principalImpairment[work.facilityId];
        if (allocation.mark > loss) allocation.mark = loss;
        allocation.prepaid = native.exitPrepaidAbsorption;
        if (allocation.prepaid > allocation.mark) allocation.prepaid = allocation.mark;
        if (allocation.prepaid != 0) {
            native.exitPrepaidAbsorption -= allocation.prepaid;
            emit IReserveManager.ExitPrepaymentConsumed(
                work.facilityId, allocation.prepaid, native.exitPrepaidAbsorption
            );
        }
        s.rounding.facilityId = work.facilityId;
        s.rounding.closureNonce = work.closureNonce;
        s.rounding.active = true;
        allocation.curator = _curator(s, native, work.facilityId, loss - allocation.prepaid);
        allocation.backstop = _backstop(s, native, work.facilityId, loss - allocation.prepaid - allocation.curator);
        if (allocation.curator + allocation.backstop != 0) {
            _burn(s, address(this), allocation.curator + allocation.backstop);
        }
        allocation.senior = loss - allocation.prepaid - allocation.curator - allocation.backstop;
        uint256 available = IsUSDfr(s.modules.vault).totalAssets();
        if (allocation.senior > available) allocation.senior = available;
        if (allocation.senior != 0) _burn(s, s.modules.vault, allocation.senior);
        allocation.unabsorbed = loss - allocation.prepaid - allocation.curator - allocation.backstop - allocation.senior;
        s.roundingUnabsorbed += allocation.unabsorbed;
        ReserveCreditLib.writeDown(native, work.facilityId, loss);
        s.recordedFace -= loss;
        // Fractional write-offs are facility losses too. Record only capital actually
        // consumed, including prior junior prepayment, before the authenticated risk hook.
        LossProtectionLib.record(allocation.prepaid + allocation.curator + allocation.backstop);
        _notify(s, work.facilityId, loss);
        uint256[5] memory after_ = _measure(s, native);
        _equal(0, before_[0] - allocation.curator - allocation.backstop - allocation.senior, after_[0]);
        _equal(1, before_[1], after_[1]);
        _equal(2, before_[2] - allocation.senior, after_[2]);
        uint256 reduction = loss - allocation.mark;
        _equal(3, before_[3] > reduction ? before_[3] - reduction : 0, after_[3]);
        _equal(7, before_[4], after_[4]); // Outstanding fee claims are neither canceled nor spent.
        delete s.rounding;
        emit AccrualRoundingAllocated(
            work.facilityId,
            work.closureNonce,
            loss,
            allocation.prepaid,
            allocation.curator,
            allocation.backstop,
            allocation.senior,
            allocation.unabsorbed,
            allocation.mark
        );
    }

    function _curator(
        ReserveAccrualStorageLib.State storage s,
        ReserveManager.ReserveStorage storage native,
        uint256 id,
        uint256 loss
    ) private returns (uint256 absorbed) {
        if (loss == 0) return 0;
        uint256 classId = s.identities[id].classId;
        ICuratorModule curator = native.lossCurator;
        uint256 expected = curator.poolBalance(classId);
        if (expected > loss) expected = loss;
        uint256 before_ = IERC20(s.modules.token).balanceOf(address(this));
        uint256 residual;
        (absorbed, residual) = curator.absorbLoss{gas: _forwardGas()}(classId, loss);
        _equal(4, expected, absorbed);
        _equal(5, loss - absorbed, residual);
        _equal(6, before_ + absorbed, IERC20(s.modules.token).balanceOf(address(this)));
    }

    /// @dev sGROVE's recorded executable reserve is drawn only after this class's curator capital.
    function _backstop(
        ReserveAccrualStorageLib.State storage s,
        ReserveManager.ReserveStorage storage native,
        uint256 id,
        uint256 loss
    ) private returns (uint256 covered) {
        if (loss == 0) return 0;
        ICascadeBackstop backstop = native.lossBackstop;
        uint256 expected = backstop.remainingCoverage(id);
        if (expected > loss) expected = loss;
        uint256 balance = IERC20(s.modules.token).balanceOf(address(this));
        covered = backstop.coverShortfall{gas: _forwardGas()}(id, loss);
        _equal(8, expected, covered);
        _equal(9, balance + covered, IERC20(s.modules.token).balanceOf(address(this)));
    }

    function _burn(ReserveAccrualStorageLib.State storage s, address from, uint256 amount) private {
        s.rounding.from = from;
        s.rounding.amount = amount;
        s.rounding.ready = true;
        IMintRedeemController(s.modules.controller).burnLoss{gas: _forwardGas()}(from, amount);
        if (s.rounding.ready) revert AccrualRounding_InvalidContinuation();
    }

    function _notify(ReserveAccrualStorageLib.State storage s, uint256 id, uint256 amount) private {
        ReserveAccrualStorageLib.Identity storage identity = s.identities[id];
        IAccrualRoundingRegistry(s.modules.registry).recordAccruedWriteDown(
            identity.classId, identity.borrowerId, identity.stateId, amount
        );
        IAccrualRoundingRisk(s.modules.defaultManager).onAccrualRounding(id, amount);
    }

    function _measure(ReserveAccrualStorageLib.State storage s, ReserveManager.ReserveStorage storage native)
        private
        view
        returns (uint256[5] memory values)
    {
        IERC20 token = IERC20(s.modules.token);
        values[0] = token.totalSupply();
        values[1] = token.balanceOf(address(this));
        values[2] = token.balanceOf(s.modules.vault);
        values[3] = native.backingValue();
        values[4] = s.loans.book.snapshot(ReserveAccrualStorageLib.now64()).unissued;
    }

    function _unissuedVaultAssets(ReserveAccrualStorageLib.State storage s, uint64 at)
        private
        view
        returns (uint256 owned)
    {
        AccrualBook.Snapshot memory claims = s.loans.book.snapshot(at);
        owned = claims.seniorUnissued;
        if (s.feeRecipient == s.modules.vault) owned += claims.feeUnissued;
    }

    function _forwardGas() private view returns (uint256) {
        uint256 available = gasleft();
        uint256 retained = available >> 3;
        if (retained < 450_000) retained = 450_000;
        if (available <= retained + 200_000) revert AccrualRounding_InsufficientGas();
        return available - retained;
    }

    function _equal(uint8 measurement, uint256 expected, uint256 actual) private pure {
        if (expected != actual) revert AccrualRounding_DeltaMismatch(measurement, expected, actual);
    }
}
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.