Skip to main content

InterestRate.sol

InterestRate

Provides functions for managing interest rates.

The library allows for the initialization, appending, and removal of interest rate records, as well as calculating interest based on these records.

Record

struct Record {
uint256 annualRateBPS;
uint256 beginTimestamp;
}

Record type

NameTypeDescription
annualRateBPSuint256Annual interest rate in BPS
beginTimestampuint256Timestamp when the interest rate becomes effective

initialized

modifier initialized(struct InterestRate.Record[] self)

Ensure that the interest rate records have been initialized before certain functions can be called. It checks whether the length of the Record array is greater than 0. Throws an error with the code Errors.INTEREST_RATE_NOT_INITIALIZED if the array is empty (it indicates that the interest rate has not been initialized).

initialize

function initialize(struct InterestRate.Record[] self, uint256 initialInterestRate) internal

Initialize the interest rate records.

  • Parameters:

    NameTypeDescription
    selfstruct InterestRate.Record[]The stored record array
    initialInterestRateuint256The initial interest rate

appendRecord

function appendRecord(struct InterestRate.Record[] self, uint256 annualRateBPS, uint256 beginTimestamp) internal

Add a new interest rate record to the array.

Annual rate is not greater than the maximum rate and that the begin timestamp is in the future, and the new record's begin timestamp is greater than the previous record's timestamp. Throws an error with the code Errors.INTEREST_RATE_NOT_INITIALIZED if the array is empty. Throws an error with the code Errors.INTEREST_RATE_OVERFLOW if the rate exceed the maximum allowed rate (100%). Throws an error with the code Errors.INTEREST_RATE_PAST_TIMESTAMP if the timestamp is in the past, ensuring that the interest rate period has not already started. Throws an error with the code Errors.INTEREST_RATE_NOT_APPENDABLE if the timestamp is greater than the last recorded timestamp, ensuring that the new record is appended in chronological order.

  • Parameters:

    NameTypeDescription
    selfstruct InterestRate.Record[]The stored record array
    annualRateBPSuint256The annual interest rate in BPS
    beginTimestampuint256Begin timestamp of this record

removeLastRecord

function removeLastRecord(struct InterestRate.Record[] self) internal returns (bool removed, struct InterestRate.Record record)

Remove the last interest rate record from the array.

The current time must be less than the begin timestamp of the last record. If the array has only one record, it returns false along with an empty record. Otherwise, it removes the last record from the array and returns true along with the removed record. Throws an error with the code Errors.INTEREST_RATE_NOT_INITIALIZED if the array is empty. Throws an error with the code Errors.INTEREST_RATE_ALREADY_APPLIED if the beginTimestamp of the last record is not in the future.

  • Parameters:

    NameTypeDescription
    selfstruct InterestRate.Record[]The stored record array
  • Return Values:

    NameTypeDescription
    removedboolWhether the last record is removed
    recordstruct InterestRate.RecordThe removed record

findRecordAt

function findRecordAt(struct InterestRate.Record[] self, uint256 timestamp) internal view returns (struct InterestRate.Record interestRate, uint256 index)

Find the interest rate record that applies to a given timestamp.

It iterates through the array from the end to the beginning and returns the first record with a begin timestamp less than or equal to the provided timestamp. Throws an error with the code Errors.INTEREST_RATE_NOT_INITIALIZED if the array is empty.

  • Parameters:

    NameTypeDescription
    selfstruct InterestRate.Record[]The stored record array
    timestampuint256Given timestamp
  • Return Values:

    NameTypeDescription
    interestRatestruct InterestRate.RecordThe record which is found
    indexuint256The index of record

calculateInterest

function calculateInterest(struct InterestRate.Record[] self, uint256 amount, uint256 from, uint256 to) internal view returns (uint256)

Calculate the interest

Throws an error with the code Errors.INTEREST_RATE_NOT_INITIALIZED if the array is empty.

  • Parameters:

    NameTypeDescription
    selfstruct InterestRate.Record[]The stored record array
    amountuint256Token amount
    fromuint256Begin timestamp (inclusive)
    touint256End timestamp (exclusive)