autonomys / astral

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

Move most of the testnet rewards logic to a reusable file and reuse it for account previous rewards #936

Open marc-aurele-besner opened 2 days ago

marc-aurele-besner commented 2 days ago

User description

Move most of the testnet rewards logic to a reusable file and reuse it for account previous rewards

image


PR Type

enhancement, tests


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
AccountPreviousRewards.tsx
Refactor Account Previous Rewards with Reusable Logic       

explorer/src/components/Consensus/Account/AccountPreviousRewards.tsx
  • Refactored to use reusable testnet rewards logic.
  • Introduced new state variables for loading and aggregation.
  • Replaced manual CSV parsing with utility functions.
  • Updated UI to reflect new data handling.
  • +108/-219
    TestnetRewardsTable.tsx
    Update Testnet Rewards Table with New Logic                           

    explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx
  • Refactored to use new testnet rewards utility functions.
  • Removed redundant code and type definitions.
  • Updated data fetching and processing logic.
  • Enhanced UI to display updated rewards information.
  • +35/-278
    testnetRewards.ts
    Introduce Testnet Rewards Utility Functions                           

    explorer/src/utils/testnetRewards.ts
  • Added utility functions for testnet rewards calculations.
  • Defined types for rewards and campaigns.
  • Implemented functions for fetching and processing rewards data.
  • Provided functions for calculating user-specific rewards.
  • +327/-0 

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

    netlify[bot] commented 2 days ago

    Deploy Preview for dev-astral ready!

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

    github-actions[bot] commented 2 days ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    โฑ๏ธ Estimated effort to review: 4 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšช
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Code Complexity
    The component has a high level of complexity and many dependencies, which might affect maintainability. Data Fetching
    The function `getTestnetRewards` performs data fetching and processing which could be optimized or handled differently to avoid potential performance issues.
    github-actions[bot] commented 2 days ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add error handling to the asynchronous handleLoad function to improve robustness ___ **Ensure that the handleLoad function handles potential errors during the asynchronous
    operation to prevent the application from crashing due to unhandled promise
    rejections.** [explorer/src/components/Consensus/Account/AccountPreviousRewards.tsx [41-46]](https://github.com/autonomys/astral/pull/936/files#diff-8e3727ff57139c5207fda22d91439d50e5316030f92ce46cf616395d4c6a25e2R41-R46) ```diff const handleLoad = useCallback(async () => { - const { rewardsData, totals } = await getTestnetRewards() - setAllRewards(rewardsData) - setTotalRewards(totals) - setIsLoaded(true) + try { + const { rewardsData, totals } = await getTestnetRewards() + setAllRewards(rewardsData) + setTotalRewards(totals) + setIsLoaded(true) + } catch (error) { + console.error('Failed to load testnet rewards:', error) + } }, []) ```
    Suggestion importance[1-10]: 8 Why: Adding error handling to the asynchronous function prevents the application from crashing due to unhandled promise rejections, which is crucial for maintaining robustness and reliability.
    8
    Add error handling to the handleLoad function to prevent crashes from unhandled exceptions ___ **Implement error handling in the handleLoad function to manage exceptions that may
    occur during the fetching and processing of testnet rewards data.** [explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx [111-116]](https://github.com/autonomys/astral/pull/936/files#diff-35e3074a14443524d99d3004b4fa803bb33ed43a0649f85383a66ac28bdc1e76R111-R116) ```diff const handleLoad = useCallback(async () => { - const { rewardsData, totals } = await getTestnetRewards() - setAllRewards(rewardsData) - setTotalRewards(totals) - setIsLoaded(true) + try { + const { rewardsData, totals } = await getTestnetRewards() + setAllRewards(rewardsData) + setTotalRewards(totals) + setIsLoaded(true) + } catch (error) { + console.error('Error loading testnet rewards:', error) + } }, []) ```
    Suggestion importance[1-10]: 8 Why: Implementing error handling in the `handleLoad` function is critical to manage exceptions during data fetching and processing, enhancing the stability and reliability of the application.
    8
    Ensure stFormattedAccountId is not empty before executing the handleAggregated function ___ **Consider checking for the presence of stFormattedAccountId before proceeding with
    the handleAggregated function to ensure that there is a valid account ID to process.** [explorer/src/components/Consensus/Account/AccountPreviousRewards.tsx [48-53]](https://github.com/autonomys/astral/pull/936/files#diff-8e3727ff57139c5207fda22d91439d50e5316030f92ce46cf616395d4c6a25e2R48-R53) ```diff const handleAggregated = useCallback(async () => { - if (!isLoaded || !accountId) return + if (!isLoaded || !accountId || !stFormattedAccountId.length) return const mergedRewards = await getUserTestnetRewards(allRewards, stFormattedAccountId) setRewards(mergedRewards) setIsAggregated(true) }, [accountId, allRewards, isLoaded, stFormattedAccountId]) ```
    Suggestion importance[1-10]: 7 Why: Checking for the presence of `stFormattedAccountId` before processing ensures that the function does not proceed with an invalid or empty ID, which is essential for correct functionality and avoiding runtime errors.
    7
    Ensure stFormattedAndMergedAddresses is valid and non-empty before executing the handleAggregated function ___ **Validate the presence of stFormattedAndMergedAddresses before proceeding in the
    handleAggregated function to ensure there are addresses to process.** [explorer/src/components/TestnetRewards/TestnetRewardsTable.tsx [118-123]](https://github.com/autonomys/astral/pull/936/files#diff-35e3074a14443524d99d3004b4fa803bb33ed43a0649f85383a66ac28bdc1e76R118-R123) ```diff const handleAggregated = useCallback(async () => { - if (!isLoaded || !stFormattedAndMergedAddresses.length) return + if (!isLoaded || !stFormattedAndMergedAddresses || !stFormattedAndMergedAddresses.length) return const mergedRewards = await getUserTestnetRewards(allRewards, stFormattedAndMergedAddresses) setRewards(mergedRewards) setIsAggregated(true) }, [allRewards, isLoaded, stFormattedAndMergedAddresses]) ```
    Suggestion importance[1-10]: 7 Why: Validating the presence of `stFormattedAndMergedAddresses` ensures that there are actual addresses to process, which is necessary for the correct execution of the function and to avoid processing errors.
    7