Skip to content

Improvement on selected token from token atlas to the exchange#287

Merged
RanaBug merged 1 commit intostagingfrom
fix/PRO-3190/hifi-endpoint-replacement
Apr 14, 2025
Merged

Improvement on selected token from token atlas to the exchange#287
RanaBug merged 1 commit intostagingfrom
fix/PRO-3190/hifi-endpoint-replacement

Conversation

@RanaBug
Copy link
Collaborator

@RanaBug RanaBug commented Apr 14, 2025

Description

  • Improvement on selected token from token atlas to the exchange for quicker receive token selection

How Has This Been Tested?

  • Existing Unit Tests and Manual Testing

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Summary by CodeRabbit

  • New Features
    • Improved token display during swaps. Now, when a token is selected, its details are prioritized for a more accurate experience, while a reliable fallback mechanism ensures consistent behavior in all scenarios.

@RanaBug RanaBug requested a review from IAmKio April 14, 2025 09:22
@RanaBug RanaBug self-assigned this Apr 14, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 14, 2025

Walkthrough

The changes update the SwapReceiveCard component’s useEffect hook by prioritizing a new logic path that checks for asset, chain, and selectedToken from the Redux state before falling back to the previous searchData processing. The component now dispatches the token selection based on detailed properties from selectedToken. Additionally, the import statements have been updated, and a new method, chainIdToChainNameTokensData, has been added to support tokens data mapping.

Changes

File(s) Change Summary
src/apps/.../SwapReceiveCard.tsx Modified the useEffect hook to first check for asset, chain, and selectedToken and dispatch token details from selectedToken; fallback to previous searchData logic; updated imports to include SelectedTokenType.
src/services/tokensData.ts Added a new method chainIdToChainNameTokensData for tokens data mapping.

Sequence Diagram(s)

sequenceDiagram
    participant Card as SwapReceiveCard
    participant Redux as Redux Store
    participant Lookup as Token Lookup (searchData)
    
    Card->>Card: Trigger useEffect
    alt selectedToken available
        Card->>Redux: Dispatch setReceiveToken(selectedToken properties)
    else selectedToken not available
        Card->>Lookup: Process searchData
        alt Native token found
            Lookup->>Redux: Dispatch setReceiveToken(native token)
        else
            Lookup->>Redux: Dispatch setReceiveToken(first token from result)
        end
    end
Loading

Poem

I'm a rabbit, hopping through the code field bright,
With new token logic, everything feels just right.
I check for assets, chains, and tokens with glee,
Dispatching actions as simple as can be.
A carrot of fresh updates fuels my joyful hop,
Celebrating neat changes at every little stop!

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@RanaBug RanaBug requested a review from vignesha22 April 14, 2025 09:23
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
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5d91c0d and 02e588a.

⛔ Files ignored due to path filters (2)
  • src/apps/pillarx-app/components/MediaGridCollection/tests/__snapshots__/DisplayCollectionImage.test.tsx.snap is excluded by !**/*.snap
  • src/apps/pillarx-app/components/PointsTile/test/__snapshots__/PointsTile.test.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • src/apps/the-exchange/components/SwapReceiveCard/SwapReceiveCard.tsx (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/apps/the-exchange/components/SwapReceiveCard/SwapReceiveCard.tsx (5)
src/apps/the-exchange/hooks/useReducerHooks.tsx (1)
  • useAppSelector (6-6)
src/apps/token-atlas/types/types.tsx (1)
  • SelectedTokenType (14-22)
src/apps/the-exchange/reducer/theExchangeSlice.ts (1)
  • setReceiveToken (71-73)
src/services/tokensData.ts (2)
  • chainIdToChainNameTokensData (211-232)
  • convertAPIResponseToTokens (267-404)
src/types/api.ts (1)
  • TokenAssetResponse (491-509)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (5)
src/apps/the-exchange/components/SwapReceiveCard/SwapReceiveCard.tsx (5)

8-8: LGTM: Import for chainIdToChainNameTokensData added correctly.

The import statement correctly adds the utility function needed for mapping chain IDs to chain names, which supports the new token selection logic.


23-24: LGTM: Required type imports added.

The imports for TokenAssetResponse and SelectedTokenType are correctly added to support the updated token selection mechanism.


44-46: LGTM: Added new Redux state selector for selectedToken.

This selector correctly retrieves the token selected in the token-atlas component, enabling improved token selection flow in the exchange.


68-80: LGTM: Improved token selection logic.

The new logic prioritizes using the selected token from the token atlas when available, which improves the user experience when selecting tokens. The code correctly maps the selected token's properties to the required format for the receive token.


81-99: LGTM: Fallback logic retained correctly.

The fallback to the original token search behavior is maintained when no selected token is available, ensuring backward compatibility.

Comment on lines +68 to 100
if (asset && chain && selectedToken) {
dispatch(
setReceiveToken({
id: selectedToken?.id || 0,
name: selectedToken?.name || '',
symbol: selectedToken?.symbol || '',
logo: selectedToken?.icon || '',
blockchain:
chainIdToChainNameTokensData(selectedToken?.chainId) || '',
contract: selectedToken?.address || '',
decimals: selectedToken?.decimals || 18,
})
);
} else {
dispatch(setReceiveToken(result[0]));
if (!searchData) return;

const result = convertAPIResponseToTokens(
searchData?.result?.data as TokenAssetResponse[],
asset || ''
);

// if it is considered a native token, Token Atlas would have handled the request
// with showing the asset as a symbol rather than an contract address
const nativeToken = result.filter(
(token) => token.blockchain === chain && token.symbol === asset
);

if (nativeToken.length > 0) {
dispatch(setReceiveToken(nativeToken[0]));
} else {
dispatch(setReceiveToken(result[0]));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update useEffect dependency array to include all dependencies.

The useEffect hook now uses chain and selectedToken in its logic, but they're missing from the dependency array. This could cause the effect not to run when these values change.

  // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [asset, searchData]);
+ }, [asset, chain, selectedToken, searchData, dispatch]);

Additionally, since you're using the ESLint disable comment, you might want to be explicit about which dependencies you're intentionally excluding, if any.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (asset && chain && selectedToken) {
dispatch(
setReceiveToken({
id: selectedToken?.id || 0,
name: selectedToken?.name || '',
symbol: selectedToken?.symbol || '',
logo: selectedToken?.icon || '',
blockchain:
chainIdToChainNameTokensData(selectedToken?.chainId) || '',
contract: selectedToken?.address || '',
decimals: selectedToken?.decimals || 18,
})
);
} else {
dispatch(setReceiveToken(result[0]));
if (!searchData) return;
const result = convertAPIResponseToTokens(
searchData?.result?.data as TokenAssetResponse[],
asset || ''
);
// if it is considered a native token, Token Atlas would have handled the request
// with showing the asset as a symbol rather than an contract address
const nativeToken = result.filter(
(token) => token.blockchain === chain && token.symbol === asset
);
if (nativeToken.length > 0) {
dispatch(setReceiveToken(nativeToken[0]));
} else {
dispatch(setReceiveToken(result[0]));
}
}
useEffect(() => {
if (asset && chain && selectedToken) {
dispatch(
setReceiveToken({
id: selectedToken?.id || 0,
name: selectedToken?.name || '',
symbol: selectedToken?.symbol || '',
logo: selectedToken?.icon || '',
blockchain:
chainIdToChainNameTokensData(selectedToken?.chainId) || '',
contract: selectedToken?.address || '',
decimals: selectedToken?.decimals || 18,
})
);
} else {
if (!searchData) return;
const result = convertAPIResponseToTokens(
searchData?.result?.data as TokenAssetResponse[],
asset || ''
);
// if it is considered a native token, Token Atlas would have handled the request
// with showing the asset as a symbol rather than an contract address
const nativeToken = result.filter(
(token) => token.blockchain === chain && token.symbol === asset
);
if (nativeToken.length > 0) {
dispatch(setReceiveToken(nativeToken[0]));
} else {
dispatch(setReceiveToken(result[0]));
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [asset, chain, selectedToken, searchData, dispatch]);

@cloudflare-workers-and-pages
Copy link

Deploying x with  Cloudflare Pages  Cloudflare Pages

Latest commit: 02e588a
Status: ✅  Deploy successful!
Preview URL: https://c1f7c377.x-e62.pages.dev
Branch Preview URL: https://fix-pro-3190-hifi-endpoint-r.x-e62.pages.dev

View logs

Copy link
Contributor

@vignesha22 vignesha22 left a comment

Choose a reason for hiding this comment

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

LGTM

@RanaBug RanaBug merged commit b3a485e into staging Apr 14, 2025
7 checks passed
This was referenced May 5, 2025
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