[DRAFT] Extract ALP components into separate contracts#160
[DRAFT] Extract ALP components into separate contracts#160jordanschalm wants to merge 36 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>
cadence/contracts/FlowALPv0.cdc
Outdated
| 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
cadence/contracts/FlowALPModels.cdc
Outdated
| 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 have gone through and made all state-modifying functions on types that are stored EImplementation-entitled.
There was a problem hiding this comment.
I just realized that we should do this for the auto balancer RecurringConfig also
Move scaledBalanceToTrueBalance, trueBalanceToScaledBalance, effectiveCollateral, effectiveDebt, and healthComputation from FlowALPv1 to FlowALPMath. Update all internal callers in FlowALPv1 to reference FlowALPMath directly. This eliminates the circular dependency that would arise when moving types to FlowALPModels. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add RiskParams interface and RiskParamsImplv1 to FlowALPModels. Update FlowALPv1 to use interface type for fields and impl for construction. Field access replaced with getter methods. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TokenSnapshot interface and TokenSnapshotImplv1 to FlowALPModels. Update FlowALPv1 to use interface type for fields and impl for construction. Field access replaced with getter methods. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…nDetails to FlowALPModels Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…Pv0 naming - Resolve all 23 conflicted files from merging main - Keep split-contract architecture: FlowALPModels, FlowALPInterestRates, FlowALPMath - Apply FlowALPv1 → FlowALPv0 rename throughout (from main) - Incorporate rebalancer contracts, global pause updates, new tests from main - Add PoolPaused event and pausePool() governance function to FlowALPv0 - All 38 tests pass Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These were created on a separate branch (and gitignored here), then added inadvertently here while merging.
Extract events from FlowALPv0 and FlowALPModels into a dedicated FlowALPEvents contract with access(account)-scoped emission functions. This centralizes event definitions and ensures only co-deployed contracts can emit protocol events. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add /// doc comments with @param lines for each event type and its corresponding emission function, describing the purpose and parameters. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace concrete TokenState struct with a TokenState struct interface and TokenStateImplv1 implementation, following the same pattern used for RiskParams, TokenSnapshot, and PoolConfig. All fields are now private (access(self)) and accessed via getter/setter functions, enabling future implementation upgrades without breaking consumers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Create InternalPosition resource interface and InternalPositionImplv1 implementation in FlowALPModels, replacing the concrete resource in FlowALPv0. Remove the ImplementationUpdates entitlement mapping, as interface methods now encapsulate all field access. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…plementations Ensure interface getters and struct fields have matching documentation for all pairs: RiskParams, TokenSnapshot, PoolConfig, TokenState, PoolState, and InternalPosition. Standardize setter documentation to reference corresponding getters and document constraints. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move EPosition, ERebalance, EGovernance, EParticipant, and EPositionAdmin entitlements to FlowALPModels alongside EImplementation. Update all references across contracts, transactions, and tests to use the FlowALPModels prefix, adding imports where needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
TokenSnapshot is an immutable value type with no need for implementation swapping, so the interface indirection was unnecessary. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ion entitlement All setter and state-mutating functions on PoolConfig, TokenState (impl), InternalPosition interfaces and their implementations now require EImplementation entitlement instead of access(all). Updated corresponding reference return types and parameters in FlowALPv0 to propagate authorized references where needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…odels Ensures every function and field across all interface and implementation types has a /// doc comment. For impl types, docs are derived from their corresponding interface declarations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Did this in a separate PR: #179 |
This PR refactors ALP types and state fields. The goal is to:
FlowALPv1contract by moving code into separate contractsChanges
FixedRateInterestCurve, andKinkInterestCurvefromFlowALPv1into a new standaloneFlowALPRateCurvescontractFlowALPEventscontractFlowALPModelsTokenSnapshot)InternalBalanceremains a struct type. But, since it is stored as part ofInternalPositionwhich uses the interface pattern, we can if necessary updateInternalBalancewhile updatingInternalPosition.)EImplementationto all state-mutating functions moved toFlowALPModels, and document that this entitlement is strictly for internal implementation use (we should never grant the entitlement outside of contract code.)FlowALPMath. 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.