Hu-Fi / Mr.Market

Mr. Market is the exchange oracle of HuFi, and a CeFi crypto bot on Mixin Messenger
https://mr-market-one.vercel.app
GNU Affero General Public License v3.0
1 stars 6 forks source link

Fix #119 filter in market page #130

Closed zed-wong closed 6 months ago

zed-wong commented 6 months ago

User description

Fix #119 filter in market page


Type

bug_fix, enhancement


Description


Changes walkthrough

Relevant files
Enhancement
11 files
constants.ts
Update Coin Type Tabs and Rename Inscription                         

interface/src/lib/helpers/constants.ts
  • Removed "favorites" and "mainstream" tabs from CoinsTypeTabs.
  • Renamed "inscription" to "inscriptions" in CoinsTypeTabs.
  • +6/-10   
    utils.ts
    Add Utility Function for Token Name Formatting                     

    interface/src/lib/helpers/utils.ts
  • Added handleCoingeckoTokenName function to shorten token names
    containing '0X'.
  • +7/-0     
    market.ts
    Refactor Active Tab Store for Market                                         

    interface/src/lib/stores/market.ts
  • Imported Writable type and used it for activeSpotTab.
  • Changed activeSecondTab to activeSpotTab.
  • +2/-3     
    coingecko.controller.ts
    Refactor Coingecko Controller for Market Category               

    server/src/modules/coingecko/coingecko.controller.ts
  • Refactored getCoinMarketsByCategory method for better readability.
  • +6/-2     
    coingecko.service.ts
    Refactor Coingecko Service for Market Data Fetching           

    server/src/modules/coingecko/coingecko.service.ts
  • Refactored coinsMarkets method for better readability and error
    handling.
  • +13/-7   
    en.json
    Update English Localization for Inscriptions                         

    interface/src/i18n/en.json - Renamed "inscription" to "inscriptions".
    +1/-1     
    zh.json
    Update Chinese Localization for Inscriptions                         

    interface/src/i18n/zh.json - Renamed "inscription" to "inscriptions" in Chinese localization.
    +1/-1     
    secondTabs.svelte
    Refactor Market Second Tabs Component                                       

    interface/src/lib/components/market/elements/secondTabs.svelte
  • Refactored to use activeCoinTab and activeSpotTab.
  • Removed unused code and simplified structure.
  • +15/-54 
    singlePair.svelte
    Improve Token Pair Display Formatting                                       

    interface/src/lib/components/market/token/singlePair.svelte
  • Integrated handleCoingeckoTokenName for token name formatting.
  • Used formatDecimals for formatting pair's last price.
  • +6/-8     
    +page.svelte
    Simplify Spot Market Pairs Filtering Logic                             

    interface/src/routes/(bottomNav)/(market)/market/spot/+page.svelte
  • Simplified filtering logic for spot market pairs based on active tab.
  • +10/-27 
    +page.svelte
    Improve Error Handling and Loading UI in Token Market Page

    interface/src/routes/(bottomNav)/(market)/market/token/+page.svelte
  • Added error handling UI for failed market data loading.
  • Replaced TokenFilterLoader with FilterLoader.
  • +27/-14 
    Configuration changes
    1 files
    home.ts
    Change Default Active Coin Tab                                                     

    interface/src/lib/stores/home.ts - Changed default value of `activeCoinTab` from 1 to 0.
    +1/-2     
    Tests
    1 files
    coingecko.service.spec.ts
    Refactor Coingecko Service Test for Market Data                   

    server/src/modules/coingecko/coingecko.service.spec.ts - Refactored test for `coinsMarkets` method for better readability.
    +5/-1     
    Bug_fix
    2 files
    tabs.svelte
    Reset Active Coin Tab on Destroy in Home Market Tabs         

    interface/src/lib/components/home/markets/tabs.svelte - Added `onDestroy` lifecycle to reset `activeCoinTab`.
    +4/-0     
    orderDetail.svelte
    Add Default State Handling in Order Detail                             

    interface/src/lib/components/spot/history/orderDetail.svelte - Added default state for order detail component.
    +1/-1     
    Cleanup
    1 files
    tokenFilterLoader.svelte
    Remove Unused Token Filter Loader Skeleton                             

    interface/src/lib/components/skeleton/market/tokenFilterLoader.svelte - Removed unused skeleton loader component.
    +0/-26   

    PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    vercel[bot] commented 6 months ago

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

    Name Status Preview Comments Updated (UTC)
    mr-market ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2024 0:34am
    railway-app[bot] commented 6 months ago

    This PR is being deployed to Railway 🚅

    railway-app[bot] commented 6 months ago

    This PR is being deployed to Railway 🚅

    Mr.Market: ◻️ REMOVED

    github-actions[bot] commented 6 months ago

    PR Description updated to latest commit (https://github.com/Hu-Fi/Mr.Market/commit/2d7be3d482542bbdeb49fd3f4187a57ef000c57b)

    github-actions[bot] commented 6 months ago

    PR Review

    (Review updated until commit https://github.com/Hu-Fi/Mr.Market/commit/1f8e01771788f5d64ecdbddb67f8958696ee83bf)

    ⏱️ Estimated effort to review [1-5] 4, due to the extensive changes across multiple files, including UI components, utility functions, and backend services. The PR touches on various aspects such as UI enhancements, localization updates, and backend service refactoring, which requires a thorough review to ensure consistency and correctness across the application.
    🧪 Relevant tests No
    🔍 Possible issues Possible Bug: The removal of the "favorites" and "mainstream" tabs without corresponding backend or state management updates could lead to unexpected behavior or errors if those categories are still referenced elsewhere in the code.
    Performance Concern: The addition of utility functions like `handleCoingeckoTokenName` directly in the UI component rendering path could lead to performance issues, especially if these functions are called frequently in large lists or tables.
    🔒 Security concerns No
    Code feedback:
    relevant fileinterface/src/lib/helpers/utils.ts
    suggestion       Consider memoizing the `handleCoingeckoTokenName` function to avoid recalculating the token name for the same input multiple times, which can improve performance in components that render large lists of tokens. [important]
    relevant lineexport const handleCoingeckoTokenName = (name: string) => {

    relevant fileinterface/src/lib/components/market/token/singlePair.svelte
    suggestion       Ensure that `formatDecimals` function handles cases where the number is very large or very small, to avoid displaying numbers in scientific notation in the UI. [important]
    relevant line{`${formatDecimals(pair.last, 8)} ${handleCoingeckoTokenName(pair.target)}`}

    relevant fileinterface/src/routes/(bottomNav)/(market)/market/spot/+page.svelte
    suggestion       Refactor the filtering logic to a utility function to improve code readability and maintainability. This function can take the `defaultsPairs` and `activeSpotTab` as inputs and return the filtered pairs. [medium]
    relevant line$: pairs = $activeSpotTab === 0 ? defaultsPairs : defaultsPairs.filter(item => item.exchange === exchangeMap[$activeSpotTab]);

    relevant fileinterface/src/lib/components/market/elements/secondTabs.svelte
    suggestion       Add a comment explaining the purpose of the `onDestroy` hook in this context, especially why it's necessary to reset `activeCoinTab` and `activeSpotTab` to 0. This will help future maintainers understand the intention behind this logic. [medium]
    relevant lineonDestroy(()=>{


    ✨ Review tool usage guide:
    **Overview:** The `review` tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be [added](https://pr-agent-docs.codium.ai/tools/review/#general-configurations) by configuring the tool. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on any PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L23) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ``` See the review [usage page](https://pr-agent-docs.codium.ai/tools/review/) for a comprehensive guide on using this tool.
    github-actions[bot] commented 6 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Maintainability
    Remove commented-out code related to the "favorites" tab. ___ **Consider removing the commented-out code related to the "favorites" tab in CoinsTypeTabs
    unless there's a clear intention to re-enable it in the near future. Keeping commented-out
    code can lead to confusion and clutter in the codebase.** [interface/src/lib/helpers/constants.ts [299-302]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-1d9500711f0f58654b9e0e95aa0e7fdc798a0b74f1c2310b09e52123e52d6bf2R299-R302) ```diff -// { -// name: "favorites", -// id: "favorites", -// }, +// Code related to "favorites" tab removed. ```
    Refactor the filtering logic for pairs to enhance maintainability. ___ **Refactor the filtering logic for pairs to use a mapping object for cleaner and more
    maintainable code.** [interface/src/routes/(bottomNav)/(market)/market/spot/+page.svelte [13-34]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-7ba2839ad827bde33f4463d7a9f5a0e8cf605545b6a4dacdde0d65eb52a7cd05R13-R34) ```diff -$: pairs = - $activeSpotTab === 0 - ? defaultsPairs - : $activeSpotTab === 1 - ? defaultsPairs.filter((item) => { - return item.exchange === "okx"; - }) - : $activeSpotTab === 2 - ? defaultsPairs.filter((item) => { - return item.exchange === "bitfinex"; - }) - : $activeSpotTab === 3 - ? defaultsPairs.filter((item) => { - return item.exchange === "mexc"; - }) - : $activeSpotTab === 4 - ? defaultsPairs.filter((item) => { - return item.exchange === "gate"; - }) - : $activeSpotTab === 5 - ? defaultsPairs.filter((item) => { - return item.exchange === "lbank"; - }) + ```
    Maintain consistency in quotation marks usage within class attributes. ___ **Ensure consistent use of quotation marks for class names to maintain code consistency and
    readability.** [interface/src/lib/components/market/token/singlePair.svelte [14]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-e71c158df546b173f765b294e2772760c5fc40ed9d1b8acddbee52575dc6bd6dR14-R14) ```diff -
    +
    ```
    Enhancement
    Use ellipsis character for truncating token names. ___ **For the handleCoingeckoTokenName function, consider using ellipsis () instead of three
    dots (...) for a more semantically correct representation when truncating token names.** [interface/src/lib/helpers/utils.ts [267]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-cca6fb1729226e759cd4dba0b09a9c83f7f109709c77fd68c77feaf0ba512380R267-R267) ```diff -return `${name.slice(0,6)}...` +return `${name.slice(0,6)}…` ```
    Validate category parameter in getCoinMarketsByCategory. ___ **Consider validating the category parameter in the getCoinMarketsByCategory method to
    ensure it matches one of the expected categories. This can prevent potential errors when
    calling the Coingecko API with an invalid category.** [server/src/modules/coingecko/coingecko.controller.ts [26-27]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-71766c6cd195ad3b631845e4c59ced4dcd6fd980b550f20859685ab69560b95eR26-R27) ```diff -@Param('category') -category: 'decentralized_finance_defi' | 'stablecoins' | 'all', +// Add validation for `category` parameter to match expected categories. ```
    Improve code readability and performance by destructuring the pair object. ___ **Consider destructuring pair to improve readability and performance by directly accessing
    the properties you need.** [interface/src/lib/components/market/token/singlePair.svelte [16]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-e71c158df546b173f765b294e2772760c5fc40ed9d1b8acddbee52575dc6bd6dR16-R16) ```diff -{handleCoingeckoTokenName(pair.base)}/{handleCoingeckoTokenName(pair.target)} + +{handleCoingeckoTokenName(base)}/{handleCoingeckoTokenName(target)} ```
    Simplify the assignment of state with optional chaining. ___ **Use optional chaining to simplify the expression for setting the `state` variable.** [interface/src/lib/components/spot/history/orderDetail.svelte [8]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-f23b2cd24acc1ec41f0fb4400075be6ed4ca2249c023bd60333e00cc92b420acR8-R8) ```diff -$: state = o ? o.state : 'ORDER_CREATED'; +$: state = o?.state || 'ORDER_CREATED'; ```
    Possible issue
    Verify activeCoinTab store's initial value change impact. ___ **Since the activeCoinTab store's initial value was changed from 1 to 0, ensure that this
    change is reflected across all components that rely on this store to prevent any
    unintended behavior or UI inconsistencies.** [interface/src/lib/stores/home.ts [6]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-bf1ad0846e88bf6d8bbf7d5cf7b6b94a23244c7a2ff7b6e80c74a7a22a3fcf7dR6-R6) ```diff -export const activeCoinTab = writable(0); +// Ensure all dependent components correctly handle the new initial value of `activeCoinTab`. ```
    Review activeCoinTab reset on component destruction. ___ **Ensure that resetting the activeCoinTab to 0 in the onDestroy lifecycle hook of
    tabs.svelte is the intended behavior, as it might lead to unexpected UI states when
    navigating away from the component.** [interface/src/lib/components/home/markets/tabs.svelte [7-9]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-fecb1d60a00e44d2707e8179f29636a7f62a6d180eacbdda9ed1b755cb33bd30R7-R9) ```diff -onDestroy(()=>{ - activeCoinTab.set(0); -}) +// Review if resetting `activeCoinTab` on component destruction is desired. ```
    Best practice
    Improve code organization by using a named function for page reload. ___ **Use a reactive statement to handle the reloading of the page instead of an inline function
    in the on:click event.** [interface/src/routes/(bottomNav)/(market)/market/token/+page.svelte [42]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-19c0a8d872890887d2cdc3e2396e8d574271959ac474c6ba1f65fc9ae81792e4R42-R42) ```diff -

    ✨ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on a PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L78) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` See the improve [usage page](https://pr-agent-docs.codium.ai/tools/improve/) for a comprehensive guide on using this tool.
    github-actions[bot] commented 6 months ago

    PR Description updated to latest commit (https://github.com/Hu-Fi/Mr.Market/commit/1f8e01771788f5d64ecdbddb67f8958696ee83bf)

    github-actions[bot] commented 6 months ago

    Persistent review updated to latest commit https://github.com/Hu-Fi/Mr.Market/commit/1f8e01771788f5d64ecdbddb67f8958696ee83bf

    github-actions[bot] commented 6 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Maintainability
    Remove commented-out code if it's no longer needed. ___ **Consider removing the commented-out code related to the "favorites" tab if it's no longer
    needed. Keeping commented-out code can lead to confusion and clutter in the codebase.** [interface/src/lib/helpers/constants.ts [299-302]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-1d9500711f0f58654b9e0e95aa0e7fdc798a0b74f1c2310b09e52123e52d6bf2R299-R302) ```diff -// { -// name: "favorites", -// id: "favorites", -// }, +// Code related to "favorites" tab removed. ```
    Add a comment explaining the default value of activeCoinTab. ___ **Since activeCoinTab is being reset to 0 in tabs.svelte on component destruction, consider
    adding a comment here to explain the default value's significance and its relation to the
    component lifecycle.** [interface/src/lib/stores/home.ts [6]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-bf1ad0846e88bf6d8bbf7d5cf7b6b94a23244c7a2ff7b6e80c74a7a22a3fcf7dR6-R6) ```diff +// Default value set to 0; reset to 0 on component destruction in tabs.svelte export const activeCoinTab = writable(0); ```
    Improve readability by using descriptive class names. ___ **Consider using a more descriptive class name instead of an empty string for better
    readability and maintainability.** [interface/src/lib/components/market/token/singlePair.svelte [15]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-e71c158df546b173f765b294e2772760c5fc40ed9d1b8acddbee52575dc6bd6dR15-R15) ```diff -
    +
    ```
    Best practice
    Use more descriptive variable names. ___ **To improve readability and maintainability, consider using a more descriptive variable
    name instead of name for the function parameter. For example, tokenName would make it
    clearer that the function expects the name of a token.** [interface/src/lib/helpers/utils.ts [265]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-cca6fb1729226e759cd4dba0b09a9c83f7f109709c77fd68c77feaf0ba512380R265-R265) ```diff -export const handleCoingeckoTokenName = (name: string) => { +export const handleCoingeckoTokenName = (tokenName: string) => { ```
    Log errors before throwing them for better debugging. ___ **To enhance error handling, consider logging the error before throwing it. This can help
    with debugging and provide more context about the error's origin.** [server/src/modules/coingecko/coingecko.service.ts [67-69]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-fc8069e1fb6be7ac5ca3b7e95e0de665542438306d2ea1ac330338bb91117b42R67-R69) ```diff +console.error(`Error fetching /coins/market for ${vs_currency}: ${error}`); throw new Error( `markets/${vs_currency} Failed to GET /coins/market: ${error.message}`, ); ```
    Use constants for tab indices to improve code readability. ___ **For better maintainability and to avoid magic numbers, consider defining constants for the
    tab indices (0 for 'token', 1 for 'spot') used in the active variable assignment. This
    will make the code more readable and easier to understand.** [interface/src/lib/components/market/elements/secondTabs.svelte [19-20]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-028779b1de0a34a553b8c4d06b2f5416e3852961a5d0caa87066cd3b43cad9f2R19-R20) ```diff -$: active = $page.url.pathname.includes(MARKET_BAR_ITEMS[0].key) ? 0 : - $page.url.pathname.includes(MARKET_BAR_ITEMS[1].key) ? 1 : 0 +const TOKEN_TAB_INDEX = 0; +const SPOT_TAB_INDEX = 1; +$: active = $page.url.pathname.includes(MARKET_BAR_ITEMS[TOKEN_TAB_INDEX].key) ? TOKEN_TAB_INDEX : + $page.url.pathname.includes(MARKET_BAR_ITEMS[SPOT_TAB_INDEX].key) ? SPOT_TAB_INDEX : TOKEN_TAB_INDEX; ```
    Use optional chaining for safer property access. ___ **To handle potential null or undefined values more gracefully, consider using optional
    chaining (?.) when accessing properties of o.** [interface/src/lib/components/spot/history/orderDetail.svelte [8]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-f23b2cd24acc1ec41f0fb4400075be6ed4ca2249c023bd60333e00cc92b420acR8-R8) ```diff -$: state = o ? o.state : 'ORDER_CREATED'; +$: state = o?.state ?? 'ORDER_CREATED'; ```
    Use an enum for exchange identifiers for better type safety. ___ **To ensure type safety and maintainability, consider defining an enum for exchange
    identifiers instead of using a plain object.** [interface/src/routes/(bottomNav)/(market)/market/spot/+page.svelte [12-18]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-7ba2839ad827bde33f4463d7a9f5a0e8cf605545b6a4dacdde0d65eb52a7cd05R12-R18) ```diff -const exchangeMap: { [key: number]: string} = { - 1: "okx", - 2: "bitfinex", - 3: "mexc", - 4: "gate", - 5: "lbank" -}; +enum Exchange { + OKX = 1, + Bitfinex = 2, + MEXC = 3, + Gate = 4, + LBank = 5 +} ```
    Performance
    Optimize performance by simplifying the condition for sorting tokens. ___ **For better performance, avoid using $marketData || $marketDataState === 'loading' in the
    ternary operation for sortedTokens as it causes unnecessary computations. Instead,
    directly check $marketDataState.** [interface/src/routes/(bottomNav)/(market)/market/token/+page.svelte [12]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-19c0a8d872890887d2cdc3e2396e8d574271959ac474c6ba1f65fc9ae81792e4R12-R12) ```diff -$: sortedTokens = sortCoins($selectedField, !$marketData || $marketDataState === 'loading' ? tokens : $marketData, $asc) +$: sortedTokens = $marketDataState === 'loading' ? tokens : sortCoins($selectedField, $marketData, $asc) ```
    Enhancement
    Disable the "Retry" button after click to improve user experience. ___ **To improve user experience, consider disabling the "Retry" button after it's clicked to
    prevent multiple reloads and indicate that an action is being taken.** [interface/src/routes/(bottomNav)/(market)/market/token/+page.svelte [42]](https://github.com/Hu-Fi/Mr.Market/pull/130/files#diff-19c0a8d872890887d2cdc3e2396e8d574271959ac474c6ba1f65fc9ae81792e4R42-R42) ```diff -

    ✨ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on a PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L78) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` See the improve [usage page](https://pr-agent-docs.codium.ai/tools/improve/) for a comprehensive guide on using this tool.