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

Prevent a larger amount to be calculated #826

Merged
merged 6 commits into from
Aug 29, 2024

Conversation

marc-aurele-besner
Copy link
Collaborator

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

User description

Prevent a larger amount to be calculated

For a reason I can't understand, it seems sometimes the shares calculated to withdraw are higher than the max shares pass to the component. So I added an extra sanity check, when calculating the % of shares selected * total shares, to ensure it does not exceed the max shares...


PR Type

Bug fix


Description

  • Added a sanity check to prevent the calculated withdrawal shares from exceeding the maximum allowed shares.
  • Introduced a new variable newAmount to improve code readability and maintainability.

Changes walkthrough 📝

Relevant files
Bug fix
ActionsModal.tsx
Add sanity check for withdrawal shares calculation             

explorer/src/components/Staking/ActionsModal.tsx

  • Added a sanity check to ensure the calculated withdrawal amount does
    not exceed the maximum allowed shares.
  • Introduced a new variable newAmount to store the calculated shares
    amount.
  • Updated the logic to set the field value based on the new sanity
    check.
  • +3/-1     

    💡 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 27, 2024

    Deploy Preview for dev-astral ready!

    Name Link
    🔨 Latest commit c4487c3
    🔍 Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/66d05fe0bbda9b0008fc8a95
    😎 Deploy Preview https://deploy-preview-826--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

    PR Reviewer Guide 🔍

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

    Possible Bug
    The calculation of newAmount might result in a loss of precision due to integer division. Consider verifying if the division truncates any significant digits, which could lead to incorrect financial calculations.

    Copy link

    github-actions bot commented Aug 27, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Robustness
    Add type checking for newValue to ensure it is a number before calculations

    To ensure that the onChange handler is robust against unexpected input types,
    consider adding type checks or assertions for newValue before performing operations
    that assume it is a number.

    explorer/src/components/Staking/ActionsModal.tsx [426-430]

     const newValue = Array.isArray(value) ? value[0] : value
    +if (typeof newValue !== 'number') {
    +  throw new Error('Expected newValue to be a number');
    +}
     const newAmount =
       (maxSharesToWithdraw * BigInt(newValue)) / BigInt(100)
     
    Suggestion importance[1-10]: 9

    Why: Adding type checks for newValue significantly improves the robustness of the code by preventing runtime errors due to unexpected input types.

    9
    Possible bug
    Prevent division by zero in the calculation of newAmount

    To ensure that the calculation of newAmount does not throw a runtime error when
    newValue is zero, it's advisable to add a check to prevent division by zero. This
    can be achieved by setting newAmount to zero directly if newValue is zero.

    explorer/src/components/Staking/ActionsModal.tsx [429-430]

    -const newAmount =
    +const newAmount = newValue === 0 ? BigInt(0) :
       (maxSharesToWithdraw * BigInt(newValue)) / BigInt(100)
     
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential bug by preventing division by zero, which is a critical improvement to ensure the robustness of the code.

    8
    Maintainability
    Refactor the calculation and conditional logic of newAmount into a separate function

    To improve readability and maintainability, consider extracting the calculation and
    conditional logic of newAmount into a separate function. This will make the onChange
    handler cleaner and easier to understand.

    explorer/src/components/Staking/ActionsModal.tsx [429-433]

    -const newAmount =
    -  (maxSharesToWithdraw * BigInt(newValue)) / BigInt(100)
    -setFieldValue(
    -  'amount',
    -  newAmount > maxSharesToWithdraw ? maxSharesToWithdraw : newAmount,
    -)
    +const calculateAmount = (value) => {
    +  const amount = (maxSharesToWithdraw * BigInt(value)) / BigInt(100);
    +  return amount > maxSharesToWithdraw ? maxSharesToWithdraw : amount;
    +}
    +const newAmount = calculateAmount(newValue);
    +setFieldValue('amount', newAmount);
     
    Suggestion importance[1-10]: 7

    Why: Refactoring the calculation into a separate function enhances code readability and maintainability, making it easier to understand and modify in the future.

    7
    Best practice
    Use a more descriptive variable name than newValue

    Consider using a more descriptive variable name than newValue for the slider value
    to enhance code readability and maintainability.

    explorer/src/components/Staking/ActionsModal.tsx [426]

    -const newValue = Array.isArray(value) ? value[0] : value
    +const sliderNewValue = Array.isArray(value) ? value[0] : value
     
    Suggestion importance[1-10]: 6

    Why: Using a more descriptive variable name improves code readability and maintainability, although it is a minor improvement compared to functional changes.

    6

    @marc-aurele-besner
    Copy link
    Collaborator Author

    @DianaPertseva This should fix the shares amount issue and the shares been 0 also

    @DianaPertseva
    Copy link

    @DianaPertseva This should fix the shares amount issue and the shares been 0 also

    Unfortunately, I see the same bug on the preview. Sent more info in dms

    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 d54de68 into main Aug 29, 2024
    13 checks passed
    @marc-aurele-besner marc-aurele-besner deleted the bug/fixWithdrawalSharesAmount branch August 29, 2024 12:08
    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