Create Next.js API routes for recording and listing dividends REFIXD#129
Conversation
|
@Mitch5000 is attempting to deploy a commit to the Cankat's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
I refixed everything from scratch, please let me know if there's anything you want me to adjust. Thank you for your patience. |
There was a problem hiding this comment.
Review ÔÇö changes requested
The dividend API implementation looks good (7 vitest tests, OpenAPI update, auto-record on distribute). However CI is failing:
src/app/api/talos/[id]/buy-token/route.ts(89,12): error TS2339: Property 'source' does not exist...
tests/buy-token.test.ts(68,78): error TS2556...
These errors may be from branch drift vs main. Please:
git rebase origin/mainpnpm exec tsc --noEmit && pnpm exec vitest run tests/dividends.test.ts- Request re-review once CI is green
Code quality is close to merge ÔÇö just needs rebase + typecheck fix.
enliven17
left a comment
There was a problem hiding this comment.
Review — changes requested
The dividend API implementation looks good (7 vitest tests, OpenAPI update, auto-record on distribute). However CI is failing:
src/app/api/talos/[id]/buy-token/route.ts(89,12): error TS2339: Property 'source' does not exist...
tests/buy-token.test.ts(68,78): error TS2556...
These errors may be from branch drift vs main. Please:
git rebase origin/mainpnpm exec tsc --noEmit && pnpm exec vitest run tests/dividends.test.ts- Request re-review once CI is green
Code quality is close to merge — just needs rebase + typecheck fix.
enliven17
left a comment
There was a problem hiding this comment.
Thanks for the dividends API work. I can't merge this version yet. The new tests use any in several places, so they will fail under the repository's current TypeScript/ESLint rules once lint is enforced. Please replace the any casts in web/tests/dividends.test.ts with typed test doubles or narrower request/route context types.
Please also add/verify the Drizzle migration metadata for the new 0008_add_dividends.sql migration if this repo expects generated migration journals, and rerun the dividends test plus a targeted ESLint check for the new route/test files.
enliven17
left a comment
There was a problem hiding this comment.
Thanks for the type-safety cleanup in the dividend tests. I re-ran the targeted checks and this still cannot be merged yet.
Blocking issues:
- Targeted ESLint fails in
web/src/app/api/talos/[id]/revenue/distribute/route.tsat line 110 because ofcatch (err: any). Please replace that withunknownand narrow it safely. - The branch is missing the current OpenAPI snapshot test script from
main;pnpm --dir web run test:openapireportsMissing script: test:openapi. Please rebase on the latestmainand update the OpenAPI snapshot for the new/api/talos/{id}/dividendspath. - This PR adds
web/drizzle/0008_add_dividends.sql. Another active PR is also adding a0008migration, so after rebasing please make sure the migration numbering is unique relative to currentmain.
Positive note: pnpm --dir web exec vitest run tests/dividends.test.ts passed locally.
…utes-for-recording-and-listing-dividends-REFIX
- buy-token route: read .source/.operations off the inner transaction (FeeBumpTransaction has no .source), fixing TS2339 - buy-token test: call zero-arg stellar mocks without spreading args, fixing TS2556 - regenerate openapi.snapshot.json to include the new dividends endpoints
|
Thanks for the review! Quick status on the CI after the latest push: ✅ Addressed Type check now passes — the 3 tsc errors were pre-existing in buy-token (fromXDR() can return a FeeBumpTransaction, which has no .source; and two zero-arg stellar mocks were being called with a spread). Fixed by reading .source/.operations off the inner transaction and calling those mocks without args. Lint — this fails on any errors, but they're all in older files unrelated to dividends. For reference, pnpm lint currently reports 88 problems (62 errors) on main vs 86 problems (60 errors) on this branch — i.e. this PR slightly reduces the count, and the new dividends files contribute zero lint errors. Happy to clean these up, but I left them out to keep this PR focused on dividends. Let me know if you'd like a separate lint-cleanup PR (or if you'd prefer I fix just the buy-token files I touched here). |
enliven17
left a comment
There was a problem hiding this comment.
Thanks for the update. The OpenAPI snapshot is fixed and the dividend/buy-token tests pass locally, but this still cannot be merged.
Blocking issues from my local check:
- Targeted ESLint still fails with
@typescript-eslint/no-explicit-anyerrors:web/src/app/api/talos/[id]/buy-token/route.tslines 72, 132, and 189web/src/app/api/talos/[id]/revenue/distribute/route.tsline 110- multiple
anyusages inweb/tests/buy-token.test.ts
- The latest commit changes
buy-tokenas part of the dividends PR. That file is unrelated to recording/listing dividends and now has visibly broken indentation aroundTransactionBuilder.fromXDRandconst ops. Please either split the buy-token fix into a separate PR or keep this PR scoped to dividends only. - Please keep the migration numbering unique after rebasing on the latest
main; this PR still addsweb/drizzle/0008_add_dividends.sql.
What passed locally:
pnpm --dir web run test:openapipnpm --dir web exec vitest run tests/dividends.test.ts tests/buy-token.test.ts
Please clean up lint and scope, then I can re-check.
|
it was nice working with you. you're was top notch 💯. |
|
nice to meet you bud! Thank you for your effort! |
Summary
Patrons need to track dividend distribution histories — the record of revenue
shared out to a Talos's Mitos/Pulse token holders over time.
Previously, POST /api/talos/[id]/revenue/distribute executed a proportional
USDC payout to patrons but never persisted any record of the event, so there
was no history to query. This PR adds the missing persistence layer and the two
endpoints requested in the acceptance criteria.
Changes
New tls_dividends table (id, talosId FK → cascade, amount, currency,
patronCount, totalPulse, source, txHash, breakdown JSONB, status, createdAt)
with a (talosId, createdAt) index, plus Drizzle relations and a hand-written
RLS migration (drizzle/0008_add_dividends.sql) consistent with the existing
row-level-security setup on every other table.
GET /api/talos/[id]/dividends — lists dividend distributions (public read,
last 50, newest first; 404 if the Talos doesn't exist).
POST /api/talos/[id]/dividends — records a new distribution event in the
database. Requires Authorization: Bearer <api_key> (via the existing
verifyAgentApiKey) and validates the body with a new Zod schema
(recordDividendSchema), guarding against non-positive amounts.
Auto-recording: POST /revenue/distribute now writes a tls_dividends
history row after a successful distribution (best-effort — a logging failure
never fails an on-chain settlement that already happened).
Docs: added Dividend / RecordDividendRequest schemas and the
/api/talos/{id}/dividends path to the OpenAPI spec (src/lib/openapi.ts).
These changes follow the existing conventions in revenue/route.ts and
patrons/route.ts (route structure, auth, Zod validation, error shapes).
Related Issues
Closes #58
Test Plan
Automated tests run
pnpm exec vitest run web/tests/dividends.test.ts → 7/7 pass
(GET list, GET 404, POST 403 unauthenticated, POST 400 invalid body,
POST 400 non-positive amount, POST 201 success, POST default fields).
Full unit suite (dividends + health + async-jobs-revenue) → 14/14 pass, no regressions.
pnpm exec next build → compiles successfully with full TypeScript
type-check; /api/talos/[id]/dividends is registered in the route manifest.
Manual verification steps performed:
Apply schema: pnpm --filter web db:push (or run drizzle/0008_add_dividends.sql)
to create tls_dividends.
POST /api/talos//dividends with Authorization: Bearer <api_key> and
body { "amount": "25.5", "patronCount": 4, "source": "manual" } → returns
201 with the created record.
GET /api/talos//dividends → returns the distribution just recorded.
Run POST /api/talos//revenue/distribute → response includes a
dividendId, and the event now appears in GET .../dividends.
Negative cases: missing amount → 400; missing/invalid Bearer key → 401/403;
unknown Talos id → 404.
Visual Changes (if applicable)
N/A — backend API only (no UI changes). The new endpoints are visible in the
existing API docs viewer at /docs via the updated OpenAPI spec.
Checklist
I have read the CONTRIBUTING.md guide.
My code follows the style guidelines of this project (Conventional Commits; matches existing route/schema patterns).
I have commented my code, particularly in hard-to-understand areas.
I have made corresponding changes to the documentation (OpenAPI spec).
My changes generate no new warnings or errors.
I have added tests that prove my fix is effective or that my feature works.
New and existing unit tests pass locally with my changes.
Closes #58