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

fix unstakeAll issue #888

Merged
merged 5 commits into from
Nov 11, 2023
Merged

fix unstakeAll issue #888

merged 5 commits into from
Nov 11, 2023

Conversation

Nick-1979
Copy link
Member

@Nick-1979 Nick-1979 commented Nov 11, 2023

because stakeAmount is rounded while showing to user hence to find if chill is needed we change the condition to use the boolean value of isUnstakeAll boolean value

Summary by CodeRabbit

  • Refactor
    • Enhanced the Review and Index components in the staking/unstake section by utilizing custom hooks for data retrieval, improving code readability and maintainability.
    • Introduced a new state variable isUnstakeAll to better handle unstaking scenarios.
    • Improved error handling within the unstake function.
  • Bug Fixes
    • Fixed the calculation of totalAfterUnstake to handle decimal being undefined.

Copy link
Contributor

coderabbitai bot commented Nov 11, 2023

Walkthrough

The changes primarily involve refactoring the Review and Index components in the unstake directory of the extension-polkagate package. The refactoring includes the introduction of custom hooks for accessing api and chain, and the addition of a new state variable isUnstakeAll. The logic and functionality of these components have been updated to accommodate these changes.

Changes

File Path Change Summary
.../unstake/Review.tsx Refactored to use custom hooks (useApi, useChain, useFormatted). Removed api, chain, and formatted props. Added isUnstakeAll prop. Updated unstake function and error handling.
.../unstake/index.tsx Added isUnstakeAll state variable. Updated logic related to unstaking. Removed api, chain, and formatted props from Review component. Passed isUnstakeAll to Review component.

Poem

🐇 Hopping through the code, making changes bold, 🍂

Refactoring with glee, as the autumn leaves unfold. 🍁

On this day in history, the armistice was told, 🕊️

Celebrating peace and progress, as our codebase remold. 💻🌍


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

@Nick-1979 Nick-1979 requested a review from AMIRKHANEF November 11, 2023 12:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 2bf3a28 and 4326d6d.
Files selected for processing (2)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (7 hunks)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (5 hunks)
Additional comments: 10
packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (6)
  • 30-38: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [30-45]

The new prop unstakeAllAmount has been added. Ensure that all instances of this component have been updated to pass this prop.

  • 47-54: The api, chain, and formatted values are now being retrieved through custom hooks instead of being passed as props. This is a good practice as it reduces the number of props that need to be passed and makes the component more self-contained.

  • 86-92: The unstake function has been updated to use the api, chain, and formatted values retrieved from the custom hooks. This is a good practice as it makes the function more self-contained and less dependent on external values.

  • 105-111: The unstake function now checks if unstakeAllAmount is true and hasNominator is true before pushing chilled() to txs. This is a logical change that should be correct if unstakeAllAmount and hasNominator are being correctly set elsewhere in the code.

  • 121-127: The txInfo object now includes the unstakeAllAmount value. This is a logical change that should be correct if unstakeAllAmount is being correctly set elsewhere in the code.

  • 138-141: The unstake function now sets isPasswordError to true if an error is caught. This is a good practice as it allows the UI to respond to the error.

packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (4)
  • 17-21: The import of Asset has been moved from line 3 to line 4. Ensure that this change does not affect other parts of the code that depend on Asset.

  • 52-57: The type of stakingAccount.stakingLedger.active has been changed to BN on line 52. Ensure that this change is compatible with the rest of the codebase. The calculation of totalAfterUnstake on line 54 has been updated to handle decimal being undefined. This is a good practice as it prevents potential runtime errors.

  • 104-110: unstakeAllAmount has been added as a dependency in the useEffect on line 104. This is a good practice as it ensures that the effect will be run whenever unstakeAllAmount changes.

  • 114-120: unstakeAllAmount has been added as a dependency in the second useEffect on line 124. This is a good practice as it ensures that the effect will be run whenever unstakeAllAmount changes.

Comment on lines 219 to 234
<Review
address={address}
amount={amount}
api={api}
chain={chain}
chilled={chilled}
estimatedFee={estimatedFee}
formatted={formatted}
hasNominator={!!stakingAccount?.nominators?.length}
maxUnlockingChunks={maxUnlockingChunks}
redeem={redeem}
redeemDate={redeemDate}
setShow={setShowReview}
show={showReview}
staked={staked}
total={totalAfterUnstake}
unbonded={unbonded}
unlockingLen={unlockingLen ?? 0}
unstakeAllAmount={unstakeAllAmount}
/>
Copy link
Contributor

Choose a reason for hiding this comment

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

api and chain have been removed from the props passed to Review component on line 222. unstakeAllAmount has been added as a prop to the Review component on line 222. Ensure that the Review component has been updated to handle these changes.

-          api={api}
-          chain={chain}
+          unstakeAllAmount={unstakeAllAmount}

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
<Review
address={address}
amount={amount}
api={api}
chain={chain}
chilled={chilled}
estimatedFee={estimatedFee}
formatted={formatted}
hasNominator={!!stakingAccount?.nominators?.length}
maxUnlockingChunks={maxUnlockingChunks}
redeem={redeem}
redeemDate={redeemDate}
setShow={setShowReview}
show={showReview}
staked={staked}
total={totalAfterUnstake}
unbonded={unbonded}
unlockingLen={unlockingLen ?? 0}
unstakeAllAmount={unstakeAllAmount}
/>
<Review
address={address}
amount={amount}
chilled={chilled}
estimatedFee={estimatedFee}
hasNominator={!!stakingAccount?.nominators?.length}
maxUnlockingChunks={maxUnlockingChunks}
redeem={redeem}
redeemDate={redeemDate}
setShow={setShowReview}
show={showReview}
total={totalAfterUnstake}
unbonded={unbonded}
unlockingLen={unlockingLen ?? 0}
unstakeAllAmount={unstakeAllAmount}
/>

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4326d6d and 4425e03.
Files selected for processing (2)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (7 hunks)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (8 hunks)
Additional comments: 12
packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (6)
  • 48-52: The state variable unstakeAllAmount has been replaced with isUnstakeAll. Ensure that the logic has been updated to handle this new state variable throughout the codebase.

  • 81-89: The logic has been updated to handle the isUnstakeAll state variable. Ensure that this logic is correct and that it behaves as expected.

  • 107-109: The isUnstakeAll state variable is used in the getFee function. Ensure that this logic is correct and that it behaves as expected.

  • 133-134: The isUnstakeAll state variable is set to false when the amount changes. Ensure that this is the intended behavior.

  • 151-152: The isUnstakeAll state variable is set to true when the onAllAmount function is called. Ensure that this is the intended behavior.

  • 219-234: The isUnstakeAll prop has been added to the Review component. Ensure that this prop is being used correctly in the Review component.

packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (6)
  • 30-38: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [30-45]

Ensure that all calls to this component throughout the codebase have been updated to match the new props.

  • 47-54: The use of hooks to replace props is a good practice for reducing prop drilling and improving code readability.

  • 86-92: Ensure that all the required variables are defined and not null before proceeding with the unstake operation. This is a good practice for error prevention.

  • 105-111: The conditional logic for pushing transactions into the txs array is clear and concise.

  • 121-127: The creation of the info object is straightforward and well-structured.

  • 143-144: The use of useCallback for the _onBackClick function is a good practice for performance optimization.

Comment on lines 138 to 140
console.log('error:', e);
setIsPasswordError(true);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding more detailed error handling and logging. This could include logging the error message to a monitoring service or displaying a user-friendly error message.

- console.log('error:', e);
+ console.error('Unstaking error:', e.message);

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
console.log('error:', e);
setIsPasswordError(true);
}
console.error('Unstaking error:', e.message);
setIsPasswordError(true);
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4425e03 and 0004ba9.
Files selected for processing (1)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (7 hunks)
Additional comments: 5
packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (5)
  • 30-38: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [30-45]

The Props interface has been updated to include isUnstakeAll. Ensure that all instances where the Review component is used have been updated to include this new prop.

  • 86-92: The unstake function now checks if api, chain, formatted, unbonded, redeem, chilled, or hasNominator are undefined before proceeding. This is a good practice as it prevents potential runtime errors.

  • 100-111: The logic for adding transactions to the txs array has been updated. Now, if unlockingLen is greater than or equal to maxUnlockingChunks, a redeem transaction is added. If isUnstakeAll is true and hasNominator is also true, a chilled transaction is added. This change seems to be in line with the pull request's goal of replacing unstakeAllAmount with isUnstakeAll.

  • 121-127: The info object now includes a date property, which is set to the current date and time. This is a good addition as it provides more information about when the unstaking action was performed.

  • 135-140: The unstake function now catches any errors that occur during its execution and sets isPasswordError to true. This is a good practice as it improves error handling.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0004ba9 and 1222b3f.
Files selected for processing (1)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (8 hunks)
Additional comments: 6
packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (6)
  • 48-51: The state variable unstakeAllAmount has been replaced with isUnstakeAll. Ensure that this change is reflected throughout the codebase.

  • 81-89: The logic related to unstakeAllAmount has been updated to use isUnstakeAll. Ensure that this change does not introduce any logical errors.

  • 107-109: The isUnstakeAll state variable is now being used in the getFee function. Ensure that this change does not introduce any logical errors.

  • 133-134: The onChangeAmount function has been updated to use isUnstakeAll instead of unstakeAllAmount. Ensure that this change does not introduce any logical errors.

  • 151-152: The onAllAmount function has been updated to use isUnstakeAll instead of unstakeAllAmount. Ensure that this change does not introduce any logical errors.

  • 219-233: The isUnstakeAll prop has been added to the Review component. Ensure that this prop is being used correctly in the Review component.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1222b3f and 4375307.
Files selected for processing (2)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (5 hunks)
  • packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (8 hunks)
Additional comments: 13
packages/extension-polkagate/src/popup/staking/solo/unstake/Review.tsx (6)
  • 8-55: > Note: This review was outside of the patch, but no patch was found that overlapped with it. Original lines [48-46]

The new prop isUnstakeAll has been added to the Review component's props. Ensure that all instances of this component have been updated to pass this prop.

  • 50-53: The api and chain props have been replaced with custom hooks useApi and useChain. This is a good practice as it reduces prop drilling and makes the component more self-contained.

  • 87-92: The unstake function has been updated to use the new isUnstakeAll prop and the custom hooks for api and chain. This is a good practice as it reduces prop drilling and makes the function more self-contained.

  • 101-112: The logic for unstaking has been updated to handle the new isUnstakeAll prop. Ensure that this logic is correct and that it behaves as expected in all scenarios.

  • 122-128: The from field in the error handling section has been updated to use String(formatted). This is a good practice as it ensures that the from field is always a string.

  • 136-142: The unstake function has been updated to handle the new isUnstakeAll prop. Ensure that this logic is correct and that it behaves as expected in all scenarios.

packages/extension-polkagate/src/popup/staking/solo/unstake/index.tsx (7)
  • 48-52: The introduction of the isUnstakeAll state variable seems to be a part of the solution to the unstaking issue. Ensure that it is being used correctly throughout the component.

  • 53-57: The calculation of totalAfterUnstake has been updated to handle decimal being undefined. This is a good practice as it prevents potential runtime errors.

  • 78-89: The condition for setting the alert has been updated to include isUnstakeAll. This seems to be a part of the solution to the unstaking issue. Ensure that the condition is correct and that it doesn't introduce any new issues.

  • 104-110: The condition for pushing chilled() to txs has been updated to use isUnstakeAll. This seems to be a part of the solution to the unstaking issue. Ensure that the condition is correct and that it doesn't introduce any new issues.

  • 132-144: The onChangeAmount function has been updated to set isUnstakeAll to false. This seems to be a part of the solution to the unstaking issue. Ensure that this change doesn't introduce any new issues.

  • 150-155: The onAllAmount function has been updated to set isUnstakeAll to true. This seems to be a part of the solution to the unstaking issue. Ensure that this change doesn't introduce any new issues.

  • 221-236: The Review component no longer receives api and chain as props but now receives staked and isUnstakeAll. Ensure that all calls to this component throughout the codebase have been updated to match the new props.

@Nick-1979 Nick-1979 merged commit da73d0f into main Nov 11, 2023
3 checks passed
@Nick-1979 Nick-1979 deleted the fixUnstakeAllIssue branch November 11, 2023 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants