likecollective-indexer: add staking_rank to /book-nft/{address} response#309
likecollective-indexer: add staking_rank to /book-nft/{address} response#309williamchong wants to merge 1 commit intolikecoin:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an optional staking_rank field to the BookNFT schema and populates it on the single-book endpoint by computing the book’s global rank based on staked_amount.
Changes:
- Extend OpenAPI
BookNFTschema with optionalstaking_rank(integer) and regenerate Go types/JSON codecs to support it. - Update
QueryNFTClassto return anNFTClassWithRankwrapper containing the computed staking rank. - Populate
staking_rankinGET /book-nft/{address}response.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| likecollective-indexer/openapi/schema.yaml | Adds staking_rank to BookNFT schema. |
| likecollective-indexer/openapi/api/oas_schemas_gen.go | Adds StakingRank OptInt field plus getters/setters on generated BookNFT. |
| likecollective-indexer/openapi/api/oas_json_gen.go | Encodes/decodes staking_rank and adds JSON support for OptInt. |
| likecollective-indexer/internal/database/nft_class.go | Adds NFTClassWithRank + staking rank computation and changes QueryNFTClass return type. |
| likecollective-indexer/internal/api/openapi/get_book_nft_evm_address.go | Sets staking_rank on the single-book response model. |
| likecollective-indexer/go.sum | Updates module checksum entries (likely from regeneration/tooling). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| amountStr := (*uint256.Int)(stakedAmount).String() | ||
| higherCount, err := r.dbService.Client().NFTClass.Query().Where(func(s *sql.Selector) { | ||
| s.Where(sql.GT(nftclass.FieldStakedAmount, 0)) | ||
| s.Where(sql.ExprP("staked_amount > $1::numeric", amountStr)) | ||
| }).Count(ctx) |
There was a problem hiding this comment.
sql.ExprP is using a hard-coded Postgres placeholder ($1::numeric). Because this selector also adds other predicates with bound parameters (e.g. sql.GT(...)), $1 can end up binding to the wrong argument, producing an incorrect rank (or a placeholder/args mismatch). Use ? placeholders with sql.ExprP (e.g. staked_amount > ?::numeric) so ent can translate and number parameters correctly, and consider referencing nftclass.FieldStakedAmount instead of the literal column name to avoid drift if the schema changes.
Compute global staking rank per book using COUNT of books with higher staked_amount. Rank is optional in the schema and only populated on the single-book endpoint where position in a sorted list is not available.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func (r *nftClassRepository) computeStakingRank(ctx context.Context, stakedAmount typeutil.Uint256) (int, error) { | ||
| amountStr := (*uint256.Int)(stakedAmount).String() | ||
| higherCount, err := r.dbService.Client().NFTClass.Query().Where(func(s *sql.Selector) { | ||
| s.Where(sql.GT(nftclass.FieldStakedAmount, 0)) | ||
| s.Where(sql.GT(nftclass.FieldStakedAmount, amountStr)) | ||
| }).Count(ctx) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return higherCount + 1, nil | ||
| } |
There was a problem hiding this comment.
computeStakingRank performs an additional COUNT(*) query for every /book-nft/{address} request. Given nft_class has no index on staked_amount (see ent/schema/nftclass.go:42-46), this will likely become a sequential scan on larger datasets and adds a second DB round-trip. Consider computing the rank in the same query that fetches the row (e.g., via a correlated subquery / window function), and/or adding an index on staked_amount if this endpoint is expected to be called frequently.
Compute global staking rank per book using COUNT of books with higher staked_amount. Rank is optional in the schema and only populated on the single-book endpoint where position in a sorted list is not available.