Skip to content

Add configurable interest rate models#407

Open
TUPM96 wants to merge 1 commit into
Smartdevs17:mainfrom
TUPM96:codex/interest-rate-models-357
Open

Add configurable interest rate models#407
TUPM96 wants to merge 1 commit into
Smartdevs17:mainfrom
TUPM96:codex/interest-rate-models-357

Conversation

@TUPM96
Copy link
Copy Markdown

@TUPM96 TUPM96 commented May 25, 2026

Closes #357

Summary

  • Adds pluggable interest rate model infrastructure for lending with Linear, Kink, Jump, and Exponential formulas.
  • Stores the active model in InterestRateConfig and adds a governance update API through update_interest_rate_model.
  • Wires the lending interest-rate/risk-monitor modules into the contract API and re-exports config/model types for generated clients.
  • Extends the hello-world interest-rate module with the same model choices and switch API.
  • Adds formula tests, lending client model-switch tests, docs, and benchmark entries for each model.

Validation

  • cargo check --manifest-path stellar-lend/contracts/lending/Cargo.toml --lib
  • cargo test --manifest-path stellar-lend/contracts/lending/Cargo.toml interest_rate --no-run (still blocked by pre-existing borrow_test/math_safety_test harness errors unrelated to this change)
  • cargo check -p stellarlend-benchmarks (still blocked by pre-existing hello-world duplicate definitions/stale tests)
  • git diff --cached --check

Notes

  • Existing variable-rate debt reads the active model at accrual time, so model switches migrate lazily.
  • Existing stable-rate debt keeps its stored stable_rate_bps snapshot; new stable borrows use the active model.

Copilot AI review requested due to automatic review settings May 25, 2026 13:41
@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

@TUPM96 is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for selectable, prebuilt interest-rate models (linear/kink/jump/exponential) and exposes endpoints to query/switch the active model, along with tests, benchmarks, and docs.

Changes:

  • Introduces InterestRateModelKind and model-specific borrow-rate calculations with shared clamping logic.
  • Adds contract APIs + tests to query/update the active interest rate model.
  • Extends benchmark suite and adds documentation for the available models.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
stellar-lend/contracts/lending/src/lib.rs Exposes interest-rate model types and adds contract entrypoints for model querying/updating.
stellar-lend/contracts/lending/src/interest_rate.rs Implements multiple interest-rate model formulas, config updates, and event publication.
stellar-lend/contracts/lending/src/interest_rate_test.rs Updates event assertions and adds coverage for switching between models.
stellar-lend/contracts/lending/src/borrow.rs Updates config update construction to include the new model field.
stellar-lend/contracts/lending/interest_rate_models.md Documents supported rate models, behavior, migration notes, and benchmarks.
stellar-lend/contracts/hello-world/src/lib.rs Adds APIs to read/switch the active interest-rate model.
stellar-lend/contracts/hello-world/src/interest_rate.rs Adds model enum and model-based rate calculation + admin model switching.
stellar-lend/benchmarks/src/lending_benchmarks.rs Adds benchmarks for switching interest-rate models.
stellar-lend/benchmarks/src/framework.rs Adds budget entries for the new interest-rate model benchmarks.
Comments suppressed due to low confidence (1)

stellar-lend/contracts/lending/src/interest_rate.rs:76

  • Using Option<u32> for model makes the API less type-safe and forces callers to cast enum variants (and allows accidental invalid codes). Prefer pub model: Option<InterestRateModelKind> in InterestRateConfigUpdate and accept/serialize the enum directly; this removes the need for from_code at the boundary and reduces invalid-input surface area.
pub struct InterestRateConfigUpdate {
    pub model: Option<u32>,
    pub base_rate_bps: Option<i128>,
    pub kink_utilization_bps: Option<i128>,
    pub slope_bps: Option<i128>,
    pub jump_slope_bps: Option<i128>,
    pub rate_floor_bps: Option<i128>,
    pub rate_ceiling_bps: Option<i128>,
    pub spread_bps: Option<i128>,
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +9 to +14
| Model | Behavior |
| --- | --- |
| `Linear` | `base + utilization * slope` |
| `Kink` | Piecewise linear. Uses `slope` below `kink_utilization_bps` and `jump_slope` above it. |
| `Jump` | Linear slope across all utilization plus an additional jump slope above the kink. |
| `Exponential` | Quadratic/cubic integer approximation for markets that need sharper high-utilization pricing. |
Comment on lines 262 to 274
pub fn supply_rate_bps(env: &Env) -> Result<i128, InterestRateError> {
let cfg = get_config(env);
let borrow = borrow_rate_bps(env)?;
let supply = borrow
.checked_sub(cfg.spread_bps)
.ok_or(InterestRateError::Overflow)?;
let supply = if borrow <= cfg.spread_bps {
0
} else {
borrow
.checked_sub(cfg.spread_bps)
.ok_or(InterestRateError::Overflow)?
};

Ok(supply.max(cfg.rate_floor_bps))
}
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.

Extract interest rate models into separate configurable module

2 participants