-
Notifications
You must be signed in to change notification settings - Fork 2
[DRAFT] Extract ALP components into separate contracts #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jordanschalm
wants to merge
12
commits into
main
Choose a base branch
from
jord/split-contracts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8068f3e
Extract InterestCurve types into FlowALPRateCurves contract
jordanschalm 4495f56
Rename FlowALPRateCurves to FlowALPInterestRates, FixedRateInterestCu…
jordanschalm 0e0971f
split pool config/state
jordanschalm 9c6a872
Extract pool config into FlowALPModels contract
jordanschalm 14efe59
Simplify Pool config setters with borrowConfig()
jordanschalm 20f7c06
Extract TokenState, PoolState, and utility functions from FlowALPv1
jordanschalm 6d9797e
Add PoolState resource interface for future upgradeability
jordanschalm 959f911
Move debugLogging and paused from PoolState to PoolConfig
jordanschalm 9a9c7eb
Replace PoolState field declarations with getter/setter functions
jordanschalm 9608242
Move collectInsurance and collectStability from TokenState to Pool
jordanschalm 0b642e0
Move getSupportedTokens and isTokenSupported to PoolConfig
jordanschalm dc21efb
Move functions to PoolState/PoolConfig and remove Pool wrappers
jordanschalm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import "FlowALPMath" | ||
|
|
||
| access(all) contract FlowALPInterestRates { | ||
|
|
||
| /// InterestCurve | ||
| /// | ||
| /// 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. | ||
| /// @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 { | ||
| post { | ||
| // Max rate is 400% (4.0) to accommodate high-utilization scenarios | ||
| // with kink-based curves like Aave v3's interest rate strategy | ||
| result <= 4.0: | ||
| "Interest rate can't exceed 400%" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// FixedCurve | ||
| /// | ||
| /// A fixed-rate interest curve implementation that returns a constant 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) | ||
| access(all) struct FixedCurve: InterestCurve { | ||
|
|
||
| access(all) let yearlyRate: UFix128 | ||
|
|
||
| init(yearlyRate: UFix128) { | ||
| pre { | ||
| yearlyRate <= 1.0: "Yearly rate cannot exceed 100%, got \(yearlyRate)" | ||
| } | ||
| self.yearlyRate = yearlyRate | ||
| } | ||
|
|
||
| access(all) fun interestRate(creditBalance: UFix128, debitBalance: UFix128): UFix128 { | ||
| return self.yearlyRate | ||
| } | ||
| } | ||
|
|
||
| /// KinkCurve | ||
| /// | ||
| /// A kink-based interest rate curve implementation. The curve has two linear segments: | ||
| /// - Before the optimal utilization ratio (the "kink"): a gentle slope | ||
| /// - After the optimal utilization ratio: a steep slope to discourage over-utilization | ||
| /// | ||
| /// This creates a "kinked" curve that incentivizes maintaining utilization near the | ||
| /// optimal point while heavily penalizing over-utilization to protect protocol liquidity. | ||
| /// | ||
| /// Formula: | ||
| /// - utilization = debitBalance / (creditBalance + debitBalance) | ||
| /// - Before kink (utilization <= optimalUtilization): | ||
| /// rate = baseRate + (slope1 × utilization / optimalUtilization) | ||
| /// - After kink (utilization > optimalUtilization): | ||
| /// rate = baseRate + slope1 + (slope2 × excessUtilization) | ||
| /// where excessUtilization = (utilization - optimalUtilization) / (1 - optimalUtilization) | ||
| /// | ||
| /// @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 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 { | ||
|
|
||
| /// The optimal utilization ratio (the "kink" point), e.g., 0.80 = 80% | ||
| access(all) let optimalUtilization: UFix128 | ||
|
|
||
| /// The base yearly interest rate applied at 0% utilization | ||
| access(all) let baseRate: UFix128 | ||
|
|
||
| /// The slope of the interest curve before the optimal point (gentle slope) | ||
| access(all) let slope1: UFix128 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should these be called gentleSlope and steepSlope instead? |
||
|
|
||
| /// The slope of the interest curve after the optimal point (steep slope) | ||
| access(all) let slope2: UFix128 | ||
|
|
||
| init( | ||
| optimalUtilization: UFix128, | ||
| baseRate: UFix128, | ||
| slope1: UFix128, | ||
| slope2: UFix128 | ||
| ) { | ||
| pre { | ||
| optimalUtilization >= 0.01: | ||
| "Optimal utilization must be at least 1%, got \(optimalUtilization)" | ||
| optimalUtilization <= 0.99: | ||
| "Optimal utilization must be at most 99%, got \(optimalUtilization)" | ||
| slope2 >= slope1: | ||
| "Slope2 (\(slope2)) must be >= slope1 (\(slope1))" | ||
| baseRate + slope1 + slope2 <= 4.0: | ||
| "Maximum rate cannot exceed 400%, got \(baseRate + slope1 + slope2)" | ||
| } | ||
| self.optimalUtilization = optimalUtilization | ||
| self.baseRate = baseRate | ||
| self.slope1 = slope1 | ||
| self.slope2 = slope2 | ||
| } | ||
|
|
||
| access(all) fun interestRate(creditBalance: UFix128, debitBalance: UFix128): UFix128 { | ||
| // If no debt, return base rate | ||
| if debitBalance == 0.0 { | ||
| return self.baseRate | ||
| } | ||
|
|
||
| // Calculate utilization ratio: debitBalance / (creditBalance + debitBalance) | ||
| // Note: totalBalance > 0 is guaranteed since debitBalance > 0 and creditBalance >= 0 | ||
| let totalBalance = creditBalance + debitBalance | ||
| let utilization = debitBalance / totalBalance | ||
|
|
||
| // If utilization is below or at the optimal point, use slope1 | ||
| if utilization <= self.optimalUtilization { | ||
| // rate = baseRate + (slope1 × utilization / optimalUtilization) | ||
| let utilizationFactor = utilization / self.optimalUtilization | ||
| let slope1Component = self.slope1 * utilizationFactor | ||
| return self.baseRate + slope1Component | ||
| } else { | ||
| // If utilization is above the optimal point, use slope2 for excess | ||
| // excessUtilization = (utilization - optimalUtilization) / (1 - optimalUtilization) | ||
| let excessUtilization = utilization - self.optimalUtilization | ||
| let maxExcess = FlowALPMath.one - self.optimalUtilization | ||
| let excessFactor = excessUtilization / maxExcess | ||
|
|
||
| // rate = baseRate + slope1 + (slope2 × excessFactor) | ||
| let slope2Component = self.slope2 * excessFactor | ||
| return self.baseRate + self.slope1 + slope2Component | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are the parameters here for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/onflow/FlowALP/pull/160/changes/BASE..dc21efb1e2fefca3fd6435033e721c0b468b9f97#diff-b68530c0b2e0dcf9d373ec5938f2b1f6222bb11bfb19e8dc333bcae7e48e9e35R10-R11.
(I'm trying to mainly move existing code and not add anything right now to keep the diff clean, but agree there should be documentation here.)