Skip to content

Backend: indexes, cursor pagination, Stellar address validation, and market cache - #1320

Merged
Ehonrie merged 5 commits into
Netwalls:mainfrom
celina005:drips/1243-1244-1245-1246
Sep 28, 2026
Merged

Ehonrie merged 5 commits into
Netwalls:mainfrom
celina005:drips/1243-1244-1245-1246

Conversation

@celina005

Copy link
Copy Markdown
Contributor

Summary

Backend: indexes, cursor pagination, Stellar address validation, and market cache

What was solved

#1243 — B-65: Add Bet index on bettor for address queries

Add database indexes to support address-based Bet queries and Market list queries: a composite index on Bet leading with bettor (plus placedAt) and a composite index on Market (status, scheduledAt), delivered via a new Prisma migration and schema.prisma update.

Addressed:

  • Changed: backend/prisma/schema.prisma, backend/prisma/migrations/20260626000000_add_market_search/migration.sql
  • Add @@index([bettor, placedAt]) (or similar) on the Bet model in backend/prisma/schema.prisma
  • Add @@index([status, scheduledAt]) on the Market model in backend/prisma/schema.prisma
  • Provide the index changes via a new Prisma migration SQL file under backend/prisma/migrations

#1244 — B-66: Cursor pagination for market bets and user bets

Add cursor-based pagination (max limit 100) to the market bets and user bets endpoints, with stable ordering by (placedAt, id) and a nextCursor in the response, plus tests covering first, middle, and last pages.

Addressed:

  • Changed: backend/src/services/bet.service.ts, backend/src/api/controllers/market.controller.ts, backend/src/api/controllers/user.controller.ts
  • Support ?cursor=&limit= query params on GET /api/markets/:id/bets and GET /api/users/:address/bets
  • Enforce a maximum limit of 100
  • Return a nextCursor field in the response

#1245 — B-67: Shared zod schema for Stellar address params

Add a shared stellarAddress zod schema built on StrKey.isValidEd25519PublicKey in the backend validation middleware, and apply it to all routes with :address params so invalid addresses return HTTP 400 with error code INVALID_ADDRESS.

Addressed:

  • Changed: backend/src/api/middleware/validate.ts, backend/src/api/middleware/validate.ts, backend/src/api/routes/bet.routes.ts, backend/src/api/middleware/validate.ts
  • Create a stellarAddress zod schema in backend/src/api/middleware/validate.ts that validates Stellar Ed25519 public keys using StrKey.isValidEd25519PublicKey from @stellar/stellar-sdk (or the SDK already used in the repo).
  • Apply the stellarAddress schema to every route param named :address (e.g. bet routes, user routes, and any other route declaring :address).
  • Ensure invalid addresses are rejected with HTTP status 400 and error code INVALID_ADDRESS.

#1246 — B-68: Cache market list and stats in Redis

Add short-TTL Redis caching for market list and stats responses in the market service, and invalidate the relevant cache keys from the indexer when BetPlaced or market status events are processed. Include cache hit/miss ratio logging to satisfy the acceptance criteria.

Addressed:

  • Changed: backend/src/services/market.service.ts, backend/src/services/indexer.service.ts, backend/src/config.ts
  • Cache market list and stats responses with a short TTL
  • Indexer must invalidate relevant cache keys on BetPlaced and status events
  • Log cache hit ratio

Changes

  • backend/prisma/schema.prisma (modify)
  • backend/prisma/migrations/20260626000000_add_market_search/migration.sql (modify)
  • backend/src/services/bet.service.ts (modify)
  • backend/src/api/controllers/market.controller.ts (modify)
  • backend/src/api/controllers/user.controller.ts (modify)
  • backend/src/api/middleware/validate.ts (modify)
  • backend/src/api/routes/bet.routes.ts (modify)
  • backend/src/services/market.service.ts (modify)
  • backend/src/services/indexer.service.ts (modify)
  • backend/src/config.ts (modify)

Approach

  1. B-65: Add Bet index on bettor for address queries #1243 — B-65: Add Bet index on bettor for address queries (Changed: backend/prisma/schema.prisma, backend/prisma/migrations/20260626000000_add_market_search/migration.sql)
  2. B-66: Cursor pagination for market bets and user bets #1244 — B-66: Cursor pagination for market bets and user bets (Changed: backend/src/services/bet.service.ts, backend/src/api/controllers/market.controller.ts, backend/src/api/controllers/user.controller.ts)
  3. B-67: Shared zod schema for Stellar address params #1245 — B-67: Shared zod schema for Stellar address params (Changed: backend/src/api/middleware/validate.ts, backend/src/api/middleware/validate.ts, backend/src/api/routes/bet.routes.ts, backend/src/api/middleware/validate.ts)
  4. B-68: Cache market list and stats in Redis #1246 — B-68: Cache market list and stats in Redis (Changed: backend/src/services/market.service.ts, backend/src/services/indexer.service.ts, backend/src/config.ts)

Issues

Closes #1243
Closes #1244
Closes #1245
Closes #1246

@Ehonrie
Ehonrie merged commit 14b003a into Netwalls:main Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants