Skip to content

likecollective-indexer: add staking_rank to /book-nft/{address} response#309

Open
williamchong wants to merge 1 commit intolikecoin:mainfrom
williamchong:main
Open

likecollective-indexer: add staking_rank to /book-nft/{address} response#309
williamchong wants to merge 1 commit intolikecoin:mainfrom
williamchong:main

Conversation

@williamchong
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 BookNFT schema with optional staking_rank (integer) and regenerate Go types/JSON codecs to support it.
  • Update QueryNFTClass to return an NFTClassWithRank wrapper containing the computed staking rank.
  • Populate staking_rank in GET /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.

Comment on lines +70 to +74
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)
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
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.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +69 to +79
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
}
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
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