Skip to content

refactor(morpho-sdk): extract public reallocation data#655

Merged
Rubilmax merged 33 commits into
mainfrom
extract-public-reallocation
May 26, 2026
Merged

refactor(morpho-sdk): extract public reallocation data#655
Rubilmax merged 33 commits into
mainfrom
extract-public-reallocation

Conversation

@Rubilmax
Copy link
Copy Markdown
Collaborator

@Rubilmax Rubilmax commented May 12, 2026

What changed

  • Added a dedicated ReallocationData state container for public allocator reallocation computation.
  • Moved shared-liquidity option and input types into morpho-sdk, including explicit timestamp-driven computation.
  • Updated MarketV1 reallocation data fetching to return ReallocationData instead of SimulationState.
  • Added parity coverage for the reallocation algorithm and updated reallocation action tests to pass the fetched block timestamp.
  • Optimized reallocation simulation to clone only mutated state entries per applied public reallocation.
  • Added regression coverage for clone preservation and disabled allocator short-circuit behavior.
  • Added a root convention to prefer _try(accessor, UnknownError) for optional domain lookups.

Fixes SDK-185

Why

This decouples morpho-sdk reallocation computation from simulation-sdk block state while keeping timestamp-sensitive accrual and pending-cap behavior explicit at the call site. The partial-clone path also avoids repeatedly deep-cloning unrelated market, vault, position, and vault-market-config state during iterative public reallocation computation.

Performance

Local comparison while reviewing the copy strategy measured:

  • Partial-clone ReallocationData: 21.23ms
  • Full-clone ReallocationData reference: 97.60ms (4.60x slower than partial clone)
  • SimulationState / immer implementation: 76.28ms (3.59x slower than partial clone)

The benchmark scaffolding was removed from the committed tests; the measurements are reported here for the PR discussion.

Validation

  • pnpm exec biome check packages/morpho-sdk/src/helpers/reallocationData.ts packages/morpho-sdk/src/helpers/reallocationData.test.ts
  • pnpm exec vitest run packages/morpho-sdk/src/helpers/reallocationData.test.ts --config vitest.config.ts --project morpho-sdk
  • pnpm --filter @morpho-org/morpho-sdk exec tsc --noEmit
  • git diff --check
  • Pre-commit hook: biome check --write --no-errors-on-unmatched && lint:address

View Slack thread

@Rubilmax Rubilmax self-assigned this May 12, 2026
@Rubilmax Rubilmax changed the title [codex] extract public reallocation data refactor(morpho-sdk): extract public reallocation data May 12, 2026
@Rubilmax Rubilmax force-pushed the Rubilmax/docs-readmes-license-v1 branch 6 times, most recently from 169a678 to be6a428 Compare May 13, 2026 07:21
@Rubilmax Rubilmax force-pushed the extract-public-reallocation branch from ab3488d to 9aad98e Compare May 13, 2026 08:22
Copy link
Copy Markdown
Collaborator Author

@Rubilmax Rubilmax left a comment

Choose a reason for hiding this comment

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

LGTM

@Rubilmax Rubilmax marked this pull request as ready for review May 13, 2026 09:40
@Rubilmax Rubilmax requested review from 0xbulma and Foulks-Plb May 13, 2026 09:45
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 6 additional findings.

Open in Devin Review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 74ed816128

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/morpho-sdk/src/helpers/computeReallocations.ts
@Rubilmax Rubilmax force-pushed the extract-public-reallocation branch from 74ed816 to 5eb6445 Compare May 13, 2026 13:27
Copy link
Copy Markdown
Collaborator Author

@Rubilmax Rubilmax left a comment

Choose a reason for hiding this comment

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

LGTM

@Rubilmax Rubilmax force-pushed the extract-public-reallocation branch from 5eb6445 to fd7e814 Compare May 13, 2026 14:28
Base automatically changed from Rubilmax/docs-readmes-license-v1 to main May 13, 2026 15:38
@Rubilmax Rubilmax requested a review from a team as a code owner May 13, 2026 15:38
@Rubilmax Rubilmax force-pushed the extract-public-reallocation branch from fd7e814 to b5d0ec4 Compare May 13, 2026 17:24
@Foulks-Plb
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 21685c9693

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/morpho-sdk/src/helpers/reallocationData.ts Outdated
Comment thread packages/morpho-sdk/src/helpers/computeReallocations.ts
@Foulks-Plb
Copy link
Copy Markdown
Collaborator

Foulks-Plb commented May 15, 2026

I got this flagged by an agentic review:

🟢 Observation #4 — holdings fetched but never consumed

marketV1.ts:1016-1020 calls fetchHolding for each vaultAddress on the loanToken. These holdings are stored in ReallocationData.holdings, but the review claims nothing in getMarketPublicReallocations / applyPublicReallocation actually reads them. According to the review, only markets, vaults, positions, and vaultMarketConfigs are consumed by the algorithm.

So the conclusion was:
→ potentially N unnecessary RPC calls per fetch. Either remove the fetch or remove holdings from InputReallocationData. Not a bug, just extra cost.

But I believe holdings are actually used in the algorithm — could you double check?

@Foulks-Plb
Copy link
Copy Markdown
Collaborator

Otherwise, this looks very solid.

Foulks-Plb
Foulks-Plb previously approved these changes May 18, 2026
@0xbulma
Copy link
Copy Markdown
Collaborator

0xbulma commented May 18, 2026

@carapulse review

@0xbulma
Copy link
Copy Markdown
Collaborator

0xbulma commented May 18, 2026

@prd-carapulse[bot] review this PR deeply. Make sure it introduces no regressions in scope

@prd-carapulse
Copy link
Copy Markdown
Contributor

prd-carapulse Bot commented May 18, 2026

Verdict: changes requested. CI green, but regressions found.

Blocking:

  • reallocationData.ts:449-484 can select a source market with VaultMarketConfig.enabled === false; applyPublicReallocation() also never checks source/target enabled. Onchain/simulation rejects this in PublicAllocator.reallocateTo, so SDK can return reverting reallocations.
  • delay behavior was removed. Old path accrued target cap headroom at block.timestamp + delay while source withdrawals used block.timestamp; new single timestamp cannot preserve that. Public API/behavior regression for morpho-sdk and liquidity-sdk-viem.
  • getReallocationData() requires block.timestamp but ReallocationData drops it; default getReallocations() falls back to market.lastUpdate. Existing thread raised this; I still consider it unresolved for cap/maxIn/delay consistency.

Release/merge:

  • PR is currently dirty against origin/main: modify/delete conflict on .changeset/get-requirements-action-general-adapter-param.md.
  • Changeset is stale and only patches @morpho-org/morpho-sdk; @morpho-org/liquidity-sdk-viem public types/deps changed too.

Verified:

  • Auth-probed morpho-org/sdks PR refactor(morpho-sdk): extract public reallocation data #655, head 33a1ca83f09312d8dd7492b03a71b53c05d932fa.
  • GitHub checks green.
  • Local focused tests passed: 37 helper/reallocation tests + 19 fork reallocation tests.
  • Local builds passed for morpho-sdk, liquidity-sdk-viem, liquidation-sdk-viem.

Track: https://carapulse.morpho.dev/dashboard/runs?run_id=gw_run_53dc11dd01e6

@Rubilmax
Copy link
Copy Markdown
Collaborator Author

  • reallocationData.ts:449-484 can select a source market with VaultMarketConfig.enabled === false; applyPublicReallocation() also never checks source/target enabled. Onchain/simulation rejects this in PublicAllocator.reallocateTo, so SDK can return reverting reallocations.

The algorithm only checks markets that are in the withdraw queue of a vault, so the market is implicitely enabled. Double-check this @prd-carapulse[bot]

  • delay behavior was removed. Old path accrued target cap headroom at block.timestamp + delay while source withdrawals used block.timestamp; new single timestamp cannot preserve that. Public API/behavior regression for morpho-sdk and liquidity-sdk-viem.

Judged useless: shared liquidity is at worst under-estimated (conservative).

  • getReallocationData() requires block.timestamp but ReallocationData drops it; default getReallocations() falls back to market.lastUpdate. Existing thread raised this; I still consider it unresolved for cap/maxIn/delay consistency.

Same reason

Comment thread packages/morpho-sdk/src/helpers/computeReallocations.ts Outdated
Comment thread packages/morpho-sdk/src/entities/reallocationData.ts
Rubilmax and others added 4 commits May 21, 2026 17:31
* refactor(liquidity-sdk-viem): use morpho reallocation data

* feat(morpho-sdk): extract bundler action encoding

* docs(morpho-sdk): document Permit2 expiration error

* test(liquidity-sdk-viem): checksum loader owner fixture

* fix(morpho-sdk): remove unused bundler signature error

* fix(morpho-sdk): derive bundler transaction value

* docs: document bundler encodeBundle errors

* test(morpho-sdk): cover bundler value derivation

* docs(morpho-sdk): clarify bundler encoding constraints

* docs(morpho-sdk): document native transfer skip behavior

* fix: account callback bundle value

* test(liquidity-sdk-viem): update loader expectations

* refactor(morpho-sdk): remove unused rewards ABI
* refactor(liquidity-sdk-viem): use morpho reallocation data

* feat(morpho-sdk): re-export blue sdk surfaces

* chore: align reallocation entity exports

* fix(morpho-sdk): align re-export subpaths

* docs: add package deprecation migration notes

* fix(morpho-sdk): preserve source augment side effects
@Rubilmax Rubilmax enabled auto-merge (squash) May 26, 2026 10:43
@Rubilmax Rubilmax merged commit 42c27ae into main May 26, 2026
6 checks passed
@Rubilmax Rubilmax deleted the extract-public-reallocation branch May 26, 2026 10:44
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