Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add leaderboard-squid and leaderboard v2 #797

Merged
merged 18 commits into from
Aug 12, 2024

Conversation

marc-aurele-besner
Copy link
Collaborator

@marc-aurele-besner marc-aurele-besner commented Aug 9, 2024

User description

Add leaderboard-squid and leaderboard v2

Screenshots

image


PR Type

enhancement, tests


Description

  • Added comprehensive type definitions for various entities in leaderboard-squid.
  • Implemented multiple GraphQL queries for retrieving leaderboard metrics.
  • Introduced a caching mechanism to optimize leaderboard data handling.
  • Updated ExplorerQueryState interface and removed unused imports.
  • Modified leaderboard page to use the AccountLeaderboard component.

Changes walkthrough 📝

Relevant files
Enhancement
5 files
v0.ts
Add comprehensive type definitions for leaderboard-squid 

indexers/leaderboard-squid/src/types/v0.ts

  • Added multiple type definitions for various entities.
  • Defined interfaces and corresponding sts.Type structures.
  • Included enums and complex nested types.
  • +3282/-1
    leaderboard.query.ts
    Add GraphQL queries for leaderboard metrics                           

    explorer/src/components/Leaderboard/leaderboard.query.ts

  • Added multiple GraphQL queries for leaderboard data.
  • Defined queries for various metrics like transfer counts, values, and
    extrinsic counts.
  • +643/-0 
    cache.ts
    Implement caching mechanism for leaderboard data                 

    indexers/leaderboard-squid/src/utils/cache.ts

  • Implemented caching mechanism for leaderboard data.
  • Added functions to load and save cache entries.
  • Defined cache structure and initialization.
  • +340/-0 
    query.ts
    Update ExplorerQueryState interface                                           

    explorer/src/states/query.ts

    • Modified ExplorerQueryState interface.
    • Removed unused import.
    +1/-13   
    page.tsx
    Update leaderboard page to use AccountLeaderboard component

    explorer/src/app/[chain]/leaderboard/page.tsx

    • Updated import to use AccountLeaderboard component.
    +2/-2     
    Additional files (token-limit)
    87 files
    leaderboard.ts
    ...                                                                                                           

    explorer/gql/types/leaderboard.ts

    ...

    +6578/-1
    staking.ts
    ...                                                                                                           

    explorer/gql/types/staking.ts

    ...

    +2841/-362
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/domains/storage.ts

    ...

    +1226/-0
    v5.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/v5.ts

    ...

    +2724/-0
    v1.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/v1.ts

    ...

    +2706/-0
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/system/storage.ts

    ...

    +405/-0 
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/subspace/storage.ts

    ...

    +390/-0 
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/balances/storage.ts

    ...

    +253/-0 
    index.tsx
    ...                                                                                                           

    explorer/src/components/Leaderboard/index.tsx

    ...

    +279/-20
    sort.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/utils/sort.ts

    ...

    +289/-0 
    account.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/storage/account.ts

    ...

    +283/-0 
    LeaderboardList.tsx
    ...                                                                                                           

    explorer/src/components/Leaderboard/LeaderboardList.tsx

    ...

    +253/-0 
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/balances/events.ts

    ...

    +312/-0 
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/domains/calls.ts

    ...

    +278/-0 
    staking.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/staking.ts

    ...

    +236/-0 
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/domains/events.ts

    ...

    +250/-0 
    operator.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/storage/operator.ts

    ...

    +191/-0 
    farmer.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/storage/farmer.ts

    ...

    +187/-0 
    v3.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/v3.ts

    ...

    +205/-0 
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/domains/constants.ts

    ...

    +213/-0 
    farmer.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/farmer.ts

    ...

    +151/-0 
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/subspace/constants.ts

    ...

    +154/-0 
    support.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/support.ts

    ...

    +142/-0 
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-fees/storage.ts

    ...

    +86/-0   
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/system/calls.ts

    ...

    +146/-0 
    nominator.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/storage/nominator.ts

    ...

    +98/-0   
    account.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/account.ts

    ...

    +108/-0 
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/balances/calls.ts

    ...

    +119/-0 
    extrinsic.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/extrinsic.ts

    ...

    +96/-0   
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/rewards/storage.ts

    ...

    +82/-0   
    processor.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/processor.ts

    ...

    +91/-0   
    index.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/index.ts

    ...

    +76/-0   
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/subspace/calls.ts

    ...

    +79/-0   
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/system/events.ts

    ...

    +94/-0   
    index.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/utils/index.ts

    ...

    +48/-0   
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/balances/constants.ts

    ...

    +59/-0   
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/system/constants.ts

    ...

    +66/-0   
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/rewards/constants.ts

    ...

    +61/-0   
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-payment/constants.ts

    ...

    +31/-0   
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-fees/events.ts

    ...

    +54/-0   
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-fees/constants.ts

    ...

    +43/-0   
    indexers.ts
    ...                                                                                                           

    explorer/src/constants/indexers.ts

    ...

    +4/-1     
    OperatorsList.tsx
    ...                                                                                                           

    explorer/src/components/Staking/OperatorsList.tsx

    ...

    +3/-7     
    fee.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/events/fee.ts

    ...

    +31/-0   
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-payment/storage.ts

    ...

    +22/-0   
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/subspace/events.ts

    ...

    +31/-0   
    index.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/blocks/index.ts

    ...

    +20/-0   
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/rewards/events.ts

    ...

    +30/-0   
    page.tsx
    ...                                                                                                           

    explorer/src/app/[chain]/leaderboard/accounts/page.tsx

    ...

    +26/-0   
    ChainProvider.tsx
    ...                                                                                                           

    explorer/src/providers/ChainProvider.tsx

    ...

    +2/-0     
    MyPositionSwitch.tsx
    ...                                                                                                           

    explorer/src/components/common/MyPositionSwitch.tsx

    ...

    +8/-2     
    LeaderboardHeader.tsx
    ...                                                                                                           

    explorer/src/components/layout/LeaderboardHeader.tsx

    ...

    +7/-3     
    staking.query.ts
    ...                                                                                                           

    explorer/src/components/Staking/staking.query.ts

    ...

    +3/-3     
    OperatorNominatorTable.tsx
    ...                                                                                                           

    explorer/src/components/Staking/OperatorNominatorTable.tsx

    ...

    +2/-2     
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/rewards/calls.ts

    ...

    +16/-0   
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/transaction-payment/events.ts

    ...

    +18/-0   
    number.ts
    ...                                                                                                           

    explorer/src/utils/number.ts

    ...

    +2/-2     
    page.tsx
    ...                                                                                                           

    explorer/src/app/[chain]/leaderboard/nominators/page.tsx

    ...

    +2/-2     
    page.tsx
    ...                                                                                                           

    explorer/src/app/[chain]/leaderboard/operators/page.tsx

    ...

    +2/-2     
    main.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/main.ts

    ...

    +15/-0   
    page.tsx
    ...                                                                                                           

    explorer/src/app/[chain]/leaderboard/farmers/page.tsx

    ...

    +2/-2     
    codegen.ts
    ...                                                                                                           

    explorer/codegen.ts

    ...

    +5/-0     
    index.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/index.ts

    ...

    +8/-0     
    constants.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/constants.ts

    ...

    +7/-0     
    events.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/events.ts

    ...

    +7/-0     
    storage.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/storage.ts

    ...

    +7/-0     
    routes.ts
    ...                                                                                                           

    explorer/src/constants/routes.ts

    ...

    +1/-0     
    calls.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/types/calls.ts

    ...

    +5/-0     
    index.ts
    ...                                                                                                           

    indexers/leaderboard-squid/src/model/index.ts

    ...

    +1/-0     
    1723206699043-Data.js
    ...                                                                                                           

    indexers/leaderboard-squid/db/migrations/1723206699043-Data.js

    ...

    +105/-0 
    docker-run.sh
    ...                                                                                                           

    indexers/leaderboard-squid/scripts/docker-run.sh

    ...

    +5/-0     
    gemini3h.jsonl
    ...                                                                                                           

    indexers/leaderboard-squid/gemini3h.jsonl

    ...

    +6/-1     
    package-lock.json
    ...                                                                                                           

    indexers/leaderboard-squid/package-lock.json

    ...

    +5168/-1
    README.md
    ...                                                                                                           

    indexers/leaderboard-squid/README.md

    ...

    +194/-0 
    schema.graphql
    ...                                                                                                           

    indexers/leaderboard-squid/schema.graphql

    ...

    +219/-0 
    commands.json
    ...                                                                                                           

    indexers/leaderboard-squid/commands.json

    ...

    +97/-0   
    package.json
    ...                                                                                                           

    indexers/leaderboard-squid/package.json

    ...

    +38/-0   
    typegen.json
    ...                                                                                                           

    indexers/leaderboard-squid/typegen.json

    ...

    +48/-0   
    LICENSE
    ...                                                                                                           

    indexers/leaderboard-squid/LICENSE

    ...

    +21/-0   
    squid.yaml
    ...                                                                                                           

    indexers/leaderboard-squid/squid.yaml

    ...

    +33/-0   
    tsconfig.json
    ...                                                                                                           

    indexers/leaderboard-squid/tsconfig.json

    ...

    +19/-0   
    .env
    ...                                                                                                           

    indexers/leaderboard-squid/.env

    ...

    +8/-0     
    docker-compose.yml
    ...                                                                                                           

    indexers/leaderboard-squid/docker-compose.yml

    ...

    +15/-0   
    astral.code-workspace
    ...                                                                                                           

    .vscode/astral.code-workspace

    ...

    +4/-0     
    .squidignore
    ...                                                                                                           

    indexers/leaderboard-squid/.squidignore

    ...

    +7/-0     
    .dockerignore
    ...                                                                                                           

    indexers/leaderboard-squid/.dockerignore

    ...

    +4/-0     
    .npmrc
    ...                                                                                                           

    indexers/leaderboard-squid/.npmrc

    ...

    +1/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    netlify bot commented Aug 9, 2024

    Deploy Preview for dev-astral ready!

    Name Link
    🔨 Latest commit 710c655
    🔍 Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/66ba206d24d30d0008898f59
    😎 Deploy Preview https://deploy-preview-797--dev-astral.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    Copy link

    github-actions bot commented Aug 9, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Complexity and Size
    The file is extremely large (3281 lines), which could make maintenance and understanding difficult. Consider splitting into smaller, more manageable components.

    Code Duplication
    There is a pattern of repeated code for creating and fetching data models (e.g., createOperatorTotalRewardsCollected and getOrCreateOperatorTotalRewardsCollected). Consider refactoring to reduce duplication, possibly by using higher-order functions or generics.

    Copy link

    github-actions bot commented Aug 9, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Correct the type of _in to match expected integer inputs

    The _in field in Int_Array_Comparison_Exp should be an array of integers instead of
    an array of arrays of integers to match the expected input type for integer
    comparisons.

    explorer/gql/types/staking.ts [41]

    -_in?: InputMaybe<Array<Array<Scalars['Int']['input']>>>;
    +_in?: InputMaybe<Array<Scalars['Int']['input']>>;
     
    Suggestion importance[1-10]: 10

    Why: This suggestion corrects a potential bug by ensuring the type matches the expected input, which is critical for the correct functioning of integer comparisons.

    10
    Best practice
    Improve type safety by specifying more precise types

    Consider using a more specific type than any for the input and output fields in the
    numeric and timestamptz types to improve type safety and clarity in your TypeScript
    code.

    explorer/gql/types/staking.ts [15-16]

    -numeric: { input: any; output: any; }
    -timestamptz: { input: any; output: any; }
    +numeric: { input: number; output: number; }
    +timestamptz: { input: Date; output: string; }
     
    Suggestion importance[1-10]: 9

    Why: This suggestion improves type safety and clarity, which is crucial in TypeScript for catching errors early and improving code maintainability. The proposed types are more specific and appropriate for the fields in question.

    9
    Use boolean type for _is_null to reflect its logical usage

    For the _is_null field in the Boolean_Comparison_Exp and Int_Array_Comparison_Exp,
    consider using a type that explicitly represents a boolean (true or false) instead
    of InputMaybe<Scalars['Boolean']['input']>, as _is_null logically represents a boolean state.

    explorer/gql/types/staking.ts [25]

    -_is_null?: InputMaybe<Scalars['Boolean']['input']>;
    +_is_null?: boolean;
     
    Suggestion importance[1-10]: 8

    Why: Using a boolean type for _is_null is more logical and improves code readability and correctness. This change aligns the type with its intended use, reducing potential confusion.

    8
    Robustness
    Add error handling to Apollo client queries to enhance robustness

    Consider adding error handling for the Apollo client query within the
    LeaderboardList component. This will improve the robustness of the component by
    handling potential errors that might occur during data fetching.

    explorer/src/components/Leaderboard/LeaderboardList.tsx [170-182]

     const {
       loading,
       data: listData,
    +  error,
       setIsVisible,
     } = useSquidQuery<
       AccountTransferSenderTotalCountQuery,
       AccountTransferSenderTotalCountQueryVariables
     >(query, {
       variables,
       skip: !inFocus,
       pollInterval: 6000,
       context: { clientName: 'leaderboard' },
     })
     
    +if (error) {
    +  console.error('Fetching error:', error);
    +  return <div>Error loading data!</div>;
    +}
    +
    Suggestion importance[1-10]: 9

    Why: Adding error handling is crucial for robustness and user experience, as it ensures that potential errors during data fetching are properly managed and communicated.

    9
    Enhancement
    Add error handling for schema generation to improve robustness

    Consider adding error handling or a fallback mechanism for the schema generation in
    the configuration. This can help ensure that the application behaves predictably in
    case the defaultIndexer.squids.leaderboard schema is not available or fails to load.

    explorer/codegen.ts [9-12]

     './gql/types/leaderboard.ts': {
    -  schema: defaultIndexer.squids.leaderboard,
    +  schema: defaultIndexer.squids.leaderboard || fallbackSchema,
       documents: ['./src/**/leaderboard.query.ts'],
       plugins: ['typescript', 'typescript-operations'],
    +  onError: (error) => {
    +    console.error('Failed to load leaderboard schema:', error);
    +  }
     },
     
    Suggestion importance[1-10]: 8

    Why: Adding error handling and a fallback mechanism for the schema generation improves the robustness and predictability of the application, especially if the schema fails to load.

    8
    Add missing fields to Bundle_Author_Order_By for consistency

    Ensure that the Bundle_Author_Order_By type includes ordering options for all fields
    in the Bundle_Author type to maintain consistency and full functionality in ordering
    queries.

    explorer/gql/types/staking.ts [508-514]

     export type Bundle_Author_Order_By = {
       account?: InputMaybe<Account_Order_By>;
       account_id?: InputMaybe<Order_By>;
       bundle?: InputMaybe<Bundle_Order_By>;
       bundle_id?: InputMaybe<Order_By>;
       id?: InputMaybe<Order_By>;
       operator?: InputMaybe<Operator_Order_By>;
       operator_id?: InputMaybe<Order_By>;
    +  // Add any missing fields here
     };
     
    Suggestion importance[1-10]: 7

    Why: While the suggestion is valid for maintaining consistency, it lacks specific details on which fields are missing. It is a good enhancement but not critical without knowing the exact missing fields.

    7
    Maintainability
    Reduce code duplication by using a map function for tabs

    Refactor the repeated use of Tab and LeaderboardList components in the Leaderboard
    component to use a map function over an array of tab configurations. This will
    reduce code duplication and improve maintainability.

    explorer/src/components/Leaderboard/index.tsx [47-123]

    -<Tab title='Transfer Sender Count'>
    -  <LeaderboardList
    -    title='Transfer Sender Count'
    -    query={Query.QUERY_ACCOUNT_TRANSFER_SENDER_TOTAL_COUNT}
    -    table='account_transfer_sender_total_count'
    -    idLink={(id) => INTERNAL_ROUTES.accounts.id.page(network, Routes.consensus, id)}
    -    valueType='number'
    -  />
    -</Tab>
    -<Tab title='Transfer Sender Value'>
    -  <LeaderboardList
    -    title='Transfer Sender Value'
    -    query={Query.QUERY_ACCOUNT_TRANSFER_SENDER_TOTAL_VALUE}
    -    table='account_transfer_sender_total_value'
    -    idLink={(id) => INTERNAL_ROUTES.accounts.id.page(network, Routes.consensus, id)}
    -  />
    -</Tab>
    -...
    +{tabs.map(tab => (
    +  <Tab title={tab.title}>
    +    <LeaderboardList
    +      title={tab.title}
    +      query={tab.query}
    +      table={tab.table}
    +      idLink={(id) => INTERNAL_ROUTES.accounts.id.page(network, Routes.consensus, id)}
    +      valueType={tab.valueType}
    +    />
    +  </Tab>
    +))}
     
    Suggestion importance[1-10]: 8

    Why: Refactoring to use a map function reduces code duplication and improves maintainability, making the codebase easier to manage and extend in the future.

    8
    Refactor scalar type definitions using a generic utility type

    Refactor the scalar type definitions to use a generic utility type for better
    maintainability and to avoid redundancy. This approach can help in managing changes
    to the scalar types more efficiently.

    explorer/gql/types/leaderboard.ts [9-17]

    +type ScalarConfig<TInput, TOutput> = { input: TInput; output: TOutput; };
     export type Scalars = {
    -  ID: { input: string; output: string; }
    -  String: { input: string; output: string; }
    -  Boolean: { input: boolean; output: boolean; }
    -  Int: { input: number; output: number; }
    -  Float: { input: number; output: number; }
    -  numeric: { input: any; output: any; }
    -  timestamptz: { input: any; output: any; }
    +  ID: ScalarConfig<string, string>;
    +  String: ScalarConfig<string, string>;
    +  Boolean: ScalarConfig<boolean, boolean>;
    +  Int: ScalarConfig<number, number>;
    +  Float: ScalarConfig<number, number>;
    +  numeric: ScalarConfig<any, any>;
    +  timestamptz: ScalarConfig<any, any>;
     };
     
    Suggestion importance[1-10]: 7

    Why: Refactoring the scalar type definitions to use a generic utility type enhances maintainability and reduces redundancy, making future changes easier to manage.

    7
    Improve the structure of the return statement for future extensibility

    Replace the direct return of the component with a more
    structured return statement that includes a React fragment. This will make it easier
    to add additional components or logic in the future without restructuring the return
    statement.

    explorer/src/app/[chain]/leaderboard/operators/page.tsx [25]

    -return <OperatorLeaderboard />
    +return (
    +  <>
    +    <OperatorLeaderboard />
    +  </>
    +)
     
    Suggestion importance[1-10]: 5

    Why: The suggestion improves maintainability by making it easier to add additional components or logic in the future. However, it is not crucial and brings only a small improvement.

    5
    Performance
    Use memoization for the isDesktop variable to improve performance

    It's recommended to use memoization for the isDesktop variable to avoid unnecessary
    re-renders when the component state or props do not change. This can enhance
    performance, especially in components that rely heavily on responsive design
    features.

    explorer/src/components/Leaderboard/index.tsx [41]

    -const isDesktop = useMediaQuery('(min-width: 640px)')
    +const isDesktop = useMemo(() => useMediaQuery('(min-width: 640px)'), []);
     
    Suggestion importance[1-10]: 7

    Why: Memoizing the isDesktop variable can enhance performance by avoiding unnecessary re-renders. This is beneficial but not critical.

    7

    @marc-aurele-besner marc-aurele-besner marked this pull request as ready for review August 11, 2024 15:00
    Copy link
    Contributor

    @clostao clostao left a comment

    Choose a reason for hiding this comment

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

    LGTM

    @marc-aurele-besner marc-aurele-besner merged commit 118cd8b into main Aug 12, 2024
    13 checks passed
    @marc-aurele-besner marc-aurele-besner deleted the feat/addLeaderboardSquid branch August 12, 2024 19:39
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants