Pull Request: Add GraphQL query limiting, public TVL, and portfolio analytics endpoints - #904
Merged
Merged
Conversation
…arYield#774) Deeply nested or overly broad GraphQL queries can drive excessive DB load. Enforce a max depth of 7 and a max complexity of 200 (each field costs 1, list-returning fields cost 10) via validation rules wired into Apollo Server, returning a descriptive error when either limit is exceeded.
…Yield#775) GET /api/v1/analytics/tvl returns total value locked plus active and funding vault counts across all non-archived vaults, with no authentication required and a 30s Cache-Control header, so dashboards no longer need admin credentials just to read platform TVL. Also adds the response types for the portfolio allocation and diversification endpoints (StellarYield#776, StellarYield#777) to types/index.ts.
…StellarYield#776, StellarYield#777) GET /api/v1/users/:address/portfolio/allocation groups a user's positions by vaults.rwa_category and returns each category's deposited total and share of the portfolio (percentages sum to 100). GET /api/v1/users/:address/portfolio/diversification scores concentration via the Herfindahl-Hirschman Index over per-vault deposits: score = (1 - HHI) * 100, so a single position scores 0 and four equally-weighted vaults score ~75.
|
@Maxwell316 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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds GraphQL query depth/complexity limiting and three new analytics endpoints.
Closes #774
Closes #775
Closes #776
Closes #777
Issues Fixed
1. GraphQL query depth and complexity limiting (#774)
Deep or complex GraphQL queries can cause excessive DB load. This adds validation-level
limits to the existing Apollo Server:
graphql-depth-limitandgraphql-query-complexity.src/graphql/queryLimits.tsexportsdepthLimitRuleandcomplexityLimitRule,wired into
ApolloServer'svalidationRules.Query depth {n} exceeds maximum of 7.field whose type resolves to a list costs 10. Exceeding it returns
Query complexity {n} exceeds maximum of 200.2. Public cross-vault TVL aggregate (#775)
GET /api/v1/admin/statsincludes platform-wide TVL but is admin-gated. Dashboards needa public equivalent:
GET /api/v1/analytics/tvlreturns{ totalValueLocked, activeVaultCount, fundingVaultCount }.totalValueLockedsumstotal_assetsacross all non-archived vaults; the two countsare vaults in the
ActiveandFundingstates respectively.analyticsRouter.Cache-Control: max-age=30.3. Portfolio asset allocation breakdown (#776)
GET /api/v1/users/:address/portfolio/allocationreturns{ allocations: [{ category, deposited, percentage }] }.vaults.rwa_category(falling back to"Uncategorized"),summing
depositedper category.percentageis left unrounded (categoryDeposited / totalDeposited * 100) so thatpercentages across categories sum to 100 within floating-point precision.
{ allocations: [] }for a user with no positions.4. Portfolio diversification score (#777)
GET /api/v1/users/:address/portfolio/diversificationreturns{ score, vaultCount, categoryCount, herfindahlIndex }.herfindahlIndexis the sum of squared per-vault deposit shares — lower means morediversified.
score = (1 - herfindahlIndex) * 100, rounded to one decimal place.score: 0; a user with equal deposits acrossfour vaults gets
score: 75.Verification
npx tsc --noEmit— cleansrc/graphql/queryLimits.test.ts,src/api/controllers/analytics.test.ts,src/services/user.portfolio-analytics.test.tsnpx vitest run— all tests pass except two pre-existing, unrelated flakes(
src/services/indexer.test.ts,src/api/controllers/admin.test.ts), both confirmedpresent on
mainprior to this change and passing when run in isolation