Add portfolioRebalance action provider (read-only plan_rebalance)#4
Add portfolioRebalance action provider (read-only plan_rebalance)#4zaryab2000 wants to merge 1 commit into
Conversation
Adds a portfolio-level rebalancer for Base mainnet. The read-only plan_rebalance action reads the wallet's known token balances, prices them in USD via the keyless DefiLlama API, measures drift from the requested target weights, and computes the minimum set of swaps to reach the target using a greedy largest-surplus to largest-deficit match. It returns a plan and never executes any transaction. Includes schemas, constants, utils, unit tests, README, and a changeset; factory re-exported from action-providers/index.ts.
|
@claude start review |
|
Claude finished @zaryab2000's task in 2m 28s —— View job Code Review:
|
| Check | Status |
|---|---|
Class extends ActionProvider<EvmWalletProvider> |
✅ |
super("portfolioRebalance", []) |
✅ |
@CreateAction with name, description, schema |
✅ |
Returns Promise<string> |
✅ |
| Errors returned, not thrown | |
Zod v4 plain .describe(), no .strip() |
✅ |
supportsNetwork arrow-function property |
✅ |
chainId is a string "8453" |
✅ |
Factory export portfolioRebalanceActionProvider (camelCase) |
✅ |
Re-exported from src/action-providers/index.ts |
✅ |
README.md present |
✅ |
| Test file present | ✅ |
| Changeset: patch, past tense, correct package | ✅ |
Summary
Must fix before upstream PR: Issues #1 (error return inconsistency) and #2 (unbounded float precision in output). Issue #3 (parallel RPC) is a meaningful performance fix. Issues #4–coinbase#7 are schema/correctness polish that upstream reviewers will likely flag.
Code review —
|
Summary
Adds a new
portfolioRebalanceActionProvider for Base mainnet with a single, read-only action:plan_rebalance.It reads the wallet's known token balances, prices them in USD via the keyless DefiLlama price API, measures each holding's drift from a requested target allocation (weights in basis points), and computes the minimum set of swaps to reach the target using a greedy largest-surplus → largest-deficit match. It returns a plan and never executes any transaction.
This is the PRD's recommended safest first slice (read-only advisor). The execution action (
rebalance_portfolio) is intentionally deferred to a follow-up.Differentiation (PRD §0.6 — important)
This is a portfolio rebalancer, not a DEX router. It operates one layer up — at the portfolio level: read all balances → value in USD → compute drift from target weights → plan the minimum set of swaps. It does not implement a new routing engine and does not compete with single-swap best-execution PRs (EZ-Path coinbase#1219/coinbase#1226, paladinTrust coinbase#1245); the future execution action is designed to compose the existing in-tree
zeroX/ensoswap actions, not replace them.What's included
portfolioRebalanceActionProvider.ts—plan_rebalanceaction (Promise<string>, errors returned not thrown)schemas.ts— plainz.object().describe()on every field, no.strip()constants.ts— Base token registry, DefiLlama price configutils.ts— valuation, drift, greedy min-swap planner, pricing, balance readsportfolioRebalanceActionProvider.test.ts— 9 unit tests (mocked wallet + fetch)index.ts,README.mdsrc/action-providers/index.tsBehavior & edge cases
supportsNetwork: true only for Base mainnet (evm, chainId8453)10000bps → otherwise returns an errorrebalanceThresholdBps(default 100) is ignored as dust → no-op planplan_rebalanceperforms zero state-changing calls (asserted in tests)Checks (all green locally)
pnpm build✅pnpm test✅ (9/9 for this provider)pnpm run lint✅pnpm run format✅Notes
This PR targets the internal
feat/swapToTargetAllocationbranch. The upstream PRE-FINAL-PR steps (stripdocs-internal/+ fork tooling, rebase on latest upstreammain, signed commits, tracking issue) are not part of this diff and remain to be done before any PR tocoinbase/agentkit.https://claude.ai/code/session_018pxKMbq6hAs4UJwWTUvBgH
Generated by Claude Code