Interest Payment
Overview
The Interest Payment contract provides interest-based payment schedules for loans backed by RestrictedLockupTokens.
The dividend functions (IDividends) are in the separate Dividends contract. They moved from InterestPayment to Dividends to make the InterestPayment contract smaller. Both contracts read the same SnapshotPeriods contract of the token.
Key Features
Interest Payment Features
- Configurable interest accrual start and end timestamps
- Ability to create multiple payment periods with different interest rates
- Admin can fund interest payments and force claim interest on behalf of token holders
- Token holders can claim interest based on their ownership during specific periods
- Support for principal amount funding and claiming at maturity
- Comprehensive admin controls for managing interest distributions
Usage
How It Works
Interest payments are distributed to recipients based on their proportional ownership of the RestrictedLockupToken at specific time periods and the configured interest rates for each period. The contract keeps track of token ownership through the SnapshotPeriods contract, which records token balances at different timestamps. Interest rates can be set differently for each payment period, allowing for flexible interest payment schedules. Role checks (e.g., Transfer Admin, Contract Admin) are routed to the external AccessControl contract.
- Anyone funds the interest payment contract with a payment token (e.g., USDC) through
fundInterest(). The call fills the period that contains the block time and the later periods. See "Paid periods". - Transfer Admin creates payment periods using
createPaymentPeriod(), defining the start and end timestamps, interest rate, and interest rate period duration for the period. - After a period finishes, the Transfer Admin sets its paid flag with
fundInterestForPeriod(amount, periodIdx). See "Paid periods". - Token holders can claim their interest by calling
claimInterest(amount)orclaimInterestForPeriod(periodIdx, amount)with an optional amount cap. - The contract calculates the amount of interest due based on the token holder's time-weighted ownership and the configured interest rate for each period.
- Interest is transferred to the token holder from the contract.
Paid periods
Each period has its own funds and a paid flag. A period pays its holders only from its own funds. The Transfer Admin sets the flag with fundInterestForPeriod when the funds cover the period. The flag is the trigger for a missed payment.
These terms apply:
- Payment clock:
nearestInterestPaymentTimestampAt(block.timestamp). The payment clock does not move back. - Finished period: a period with the payment clock at or after its end. A period that contains the accrual end is also finished when the payment clock is at the accrual end. A period that is not finished is an open period.
- Ongoing period: the first period with an end after the payment clock. Before the accrual start, it is period 0. When the payment clock is at the accrual end, no period is ongoing. The periods after the ongoing period are the later periods.
fundIntereststarts at the first period with an end after the block time instead (see "fundInterestfills the ongoing period and the later periods"). - Started period: a period with the payment clock after its start. A holder accrues in a period only after it starts. When the payment clock is at the start of the ongoing period, that period did not start.
reclaimTotalInterestuses the block time instead of the payment clock (see "Reclaims and the funds of a period"). - Funds of a period: all money that went into the period, less the money that
reclaimTotalInteresttook from it.periodFundedInterest(periodIdx)returns this value. A claim or a reclaim of a holder does not lower it. - Money outside all periods: the money that no period holds.
unassignedInterest()returns this value. - Projected total: the total of the period at its end. For a finished period, it is the period total. For an open period, see "The projected total".
- Need: the projected total less the funds of the period, or 0. For a period that is not finished, the need can also include a margin of 1 wei (see "The margin of 1 wei").
requiredInterestFunding(periodIdx)returns the need. - Surplus: the funds of the period less the projected total and the margin, or 0.
The flags are in period order. paidPeriodsCount() is the count of flags, and the next period to flag has this index.
The projected total
For an open period, the projected total is the total to date plus the interest of the rest of the period. The total to date stops at the payment clock. The rest has two parts:
- From the payment clock (or the start of the period, if later) to the block time: the recorded supply of each second, from the supply ledger (
SnapshotPeriods). - From the block time to the end of the period: the current supply.
The interest of the rest rounds up. These rules apply:
- The accrual end stops the projection. A period that starts at or after the accrual end has a projected total of 0.
- Before the accrual end, a pause-after time does not stop the projection. The projected total of a period with a stopped clock still counts the time to its end. The paused time before the block time counts at the recorded supply. From the accrual end (
block.timestamp >= interestAccrualEndTimestamp), the projected total stops at the stopped clock (see "A pause-after time at maturity"). - An end off the payment grid is not rounded down.
- A transfer does not change the supply, so it does not change the projected total.
- A mint raises the projected total of the ongoing period and the later periods from the mint time. A burn lowers it from the burn time.
- Inside the accrual window and without a pause-after time, the payment clock and the block time are in the same payment tick. With a tick of 1 second, they are equal, and the projection does not read the ledger.
The projected total is the total of the period at its end, or 1 wei above it. This rule applies if the supply does not change after the block time. The projected total is never below the total to date.
The margin of 1 wei
The projected total is the total to date, rounded down, plus the interest of the rest, rounded up. At the same supply, this sum can grow by 1 wei when the payment clock moves. Thus the need of an open period includes a margin of 1 wei. A period with a need of 0 pays the full share to each holder. These rules apply:
- The margin applies only when the interest of the rest of the period is above 0. A finished period has no margin, so its need is exact.
- At a later payment tick,
requiredInterestFundingof a filled period that is not finished can return 1 wei. The period still pays the full share. - The claims use the projected total without the margin (see "The pro-rata part").
reclaimTotalInterestdoes not take the funds of a started period below its projected total and the margin.
fundInterest fills the ongoing period and the later periods
Any account can call fundInterest(amount). A prefund through PurchaseContract also calls it. The call divides amount in this order:
- The period that contains the block time gets its need.
- Each later period gets its need, in period order.
- The rest goes to the money outside all periods.
The block time decides the first period, not the payment clock. The prefund of a purchase pays the minted tokens, and they accrue from the mint time. Without a pause-after time, the block time and the payment clock are in the same period, with one exception: a period with an end off the grid, from its end to the next grid point. These rules apply:
- A period that ended by the block time gets nothing from
fundInterest, also when it has no flag. - While a pause-after time stops the clock in an earlier period, that period gets nothing.
fundInterestForPeriodcan fill its need, but it sets no flag. See "Funds for the period with a stopped clock". - A period that starts at or after the accrual end gets nothing, because its projected total is 0.
- If no period ends after the block time, or the payment clock is at the accrual end, all of
amountgoes to the money outside all periods. For example, no period exists, or the last period ended. - A paused period (
pausePaymentPeriod) gets its need. The pause blocks only the claims of the holders. fundInterestdoes not move the money outside all periods into a period.
createPaymentPeriod moves the money outside all periods
createPaymentPeriod moves the money outside all periods into the new period, up to the need of the new period. The need uses the current supply. The rest stays outside all periods. This rule also applies to a new period that is already finished. reclaimTotalInterest can also take the money outside all periods (see "Reclaims and the funds of a period").
fundInterestForPeriod pays a finished period
Only the Transfer Admin can call fundInterestForPeriod(amount, periodIdx). The checks run in this order, before the transfer:
periodIdxmust bepaidPeriodsCount(). Otherwise the call reverts withInterestPayment_PeriodNotNextToPay(nextIdx).- The period must be finished, or a passed pause-after time must stop the clock inside it. Otherwise the call reverts with
InterestPayment_PeriodNotFinished(periodIdx). amountmust not be more than the need of the period. Otherwise the call reverts withInterestPayment_AmountAboveNeed(need).
Then amount goes into the funds of the period:
- If the funds then cover the period total, the call sets the flag and emits
PeriodPaid(periodIdx, funds). - If not, the money stays in the period without the flag. Each holder can claim a pro-rata part at once (see "The pro-rata part"). A later call with the rest of the need sets the flag.
- An amount of 0 sets the flag when the funds already cover the period, for example after prefunds filled it.
- For a period that the funds do not cover, an amount of 0 reverts with
InterestPayment_PeriodInterestNotFullyFunded(funds, required).requiredis the period total.
Funds for the period with a stopped clock. While a passed pause-after time stops the clock inside a period, fundInterestForPeriod accepts that period. The amount goes into its funds, up to its need, but the call sets no flag. Only a full period can be paid, and the paused time is not paid. These rules apply:
- Before the accrual end, the need counts the paused time, because the pause-after time can still move. After the accrual end, the need stops at the clock, and
reclaimTotalInterestcan take the funds of the paused time. - When the funds cover the projected total, each holder gets the full share up to the clock.
- A call that adds nothing reverts with
InterestPayment_PeriodNotFinished(periodIdx). isPaidThroughstays false for each time at or after the end of this period. An off-chain reader must also readpaymentPausedAfterTimestamp.- When the clock passes the end of the period, the usual checks apply. An amount of 0 then sets the flag if the funds cover the total.
- The periods between this period and the period of the block time get no funds:
fundIntereststarts at the block time, and check 1 rejects them. First move the pause-after time or unpause, then fund each finished period in period order.
Check 1 keeps the flags in period order. Thus fundInterestForPeriod can send money only to the next period to pay. fundInterest gives money only to open periods. While an older period has no flag, a later period pays only from the money that it got while it was open. The exception is a new period that is already finished: createPaymentPeriod can move money into it.
Flag a period
Do these steps after the end of each period:
- Make sure that the period is finished:
nearestInterestPaymentTimestampAt(now)is at or afterperiodEndTimestamp(periodIdx). - Make sure that
periodIdxis equal topaidPeriodsCount(). If an earlier period has no flag, flag it first. - Read
requiredInterestFunding(periodIdx). - Approve the payment token for the amount of step 3.
- Call
fundInterestForPeriod(amount, periodIdx)with the amount of step 3. Do not add a margin, because a larger amount reverts. - Make sure that
isPeriodPaid(periodIdx)returns true.
If step 3 returns 0, the prefunds filled the period. Then do step 5 with an amount of 0. The total of a finished period does not change, so the value of step 3 stays correct. A second call with the same index reverts with InterestPayment_PeriodNotNextToPay(nextIdx), so a retry does not send money to the next period.
To pay more than one period, call fundInterestForPeriods(amount, periodIdxs). The call makes one transfer. These rules apply:
- The list starts at
paidPeriodsCount()and goes up by 1. An empty list reverts withInterestPayment_InvalidPeriod. - The amount fills each listed period to its need, in list order, with the rules of
fundInterestForPeriod. - Send the sum of
requiredInterestFundingof the listed periods. An amount above this sum reverts withInterestPayment_AmountAboveNeed(sum). - A short amount is permitted only when the shortage is in the last listed period, and that period gets more than 0. That period keeps the money without the flag. If an earlier listed period does not get its full need, the next index reverts with
InterestPayment_PeriodNotNextToPay. - If one check fails, the whole call reverts.
Fund the figures that the contract reports. Do not fund the sum of the holder views. Each holder amount rounds down, so the sum can be less than the period total.
Reclaim the surplus of a finished period
A finished period can keep a surplus. Examples: the margin of 1 wei, or the funds above its total after a burn or a rate cut. The surplus of a finished period is periodFundedInterest(periodIdx) less periodTotalInterest(periodIdx), or 0. After the end of each period, select one of these two choices:
- Reclaim the surplus with
reclaimTotalInterest(amount). The call sends the money to the reclaimer address. The parts of the holders do not change. - Keep the surplus. The money stays in the period. No holder can claim it, and no other period can use it. A later
reclaimTotalInterestcan take it.
To reclaim the surplus, do these steps:
- Make sure that the period is finished (see "Flag a period").
- Read
periodFundedInterest(periodIdx)andperiodTotalInterest(periodIdx). The surplus is the difference, or 0. - If the surplus is more than 0, call
reclaimTotalInterest(surplus).
reclaimTotalInterest takes the money outside all periods first, and then the surplus of each earlier period. If one of these is more than 0, step 3 takes it first, and a part of the surplus stays in the period. reclaimInterestForAllRecipients does not take a surplus. It takes only the period total less the claimed and the reclaimed interest of the period.
The pro-rata part
Each period pays only from its own funds. If the funds of a period are at or above its projected total, each holder can claim the full share to date. If not, the part of a holder is accrued * funds / total. The amount that the holder already took from the period is subtracted from this value. These values apply:
accruedis the accrual of the holder in the period, up to the payment clock.fundsis the funds of the period.totalis the projected total of the period, without the margin of 1 wei.
A late claimant gets the same ratio as an early claimant. The sum of the accruals of all holders is not more than the projected total. Thus the sum of the parts is not more than the funds of the period. A period never pays from the funds of a different period.
A period with no funds pays nothing. A claim of that period alone reverts with InterestPayment_NoFundsToClaim. claimInterest and the other loops skip it. Thus a holder can claim a later period in full while an older period without funds pays nothing.
These events inside the ongoing period raise the projected total and lower the ratio:
- a mint without a prefund;
shiftInterestAccrualEndwhen the accrual end is inside the period.
Then an earlier claimant keeps a larger part. The funds of the period cap the sum of the payments. Money that comes later pays the rest: fundInterest for an open period, or fundInterestForPeriod for a finished period.
Limits of the projection
The projection counts the recorded supply up to the block time, and the current supply after it. A supply change after the block time changes the projection:
- A mint raises the projected total of the period that contains the mint time and of the later periods. These periods have a need again until
fundInterestfills them. Until then, the holders of a short period claim a pro-rata part. - A burn lowers the projected total. The funds above it are surplus, and
reclaimTotalInterestcan take them.
While a pause-after time is set, calculateInterestFor stops at that time, but the projected total does not (see "The prefund of a purchase").
No holder gets more than the share, and the contract keeps all of the money.
The ongoing period
To let the holders claim the full share to date, fund the period that contains the block time in advance with fundInterest. A purchase through PurchaseContract does this with its prefund. If the funds of that period are short, the holders claim a pro-rata part. After the end of the period, fundInterestForPeriod adds the rest of the need. The holders who claimed a part before then get the difference, and no holder gets a part two times.
PurchaseContract.executePurchaseWithInterest mints the tokens before it calls fundInterest. Thus the need of the period that contains the block time includes the new tokens, and the prefund fills that period first. For example, the ongoing period is full at 1,000 USDC for 1,000 tokens. At the start of the payment tick of day 15 of 30, a buyer gets 1,000 tokens with a prefund of 500 USDC. Then the period gets the 500 USDC and pays 100 %. With the prefund before the mint, the 500 USDC goes to the next period, and the ongoing period pays 66.7 %.
The prefund of a purchase
The projection counts a mint from the mint time. Thus count the prefund of a purchase from the mint time. Do these steps for each purchase:
- Read
end:periodEndTimestamp(lastIdx).lastIdxis the last period that the prefund must cover. - Set the prefund to
calculateInterestFor(mintAmount, now, end).nowis the block time at the computation.
These rules apply:
- The purchase executes after the computation. Thus the prefund is more than the need by the interest of the minted tokens for the time between them. The extra money goes to a later period or to the money outside all periods. A prefund from an earlier time, for example from the payment clock, puts more money outside all periods.
calculateInterestForrounds down in each period, but the projected total rounds up, and the need adds the margin of 1 wei. Thus each filled period can need a few wei.fundInterestorfundInterestForPeriodpays them.- While a pause-after time is set,
calculateInterestForstops at that time, but the projected total does not. Then the prefund of step 2 is less than the need.