Skip to content
Verified contractExactBUSL-1.1

ReserveAccrualViewsLib

Address0x7a0cbe736b1ed4ea52ce4edf24fdf0f5d5608244
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,446
Creation transaction
0xe6c10fa7f8…25161bfb8f

93 files

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

src/libraries/ReserveAccrualViewsLib.sol

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

import {ReserveManager} from "../ReserveManager.sol";
import {ReserveStorageLib} from "./ReserveStorageLib.sol";
import {IAccrualLifecycle} from "../interfaces/IAccrualLifecycle.sol";
import {AccrualLoans} from "./AccrualLoans.sol";
import {AccrualBook} from "./AccrualBook.sol";
import {ReserveAccrualStorageLib} from "./ReserveAccrualStorageLib.sol";
/// @notice Typed debt views and bounded coherent batches for the reserve-owned accrual book.

library ReserveAccrualViewsLib {
    using AccrualLoans for AccrualLoans.State;

    /// @notice A batch exceeds the reserve's existing 100-facility native admission bound.
    error ReserveAccrualViews_TooManyLoans(uint256 count);

    function debt(uint256 id) public view returns (bytes memory) {
        IAccrualLifecycle.Debt memory d;
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        if (!s.enabled || !s.identities[id].known) return abi.encode(d);
        AccrualLoans.Loan storage loan = s.loans.loans[id];
        (d.principal, d.interest, d.accruedThrough) = s.loans.loanFace(id, ReserveAccrualStorageLib.now64());
        d.balanceCeiling = loan.balanceCeiling;
        d.nextCapitalization = loan.nextCapitalization;
        d.maturity = loan.legalMaturity;
        d.pik = loan.pik;
        d.active = loan.active;
        d.known = true;
        return abi.encode(d);
    }

    /// @notice Separates contractual cash and PIK balances without scanning facilities or history.
    function interestLegs(uint256 id) public view returns (bytes memory) {
        IAccrualLifecycle.InterestLegs memory result;
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        if (!s.enabled || !s.identities[id].known) return abi.encode(result);
        AccrualLoans.Loan storage loan = s.loans.loans[id];
        uint256 interest;
        if (loan.mixed) {
            (result.cash, result.pik, result.accruedThrough) =
                s.loans.loanInterestLegs(id, ReserveAccrualStorageLib.now64());
            result.mixed = true;
        } else {
            (, interest, result.accruedThrough) = s.loans.loanFace(id, ReserveAccrualStorageLib.now64());
            if (loan.pik) result.pik = interest;
            else result.cash = interest;
        }
        result.known = true;
        return abi.encode(result);
    }

    /// @notice Recorded plus earned unposted native face, without enumerating facilities.

    function deployed(ReserveManager.ReserveStorage storage native, uint256 id, bool total)
        public
        view
        returns (uint256)
    {
        return total
            ? native.totalDeployedPrincipal + ReserveAccrualStorageLib.unposted()
            : native.deployed[id] + ReserveAccrualStorageLib.facilityUnposted(id);
    }

    /// @notice Unposted earned face for one facility, through the coherent portfolio frontier.
    function unpostedLoan(uint256 id) public view returns (uint256) {
        return ReserveAccrualStorageLib.facilityUnposted(id);
    }

    /// @notice Memoize the common frontier for a bounded batch, retaining exact per-loan arithmetic.
    /// @dev No transient writes are attempted from a STATICCALL. Each query observes current
    ///      state, so repayment, capitalization, checkpointing and servicing cannot leave a stale cache.
    function unpostedLoans(uint256[] calldata ids) public view returns (bytes memory) {
        if (ids.length > 100) revert ReserveAccrualViews_TooManyLoans(ids.length);
        uint256[] memory amounts = new uint256[](ids.length);
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        if (!s.enabled || ids.length == 0) return abi.encode(amounts);
        uint64 at = ReserveAccrualStorageLib.frontier(s);
        for (uint256 i; i < ids.length; ++i) {
            AccrualBook.Entry storage entry = s.loans.book.entries[ids[i]];
            if (entry.known) amounts[i] = entry.clock.value + entry.clock.rate * (at - entry.clock.at) - entry.posted;
        }
        return abi.encode(amounts);
    }

    /// @notice Native backing with a coherent delivery snapshot. Only recognition observes custody.
    function backing(ReserveManager.ReserveStorage storage native, bool recognized)
        public
        view
        returns (uint256 value)
    {
        ReserveAccrualStorageLib.State storage s = ReserveAccrualStorageLib.state();
        if (s.delivery.active) return recognized ? s.delivery.recognizedBacking : s.delivery.backing;
        value = ReserveStorageLib.backingValue(native);
        if (recognized) {
            uint256 live = native.usdcToken.balanceOf(address(this));
            uint256 shortfall =
                native.idleUSDCUnits > live ? ReserveStorageLib.normalize(native.idleUSDCUnits - live) : 0;
            value = value > shortfall ? value - shortfall : 0;
        }
    }
}
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.