Skip to content

Split FlowALPv0 further: extract health math and position resources#183

Merged
liobrasil merged 15 commits intomainfrom
liob/split-v0-health-position-resources
Mar 11, 2026
Merged

Split FlowALPv0 further: extract health math and position resources#183
liobrasil merged 15 commits intomainfrom
liob/split-v0-health-position-resources

Conversation

@liobrasil
Copy link
Contributor

Summary

  • extract health and balance-sheet math helpers into new FlowALPHealth contract
  • extract user-facing position resources into new FlowALPPositionResources contract
  • keep FlowALPv0 as the orchestration/entrypoint layer, delegating to extracted contracts
  • rewire rebalancer, transactions, and tests to use extracted position resource types
  • add new contract aliases in flow.json and deploy order updates in test helpers

Why

This continues Jordan split strategy by reducing FlowALPv0 surface area and isolating pure calculation logic from resource-wrapper logic while preserving behavior.

Validation

  • run_tests.sh
  • targeted health/position tests including funds_required_for_target_health_test and position_lifecycle_happy_test

Base automatically changed from jord/split-contracts to main February 26, 2026 23:11
liobrasil and others added 5 commits February 26, 2026 20:22
…ional

Add doc comments to all four public functions in FlowALPHealth following
the positionSatisfiesMinimumBalance doc style. Change interest index
parameters (withdrawCreditInterestIndex, depositDebitInterestIndex) to
optional types, passing nil when the balance direction doesn't match
instead of a dummy 1.0 value.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Member

@jordanschalm jordanschalm left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for adding this.

I verified that changes were move-only using the following diff commands against this commit (asked Claude to get the appropriate lines to diff on each branch):

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '650,709p') <(git show origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc |
  sed -n '7,71p') | delta --side-by-side --file-style omit --line-fill-method ansi

  diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '713,832p') <(git show origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc |
  sed -n '75,190p') | delta --side-by-side --file-style omit --line-fill-method ansi

  diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '890,966p') <(git show origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc |
  sed -n '193,270p') | delta --side-by-side --file-style omit --line-fill-method ansi

  diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '970,1072p') <(git show origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc |
  sed -n '274,377p') | delta --side-by-side --file-style omit --line-fill-method ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '2411,2658p') <(git show
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPPositionResources.cdc | sed -n '21,268p') | delta --side-by-side --file-style omit --line-fill-method
  ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '2665,2709p') <(git show
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPPositionResources.cdc | sed -n '275,319p') | delta --side-by-side --file-style omit --line-fill-method
  ansi

I also added this follow-up which adds documentation to the FlowALPHealth functions: #185

@liobrasil liobrasil marked this pull request as ready for review March 3, 2026 17:25
@liobrasil liobrasil requested a review from a team as a code owner March 3, 2026 17:25
Copy link
Member

@holyfuchs holyfuchs left a comment

Choose a reason for hiding this comment

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

It looks like the merge caused some regressions.
Running the diff commands from jordan gives multiple changes.

@liobrasil liobrasil requested a review from holyfuchs March 11, 2026 06:26
@holyfuchs
Copy link
Member

Had claude generate new diffs it seems there are some changes.
We probably need to merge this quickly or split this up into different PRs if we want to merge this cleanly.

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '695,755p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc | \
  sed -n '6,87p') | delta --side-by-side --file-style omit --line-fill-method ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '757,933p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc | \
  sed -n '89,223p') | delta --side-by-side --file-style omit --line-fill-method ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '935,1012p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc | \
  sed -n '225,319p') | delta --side-by-side --file-style omit --line-fill-method ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '1013,1118p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPHealth.cdc | \
  sed -n '321,445p') | delta --side-by-side --file-style omit --line-fill-method ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '2446,2704p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPPositionResources.cdc | sed -n '10,269p') | delta --side-by-side --file-style omit --line-fill-method \
  ansi

diff -u -w <(git show origin/main:cadence/contracts/FlowALPv0.cdc | sed -n '2706,2755p') <(git show \
  origin/liob/split-v0-health-position-resources:cadence/contracts/FlowALPPositionResources.cdc | sed -n '270,319p') | delta --side-by-side --file-style omit --line-fill-method \
  ansi

@liobrasil
Copy link
Contributor Author

liobrasil commented Mar 11, 2026

Correction from my previous comment: getInterestCurveParams had already landed on main. I restored that getter, its helper script, and the docs mention in 832273a, so this branch should no longer delete that functionality when merged.

At this point, the remaining diffs should mainly be the structural ones required by the split itself. Exhaustive list:

  1. FlowALPHealth is a new extracted contract, so the health math moved out of FlowALPv0.
  2. FlowALPPositionResources is a new extracted contract, so Position, PositionManager, PositionSink, and PositionSource moved out of FlowALPv0.
  3. The extracted FlowALPHealth helpers changed from access(self) to access(account) because they are no longer local methods on FlowALPv0; they now have to be callable cross-contract within the same account without being made public.
  4. The extracted FlowALPHealth helper signatures now take resolved inputs (balance, price, borrowFactor, collateralFactor, interest index, debug flag) instead of reading self.config, _borrowUpdatedTokenState, and position.getBalance(...) internally, because after extraction the helper contract cannot access FlowALPv0.Pool internals.
  5. FlowALPModels adds the PositionPool interface, and FlowALPv0.Pool conforms to it.
  6. The Position capability type changes from &Pool to &{FlowALPModels.PositionPool} because FlowALPPositionResources cannot depend on the concrete FlowALPv0.Pool type without recreating a contract dependency cycle. This is still the same underlying pool resource, just a narrowed interface type.
  7. createPosition / createPositionManager now construct and return FlowALPPositionResources.Position / FlowALPPositionResources.PositionManager and route through FlowALPPositionResources factory functions, because those resource types were moved out of FlowALPv0.
  8. FlowALPPositionResources.createPosition is access(account) because only the protocol account should mint canonical wrapper resources, and FlowALPv0 now has to call that factory across contract boundaries.
  9. The follow-on updates in rebalancer contracts, transactions, tests, and flow.json are import/type/address rewires caused by the moved contracts/resources, not intended logic changes.

Other than the doc comments in FlowALPHealth and the explanatory sentence above access(account) createPosition (kept intentionally for clarity), the PR should now be much closer to reorganization-only while preserving the already-on-main interest curve getter functionality.

@liobrasil liobrasil merged commit d41a43d into main Mar 11, 2026
1 check passed
@liobrasil liobrasil deleted the liob/split-v0-health-position-resources branch March 11, 2026 18:31
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.

3 participants