Lodestar lending contracts refresh accrued debt before processing repayments
Lodestar lending contracts accrue market interest before applying a repayment to the borrower's debt balance. The Arbitrum protocol's published CToken implementation updates its borrow index, total borrows, and reserves from the last accrual checkpoint. It then calculates the account balance, which can exceed an earlier reading when additional interest has accrued.
Bottom line: In an ERC-20 market, repayment reduces refreshed debt by the underlying tokens actually received, which may differ from the requested amount.
CToken holds the checkpoint while the rate model supplies the rate
Each lending market keeps its own accrual checkpoint and borrow index in its CToken contract. The interest-rate model supplies a rate using that market's cash, total borrows, and reserves. The Comptroller separately checks whether the requested action satisfies its rules. Together, these components connect a market-wide interest calculation to an individual repayment. The account's own debt snapshot records its saved balance and the market index at that snapshot. A checkpoint in one market therefore does not establish freshness for every balance that an account holds.
Elapsed blocks determine the next accrual interval
Accrual advances the market from its stored block checkpoint to the block in which the calculation executes. The contract subtracts
accrualBlockNumber
from its current block number to obtain the elapsed interval.
Before changing storage,
accrueInterest
reads the market's cash, existing borrows, reserves, and borrow index. It asks the configured interest-rate model for the per-block borrow rate using cash, total borrows, and reserves. The rate therefore uses the state before repayment brings additional cash into the market. Repayment changes cash and total borrows after this calculation, which can alter the rate used at a later checkpoint.
For that interval, the implementation multiplies the per-block rate by the number of elapsed blocks. It applies this simple-interest factor to stored total borrows, then adds the resulting interest to debt. The returned rate applies across that interval; the function does not reconstruct a separate historical rate for each block. The reserve factor determines how much accrued interest enters reserves. The same interval factor increases the market borrow index. Fixed-point scaling and integer rounding form part of the Solidity calculation, so a decimal calculation may not reproduce every smallest-unit result.
Afterward, the contract stores the new block checkpoint, index, total borrows, and reserves. An
AccrueInterest
event records the calculation's cash input, accumulated interest, updated index, and updated total borrows.
If another accrual call reaches the same market in the same block, the function returns without adding another interval. Translating that block interval into clock time requires a separate block-timing assumption.
The borrow index carries market interest into account balances
The account's debt comes from its saved borrow balance multiplied by the market index, divided by its saved index. This calculation lets an account reflect market interest without writing a separate interest update to every borrower. An accrual call changes shared market state; the balance calculation applies that state to the selected account. A zero saved balance returns zero without dividing by an account index.
The field named
principal
holds the balance saved at the account's latest borrow or repayment update. That value can already include earlier
accrued interest. A further borrow adds new debt to the indexed balance, while repayment subtracts the amount received. Both actions save the resulting account balance with the market's borrow index. Comparing that field directly with the original amount borrowed can misidentify accrued interest as unexplained new borrowing.
Repayment applies the received amount after refreshing debt
The published CToken repayment path calls accrueInterest before reading the borrower's indexed debt for the selected market.
Its
repayBorrowFresh
function checks Comptroller permission and requires its checkpoint to match the execution block. Accrual and the payment occur within the same repayment call, without requiring a separate preliminary accrual transaction. A fixed repayment equal to an older balance can therefore leave debt when interest has increased the balance since that reading.
For an ERC-20 market, CErc20 measures how much underlying the contract receives during transfer. That received amount reduces both the borrower's refreshed debt and the market's total borrows. The requested transfer amount and credited repayment can differ when the underlying token charges a transfer fee. The
RepayBorrow
event distinguishes the actual repayment from the remaining account debt.
Why can a stored balance differ from a current balance?
A stored balance uses the market's saved borrow index, while a current-balance call runs accrual before calculating debt. The distinction concerns market freshness, even when both calculations address the same borrower.
borrowBalanceStored
does not advance the checkpoint;
borrowBalanceCurrent
runs accrual as part of its execution. A previously returned current balance also belongs to its execution context. Later blocks can add interest, so the word current does not turn a returned amount into a lasting repayment quote.
Accrued reserves and supplier claims use the same market totals
Accrual also changes supplier accounting because the market-token exchange rate includes total borrows after interest. Where market-token supply is nonzero, the exchange rate derives from cash plus total borrows, minus reserves, divided by market-token supply. The reserve allocation removes its share of accrued interest from the value attributed to suppliers. This connects the borrower's growing debt with the lending market's deposit claims without treating every unit of borrower interest as supplier income.
Supplier market tokens, called lTokens, represent claims on underlying assets. Their exchange rate differs from the borrow index that scales account debt. A higher claim value also does not create immediately available cash. Redemption still needs sufficient market liquidity and, when the deposit supports borrowing, permission under the account's collateral rules. LODE staking revenue is another use of protocol reserves; it remains separate from the account's debt-index calculation.
Parameter changes preserve a checkpoint between configurations
Replacing the configured interest-rate model through
_setInterestRateModel
first accrues interest with the existing model. A successful authorized update then installs the replacement. The setter also requires a fresh market checkpoint and a contract that identifies itself as an interest-rate model.
The reserve-factor setter follows the same accrue-before-change pattern. These market setters preserve the existing configuration for the interval that they accrue. Replacing the model changes the rate calculation, while changing the reserve factor changes the share of accrued interest allocated to reserves.
A repayment estimate needs matching market and account state
To understand why a recorded repayment leaves debt, compare the payment credit with the refreshed account balance. A stored reading and a repayment event can describe different checkpoints.
-
If the displayed figure comes from
borrowBalanceStored, associate it with the market's saved index. - If additional blocks elapsed before repayment, include the accrual that precedes the debt reduction.
- If the ERC-20 transfer delivers less than requested, use the received amount as the repayment credit.
-
If
RepayBorrowshows remaining debt, distinguish that balance from the event's actual repayment amount. - If the repayment call reverts, do not treat its attempted accrual or transfer as a completed payment.
Freshness guards and transfer checks can still stop repayment
A repayment needs permission, a fresh checkpoint, and a successful incoming transfer before its debt reduction can complete. The ERC-20 path can revert when the payer lacks enough underlying tokens or the required spending allowance. The accrual function also rejects a borrow rate above its internal maximum. Those failures concern different stages, so a freshness check alone does not explain every failed call.
A reverted repayment call rolls back changes within that call, including its earlier accrual. A later successful interaction can then accrue from the last checkpoint that actually persisted.
RepayBorrow
records the amount credited and the remaining account debt. An
AccrueInterest
event alone cannot establish that underlying tokens reached the market as a repayment.
Lodestar: common questions
How does type(uint).max work in an ERC-20 repayment?
The published CErc20 repayment path treats type(uint).max as a request to select the borrower's full debt after accrual. This special input avoids fixing the transfer request to an earlier balance. The payer still needs sufficient underlying tokens and allowance; a token transfer deduction can leave residual debt even with this input.
What happens if an ERC-20 repayment credits more than the outstanding debt?
A repayment that credits more than the refreshed account debt reverts when the debt subtraction underflows. The relevant comparison uses the amount actually received by the contract. The ordinary fixed-amount branch does not automatically cap that input at the outstanding balance, so an oversized requested payment is not a general method for closing debt.
When does repayment stop borrowing interest in the selected market?
Once repayment reduces that market's saved debt to zero, the account accrues no further borrowing interest there unless it borrows again. Market accrual can continue for other borrowers. Debt in another market remains subject to its own accounting, while supply earnings and LODE staking rewards follow separate mechanisms.
Which units does the contract expect for a fixed repayAmount?
For a fixed payment through the published CErc20 interface, repayAmount uses the underlying token's smallest units, so an integration must apply that token's decimals when encoding the payment; lToken balances use separate token units and cannot substitute directly for the underlying repayment amount.
Is an interface's annual rate the input to accrueInterest?
The accrual function obtains a fixed-point per-block rate directly from its configured model, without accepting a caller-supplied annual percentage. An interface can annualize rates for display, but that calculation is separate. Applying the displayed annual percentage to an elapsed block count would mix incompatible units.
Does earning LODE incentives reduce the debt used in repayment calculations?
Accruing LODE incentives does not subtract them from the CToken loan balance. The debt calculation uses the borrower's saved balance and the market borrow index. A repayment credit depends on underlying assets received by the lending contract. Reward accounting remains separate from the balance that the repayment function reduces.
Updated on