Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cadence/contracts/FlowALPEvents.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ access(all) contract FlowALPEvents {
)

/// Emitted when the insurance rate for a token is updated by governance.
/// The insurance rate is an annual fraction of debit interest diverted to the insurance fund.
/// The insurance rate is a fee of accrued debit interest diverted to the insurance fund.
///
/// @param poolUUID the UUID of the pool containing the token
/// @param tokenType the type identifier string of the token whose rate changed
/// @param insuranceRate the new annual insurance rate (e.g. 0.001 for 0.1%)
/// @param insuranceRate the new insurance fee (e.g. 0.001 for 0.1%)
access(all) event InsuranceRateUpdated(
poolUUID: UInt64,
tokenType: String,
Expand All @@ -167,11 +167,11 @@ access(all) contract FlowALPEvents {
)

/// Emitted when the stability fee rate for a token is updated by governance.
/// The stability fee rate is an annual fraction of debit interest diverted to the stability fund.
/// The stability fee rate is a fee of accrued debit interest diverted to the stability fund.
///
/// @param poolUUID the UUID of the pool containing the token
/// @param tokenType the type identifier string of the token whose rate changed
/// @param stabilityFeeRate the new annual stability fee rate (e.g. 0.05 for 5%)
/// @param stabilityFeeRate the new stability fee (e.g. 0.05 for 5%)
access(all) event StabilityFeeRateUpdated(
poolUUID: UInt64,
tokenType: String,
Expand Down
8 changes: 4 additions & 4 deletions cadence/contracts/FlowALPInterestRates.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ access(all) contract FlowALPInterestRates {
///
/// A simple interface to calculate interest rate for a token type.
access(all) struct interface InterestCurve {
/// Returns the annual interest rate for the given credit and debit balance, for some token T.
/// Returns the annual nominal interest rate for the given credit and debit balance, for some token T.
/// @param creditBalance The credit (deposit) balance of token T
/// @param debitBalance The debit (withdrawal) balance of token T
access(all) fun interestRate(creditBalance: UFix128, debitBalance: UFix128): UFix128 {
Expand All @@ -19,10 +19,10 @@ access(all) contract FlowALPInterestRates {

/// FixedCurve
///
/// A fixed-rate interest curve implementation that returns a constant yearly interest rate
/// A fixed-rate interest curve implementation that returns a constant nominal yearly interest rate
/// regardless of utilization. This is suitable for stable assets like MOET where predictable
/// rates are desired.
/// @param yearlyRate The fixed yearly interest rate as a UFix128 (e.g., 0.05 for 5% APY)
/// @param yearlyRate The fixed yearly nominal rate as a UFix128 (e.g., 0.05 for a 5% nominal yearly rate)
access(all) struct FixedCurve: InterestCurve {

access(all) let yearlyRate: UFix128
Expand Down Expand Up @@ -64,7 +64,7 @@ access(all) contract FlowALPInterestRates {
/// This matches the live TokenState accounting used by FlowALP.
///
/// @param optimalUtilization The target utilization ratio (e.g., 0.80 for 80%)
/// @param baseRate The minimum yearly interest rate (e.g., 0.01 for 1% APY)
/// @param baseRate The minimum yearly nominal rate (e.g., 0.01 for a 1% nominal yearly rate)
/// @param slope1 The total rate increase from 0% to optimal utilization (e.g., 0.04 for 4%)
/// @param slope2 The total rate increase from optimal to 100% utilization (e.g., 0.60 for 60%)
access(all) struct KinkCurve: InterestCurve {
Expand Down
21 changes: 18 additions & 3 deletions cadence/lib/FlowALPMath.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ access(all) contract FlowALPMath {
return diffBps <= maxDeviationBps
}

/// Converts a yearly interest rate to a per-second multiplication factor (stored in a UFix128 as a fixed point
/// number with 18 decimal places). The input to this function will be just the relative annual interest rate
/// (e.g. 0.05 for 5% interest), and the result will be the per-second multiplier (e.g. 1.000000000001).
/// Converts a nominal yearly interest rate to a per-second multiplication factor (stored in a UFix128 as a fixed
/// point number with 18 decimal places). The input to this function is the relative nominal annual rate
/// (e.g. 0.05 for a 5% nominal yearly rate), and the result is the per-second multiplier
/// (e.g. 1.000000000001). For positive rates, the effective one-year growth will be slightly higher than the
/// nominal rate because interest compounds over time.
access(all) view fun perSecondInterestRate(yearlyRate: UFix128): UFix128 {
let perSecondScaledValue = yearlyRate / 31_557_600.0 // 365.25 * 24.0 * 60.0 * 60.0
assert(
Expand All @@ -111,6 +113,19 @@ access(all) contract FlowALPMath {
return perSecondScaledValue + 1.0
}

/// Returns the effective annual yield (EAY) for a given nominal yearly rate, assuming discrete per-second compounding.
///
/// Formula: EAY = (1 + nominalRate / secondsPerYear) ^ secondsPerYear - 1
///
/// For example, a nominal rate of 100% (1.0) produces an effective rate of about 171.8281776413%
/// under discrete per-second compounding: (1 + 1 / 31_557_600) ^ 31_557_600 - 1.
/// This is extremely close to the continuous-compounding limit of e - 1.
access(all) view fun effectiveYearlyRate(nominalYearlyRate: UFix128): UFix128 {
let perSecondRate = FlowALPMath.perSecondInterestRate(yearlyRate: nominalYearlyRate)
let compounded = FlowALPMath.powUFix128(perSecondRate, 31_557_600.0)
return compounded - 1.0
}

/// Returns the compounded interest index reflecting the passage of time
/// The result is: newIndex = oldIndex * perSecondRate ^ seconds
access(all) view fun compoundInterestIndex(
Expand Down
2 changes: 1 addition & 1 deletion cadence/tests/TEST_COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ The `test_helpers.cdc` file provides:
3. **FLOW Debit Interest**
- KinkCurve-based interest rates
- Variable rates based on utilization
- Interest compounds continuously
- Interest compounds via discrete per-second updates

4. **FLOW Credit Interest**
- LP earnings with insurance spread
Expand Down
46 changes: 46 additions & 0 deletions cadence/tests/effective_interest_rate_test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Test
import BlockchainHelpers

import "FlowALPMath"
import "test_helpers.cdc"

access(all)
fun setup() {
let err = Test.deployContract(
name: "FlowALPMath",
path: "../lib/FlowALPMath.cdc",
arguments: []
)
Test.expect(err, Test.beNil())
}

access(all) struct TestCase {
access(all) let nominal: UFix128
access(all) let expected: UFix128

init(nominal: UFix128, expected: UFix128) {
self.nominal = nominal
self.expected = expected
}
}

access(all)
fun test_effectiveYearlyRate() {
let delta: UFix128 = 0.0001
let testCases = [
TestCase(nominal: 0.01, expected: 0.01005016708), // ≈ e^0.01 - 1
TestCase(nominal: 0.02, expected: 0.02020134003), // ≈ e^0.02 - 1
TestCase(nominal: 0.05, expected: 0.05127109638), // ≈ e^0.05 - 1
TestCase(nominal: 0.50, expected: 0.6487212707), // ≈ e^0.5 - 1
TestCase(nominal: 1.0, expected: 1.7182818285), // ≈ e^1 - 1
TestCase(nominal: 4.0, expected: 53.5981500331) // ≈ e^4 - 1
]
for testCase in testCases {
let effective = FlowALPMath.effectiveYearlyRate(nominalYearlyRate: testCase.nominal)
let diff = effective > testCase.expected ? effective - testCase.expected : testCase.expected - effective
Test.assert(
diff <= delta,
message: "effectiveYearlyRate(\(testCase.nominal.toString())) expected ~\(testCase.expected.toString()), got \(effective.toString()), diff \(diff.toString()) exceeds delta \(delta.toString())"
)
}
}
86 changes: 44 additions & 42 deletions cadence/tests/interest_accrual_integration_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import "test_helpers.cdc"
// - Focuses on protocol solvency and insurance mechanics
//
// Interest Rate Configuration:
// - MOET: FixedCurve at 4% APY (rate independent of utilization)
// - MOET: FixedCurve at a 4% nominal yearly rate (rate independent of utilization)
// - Flow: KinkCurve with Aave v3 Volatile One parameters
// (45% optimal utilization, 0% base, 4% slope1, 300% slope2)
// =============================================================================
Expand All @@ -40,18 +40,19 @@ access(all) var snapshot: UInt64 = 0
// Interest Rate Parameters
// =============================================================================

// MOET: FixedCurve (Spread Model)
// MOET: FixedCurve (Protocol-Fee Spread Model)
// -----------------------------------------------------------------------------
// In the spread model, the curve defines the DEBIT rate (what borrowers pay).
// The CREDIT rate is derived as: creditRate = debitRate - insuranceRate
// In the fixed-curve path, the curve defines the DEBIT rate (what borrowers pay).
// The CREDIT rate is derived from the debit rate after protocol fees.
// This ensures lenders always earn less than borrowers pay, with the
// difference going to the insurance pool for protocol solvency.
// difference allocated by the configured protocol fee settings.
//
// Example at 4% debit rate with 0.1% insurance:
// - Borrowers pay: 4.0% APY
// - Lenders earn: 3.9% APY
// - Insurance: 0.1% APY (collected by protocol)
access(all) let moetFixedRate: UFix128 = 0.04 // 4% APY debit rate
// Example at a 4% nominal yearly debit rate:
// - Borrowers pay: 4.0% nominal yearly debit rate
// - Lenders earn: a lower nominal yearly credit rate after protocol fees
// - Protocol Fees are comprised of two parts -
// - Insurance/Stability: configurable fees of accrued debit interest
access(all) let moetFixedRate: UFix128 = 0.04 // 4% nominal yearly debit rate

// FlowToken: KinkCurve (Aave v3 Volatile One Parameters)
// -----------------------------------------------------------------------------
Expand All @@ -64,10 +65,10 @@ access(all) let moetFixedRate: UFix128 = 0.04 // 4% APY debit rate
// - If utilization > optimal: rate = baseRate + slope1 + ((util-optimal)/(1-optimal)) × slope2
//
// At 40% utilization (below 45% optimal):
// - Rate = 0% + (40%/45%) × 4% ≈ 3.56% APY
// - Rate = 0% + (40%/45%) × 4% ≈ 3.56% nominal yearly rate
//
// At 80% utilization (above 45% optimal):
// - Rate = 0% + 4% + ((80%-45%)/(100%-45%)) × 300% ≈ 195% APY
// - Rate = 0% + 4% + ((80%-45%)/(100%-45%)) × 300% ≈ 195% nominal yearly rate
access(all) let flowOptimalUtilization: UFix128 = 0.45 // 45% kink point
access(all) let flowBaseRate: UFix128 = 0.0 // 0% base rate
access(all) let flowSlope1: UFix128 = 0.04 // 4% slope below kink
Expand Down Expand Up @@ -160,15 +161,15 @@ fun test_moet_debit_accrues_interest() {
// -------------------------------------------------------------------------
// STEP 4: Configure MOET Interest Rate
// -------------------------------------------------------------------------
// Set MOET to use a FixedCurve at 4% APY.
// Set MOET to use a FixedCurve at a 4% nominal yearly rate.
// This rate is independent of utilization - borrowers always pay 4%.
// Note: Interest curve must be set AFTER LP deposit to ensure credit exists.
setInterestCurveFixed(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
yearlyRate: moetFixedRate
)
log("Set MOET interest rate to 4% APY (after LP deposit)")
log("Set MOET interest rate to 4% nominal yearly rate (after LP deposit)")

let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
Expand Down Expand Up @@ -305,7 +306,7 @@ fun test_moet_debit_accrues_interest() {
// Expected Growth Calculation
// -------------------------------------------------------------------------
// Per-second compounding: (1 + r / 31_557_600) ^ seconds - 1
// At 4% APY for 30 days (2,592,000 seconds):
// At a 4% nominal yearly rate for 30 days (2,592,000 seconds):
// Growth = (1 + 0.04 / 31_557_600) ^ 2_592_000 - 1 ≈ 0.328%
//
// We use a wide tolerance range because:
Expand Down Expand Up @@ -337,10 +338,10 @@ fun test_moet_debit_accrues_interest() {
// - Time advances 30 days
// - Verify: LP credit increased, growth rate is in expected range
//
// Key Insight (FixedCurve Spread Model):
// - debitRate = 4.0% (what borrowers pay, defined by curve)
// - insuranceRate = 0.1% (protocol reserve)
// - creditRate = debitRate - insuranceRate = 3.9% (what lenders earn)
// Key Insight (FixedCurve Protocol-Fee Spread):
// - debitRate is defined by the curve
// - creditRate is the debit rate after protocol fees
// - creditRate remains below debitRate
// =============================================================================
access(all)
fun test_moet_credit_accrues_interest_with_insurance() {
Expand Down Expand Up @@ -394,7 +395,7 @@ fun test_moet_credit_accrues_interest_with_insurance() {
// -------------------------------------------------------------------------
// STEP 4: Configure MOET Interest Rate
// -------------------------------------------------------------------------
// Set 4% APY debit rate. Credit rate will be ~3.9% after insurance deduction.
// Set a 4% nominal yearly debit rate. Credit rate will be lower after protocol fees.
setInterestCurveFixed(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -485,9 +486,9 @@ fun test_moet_credit_accrues_interest_with_insurance() {
// -------------------------------------------------------------------------
// Expected Credit Growth Calculation
// -------------------------------------------------------------------------
// Debit rate: 4% APY (what borrowers pay)
// Insurance: 0.1% APY (protocol reserve)
// Credit rate: 4% - 0.1% = 3.9% APY (what LPs earn)
// Debit rate: 4% nominal yearly rate (what borrowers pay)
// Protocol fees: configured insurance plus stability fee fractions
// Credit rate: lower than the debit rate after protocol fees
//
// 30-day credit growth ≈ 3.9% × (30/365) ≈ 0.32%
//
Expand Down Expand Up @@ -523,7 +524,7 @@ fun test_moet_credit_accrues_interest_with_insurance() {
// Key Insight (KinkCurve):
// At 40% utilization (below 45% optimal kink):
// - Rate = baseRate + (utilization/optimal) × slope1
// - Rate = 0% + (40%/45%) × 4% ≈ 3.56% APY
// - Rate = 0% + (40%/45%) × 4% ≈ 3.56% nominal yearly rate
// =============================================================================
access(all)
fun test_flow_debit_accrues_interest() {
Expand Down Expand Up @@ -685,7 +686,7 @@ fun test_flow_debit_accrues_interest() {
// -------------------------------------------------------------------------
// Utilization = 4,000 / 10,000 = 40% (below 45% optimal)
// Rate = baseRate + (util/optimal) × slope1
// = 0% + (40%/45%) × 4% ≈ 3.56% APY
// = 0% + (40%/45%) × 4% ≈ 3.56% nominal yearly rate
//
// 30-day growth ≈ 3.56% × (30/365) ≈ 0.29%
let minExpectedDebtGrowth: UFix64 = 0.002 // 0.2%
Expand Down Expand Up @@ -891,15 +892,15 @@ fun test_flow_credit_accrues_interest_with_insurance() {
// - LP deposits 10,000 MOET
// - Borrower deposits 10,000 FLOW and borrows MOET
// - Insurance rate set to 1% (higher than default 0.1% for visibility)
// - Debit rate set to 10% APY
// - Debit rate set to a 10% nominal yearly rate
// - Time advances 1 YEAR
// - Verify: Insurance spread ≈ 1% (debit rate - credit rate)
//
// Key Insight (FixedCurve Spread Model):
// - debitRate = 10% (what borrowers pay)
// - insuranceRate = 1% (protocol reserve)
// - creditRate = debitRate - insuranceRate = 9% (what LPs earn)
// - Spread = debitRate - creditRate = 1%
// Key Insight (FixedCurve Protocol-Fee Spread):
// - debitRate is set by the fixed curve
// - insurance/stability remain configured fee parameters
// - creditRate is reduced relative to debitRate by those protocol fees
// - the realized spread shows up as a lower lender growth rate than borrower growth rate
// =============================================================================
access(all)
fun test_insurance_deduction_verification() {
Expand Down Expand Up @@ -952,7 +953,7 @@ fun test_insurance_deduction_verification() {
//
// Insurance Rate: 1% (vs default 0.1%)
// Debit Rate: 10% (vs default 4%)
// Expected Credit Rate: 10% - 1% = 9%
// Expected Credit Rate: lower than 10% after protocol fees
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1012,8 +1013,8 @@ fun test_insurance_deduction_verification() {
// =========================================================================
// Using 1 year (31,557,600 seconds for 365.25 days) makes the percentage calculations
// straightforward. With per-second discrete compounding:
// - 10% APY → (1 + 0.10 / 31_557_600) ^ 31_557_600 - 1 ≈ 10.52% effective rate
// - 9% APY → (1 + 0.09 / 31_557_600) ^ 31_557_600 - 1 ≈ 9.42% effective rate
// - 10% nominal yearly rate → (1 + 0.10 / 31_557_600) ^ 31_557_600 - 1 ≈ 10.52% effective rate
// - 9% nominal yearly rate → (1 + 0.09 / 31_557_600) ^ 31_557_600 - 1 ≈ 9.42% effective rate
// - Spread should be approximately 1%
Test.moveTime(by: ONE_YEAR)
Test.commitBlock()
Expand Down Expand Up @@ -1049,9 +1050,10 @@ fun test_insurance_deduction_verification() {
// =========================================================================
// ASSERTION: Verify Insurance Spread
// =========================================================================
// For FixedCurve (spread model):
// - debitRate = creditRate + insuranceRate
// - insuranceSpread = debitRate - creditRate ≈ insuranceRate
// For FixedCurve:
// - debitRate is the curve-defined nominal yearly rate
// - creditRate is the debit rate after protocol fees
// - insuranceSpread = actualDebtRate - actualCreditRate
//
// With 10% debit and 1% insurance, spread should be ~1%
// (Slight variation due to per-second compounding effects)
Expand Down Expand Up @@ -1158,12 +1160,12 @@ fun test_combined_all_interest_scenarios() {
// -------------------------------------------------------------------------
// STEP 5: Configure Interest Curves for Both Tokens
// -------------------------------------------------------------------------
// MOET: FixedCurve at 4% APY (spread model)
// MOET: FixedCurve at a 4% nominal yearly rate (fixed-curve spread model)
// Flow: KinkCurve with Aave v3 Volatile One parameters
setInterestCurveFixed(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
yearlyRate: moetFixedRate // 4% APY
yearlyRate: moetFixedRate // 4% nominal yearly rate
)
setInterestCurveKink(
signer: PROTOCOL_ACCOUNT,
Expand Down Expand Up @@ -1324,14 +1326,14 @@ fun test_combined_all_interest_scenarios() {
// Assertion Group 2: Health Factor Changes
// -------------------------------------------------------------------------
// Borrower1 (Flow collateral, MOET debt):
// - MOET debit rate: 4% APY
// - MOET debit rate: 4% nominal yearly rate
// - Flow credit rate: lower than Flow debit rate due to insurance spread
// - Net effect: Debt grows faster than collateral → Health DECREASES
Test.assert(b1HealthAfter < b1HealthBefore, message: "Borrower1 health should decrease")

// Borrower2 (MOET collateral, Flow debt):
// - MOET credit rate: ~3.9% APY (4% debit - 0.1% insurance)
// - Flow debit rate: ~2.5% APY (at 28.6% utilization)
// - MOET credit rate: lower than the MOET debit rate after protocol fees
// - Flow debit rate: ~2.5% nominal yearly rate (at 28.6% utilization)
// - Collateral (3,000 MOET) earning more absolute interest than debt (2,000 Flow)
// - Net effect: Health INCREASES
Test.assert(b2HealthAfter > b2HealthBefore, message: "Borrower2 health should increase (collateral interest > debt interest)")
Expand Down
Loading
Loading