Library for managing loan payments. Each payment has a principal amount that is due at the end of the maturity period and defaults after the grace period. An interest rate is applied linearly to the principal amount over time until the payment is due. A premium rate is added to the rate thereafter.

State Variables

INTEREST_SCALE

uint256 private constant INTEREST_SCALE = 1e4;

YEAR_IN_SECONDS

uint256 private constant YEAR_IN_SECONDS = 365 days;

Functions

matured

Whether the principal is due at a given time.

function matured(Payment memory self, uint48 at) internal pure returns (bool);

maturedAt

The time when the principal is due.

function maturedAt(Payment memory self) internal pure returns (uint48);

defaulted

Whether the payment is defaulted.

function defaulted(Payment memory self) internal view returns (bool);

defaulted

Whether the payment is defaulted at a given time.

function defaulted(Payment memory self, uint48 at) internal pure returns (bool);

defaultedAt

The time when the payment is defaulted.

function defaultedAt(Payment memory self) internal pure returns (uint48);

accruedInterest

The total amount of interest accrued. This includes both regular and premium interest.

function accruedInterest(Payment memory self, uint48 at) internal pure returns (uint256);

regularAccruedInterest

The total amount of regular interest accrued.

function regularAccruedInterest(Payment memory self, uint48 at) internal pure returns (uint256);

premiumAccruedInterest

The total amount of premium interest accrued.

function premiumAccruedInterest(Payment memory self, uint48 at) internal pure returns (uint256);

_accruedInterest

Linear interpolation of the interest linearly accrued between two timepoints. Precision is maintained by scaling the rate by 1e4.

function _accruedInterest(Payment memory self, uint88 rate, uint48 since, uint48 at) private pure returns (uint256);

Structs

Payment

struct Payment {
    uint96 principal;
    uint48 fundedAt;
    uint32 maturityPeriod;
    uint32 gracePeriod;
    uint24 interestRate;
    uint24 premiumRate;
}