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

feat: ability to switch chains in the swap component #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mikelord007
Copy link

@mikelord007 mikelord007 commented Aug 24, 2024

Related issue: #11 (comment)

Summary by CodeRabbit

  • New Features

    • Introduced chain switching functionality in the Swap component, allowing users to switch between blockchain networks more intuitively.
    • Added new blockchain networks, baseSepolia and polygonAmoy, enhancing the application’s operational capabilities.
  • User Experience Enhancements

    • Implemented a loading spinner on the button for chain switching, providing real-time feedback on the action's status.
    • Streamlined button behavior to reflect loading states during the chain-switching process, improving interactivity.

Copy link
Contributor

coderabbitai bot commented Aug 24, 2024

Walkthrough

Walkthrough

The changes enhance the Swap component by introducing functionality for switching blockchain networks. Two new properties, switchToRightChain and isSwitchingChainsPending, were added to manage the chain-switching process effectively. The component now allows users to interactively switch chains while providing visual feedback during the operation.

Changes

Files Change Summary
src/components/Swap/Layout.tsx Added switchToRightChain function and isSwitchingChainsPending boolean to SwapLayoutProps. Updated button behavior for chain switching and user feedback.
src/components/Swap/index.tsx Integrated useSwitchChain from wagmi. Introduced switchToRightChain function for managing chain switching and passed new props to child components.
src/wagmi.ts Updated blockchain configuration to include new networks: baseSepolia and polygonAmoy, expanding operational capabilities.

Possibly related issues


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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, codebase verification and nitpick comments (3)
src/wagmi.ts (1)

6-7: Chains Not Found in wagmi Library

The search results indicate that baseSepolia and polygonAmoy are referenced in src/wagmi.ts, but there is no evidence that these chains are defined or exported from the wagmi library. This suggests they might not be supported by the library as expected.

  • File: src/wagmi.ts
    • Lines: References to baseSepolia and polygonAmoy were found, but their definitions were not located in the wagmi library.

Please verify the definitions and ensure these chains are correctly integrated into the library or provide custom implementations if necessary.

Analysis chain

Ensure the new chains are correctly imported.

The imports for baseSepolia and polygonAmoy are correctly added. Verify that these chains are supported by the wagmi library.

Run the following script to confirm the presence of baseSepolia and polygonAmoy in the wagmi library:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the presence of `baseSepolia` and `polygonAmoy` in the wagmi library.

# Test: Search for the chain definitions. Expect: Definitions for both chains.
rg --type ts "baseSepolia|polygonAmoy" | grep "export"

Length of output: 54


Script:

#!/bin/bash
# Description: Search for any occurrences of `baseSepolia` and `polygonAmoy` in the codebase.

# Search for any mention of `baseSepolia` and `polygonAmoy` in the TypeScript files.
rg --type ts "baseSepolia|polygonAmoy" -A 3 -B 3

Length of output: 521

src/components/Swap/Layout.tsx (2)

65-66: Add new props to SwapLayoutProps.

The switchToRightChain and isSwitchingChainsPending props are added to the interface. Ensure these props are correctly typed and documented.

Consider adding comments to describe the purpose and usage of these new props for better maintainability.


373-378: Enhance user feedback during chain switching.

The button text and loading spinner provide user feedback during chain switching. Ensure the feedback is clear and consistent with the application's design language.

Consider adding additional feedback mechanisms, such as tooltips or notifications, to further enhance user experience.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3f698d1 and a29de45.

Files selected for processing (3)
  • src/components/Swap/Layout.tsx (3 hunks)
  • src/components/Swap/index.tsx (4 hunks)
  • src/wagmi.ts (1 hunks)
Additional comments not posted (6)
src/wagmi.ts (1)

17-18: Verify the addition of new chains in the configuration.

The chains baseSepolia and polygonAmoy are added to the chains array. Ensure that the application logic supports these new chains.

Verify that the application logic supports these new chains by checking their usage in the codebase.

src/components/Swap/index.tsx (4)

15-15: Confirm the usage of useSwitchChain.

The useSwitchChain hook is imported to facilitate chain switching. Ensure that this hook is correctly utilized and integrated within the component.

Verify the correct usage of useSwitchChain by checking its implementation details in the wagmi library.


33-33: Ensure proper handling of isSwitchingChainsPending.

The isPending state from useSwitchChain is renamed to isSwitchingChainsPending. Ensure this state is correctly managed and used in the component.

Verify that the isPending state is correctly mapped and utilized in the component logic.


84-86: Review the switchToRightChain function logic.

The switchToRightChain function conditionally switches chains based on the selected source token. Ensure the function handles all edge cases, such as invalid or missing chain IDs.

Verify that the function handles cases where sourceTokenSelected might be undefined or have an invalid chain_id.


204-205: Pass new props to SwapLayout.

The switchToRightChain and isSwitchingChainsPending props are passed to SwapLayout. Ensure these props are correctly utilized in the child component.

Verify that these props are correctly used in SwapLayout by checking their implementation details.

src/components/Swap/Layout.tsx (1)

98-99: Integrate new props into the component.

The switchToRightChain and isSwitchingChainsPending props are integrated into the component. Ensure these props are used effectively to enhance user interaction.

Verify that the integration of these props aligns with the intended functionality and user experience enhancements.

@fadeev fadeev self-requested a review August 25, 2024 08:56
Copy link
Member

@fadeev fadeev left a comment

Choose a reason for hiding this comment

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

Looks good. Please, disable button when network switching is in progress and add button text to the right of the spinner "Switching to BNB..." (or whatever the network name is).

Screenshot 2024-08-25 at 17 55 45

@fadeev
Copy link
Member

fadeev commented Sep 6, 2024

@mikelord007 please, implement the requested changes. Thanks!

@mikelord007
Copy link
Author

@fadeev done

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a29de45 and 0054032.

Files selected for processing (1)
  • src/components/Swap/Layout.tsx (3 hunks)
Additional comments not posted (1)
src/components/Swap/Layout.tsx (1)

375-385: Enhance button accessibility and provide clear user feedback.

The button for switching networks uses the switchToRightChain function for its onClick handler and isSwitchingChainsPending to control its disabled state. This setup is functional, but consider enhancing the user experience:

  1. Accessibility: Add an aria-label to the button to improve accessibility, especially when the button is in the loading state.
  2. User Feedback: The text "Switching to {sourceTokenSelected.chain_name}" provides dynamic feedback based on the selected source token. Ensure that sourceTokenSelected is always defined when this text is displayed to avoid runtime errors.
<Button
  variant="outline"
  onClick={switchToRightChain}
  disabled={isSwitchingChainsPending}
+ aria-label={isSwitchingChainsPending ? `Switching to ${sourceTokenSelected?.chain_name}` : "Switch Network"}
>
  {isSwitchingChainsPending ? (
    <>
      <Loader2 className="h-4 w-4 mr-2 animate-spin" />
      <span>Switching to {sourceTokenSelected?.chain_name}</span>
    </>
  ) : (
    "Switch Network"
  )}
</Button>

src/components/Swap/Layout.tsx 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