Skip to content

SHARD-2140: add unstakeCooldown logic #106

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

Merged
merged 4 commits into from
Apr 2, 2025
Merged

Conversation

PudgyPug
Copy link
Contributor

@PudgyPug PudgyPug commented Mar 31, 2025

PR Type

enhancement, bug fix


Description

  • Implemented unstake cooldown logic in StakeDisplay.

  • Added unstakeInterval to AccountStakeInfo model.

  • Introduced Constants for managing local storage keys.

  • Enhanced tooltip logic for unstake button.


Changes walkthrough 📝

Relevant files
Enhancement
StakeDisplay.tsx
Implement unstake cooldown and enhance tooltips                   

components/molecules/StakeDisplay.tsx

  • Added unstake cooldown logic and tooltip updates.
  • Introduced Constants for local storage management.
  • Modified button disable logic based on cooldown.
  • +50/-11 
    useUnstake.ts
    Add unstake cooldown storage logic                                             

    hooks/useUnstake.ts

  • Set local storage item for unstake cooldown.
  • Imported Constants for local storage key management.
  • +2/-0     
    account-stake-info.ts
    Extend AccountStakeInfo with unstakeInterval                         

    model/account-stake-info.ts

    • Added unstakeInterval property to interface.
    +1/-0     
    Miscellaneous
    useStake.ts
    Clean up unused imports                                                                   

    hooks/useStake.ts

    • Removed unused error handling imports.
    +0/-2     
    Configuration changes
    constants.ts
    Add Constants for local storage keys                                         

    utils/constants.ts

    • Introduced Constants object for key management.
    +3/-0     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🏅 Score: 85
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Local Storage Usage

    The use of local storage for managing the unstake cooldown might lead to inconsistencies if the user accesses the application from different devices or browsers. Consider using a more centralized storage solution.

    useEffect(() => {
      const handleStorageChange = (e: StorageEvent) => {
        if (e.key === Constants.UNSTAKE_COOLDOWN_KEY) {
          setLastUnstake(e.newValue ? parseInt(e.newValue) : 0)
        }
      }
    
      window.addEventListener('storage', handleStorageChange)
      return () => window.removeEventListener('storage', handleStorageChange)
    }, [])
    Tooltip Logic

    The tooltip logic for the unstake button is complex and might be difficult to maintain. Consider refactoring for clarity and maintainability.

    function unstakeTooltip() {
      if (hasNodeStopped) {
        if (nodeStatus?.stakeState?.unlocked === false && nodeStatus?.stakeState?.remainingTime > 0) {
          return `Node is currently stopped and is being removed from the active validator list. Please wait for another ${formatRemainingTime(
            nodeStatus.stakeState.remainingTime
          )} before you can remove your stake.`
        }
    
        if (cooldown > 0) {
          return `You have recently removed your stake. Please wait for another ${formatRemainingTime(
            cooldown
          )} before you can remove your stake.`
        }
      }
      return 'It is not recommended to unstake while validating. If absolutely necessary, use the force remove option in settings to remove stake (Not Recommended).'

    Comment on lines +75 to +76
    const cooldown = stakeInfo.unstakeInterval - (Date.now() - lastUnstakeTime)
    return cooldown > 0 ? cooldown : 0

    Choose a reason for hiding this comment

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

    Suggestion: Ensure that stakeInfo.unstakeInterval and lastUnstakeTime are valid numbers before performing arithmetic operations to prevent potential runtime errors. [possible issue, importance: 7]

    Suggested change
    const cooldown = stakeInfo.unstakeInterval - (Date.now() - lastUnstakeTime)
    return cooldown > 0 ? cooldown : 0
    const cooldown = (typeof stakeInfo.unstakeInterval === 'number' && typeof lastUnstakeTime === 'number')
    ? stakeInfo.unstakeInterval - (Date.now() - lastUnstakeTime)
    : 0;
    return cooldown > 0 ? cooldown : 0;

    Comment on lines 43 to 44
    window.addEventListener('storage', handleStorageChange)
    return () => window.removeEventListener('storage', handleStorageChange)

    Choose a reason for hiding this comment

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

    Suggestion: Verify that handleStorageChange is properly defined and handles all potential edge cases, such as null or undefined values for e.newValue. [general, importance: 6]

    Suggested change
    window.addEventListener('storage', handleStorageChange)
    return () => window.removeEventListener('storage', handleStorageChange)
    window.addEventListener('storage', handleStorageChange)
    return () => window.removeEventListener('storage', handleStorageChange)
    function handleStorageChange(e: StorageEvent) {
    if (e.key === Constants.UNSTAKE_COOLDOWN_KEY) {
    const newValue = e.newValue ? parseInt(e.newValue) : 0;
    if (!isNaN(newValue)) {
    setLastUnstake(newValue);
    }
    }
    }

    @PudgyPug PudgyPug changed the title Draft: SHARD-2140: add unstakeCooldown logic SHARD-2140: add unstakeCooldown logic Apr 1, 2025
    @mhanson-github mhanson-github merged commit cf2ec85 into mainnet-launch Apr 2, 2025
    5 checks passed
    @mhanson-github mhanson-github deleted the SHARD-2140 branch April 2, 2025 16:29
    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.

    4 participants