Skip to content

Feat/accounts balance endpoint - #48

Merged
Miracle656 merged 7 commits into
Miracle656:mainfrom
K1NGD4VID:feat/accounts-balance-endpoint
Sep 1, 2026
Merged

Feat/accounts balance endpoint#48
Miracle656 merged 7 commits into
Miracle656:mainfrom
K1NGD4VID:feat/accounts-balance-endpoint

Conversation

@K1NGD4VID

Copy link
Copy Markdown
Contributor

Summary

Related issue

Type of change

  • Bug fix
  • New feature
  • Refactor
  • Docs
  • Tests
  • CI / tooling

Checklist

  • I have read CONTRIBUTING.md
  • npx tsc --noEmit passes
  • npm run build passes
  • I added / updated tests where relevant
  • I updated docs where relevant

@drips-wave

drips-wave Bot commented Apr 25, 2026

Copy link
Copy Markdown

@K1NGD4VID Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Miracle656 Miracle656 mentioned this pull request Apr 26, 2026
11 tasks

@Miracle656 Miracle656 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The accounts balance endpoint is solid work — Horizon lookup, proper 404 handling, and tests. A few things to fix:

1. Remove scratch/generate_fixtures.js
This file appears in #45, #46, and now here. It's a debug script and shouldn't be in the codebase. git rm scratch/generate_fixtures.js.

2. Stacked PRs create duplicate diff
Your PR includes all the changes from #45 (decoder) and #46 (token caching) on top of the accounts endpoint. This makes the diff hard to review in isolation. Once #45 and #46 are merged (after they remove the scratch file), please rebase this PR so the diff only shows the accounts-related additions.

3. Unchecked checklist
Please check the boxes that apply (npx tsc --noEmit, npm run build, tests added) so we know the PR was self-validated before review.

@K1NGD4VID
K1NGD4VID force-pushed the feat/accounts-balance-endpoint branch from 46ecffd to 3227bf7 Compare April 26, 2026 08:39
@K1NGD4VID
K1NGD4VID requested a review from Miracle656 April 26, 2026 08:53
@K1NGD4VID

Copy link
Copy Markdown
Contributor Author

I've completed the validation and cleanup for the accounts-balance-endpoint PR (#48).

Here is the update on the requested changes:

Removed scratch/generate_fixtures.js: I've confirmed that this file is now removed from all three branches (#45, #46, and #48). Since #48 is rebased on the others, the cleanup is propagated through the entire stack.
Validated Checklist: I've self-validated the PR with the following results:
npx tsc --noEmit: Passed with no type errors.
npm run build: Success.
npm test: All 58 tests passed (including the new account balance route and 404 handling tests).
Stacked PRs & Rebasing:
I've rebased the PR stack so that #48 is cleanly on top of #46, which is on top of #45.
As you noted, the current diff includes changes from the previous PRs because they haven't been merged into main yet.
Since the accounts endpoint depends on the TokenMetadata caching from #46, I'll perform the final rebase onto main as soon as #45 and #46 are merged to ensure the PR diff only shows the accounts-specific additions.
Current Branch Status:

Branch: feat/accounts-balance-endpoint
State: Rebased, tested, and force-pushed.
Ready for the final review once the dependencies are merged!

@Miracle656 Miracle656 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This PR has fallen behind main and now shows merge conflicts (and it's been a few weeks since the last update). The feature itself is welcome — could you rebase on the latest main and resolve the conflicts? Once it's mergeable I'll review and merge. Thanks!

@K1NGD4VID
K1NGD4VID force-pushed the feat/accounts-balance-endpoint branch from 3227bf7 to 2a0df87 Compare June 14, 2026 11:31
@K1NGD4VID
K1NGD4VID force-pushed the feat/accounts-balance-endpoint branch from 2a0df87 to 5809806 Compare June 14, 2026 11:47
@Miracle656

Copy link
Copy Markdown
Owner

Friendly nudge — a couple of things to resolve here: (1) the account balance need is now largely covered by the merged GET /accounts/:address/summary (per-asset totals) and /accounts/:address/transfers endpoints, so please scope this down to what those don't already provide; (2) this PR is large (13 files: schema, indexer, metrics, tokenCache) and its tokenCache work overlaps your own #46 — please consolidate the two and clarify the scope. Then rebase on main.

If you're still up for it, please rebase on the latest main and address the review by 2026-07-01 — otherwise I'll close this to keep the queue tidy (you can always reopen). Thanks for contributing! 🙏

@K1NGD4VID

Copy link
Copy Markdown
Contributor Author

CI is failing on this PR — the Integration tests check failed while Typecheck, DB chaos test, and GitGuardian all passed. Also, the review status is CHANGES_REQUESTED. Please address the review feedback and fix the integration test failure.

@Miracle656

Copy link
Copy Markdown
Owner

Conflicting with mainsrc/api.ts has moved on since this branch was cut. Please rebase onto current main and resolve the conflicts, and I'll review the accounts balance endpoint. Thanks!

…-endpoint

# Conflicts:
#	package-lock.json
#	package.json
#	prisma/schema.prisma
#	src/__tests__/metrics.test.ts
#	src/__tests__/tokenCache.test.ts
#	src/api.ts
#	src/db.ts
#	src/indexer.ts
#	src/metrics.ts
#	src/rpc.ts
#	src/tokenCache.ts
Derived per-token balances for an address, summing what it received and
subtracting what it sent across the indexed history.

Rebased onto main, which has moved a long way since this branch: the token
cache landed via Miracle656#46 and the metrics module via Miracle656#175, so those parts of this
PR are dropped as duplicates and what remains is the balance endpoint itself.

Three fixes to the query on the way in:

- The table reference was unqualified, `FROM "TokenTransfer"`. Every other raw
  query in db.ts uses `"wraith"."TokenTransfer"`, because the models declare
  @@Schema("wraith") — unqualified it resolves only if search_path happens to
  include the schema, so it would work locally and fail on a deployment that
  sets search_path differently.

- No network predicate. Summing both chains' transfers for one address gives a
  number that corresponds to no balance anywhere. Now takes the network and
  filters on it, with the route reading it from the selector so an unknown
  network 400s instead of silently answering for the default.

- The metrics timer was started and stopped around the query but not in a
  finally, so a throw leaked it. Uses observeDbQuery, which times failures
  too — a query that takes eight seconds and then fails is the one worth
  seeing.

Mounted on the existing accounts router rather than a second one, so it sits
beside /summary and /transfers and inherits the network middleware.

The response keeps this PR's honesty about what the number is — a sum over the
indexed window, not an on-chain read — and returns both the raw stroop amount
and the display string, so a consumer doing arithmetic does not have to parse
the decimal back and guess the scale.

tsc clean; full suite 402 passed.

@Miracle656 Miracle656 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Approved and merging — rebased and reworked on your branch (cfbaf72), since the wave has closed.

The best thing about this PR is the honesty in the response. derived_from_ledger: true plus a note saying the figure may not include pre-indexer history is the correct instinct, and it is the one most people skip. A balance endpoint that returns a bare number implies an on-chain read, and a caller has no way to discover otherwise — the value is a plausible-looking number that happens to be low for anyone who held tokens before the indexer's start ledger. Saying so in the payload is the difference between a useful endpoint and a misleading one. I kept it, in camelCase to match the rest of the API, and made the note more explicit about why.

Three fixes to the query:

  1. The table reference was unqualifiedFROM "TokenTransfer". Every other raw query in db.ts uses "wraith"."TokenTransfer", because the models declare @@schema("wraith"). Unqualified, it resolves only if search_path happens to include the schema — so it works locally and fails on a deployment that sets search_path differently. That is the worst failure shape: correct in dev, broken in prod, with an error message pointing at a missing relation rather than at a missing qualifier.
  2. No network predicate. Summing both chains' transfers for one address produces a number that corresponds to no balance anywhere. It takes the network now and filters on it, with the route reading the selector so an unrecognised network 400s rather than silently answering for the default.
  3. The metrics timer was not in a finally, so a throwing query leaked it. Uses observeDbQuery, which times failures too — the query that runs for eight seconds and then fails is exactly the one you want in the histogram.

I also mounted it on the existing accounts router instead of a second one, so it sits beside /summary and /transfers and inherits the network middleware, and added the raw stroop amount alongside the display string — otherwise a consumer doing arithmetic has to parse the decimal back into an integer and guess the scale.

On the rebase: main has moved a long way since June. The token cache landed via #46 earlier today and the metrics module via #175, so those parts of this PR are duplicates now and are dropped. What is left is the balance endpoint, which is what the PR is named for.

Your tests carried over with the response shape updated, plus network scoping and the raw/display split.

Verified: tsc --noEmit clean, full suite 402 passed.

@Miracle656
Miracle656 merged commit 5e62e8f into Miracle656:main Sep 1, 2026
3 of 4 checks passed
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