Skip to content

fix: correct rate scaling, liquidity accounting, and code clarity issues#2

Draft
FabianSanchezD with Copilot wants to merge 4 commits into
feat/protocol-updatefrom
copilot/sub-pr-1
Draft

fix: correct rate scaling, liquidity accounting, and code clarity issues#2
FabianSanchezD with Copilot wants to merge 4 commits into
feat/protocol-updatefrom
copilot/sub-pr-1

Conversation

Copilot AI commented Jan 3, 2026

Copy link
Copy Markdown

Addressed unresolved review feedback regarding scaling inconsistencies, state management bugs, and code clarity issues in the lending protocol contracts.

Rate Scaling Fix

Problem: Interest calculation divided by YEAR_MS only, ignoring the 1e9 rate scaling factor. With base_rate = 10_000_000_000 (10% scaled), this calculated 10 billion percent interest instead of 10%.

Solution: Created INTEREST_DENOMINATOR = YEAR_MS × RATE_SCALE constant for proper scaling:

// Before: interest = borrowed × rate × elapsed / YEAR_MS
// After: interest = borrowed × rate × elapsed / (YEAR_MS × RATE_SCALE)
const INTEREST_DENOMINATOR: u128 = 31_557_600_000_000_000_000u128;
let interest = (total_borrowed as u128)
    .checked_mul(rate as u128)
    .and_then(|v| v.checked_mul(elapsed as u128))
    .and_then(|v| v.checked_div(INTEREST_DENOMINATOR))
    .unwrap_or(0) as Balance;

Liquidity Accounting Fix

Problem: disburse() decreased total_liquidity and receive_repayment() increased it, double-counting interest already added by accrue_interest().

Solution: total_liquidity now represents total pool value (available + borrowed). Only accrue_interest() modifies it:

  • disburse(): increases total_borrowed, total_liquidity unchanged
  • receive_repayment(): decreases total_borrowed, total_liquidity unchanged (interest already accrued)

Other Fixes

  • DEFAULT_BOOST: Changed from 2_000_000_000 to 2 (raw star count, not scaled value)
  • Removed underscore from purpose parameter with clarifying comment
  • Fixed unused mut warning in disburse function
  • Moved rate constants to module level

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 3, 2026 23:52
…larity

Co-authored-by: FabianSanchezD <85092684+FabianSanchezD@users.noreply.github.com>
Co-authored-by: FabianSanchezD <85092684+FabianSanchezD@users.noreply.github.com>
Co-authored-by: FabianSanchezD <85092684+FabianSanchezD@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor contract logic for protocol update fix: correct rate scaling, liquidity accounting, and code clarity issues Jan 4, 2026
Copilot AI requested a review from FabianSanchezD January 4, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants