autonomys / astral

Home of our Block Explorer
https://explorer.subspace.network
11 stars 9 forks source link

Sync prod with main #924

Closed marc-aurele-besner closed 1 week ago

marc-aurele-besner commented 1 week ago

User description

Sync prod with main

Including


PR Type

Enhancement, Configuration changes


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
7 files
mappingHandlers.ts
Implement staking event and call handlers for mainnet       

indexers/mainnet/staking/src/mappings/mappingHandlers.ts
  • Implemented event and call handlers for staking operations.
  • Added logic for handling domain instantiation and operator
    registration.
  • Integrated database operations for deposits, withdrawals, and rewards.
  • Added assertions and logging for event handling.
  • +1096/-0
    db.ts
    Add database helper functions for staking entities             

    indexers/mainnet/staking/src/mappings/db.ts
  • Added helper functions for database operations related to domains,
    accounts, and operators.
  • Implemented functions to check and get various entities like Domain,
    Account, Operator, etc.
  • Created functions to handle deposits and withdrawals.
  • +566/-0 
    db.ts
    Implement leaderboard database operations for mainnet       

    indexers/mainnet/leaderboard/src/mappings/db.ts
  • Added functions to manage leaderboard-related database operations.
  • Implemented functions to track account transfer counts and values.
  • Added functions for handling operator and nominator statistics.
  • +605/-0 
    mappingHandlers.ts
    Add leaderboard event handlers for mainnet indexers           

    indexers/mainnet/leaderboard/src/mappings/mappingHandlers.ts
  • Added event handlers for transfer, remark, and extrinsic events.
  • Implemented logic for updating leaderboard statistics.
  • Integrated database operations for leaderboard data.
  • +383/-0 
    mappingHandlers.ts
    Implement consensus event and call handlers for mainnet   

    indexers/mainnet/consensus/src/mappings/mappingHandlers.ts
  • Added block, call, and event handler functions for consensus.
  • Implemented logic for handling consensus-related events.
  • Integrated database operations for block and event data.
  • +223/-0 
    db.ts
    Add database operations for consensus data management       

    indexers/mainnet/consensus/src/mappings/db.ts
  • Added functions for creating and saving blocks, logs, and events.
  • Implemented database operations for consensus data.
  • Added functions for managing account history and transfers.
  • +233/-0 
    helper.ts
    Add helper functions for consensus operations                       

    indexers/mainnet/consensus/src/mappings/helper.ts
  • Added helper functions for consensus operations.
  • Implemented functions to get block author and account balance.
  • Added logic to prevent indexing too close to the chain head.
  • +90/-0   
    Configuration changes
    4 files
    project.ts
    Configure staking project with event and call handlers     

    indexers/mainnet/staking/project.ts
  • Configured the staking project with event and call handlers.
  • Set up environment variables and network configurations.
  • Defined data sources and mappings for staking events.
  • +255/-0 
    project.ts
    Configure leaderboard project with event handlers               

    indexers/mainnet/leaderboard/project.ts
  • Configured the leaderboard project with event handlers.
  • Set up environment variables and network configurations.
  • Defined data sources and mappings for leaderboard events.
  • +174/-0 
    project.ts
    Configure consensus project with block and event handlers

    indexers/mainnet/consensus/project.ts
  • Configured the consensus project with block, call, and event handlers.
  • Set up environment variables and network configurations.
  • Defined data sources and mappings for consensus events.
  • +102/-0 
    general.ts
    Add constant for testnet address prefix                                   

    explorer/src/constants/general.ts - Introduced a new constant `SUBSPACE_ACC_PREFIX_TESTNET`.
    +1/-0     
    Additional files (token-limit)
    107 files
    TestnetRewardsTable.tsx
    ...                                                                                                           

    explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx ...
    +13/-3   
    models.ts
    ...                                                                                                           

    indexers/mainnet/staking/src/mappings/models.ts ...
    +49/-0   
    utils.ts
    ...                                                                                                           

    indexers/mainnet/consensus/src/mappings/utils.ts ...
    +50/-0   
    helper.ts
    ...                                                                                                           

    indexers/mainnet/leaderboard/src/mappings/helper.ts ...
    +34/-0   
    project.ts
    ...                                                                                                           

    indexers/taurus/consensus/project.ts ...
    +3/-3     
    project.ts
    ...                                                                                                           

    indexers/taurus/leaderboard/project.ts ...
    +3/-3     
    project.ts
    ...                                                                                                           

    indexers/taurus/staking/project.ts ...
    +3/-3     
    formatAddress.ts
    ...                                                                                                           

    explorer/src/utils/formatAddress.ts ...
    +5/-2     
    types.ts
    ...                                                                                                           

    indexers/mainnet/consensus/src/mappings/types.ts ...
    +24/-0   
    index.ts
    ...                                                                                                           

    indexers/mainnet/consensus/src/index.ts ...
    +3/-0     
    index.ts
    ...                                                                                                           

    indexers/mainnet/leaderboard/src/index.ts ...
    +3/-0     
    index.ts
    ...                                                                                                           

    indexers/mainnet/staking/src/index.ts ...
    +3/-0     
    constants.ts
    ...                                                                                                           

    indexers/mainnet/staking/src/mappings/constants.ts ...
    +1/-0     
    db.js
    ...                                                                                                           

    indexers/taskboard/utils/db.js ...
    +2/-2     
    index.js
    ...                                                                                                           

    indexers/taskboard/constants/index.js ...
    +1/-1     
    docker-compose.yml
    ...                                                                                                           

    docker-compose.yml ...
    +45/-274
    schema.graphql
    ...                                                                                                           

    indexers/mainnet/staking/schema.graphql ...
    +387/-0 
    schema.graphql
    ...                                                                                                           

    indexers/mainnet/leaderboard/schema.graphql ...
    +223/-0 
    schema.graphql
    ...                                                                                                           

    indexers/mainnet/consensus/schema.graphql ...
    +172/-0 
    .env
    ...                                                                                                           

    .env ...
    +22/-18 
    staking_domains.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_domains.yaml ...
    +101/-1 
    staking_operators.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_operators.yaml ...
    +99/-1   
    docker-compose.prod.yml
    ...                                                                                                           

    docker-compose.prod.yml ...
    +5/-41   
    staking_nominators.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_nominators.yaml ...
    +89/-1   
    package.json
    ...                                                                                                           

    indexers/package.json ...
    +5/-3     
    package.json
    ...                                                                                                           

    indexers/mainnet/consensus/package.json ...
    +49/-0   
    tables.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/tables.yaml ...
    +47/-1   
    README.md
    ...                                                                                                           

    indexers/mainnet/README.md ...
    +68/-0   
    package.json
    ...                                                                                                           

    indexers/mainnet/leaderboard/package.json ...
    +46/-0   
    package.json
    ...                                                                                                           

    indexers/mainnet/staking/package.json ...
    +46/-0   
    README.md
    ...                                                                                                           

    indexers/mainnet/staking/README.md ...
    +42/-0   
    README.md
    ...                                                                                                           

    indexers/mainnet/consensus/README.md ...
    +42/-0   
    README.md
    ...                                                                                                           

    indexers/mainnet/leaderboard/README.md ...
    +42/-0   
    consensus_extrinsics.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_extrinsics.yaml ...
    +68/-1   
    consensus_blocks.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_blocks.yaml ...
    +62/-1   
    staking_withdrawals.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_withdrawals.yaml ...
    +59/-1   
    consensus_events.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_events.yaml ...
    +62/-1   
    staking_deposits.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_deposits.yaml ...
    +56/-1   
    README.md
    ...                                                                                                           

    indexers/gemini-3h/README.md ...
    +6/-12   
    README.md
    ...                                                                                                           

    indexers/taurus/README.md ...
    +6/-12   
    consensus_accounts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_accounts.yaml ...
    +56/-1   
    consensus_logs.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_logs.yaml ...
    +47/-1   
    consensus_rewards.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_rewards.yaml ...
    +47/-1   
    consensus_account_profiles.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_account_profiles.yaml ...
    +45/-1   
    consensus_extrinsic_modules.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_extrinsic_modules.yaml ...
    +43/-1   
    consensus_event_modules.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_event_modules.yaml ...
    +43/-1   
    LICENSE
    ...                                                                                                           

    indexers/mainnet/consensus/LICENSE ...
    +21/-0   
    LICENSE
    ...                                                                                                           

    indexers/mainnet/leaderboard/LICENSE ...
    +21/-0   
    LICENSE
    ...                                                                                                           

    indexers/mainnet/staking/LICENSE ...
    +21/-0   
    consensus_account_rewards.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_account_rewards.yaml ...
    +40/-1   
    consensus_sections.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_sections.yaml ...
    +40/-1   
    consensus_account_histories.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_account_histories.yaml ...
    +36/-1   
    consensus_log_kinds.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_log_kinds.yaml ...
    +31/-1   
    consensus_transfers.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/consensus_transfers.yaml ...
    +31/-1   
    staking_accounts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/staking_accounts.yaml ...
    +25/-1   
    tsconfig.json
    ...                                                                                                           

    indexers/mainnet/consensus/tsconfig.json ...
    +20/-0   
    tsconfig.json
    ...                                                                                                           

    indexers/mainnet/leaderboard/tsconfig.json ...
    +20/-0   
    tsconfig.json
    ...                                                                                                           

    indexers/mainnet/staking/tsconfig.json ...
    +20/-0   
    lerna.json
    ...                                                                                                           

    indexers/lerna.json ...
    +8/-1     
    package.json
    ...                                                                                                           

    indexers/gemini-3h/consensus/package.json ...
    +2/-2     
    init-db.sql
    ...                                                                                                           

    indexers/db/docker-entrypoint-initdb.d/init-db.sql ...
    +1/-14   
    package.json
    ...                                                                                                           

    indexers/taurus/consensus/package.json ...
    +2/-2     
    astral.code-workspace
    ...                                                                                                           

    .vscode/astral.code-workspace ...
    +4/-0     
    leaderboard_nominator_deposits_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_nominator_deposits_total_counts.yaml ...
    +17/-1   
    leaderboard_nominator_deposits_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_nominator_deposits_total_values.yaml ...
    +17/-1   
    leaderboard_nominator_withdrawals_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_nominator_withdrawals_total_counts.yaml ...
    +17/-1   
    leaderboard_farmer_vote_and_block_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_vote_and_block_total_counts.yaml ...
    +17/-1   
    leaderboard_farmer_vote_and_block_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_vote_and_block_total_values.yaml ...
    +17/-1   
    leaderboard_account_extrinsic_failed_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_extrinsic_failed_total_counts.yaml ...
    +17/-1   
    leaderboard_account_extrinsic_success_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_extrinsic_success_total_counts.yaml ...
    +17/-1   
    leaderboard_account_transaction_fee_paid_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_transaction_fee_paid_total_values.yaml ...
    +17/-1   
    leaderboard_operator_deposits_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_deposits_total_counts.yaml ...
    +17/-1   
    leaderboard_operator_deposits_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_deposits_total_values.yaml ...
    +17/-1   
    leaderboard_operator_total_rewards_collecteds.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_total_rewards_collecteds.yaml ...
    +17/-1   
    leaderboard_operator_total_tax_collecteds.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_total_tax_collecteds.yaml ...
    +17/-1   
    leaderboard_operator_withdrawals_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_withdrawals_total_counts.yaml ...
    +17/-1   
    leaderboard_account_extrinsic_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_extrinsic_total_counts.yaml ...
    +17/-1   
    leaderboard_account_transfer_receiver_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_transfer_receiver_total_counts.yaml ...
    +17/-1   
    leaderboard_account_transfer_receiver_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_transfer_receiver_total_values.yaml ...
    +17/-1   
    leaderboard_account_transfer_sender_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_transfer_sender_total_counts.yaml ...
    +17/-1   
    leaderboard_account_transfer_sender_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_transfer_sender_total_values.yaml ...
    +17/-1   
    leaderboard_farmer_block_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_block_total_counts.yaml ...
    +17/-1   
    leaderboard_farmer_block_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_block_total_values.yaml ...
    +17/-1   
    leaderboard_farmer_vote_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_vote_total_counts.yaml ...
    +17/-1   
    leaderboard_farmer_vote_total_values.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_farmer_vote_total_values.yaml ...
    +17/-1   
    leaderboard_account_remark_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_account_remark_counts.yaml ...
    +17/-1   
    leaderboard_operator_bundle_total_counts.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/leaderboard_operator_bundle_total_counts.yaml ...
    +17/-1   
    dictionary_extrinsics.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/dictionary_extrinsics.yaml ...
    +17/-1   
    package.json
    ...                                                                                                           

    indexers/gemini-3g/testnet-rewards/package.json ...
    +1/-1     
    dictionary_events.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/dictionary_events.yaml ...
    +14/-1   
    dictionary_spec_versions.yaml
    ...                                                                                                           

    indexers/db/metadata/databases/default/tables/dictionary_spec_versions.yaml ...
    +12/-1   
    typegen.json
    ...                                                                                                           

    indexers/mainnet/consensus/typegen.json ...
    +8/-0     
    dictionary
    ...                                                                                                           

    indexers/dictionary ...
    +1/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310960555_alter_table_consensus_extrinsic_modules_alter_column_id/up.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730311021220_alter_table_consensus_log_kinds_alter_column_id/up.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310946777_alter_table_consensus_event_modules_alter_column_id/up.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310983740_alter_table_consensus_account_profiles_alter_column_id/up.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310997319_alter_table_consensus_account_rewards_alter_column_id/up.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310960555_alter_table_consensus_extrinsic_modules_alter_column_id/down.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730311021220_alter_table_consensus_log_kinds_alter_column_id/down.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310914662_alter_table_consensus_sections_alter_column_id/up.sql ...
    +2/-1     
    up.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310972982_alter_table_consensus_accounts_alter_column_id/up.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310946777_alter_table_consensus_event_modules_alter_column_id/down.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310983740_alter_table_consensus_account_profiles_alter_column_id/down.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310997319_alter_table_consensus_account_rewards_alter_column_id/down.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310914662_alter_table_consensus_sections_alter_column_id/down.sql ...
    +2/-1     
    down.sql
    ...                                                                                                           

    indexers/db/migrations/default/1730310972982_alter_table_consensus_accounts_alter_column_id/down.sql ...
    +2/-1     

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    netlify[bot] commented 1 week ago

    Deploy Preview for dev-astral ready!

    Name Link
    Latest commit 7b743f0e679fec5efb9069e2f342c0b7e74df5fa
    Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/672ca9a73ccf140008478aa7
    Deploy Preview https://deploy-preview-924--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.

    netlify[bot] commented 1 week ago

    Deploy Preview for astral-prod ready!

    Name Link
    Latest commit 7b743f0e679fec5efb9069e2f342c0b7e74df5fa
    Latest deploy log https://app.netlify.com/sites/astral-prod/deploys/672ca9a78e527f0009d288e3
    Deploy Preview https://deploy-preview-924--astral-prod.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.

    socket-security[bot] commented 1 week ago

    New and removed dependencies detected. Learn more about Socket for GitHub โ†—๏ธŽ

    Package New capabilities Transitives Size Publisher
    npm/@autonomys/auto-consensus@1.0.1 None 0 78.2 kB marcaurelebesner
    npm/@autonomys/auto-utils@1.0.1 filesystem Transitive: environment, network +83 136 MB marcaurelebesner

    ๐Ÿšฎ Removed packages: npm/@autonomys/auto-consensus@0.2.0, npm/@autonomys/auto-consensus@0.6.8, npm/@autonomys/auto-utils@0.2.0, npm/@autonomys/auto-utils@0.6.8, npm/@subsquid/graphql-server@4.5.1, npm/@subsquid/ss58@2.0.2, npm/@subsquid/substrate-metadata-explorer@3.1.2, npm/@subsquid/substrate-processor@8.3.0, npm/@subsquid/substrate-typegen@8.1.0, npm/@subsquid/typeorm-codegen@2.0.0, npm/@subsquid/typeorm-migration@1.3.0, npm/@subsquid/typeorm-store@1.4.0, npm/@types/node@20.12.7, npm/pg@8.11.5, npm/typeorm@0.3.20, npm/typescript@5.4.5

    View full reportโ†—๏ธŽ

    github-actions[bot] commented 1 week ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    **๐ŸŽซ Ticket compliance analysis ๐Ÿ”ถ** **[919](https://github.com/autonomys/astral/issues/919) - Fully compliant** Fully compliant requirements: - Simplify Docker setup by consolidating network configurations into environment variables. - Update Docker Compose to dynamically use network IDs and remove specific network configurations. - Add new database table configurations for consensus and leaderboard schemas. - Remove creation of specific network databases in the initialization script. - Update environment variable names for chain ID and RPC URLs across multiple project files. **[920](https://github.com/autonomys/astral/issues/920) - Fully compliant** Fully compliant requirements: - Implement event and call handlers for staking, consensus, and leaderboard indexers on the mainnet. - Add database helper functions for managing domain, operator, and nominator data. - Configure projects with appropriate handlers and network settings. - Define TypeScript interfaces for various data structures used in the indexers. - Integrate logic for handling staking-related events, including deposits, withdrawals, and operator registration. - Add leaderboard management logic, including sorting and ranking based on events. **[923](https://github.com/autonomys/astral/issues/923) - Fully compliant** Fully compliant requirements: - Add support for formatting addresses with a testnet prefix in the `TestnetRewardsTable` component. - Introduce a new constant `SUBSPACE_ACC_PREFIX_TESTNET` for testnet address handling. - Enhance the `formatAddress` utility function to accept a custom SS58 format, allowing flexible address encoding.
    โฑ๏ธ Estimated effort to review: 5 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Possible Bug
    The logic for handling various blockchain events and calls in the staking indexer seems complex and could potentially introduce bugs or inefficiencies. It's crucial to ensure that all edge cases are handled correctly, especially around the management of deposits, withdrawals, and rewards.
    github-actions[bot] commented 1 week ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Ensure atomic operations for account record modifications to maintain data integrity ___ **Use a transaction or similar mechanism to ensure that the creation or update of
    account records and the subsequent save operation are atomic. This prevents data
    inconsistencies during concurrent access.** [indexers/mainnet/leaderboard/src/mappings/db.ts [36-48]](https://github.com/autonomys/astral/pull/924/files#diff-7a7163f599d7374df6dc09344f2650dd3f4f3f0df166044f6b581c7178cac3eaR36-R48) ```diff -account = AccountTransferSenderTotalCount.create({ - id, - rank: 0, - value, - lastContributionAt, - createdAt: blockNumber, - updatedAt: blockNumber, -}); -... -await account.save(); +const transaction = await database.transaction(); +try { + account = AccountTransferSenderTotalCount.create({ + id, + rank: 0, + value, + lastContributionAt, + createdAt: blockNumber, + updatedAt: blockNumber, + }, { transaction }); + await account.save({ transaction }); + await transaction.commit(); +} catch (error) { + await transaction.rollback(); + throw error; +} ```
    Suggestion importance[1-10]: 9 Why: Using transactions to ensure atomic operations when creating or updating records is vital for maintaining data consistency, especially in environments with concurrent access.
    9
    Add error handling for data fetching to improve robustness ___ **Consider handling potential exceptions when fetching data with
    AccountTransferSenderTotalCount.get(id). This can prevent the application from
    crashing due to unhandled promise rejections if the data fetch fails.** [indexers/mainnet/leaderboard/src/mappings/db.ts [34]](https://github.com/autonomys/astral/pull/924/files#diff-7a7163f599d7374df6dc09344f2650dd3f4f3f0df166044f6b581c7178cac3eaR34-R34) ```diff -let account = await AccountTransferSenderTotalCount.get(id); +let account; +try { + account = await AccountTransferSenderTotalCount.get(id); +} catch (error) { + console.error('Failed to fetch account:', error); + throw error; // or handle error appropriately +} ```
    Suggestion importance[1-10]: 8 Why: Adding error handling for data fetching is crucial to prevent the application from crashing due to unhandled promise rejections, enhancing the robustness of the application.
    8
    Implement transaction handling for entity creation to ensure data consistency ___ **Use a transaction or rollback mechanism when creating new entities to ensure data
    consistency in case of partial failures.** [indexers/mainnet/staking/src/mappings/db.ts [37]](https://github.com/autonomys/astral/pull/924/files#diff-86b8a40e16b8cf563207beb553fd0e739a0c25d54d3c6697578084d68c60c85dR37-R37) ```diff -domain = Domain.create({ +domain = await Domain.create({ id, ... +}).catch(error => { + console.error('Failed to create domain:', error); + throw error; // or handle rollback as appropriate }); ```
    Suggestion importance[1-10]: 7 Why: Implementing error handling and rollback mechanisms during entity creation is a best practice to ensure data consistency. This suggestion is relevant and would help in maintaining the integrity of data in case of errors during the creation process.
    7
    Add logging to track entity creation and saving operations ___ **Implement logging for critical steps within the entity update or creation process to
    aid in debugging and monitoring the application's behavior.** [indexers/mainnet/leaderboard/src/mappings/db.ts [36-48]](https://github.com/autonomys/astral/pull/924/files#diff-7a7163f599d7374df6dc09344f2650dd3f4f3f0df166044f6b581c7178cac3eaR36-R48) ```diff account = AccountTransferSenderTotalCount.create({ id, rank: 0, value, lastContributionAt, createdAt: blockNumber, updatedAt: blockNumber, }); +console.log(`Account created: ${id}`); ... await account.save(); +console.log(`Account saved: ${id}`); ```
    Suggestion importance[1-10]: 4 Why: Implementing logging for critical operations aids in debugging and monitoring, providing visibility into the application's behavior, although it's a relatively minor enhancement compared to other suggestions.
    4
    Maintainability
    Refactor repetitive entity retrieval and creation into a generic function to enhance maintainability ___ **Refactor the repeated code pattern in helper functions into a generic function to
    reduce code duplication and improve maintainability.** [indexers/mainnet/staking/src/mappings/db.ts [29-84]](https://github.com/autonomys/astral/pull/924/files#diff-86b8a40e16b8cf563207beb553fd0e739a0c25d54d3c6697578084d68c60c85dR29-R84) ```diff -export async function checkAndGetDomain( - domainId: string, - blockNumber: bigint -): Promise { - ... -} -export async function checkAndGetAccount( - id: string, - blockNumber: number -): Promise { - ... +async function checkAndGetEntity(entity: T, id: string, blockNumber: number): Promise { + let result = await entity.get(id); + if (!result) { + result = entity.create({ + id, + createdAt: BigInt(blockNumber), + updatedAt: BigInt(blockNumber), + ... + }); + } + return result; } ```
    Suggestion importance[1-10]: 9 Why: Refactoring repetitive patterns into a generic function significantly enhances code maintainability and reduces duplication. This suggestion is highly relevant as it addresses multiple similar function patterns in the file, providing a clear, maintainable solution.
    9
    Refactor repetitive entity handling into a generic function to enhance code maintainability ___ **Refactor the repeated logic for entity creation and updating into a generic function
    to reduce code duplication and improve maintainability.** [indexers/mainnet/leaderboard/src/mappings/db.ts [36-48]](https://github.com/autonomys/astral/pull/924/files#diff-7a7163f599d7374df6dc09344f2650dd3f4f3f0df166044f6b581c7178cac3eaR36-R48) ```diff -if (!account) { - account = AccountTransferSenderTotalCount.create({ - id, - rank: 0, - value, - lastContributionAt, - createdAt: blockNumber, - updatedAt: blockNumber, - }); -} else { - account.value += value; - account.updatedAt = blockNumber; +async function updateOrCreateAccount(entity, id, value, blockNumber, lastContributionAt) { + let account = await entity.get(id); + if (!account) { + account = entity.create({ + id, + rank: 0, + value, + lastContributionAt, + createdAt: blockNumber, + updatedAt: blockNumber, + }); + } else { + account.value += value; + account.updatedAt = blockNumber; + } + await account.save(); + return account; } -await account.save(); +... +await updateOrCreateAccount(AccountTransferSenderTotalCount, id, value, blockNumber, lastContributionAt); ```
    Suggestion importance[1-10]: 7 Why: Refactoring repeated logic into a generic function reduces code duplication and improves maintainability, making the codebase cleaner and easier to manage.
    7
    Possible bug
    Improve error handling in the address formatting function to ensure robustness ___ **Ensure that the formatAddress function correctly handles potential exceptions or
    errors that might arise from the keyring.encodeAddress method, especially since the
    address formatting is critical for filtering rewards based on addresses.** [explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx [13]](https://github.com/autonomys/astral/pull/924/files#diff-35e3074a14443524d99d3004b4fa803bb33ed43a0649f85383a66ac28bdc1e76R13-R13) ```diff -address = keyring.encodeAddress(accountId, ss58Format) +try { + address = keyring.encodeAddress(accountId, ss58Format) +} catch (error) { + console.error('Error formatting address:', error); + return undefined; +} ```
    Suggestion importance[1-10]: 8 Why: Enhancing error handling in the address formatting function is crucial as it directly impacts the functionality of filtering rewards based on addresses, which is a critical operation.
    8
    Ensure the useMemo hook's dependency array is correctly specified for accurate re-computation ___ **Verify the dependency array of the useMemo hook used for
    stFormattedAndMergedAddresses to ensure that all variables which affect the
    computation are included, preventing bugs related to stale closures.** [explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx [124]](https://github.com/autonomys/astral/pull/924/files#diff-35e3074a14443524d99d3004b4fa803bb33ed43a0649f85383a66ac28bdc1e76R124-R124) ```diff -}, [mySubspaceWallets, subspaceAccount]) +}, [mySubspaceWallets, subspaceAccount, SUBSPACE_ACC_PREFIX_TESTNET]) ```
    Suggestion importance[1-10]: 7 Why: Including all relevant dependencies in the useMemo hook's array is essential to ensure that the computed value updates correctly, preventing potential bugs from stale closures.
    7
    Enhancement
    Add error handling to database operations to improve robustness ___ **Consider adding error handling for database operations to manage exceptions or
    failures gracefully.** [indexers/mainnet/staking/src/mappings/db.ts [35]](https://github.com/autonomys/astral/pull/924/files#diff-86b8a40e16b8cf563207beb553fd0e739a0c25d54d3c6697578084d68c60c85dR35-R35) ```diff -let domain = await Domain.get(id); +let domain; +try { + domain = await Domain.get(id); +} catch (error) { + console.error('Failed to fetch domain:', error); + throw error; // or handle error as appropriate +} ```
    Suggestion importance[1-10]: 8 Why: Adding error handling around database get operations is crucial for robustness, especially in a production environment where failures can occur. This suggestion correctly identifies a potential improvement area and provides a practical implementation.
    8
    Avoid hardcoding values that may vary in different contexts for more flexible code ___ **Replace the hardcoded ss58Format: 42 with a dynamic value or ensure it's correctly
    set according to the context, as hardcoding may lead to incorrect address formatting
    across different network contexts.** [explorer/src/utils/formatAddress.ts [10]](https://github.com/autonomys/astral/pull/924/files#diff-ffccfbbcb813309b5f6c40cd232c7300b9f986a6be2f8a3d83dbed4d756f1424R10-R10) ```diff -const keyring = new Keyring({ type: 'sr25519', ss58Format: 42 }) +const keyring = new Keyring({ type: 'sr25519', ss58Format }) ```
    Suggestion importance[1-10]: 6 Why: Replacing hardcoded values with dynamic ones enhances the flexibility and correctness of the code across different network contexts, which is important for maintaining accurate functionality in a multi-network environment.
    6
    Performance
    Improve performance by memoizing the campaigns object ___ **Consider memoizing the campaigns object to avoid unnecessary re-renders and
    computations, especially if it relies on data that does not change frequently.** [explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx [126-128]](https://github.com/autonomys/astral/pull/924/files#diff-35e3074a14443524d99d3004b4fa803bb33ed43a0649f85383a66ac28bdc1e76R126-R128) ```diff -const campaigns: Record = useMemo( - () => ({ - aries: { +const campaigns: Record = useMemo(() => ({ + aries: { + ... + }, +}), [/* dependencies if any */]); ```
    Suggestion importance[1-10]: 5 Why: Memoizing the campaigns object can potentially improve performance by avoiding unnecessary re-renders, although the impact may be moderate depending on the frequency of updates and complexity of the object.
    5
    Optimize entity creation by removing redundant fields ___ **Optimize the creation of new entities by checking the necessity of each field and
    removing any redundant or unused fields.** [indexers/mainnet/staking/src/mappings/db.ts [37]](https://github.com/autonomys/astral/pull/924/files#diff-86b8a40e16b8cf563207beb553fd0e739a0c25d54d3c6697578084d68c60c85dR37-R37) ```diff domain = Domain.create({ id, - sortId: BigInt(id), ... }); ```
    Suggestion importance[1-10]: 5 Why: While the suggestion to remove redundant fields could potentially simplify the entity creation, it lacks specific details on which fields are redundant and why. The suggestion is somewhat valid but needs more precise details to be fully actionable.
    5