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(request): correctly identify native tokens #434

Merged
merged 1 commit into from
Oct 11, 2024

Conversation

jjramirezn
Copy link
Contributor

When fetching the price token we were inadvertently changing the token address, so then we did not recognized it as native. Fixed that

Also did some refactoring around the address zero comparison which also made unnecessary the individual utils file for Request/Pay

Copy link

vercel bot commented Oct 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
peanut-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 10, 2024 1:02pm

Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

📝 Walkthrough

Walkthrough

The pull request introduces significant modifications to the InitialView component, enhancing state management and code clarity. An enumeration, RequestStatus, is added to track various payment request states. The peanut SDK import is updated for better type safety. The useEffect hooks are refined for improved asynchronous handling, and error management is enhanced in the handleOnNext function. Additionally, the utils.ts file is deleted, removing several constants and interfaces, while utility functions across other files are updated for better address validation.

Changes

File Change Summary
src/components/Request/Pay/Views/Initial.view.tsx Modified to improve state management; added RequestStatus enum; refined useEffect hooks; updated handleOnNext function.
src/components/Request/Pay/utils.ts Deleted; removed ADDRESS_ZERO, TokenData, EPeanutLinkType, and RequestStatus.
src/hooks/useBalance.tsx Updated to improve readability; replaced hardcoded zero address checks with utility function calls.
src/utils/fetch.utils.ts Modified fetchTokenPrice to use utility function for zero address validation.
src/utils/general.utils.ts Updated address handling; added isAddressZero function; modified areTokenAddressesEqual function.

Possibly related PRs

  • Fix build #377: Changes in Initial.view.tsx related to network switching and transaction fee estimation may connect with the modifications in the main PR, which also involves the InitialView component.
  • Fix fetching tx fee #379: Updates to Initial.view.tsx that enhance the handling of states and error management align with the changes made in the main PR, particularly regarding the linkState and error handling.
  • Asserting values #382: The addition of state variables in Initial.view.tsx for managing transaction processes relates to the enhancements made in the main PR, which also modifies the InitialView component.
  • fix(request): two bugs when preparing pay transaction #404: Changes in Initial.view.tsx regarding token address comparisons and error handling are relevant to the main PR's focus on improving the InitialView component.
  • Routes error on request pay #413: The restructuring of context usage and error handling in `Initial.view.tsx connects with the main PR's updates to the same component, focusing on transaction fee estimation and state management.
  • feat(request): pre-load requested pair on pay #432: The introduction of a new useEffect in Initial.view.tsx for pre-loading token and chain data aligns with the main PR's modifications to the InitialView component.
  • fix(request): recalculate route request pay #433: The improvements in error handling and loading states in Initial.view.tsx are relevant to the main PR's focus on enhancing the InitialView` component's functionality.

Suggested labels

Being worked on

Suggested reviewers

  • Hugo0

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
src/utils/fetch.utils.ts (2)

62-64: Approve changes with a suggestion for improvement

The refactoring to use utils.isAddressZero(tokenAddress) is a good improvement. It enhances code readability and centralizes the zero address check logic, which aligns with the PR objectives.

However, consider extracting the hardcoded address '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' into a named constant for better maintainability and clarity. This could be defined at the module level or imported from a constants file if used elsewhere.

Consider applying this change:

+ const ALTERNATIVE_ZERO_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';

export const fetchTokenPrice = async (
    // ... (rest of the function remains the same)
    body: JSON.stringify({
        tokenAddress: utils.isAddressZero(tokenAddress)
-           ? '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
+           ? ALTERNATIVE_ZERO_ADDRESS
            : tokenAddress,
        chainId,
    }),
    // ... (rest of the function remains the same)

This change would improve code readability and make it easier to update the alternative address if needed in the future.


Line range hint 29-94: Consider enhancing error handling and API response processing

While the main changes look good, here are some suggestions to further improve the function:

  1. Error Handling: The current error handling only logs to the console. Consider adding more robust error handling, such as returning an error object or throwing a custom error that can be caught and handled by the caller.

  2. API Response Validation: The function assumes the existence of certain properties in the API response. Consider adding checks for these properties to prevent potential runtime errors if the API response structure changes.

  3. Stablecoin Check: The use of utils.estimateStableCoin is not immediately clear. Consider adding a comment explaining the purpose and logic of this check.

Here's an example of how you might implement these suggestions:

export const fetchTokenPrice = async (
    tokenAddress: string,
    chainId: string,
    host?: string
): Promise<ITokenPriceData | { error: string }> => {
    try {
        // ... (existing code)

        if (mobulaResponse.ok) {
            const data = json.data;
            if (!data || typeof data !== 'object') {
                throw new Error('Invalid API response structure');
            }

            const decimals = data.decimals ?? data.contracts?.find((contract) => contract.blockchainId === chainId)?.decimals;
            if (decimals === undefined) {
                throw new Error('Unable to determine token decimals');
            }

            let tokenData = {
                price: data.price,
                chainId: chainId,
                address: tokenAddress,
                name: data.name,
                symbol: data.symbol,
                decimals,
                logoURI: data.logo,
            };

            // Check if the token is a stablecoin and adjust price if necessary
            if (utils.estimateStableCoin(data.price)) {
                tokenData.price = 1;
            }

            return tokenData;
        } else {
            return { error: `API request failed with status ${mobulaResponse.status}` };
        }
    } catch (error) {
        console.error('Error fetching token price:', error);
        return { error: `Failed to fetch token price for ${tokenAddress} on chain ${chainId}` };
    }
}

This implementation includes more robust error handling, API response validation, and a comment explaining the stablecoin check. It also changes the return type to include a possible error object, allowing the caller to handle errors more effectively.

src/hooks/useBalance.tsx (2)

4-4: LGTM! Consider using named imports for better clarity.

The addition of the utility import aligns with the PR objectives and improves code organization. However, consider using named imports for better clarity and to potentially benefit from tree-shaking.

You could refactor the import statement as follows:

-import * as utils from '@/utils'
+import { isAddressZero } from '@/utils'

This change would make it clear which specific utility functions are being used in this file.


126-128: LGTM! Consider using consistent return style for readability.

The refactoring to use utils.isAddressZero() improves code clarity and maintainability. It aligns well with the PR objectives.

For consistency and improved readability, consider using explicit return statements:

-if (utils.isAddressZero(a.address))
-    return -1
-if (utils.isAddressZero(b.address))
-    return 1
+if (utils.isAddressZero(a.address)) {
+    return -1;
+}
+if (utils.isAddressZero(b.address)) {
+    return 1;
+}

This change makes the code style more consistent with the rest of the function.

src/components/Request/Pay/Views/Initial.view.tsx (2)

Line range hint 273-292: Refactor to eliminate duplicated code in handleOnNext function

The handleOnNext function contains duplicated code in both the if (!isXChain) and else blocks, such as submitting the request fulfillment, saving fulfillment details, and updating state. This duplication increases maintenance effort and the risk of inconsistencies. Consider refactoring the shared logic into a separate function to improve maintainability and reduce redundancy.

Apply this refactor to extract common code:

 const handleOnNext = async () => {
     const amountUsd = (Number(requestLinkData.tokenAmount) * (tokenPriceData?.price ?? 0)).toFixed(2)
     try {
         setErrorState({ showError: false, errorMessage: '' })
         if (!unsignedTx) return
         if (!isXChain) {
             await checkUserHasEnoughBalance({ tokenValue: requestLinkData.tokenAmount })
             if (selectedTokenData?.chainId !== String(currentChain?.id)) {
                 await switchNetwork(selectedTokenData!.chainId)
             }
             setLoadingState('Sign in wallet')
             const hash = await sendTransactions({
                 preparedDepositTxs: { unsignedTxs: [unsignedTx] },
                 feeOptions: undefined,
             })
-            setLoadingState('Executing transaction')
-            await peanut.submitRequestLinkFulfillment({
-                chainId: requestLinkData.chainId,
-                hash: hash ?? '',
-                payerAddress: address ?? '',
-                link: requestLinkData.link,
-                apiUrl: '/api/proxy/patch/',
-                amountUsd,
-            })
-            const currentDate = new Date().toISOString()
-            utils.saveRequestLinkFulfillmentToLocalStorage({
-                details: {
-                    ...requestLinkData,
-                    destinationChainFulfillmentHash: hash ?? '',
-                    createdAt: currentDate,
-                },
-                link: requestLinkData.link,
-            })
-            setTransactionHash(hash ?? '')
-            onNext()
         } else {
             await checkUserHasEnoughBalance({ tokenValue: estimatedFromValue })
             if (selectedTokenData!.chainId !== String(currentChain?.id)) {
                 await switchNetwork(selectedTokenData!.chainId)
             }
             setLoadingState('Sign in wallet')
             const xchainUnsignedTxs = await createXChainUnsignedTx()
             const { unsignedTxs } = xchainUnsignedTxs
             const hash = await sendTransactions({
                 preparedDepositTxs: { unsignedTxs },
                 feeOptions: undefined,
             })
-            setLoadingState('Executing transaction')
-            await peanut.submitRequestLinkFulfillment({
-                chainId: requestLinkData.chainId,
-                hash: hash ?? '',
-                payerAddress: address ?? '',
-                link: requestLinkData.link,
-                apiUrl: '/api/proxy/patch/',
-                amountUsd,
-            })
-            const currentDate = new Date().toISOString()
-            utils.saveRequestLinkFulfillmentToLocalStorage({
-                details: {
-                    ...requestLinkData,
-                    destinationChainFulfillmentHash: hash ?? '',
-                    createdAt: currentDate,
-                },
-                link: requestLinkData.link,
-            })
-            setTransactionHash(hash ?? '')
-            onNext()
         }
+        // Common code extracted from both branches
+        setLoadingState('Executing transaction')
+        await peanut.submitRequestLinkFulfillment({
+            chainId: requestLinkData.chainId,
+            hash: hash ?? '',
+            payerAddress: address ?? '',
+            link: requestLinkData.link,
+            apiUrl: '/api/proxy/patch/',
+            amountUsd,
+        })
+        const currentDate = new Date().toISOString()
+        utils.saveRequestLinkFulfillmentToLocalStorage({
+            details: {
+                ...requestLinkData,
+                destinationChainFulfillmentHash: hash ?? '',
+                createdAt: currentDate,
+            },
+            link: requestLinkData.link,
+        })
+        setTransactionHash(hash ?? '')
+        onNext()
     } catch (error) {
         const errorString = utils.ErrorHandler(error)
         setErrorState({
             showError: true,
             errorMessage: errorString,
         })
         console.error('Error while submitting request link fulfillment:', error)
     } finally {
         setLoadingState('Idle')
     }
 }

Also applies to: 305-324


Line range hint 97-101: Add error handling in fetchTokenSymbol function

The fetchTokenSymbol function does not handle errors that may occur when fetching token contract details. If an error occurs, it could lead to unhandled promise rejections or runtime errors. Consider adding a try...catch block to handle potential errors and ensure the application remains stable.

Apply this diff to implement error handling:

 const fetchTokenSymbol = async (chainId: string, address: string) => {
+    try {
         const provider = await peanut.getDefaultProvider(chainId)
         const tokenContract = await peanut.getTokenContractDetails({
             address,
             provider,
         })
         setTokenRequestedSymbol(tokenContract?.symbol ?? '')
+    } catch (error) {
+        console.error('Failed to fetch token symbol:', error)
+        setTokenRequestedSymbol('')
+    }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e325f51 and a5511f2.

📒 Files selected for processing (5)
  • src/components/Request/Pay/Views/Initial.view.tsx (3 hunks)
  • src/components/Request/Pay/utils.ts (0 hunks)
  • src/hooks/useBalance.tsx (2 hunks)
  • src/utils/fetch.utils.ts (1 hunks)
  • src/utils/general.utils.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • src/components/Request/Pay/utils.ts
🧰 Additional context used
🔇 Additional comments (5)
src/utils/fetch.utils.ts (1)

Line range hint 1-94: Summary of review for src/utils/fetch.utils.ts

The changes made to the fetchTokenPrice function align well with the PR objectives, particularly in refactoring the zero address comparison. The use of utils.isAddressZero improves code readability and maintainability.

Key points:

  1. The main change effectively addresses the issue of correctly identifying native tokens.
  2. The refactoring eliminates the need for a separate utility file, as mentioned in the PR objectives.
  3. There are opportunities for further improvements in error handling, API response validation, and code clarity.

Overall, the changes are a step in the right direction. Implementing the suggested improvements would further enhance the robustness and maintainability of this function.

src/hooks/useBalance.tsx (1)

Line range hint 1-180: Overall, the changes look good and align with the PR objectives.

The modifications to useBalance.tsx improve code organization and readability without altering the core functionality. The refactoring of zero address comparisons and the centralization of utility functions are positive changes that enhance maintainability.

A few minor suggestions were made for further improvements, but these are not critical. The changes successfully address the issue of correctly identifying native tokens as mentioned in the PR objectives.

src/utils/general.utils.ts (3)

328-333: LGTM: Improved address comparison using ethers.constants.AddressZero

The changes to areTokenAddressesEqual function enhance code maintainability by using ethers.constants.AddressZero instead of hardcoded addresses. This is a good practice that reduces the risk of errors and improves readability.


336-338: LGTM: New utility function isAddressZero added

The new isAddressZero function is a valuable addition that provides a clear and reusable way to check if an address is the zero address. It effectively leverages the existing areTokenAddressesEqual function, promoting code reuse.


336-338: 🛠️ Refactor suggestion

Consider refactoring zero address checks across the codebase

With the introduction of the isAddressZero utility function, we have an opportunity to standardize and simplify zero address checks throughout the codebase. This can improve consistency and readability.

Consider replacing direct comparisons to zero addresses or uses of areTokenAddressesEqual for zero address checks with this new utility function. For example:

- if (address === '0x0000000000000000000000000000000000000000')
+ if (isAddressZero(address))

or

- if (areTokenAddressesEqual(address, '0x0000000000000000000000000000000000000000'))
+ if (isAddressZero(address))
✅ Verification successful

Zero address checks are already standardized across the codebase

Verified that there are no additional direct zero address comparisons outside the utility functions, ensuring consistency and improved readability.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for potential places to use the new isAddressZero function
rg --type typescript --type javascript -e "0x0{40}" -e "constants\.AddressZero" -e "areTokenAddressesEqual.*AddressZero"

Length of output: 157


Script:

# Updated shell script to search for potential places to use the new isAddressZero function
rg --glob "*.ts" --glob "*.js" -e "0x0{40}" -e "constants\.AddressZero" -e "areTokenAddressesEqual.*AddressZero"

Length of output: 446

src/utils/general.utils.ts Show resolved Hide resolved
src/utils/general.utils.ts Show resolved Hide resolved
src/utils/fetch.utils.ts Show resolved Hide resolved
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