Skip to content

fix: Read category index instead of tag index in category_stats #876

Description

@Samuel1505

Description

// analytics.rs:50-59
pub fn category_stats(env: &Env, category_id: u32) -> CategoryStats {
    ...
    // Iterate through tag index to find grants in this category
    let grant_ids = Storage::get_tag_index(env, category_id);
    ...
}

Storage::get_tag_index (storage/helpers.rs, line 927) reads DataKey::Grant(GrantKey::TagIndex(u32)), which is populated only by grant_tags::hash_tag's hash of a freeform tag string — a 32-bit hash value, not a small sequential category id. The correct per-category index is Storage::get_category_index (storage/helpers.rs, line 953), keyed by GrantKey::CategoryIndex(u32), which is exactly what grant_tags::find_by_category correctly uses elsewhere in the codebase.

Since a small integer category_id (0, 1, 2, …) will essentially never collide with a 32-bit tag hash, get_tag_index(category_id) returns an empty list for every real category. Any number of grants can be correctly tagged into a category via grant_tags::tag_grant, but analytics_category_stats(category_id) will always report total_grants = 0, completed_grants = 0, total_funded = 0, success_rate_bps = 0. This also silently breaks build_snapshot's "top category by funding" logic (which calls category_stats internally and picks the category with the highest total_funded, initialized to 0) — top_category_id can never be selected, with no error surfaced to callers.

Technical Requirements

Files to update

  • contracts/contracts/stellar-grants/src/analytics.rs (category_stats, lines 50-93)

Fix direction

let grant_ids = Storage::get_category_index(env, category_id);

Acceptance Criteria

  • analytics_category_stats(category_id) correctly reflects grants tagged into that category via grant_tags::tag_grant.
  • A test tags several grants into a category, calls category_stats, and asserts total_grants/total_funded match expectations (nonzero).
  • build_snapshot's top-category selection works correctly once fed real per-category data.
  • cargo test passes.

Estimated Effort

Beginner: 2 hours
Intermediate: 1 hour
Expert: 0.5 hours

How to work this issue

  1. Read contracts/ContributionGuide.md for the contribution workflow.
  2. Comment on the issue to claim it before starting.
  3. Branch: fix/issue-931-analytics-category-index.
  4. Run cargo fmt, cargo clippy -- -D warnings, cargo test before opening your PR.
  5. Use a Conventional Commit message, e.g. fix: read category index instead of tag index in category_stats.

Before you start

If you find this project interesting, please consider starring the repository on GitHub. It helps the project gain visibility and supports the Drips Wave program that rewards contributors for merged fixes like this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions