Skip to content

feat(api): add v2 /chain/globals endpoint - #37

Open
rferrari wants to merge 8 commits into
SkateHive:mainfrom
rferrari:feat/v2-chain-globals
Open

feat(api): add v2 /chain/globals endpoint#37
rferrari wants to merge 8 commits into
SkateHive:mainfrom
rferrari:feat/v2-chain-globals

Conversation

@rferrari

@rferrari rferrari commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

What

Adds GET /api/v2/chain/globals — the chain-wide constants clients need for HP and vote-value math, in one edge-cached read.

Third in the series of small PRs closing API read gaps for the "read-through-API" migration.

⚠️ Stacked on #35 (feat/v2-post-endpoint) for the vitest runner + cacheHeaders helper. Until #35 merges, this diff also shows #35's commits; it slims automatically once #35 lands. Review/merge #35 first. (Sibling of #36; the two are independent.)

Why

useHivePower on the webapp fires three RPC calls per user, per page to compute HP and "$ per vote":

  • VESTS↔HP ratio — from dynamic global properties
  • reward poolreward_balance / recent_claims from the post reward fund
  • HIVE/USD median price — to turn payouts into dollars

These are identical for every user and slow-moving, so one cached endpoint replaces N per-user RPC bursts.

How — HAFSQL-first, dhive fallback

Following the principle that reads should prefer HAFSQL and use dhive as the resilience layer:

  • Global properties → HAFSQL first (dynamic_global_properties view, newest row by block_num), dhive RPC fallback if the view is unavailable/empty. HAFSQL stores raw integers and RPC returns asset strings, so both are normalized to a single numeric shape (with a source field so consumers can see which path served it).
  • Reward fund + median price → dhive RPC — genuinely not exposed by HAFSQL (only raw operation_*_reward_table / operation_feed_publish_table logs exist, not current pool/median state). Verified directly against our HAFSQL instance.
  • Edge-cached s-maxage=300, stale-while-revalidate=150.

Response shape

{
  "success": true,
  "data": {
    "globalProperties": {
      "total_vesting_fund_hive": 212276626.544,
      "total_vesting_shares": 343888772092.4087,
      "vests_per_hive": 1620.003,
      "head_block_number": 107803905,
      "source": "hafsql"
    },
    "rewardFund": { "reward_balance": "1086575.137 HIVE", "recent_claims": "763634274937908862", "...": "..." },
    "medianPrice": { "base": "0.050 HBD", "quote": "1.000 HIVE" }
  }
}

Testing

  • Unit (tests/unit/chain-globals.route.test.ts, 5 tests, no live DB — HAFSQL + HiveClient mocked): HAFSQL path (asserts no DGPO RPC), RPC fallback on both HAFSQL throw and empty rows, cross-source number-normalization parity, the get_reward_fund(['post']) request, and the 500 path.
  • Live: verified against real HAFSQL + RPC — source: hafsql at the current head block, shares/fund matches vests_per_hive exactly, reward fund + median populated.
  • Smoke (tests/test-api-endpoints.sh): chain globals check added.
  • pnpm lint clean; pnpm build compiles the route as dynamic.

Notes

  • Additive only — no changes to existing routes, no webapp changes.

🤖 Generated with Claude Code

rferrari and others added 8 commits July 3, 2026 00:56
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Fetches a single post from HAFSQL with the same column shape, children
count and vote aggregation as the feed, then runs it through
dealiasSoftPosts so lite-user (@skateuser) identity is preserved. Serves
edge-cached (s-maxage=300) so post pages, OG images and Farcaster frames
stop hitting Hive RPC directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Covers the happy path (single post by author/permlink) and the 404
(nonexistent permlink) in the existing status-code smoke suite.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Introduces vitest (+ test/test:watch scripts and a config that mirrors
the '@/*' tsconfig alias) as the unit-test framework for API routes.
Unit tests live in tests/unit/ so route folders with literal-bracket
segments (e.g. [author]) don't break glob includes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Covers 200 (normalized shape + 300s edge-cache headers), query
parameterization, soft-post de-alias delegation, 404 (short cache), and
500 (no cache). HAFSQL and dealiasSoftPosts are mocked so the suite runs
with no live DB.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Bundles the chain-wide constants clients need for HP and vote-value math:
dynamic global properties (VESTS<->HP ratio), the post reward fund
(reward_balance / recent_claims) and the HIVE/USD median price.

Follows a HAFSQL-first, dhive-fallback pattern: global properties come
from the HAFSQL dynamic_global_properties view (normalized from raw
integers) and fall back to dhive RPC if the view is unavailable/empty.
Reward fund and median price are dhive-only (HAFSQL doesn't expose them).
Edge-cached at s-maxage=300 since these are identical per-user and
slow-moving.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Covers the HAFSQL path (no DGPO RPC), RPC fallback on both HAFSQL throw
and empty rows, number normalization parity between sources, the post
reward-fund request, and the 500 path. HAFSQL and HiveClient are mocked.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@rferrari, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 24 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c2711b08-14c8-4f7c-aa8a-bb07a36f6ac8

📥 Commits

Reviewing files that changed from the base of the PR and between 3bb3c8d and 478c2db.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (8)
  • package.json
  • src/app/api/v2/chain/globals/route.ts
  • src/app/api/v2/post/[author]/[permlink]/route.ts
  • src/lib/cache-headers.ts
  • tests/test-api-endpoints.sh
  • tests/unit/chain-globals.route.test.ts
  • tests/unit/post.route.test.ts
  • vitest.config.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@rferrari

rferrari commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

Part of #38 (read-through-API tracking issue).

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.

1 participant