[DRAFT] Extract ALP components into separate contracts#160
[DRAFT] Extract ALP components into separate contracts#160jordanschalm wants to merge 12 commits intomainfrom
Conversation
Move InterestCurve interface, FixedRateInterestCurve, and KinkInterestCurve from FlowALPv1 into a new standalone FlowALPRateCurves contract to improve modularity and allow rate curve types to be reused independently. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…rve to FixedCurve, KinkInterestCurve to KinkCurve Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move 9 pool config fields (priceOracle, collateralFactor, borrowFactor, positionsProcessedPerCallback, liquidationTargetHF, warmupSec, lastUnpausedAt, dex, dexOracleDeviationBps) into a PoolConfigImpl struct in a new FlowALPModels contract, centralizing config validation logic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace pure-delegation Pool wrapper functions (setDexOracleDeviationBps, setDEX) with a single borrowConfig() function that returns a mutable reference to the pool's PoolConfig. Callers can now invoke config setters directly. Wrapper functions with events/side effects are preserved. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move TokenState struct and EImplementation entitlement to FlowALPModels. Create PoolStateImpl resource in FlowALPModels to hold Pool's movable state fields (globalLedger, reserves, insuranceFund, etc.). Move pure utility functions (perSecondInterestRate, compoundInterestIndex, dexOraclePriceDeviationInRange) to FlowALPMath, keeping thin wrappers in FlowALPv1 for backwards compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Pool.state is now typed as @{FlowALPModels.PoolState} instead of the
concrete @FlowALPModels.PoolStateImpl, matching the PoolConfig/PoolConfigImpl
pattern and allowing the implementation to be swapped in future upgrades.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These are configuration fields, not state fields. Moving them to PoolConfig with getter/setter methods ensures they are upgradeable. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
All fields on the PoolState interface are now accessed through getter/setter methods rather than direct field access. This ensures the interface is upgradeable - fields cannot be changed in interface upgrades, but function signatures can evolve. Also fixes a test that relied on dictionary key ordering for position balances. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These functions interact with pool-level state (reserves, insurance fund) and are better suited as Pool methods that accept a TokenState reference. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Base supported token logic on collateralFactor dictionary keys in PoolConfig rather than globalLedger keys in PoolState. Pool methods now delegate to config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Move _borrowOrCreateReserveVault to PoolState as borrowOrCreateReserve - Move _getSwapperForLiquidation to PoolConfig as getSwapperForLiquidation - Remove setLiquidationParams, setPauseParams, pausePool wrappers - Remove setDebugLogging wrapper (callers use borrowConfig() directly) - Remove unused events: LiquidationParamsUpdated, PauseParamsUpdated, PoolPaused - Update transactions to use borrowConfig() for pause and debug logging Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| @@ -1735,22 +999,11 @@ access(all) contract FlowALPv1 { | |||
|
|
|||
| /// Returns a reference to the reserve vault for the given type, if the token type is supported. | |||
| /// If no reserve vault exists yet, and the token type is supported, the reserve vault is created. | |||
| access(self) fun _borrowOrCreateReserveVault(type: Type): &{FungibleToken.Vault} { | |||
There was a problem hiding this comment.
Didn't move docs over when refactoring
| @@ -1996,13 +1249,6 @@ access(all) contract FlowALPv1 { | |||
| /// - No swapper is configured for the given token pair (seizeType -> debtType) | |||
| /// | |||
| /// @param seizeType: The collateral token type to swap from | |||
| /// @param debtType: The debt token type to swap to | |||
| let diffPct: UFix64 = dexPrice < oraclePrice ? diff / dexPrice : diff / oraclePrice | ||
| let diffBps = UInt16(diffPct * 10_000.0) | ||
| return diffBps <= maxDeviationBps | ||
| return FlowALPMath.dexOraclePriceDeviationInRange(dexPrice: dexPrice, oraclePrice: oraclePrice, maxDeviationBps: maxDeviationBps) |
There was a problem hiding this comment.
fix this duplication
|
I did something similar, MVC pattern ( like https://github.com/jordanschalm/cadence-interface-upgrade-pattern) I took a different approach ( used attachments basically ) and state code is generated. ( just pushed maybe inspires something: https://github.com/bluesign/upgradeable Also my old splitting attempt on credit market ( https://github.com/bluesign/fcm-core/tree/master/cadence/contracts ) ( not using the upgradeable but similar ideas ) |
joshuahannan
left a comment
There was a problem hiding this comment.
Really good changes! I think you're on the right track
| self.yearlyRate = yearlyRate | ||
| } | ||
|
|
||
| access(all) fun interestRate(creditBalance: UFix128, debitBalance: UFix128): UFix128 { |
There was a problem hiding this comment.
what are the parameters here for?
There was a problem hiding this comment.
(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.)
| access(all) let baseRate: UFix128 | ||
|
|
||
| /// The slope of the interest curve before the optimal point (gentle slope) | ||
| access(all) let slope1: UFix128 |
There was a problem hiding this comment.
should these be called gentleSlope and steepSlope instead?
| /// | ||
| /// This entitlement is used internally by the protocol to maintain state consistency | ||
| /// and process queued operations. It should not be granted to external users. | ||
| access(all) entitlement EImplementation |
There was a problem hiding this comment.
I don't like using E for entitlement names. I think it should just be Implementation, or something else. Typically we use verbs and sometimes nouns
| access(all) fun setPriceOracle(_ newOracle: {DeFiActions.PriceOracle}, defaultToken: Type) | ||
| access(all) fun setCollateralFactor(tokenType: Type, factor: UFix64) | ||
| access(all) fun setBorrowFactor(tokenType: Type, factor: UFix64) | ||
| access(all) fun setPositionsProcessedPerCallback(_ count: UInt64) | ||
| access(all) fun setLiquidationTargetHF(_ targetHF: UFix128) | ||
| access(all) fun setWarmupSec(_ warmupSec: UInt64) | ||
| access(all) fun setLastUnpausedAt(_ time: UInt64?) | ||
| access(all) fun setDex(_ dex: {DeFiActions.SwapperProvider}) | ||
| access(all) fun setDexOracleDeviationBps(_ bps: UInt16) | ||
| access(all) fun setPaused(_ paused: Bool) | ||
| access(all) fun setDebugLogging(_ enabled: Bool) |
There was a problem hiding this comment.
Is it safe to make all these access(all)? Should they be access(contract) or access(account) instead just to be safe?
There was a problem hiding this comment.
I just realized that we should do this for the auto balancer RecurringConfig also
This PR refactors ALP types and state fields. The PR is not ready for detailed review, but is ready for feedback on the high-level approach.
The goal is to:
FlowALPv1contract by moving code into separate contracts and removing duplicated functionalityChanges
FlowALPModelsPool.borrowConfigfunction, rather thanPooldefining setters itselfFlowALPMath. In this PR we decided to retain the separate Math contract. Given that, I think it makes sense to delegate pure math-related functions into that contract.Comments
FlowALPModelsand havingaccess(account) fun emitEventfunctions for them.EImplementationis useful. If it is, apply and use it consistently.