Skip to content

fix(portfolio): compute stake, rewards and fees as the contract does - #892

Open
adamsoffer wants to merge 2 commits into
nextfrom
claude/agitated-faraday-ae6534
Open

adamsoffer wants to merge 2 commits into
nextfrom
claude/agitated-faraday-ae6534

Conversation

@adamsoffer

@adamsoffer adamsoffer commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Stake and reward history were computed from the subgraph's shares, which is an approximation. If a delegator claims before their orchestrator's reward() call in a round, the contract forfeits that round's reward for them, but shares doesn't reflect it, so the explorer still counted it. Bonding, unbonding and rebonding also claim first.

Example: 100 LPT, +10% in rounds 11 and 12, claimed in round 11 before reward. The contract says 110 and no round-11 reward; the explorer showed 121 and 10.

This came out of review on livepeer/subgraph#217: @jmulq pointed out that shares rounds in a different order than pendingStake(), and suggested using the bonded amount and starting factor instead. Following up showed the difference can be more than rounding: a claim before reward() overstates stake by that round's reward.

Example

Delegator stake before and after: 9.14 LPT on beta, 9.09 LPT on the preview

0x6Ac5…2CeE claimed before its orchestrator's reward call in rounds 3867, 3868, 3922 and 3926, so the contract didn't pay it those four rounds' rewards. It hasn't bonded, unbonded or rebonded since, so nothing recomputed its shares, and beta still counts them.

Wallet Beta This PR pendingStake()
0x6Ac5…2CeE 9.1411 9.0904 9.0904
0x353e…C7D6 167.8301 167.7516 167.7516
0x5B5d…1190 2,474.7545 2,473.5933 2,473.5933

Values are the pages' unrounded stake in round 4423, to four decimals; the app displays fewer.

A scan of Arbitrum's claim history (410 ordinary claims before reward) found 12 wallets in this position. More wallets are affected in reward history, which a later bond doesn't correct.

Changes

  • Stake, rewards and fees use the contract's formula: the stake bonded at the last claim, grown by round-final cumulative factors, in BigInt wei with the contract's rounding.
  • Each claim is ordered against the reward call of the orchestrator it claimed from (the old one on a switch), by block and log index. A claim that came first earns no reward that round.
  • Fees for a claim round come from what the claim event says was paid.
  • Deployments that don't write a snapshot for a claim are still supported: it is rebuilt from the claim event.
  • An orchestrator's commission resets only on its own claims, and rewards on staked commission are counted when a self-claim follows the reward call.
  • Orchestrator delegator lists use the same formula. shares is no longer queried.

Verification

  • Typecheck, lint and jest pass (337 tests).
  • pendingStake and pendingFees: 80 of 80 delegators match to the wei (39 self-delegated; 2 whose last claim was before reward).
  • 155 ordinary claims (55 before reward, 9 switches): claimed rewards match the event to the wei; reconstructed reward and fee history match within float precision.
  • The chain checks read inputs from the contract, not the subgraph. Self-claim history and missing claim snapshots are covered by unit tests only.

Before merging

  • Account pages on the preview, with the live subgraph: the three wallets above load and match pendingStake() to four decimals.
  • Check a multi-wallet portfolio and an orchestrator page on the preview.
  • Check whether the deployed subgraph includes commit 88bb219 of feat: cumulative factors and delegator shares for O(1) stake computation subgraph#217: it doesn't. Claims have no snapshots, and reward factors are off in their last few digits. The two wallets checked (0x6Ac5…2CeE, 0x5B5d…1190) still match pendingStake() to the wei; a very large stake could be off by a few wei until it's deployed.

Known limits

  • Orchestrators' fees in a round they claim in: the claim event gives one total for fees on their own stake and their fee cut together, so how much of that round's fees on their own stake were paid is an estimate. Other rounds aren't affected, and neither is stake.
  • Wallets that claimed stake migrated from Ethereum (LIP-73): their history before that claim is rebuilt, and can be off by about one round's reward. Their stake today isn't affected. This was already the case.

🤖 Generated with Claude Code

Subgraph `shares` can't represent a claim made before the orchestrator's
reward() call, which forfeits that round's reward, so stake and reward
history counted it. Use the contract's formula instead: the stake bonded
at the last claim, grown by the round-final cumulative factors, in wei
with its rounding.

- Order each claim against the old delegate's reward call by block and log
  index, and count no reward for a claim that came first.
- Take a claim round's fees from what the claim event says it paid.
- Rebuild the snapshot of a claim on deployments that don't write one.
- Reset an orchestrator's commission only on its own claims, and count what
  staked commission earned in a round claimed after its reward call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
explorer-arbitrum-one Ready Ready Preview Nov 30, 2026 4:02pm UTC

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Fees earned by staked reward commission are omitted from total fees, understating historical earnings and trailingFeeRate.

1 open issue
What changed in this PR

This PR replaces share-based estimates with contract-style calculations for portfolio stake, rewards, fees, and orchestrator delegator balances.

Changes:

  • Uses bonded amounts and cumulative factors with BigInt rounding.
  • Orders claims against reward calls, reconstructs missing claim snapshots, and updates commission accounting.
  • Updates subgraph queries, mock data, documentation, and regression tests.
File Description
scripts/​mock/​server.mjs Adds claim and reward-call resolvers; updates delegator responses.
scripts/​mock/​README.md Updates the fixture description.
scripts/​mock/​fixtures.mjs Adds self-claim events to mock data.
README.md Documents contract-style formulas and claim timing.
lib/​subgraph/​portfolio.ts Fetches earnings claims and reward-call ordering data; removes shares.
lib/​subgraph/​portfolio.test.ts Tests parsing event ordering information.
lib/​subgraph/​network.ts Uses contract-style stake calculations for delegator lists.
lib/​portfolio/​pre-claim.ts Represents migration history using bonded amounts.
lib/​portfolio/​pre-claim.test.ts Adapts migration tests to bonded positions.
lib/​portfolio/​compute.ts Implements factor-based calculations and claim-aware history.
lib/​portfolio/​compute.test.ts Expands coverage for claim timing, fees, commissions, and rounding.
lib/​config.ts Updates documentation of required subgraph fields.

Review effort: Balanced

Comment thread lib/portfolio/compute.ts Resolved
Only the fee cut was counted in an orchestrator's fee history, not what its
unclaimed reward commission earned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Exact financial balances depend on unverified deployment of subgraph rounding commit 88bb219, and live multi-wallet portfolio and orchestrator-page checks remain outstanding.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@adamsoffer
adamsoffer requested a review from rickstaa November 30, 2026 16:29

This branch was successfully deployed

1 active deployment
Preview — d0584d10 Deployed Nov 30, 2026 by vercel[bot]
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.

2 participants