Hu-Fi / Mr.Market

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

Add strategy orders and fetching for Order History Page #144

Closed Faouzijedidi1 closed 7 months ago

Faouzijedidi1 commented 7 months ago

Type

enhancement, bug_fix


Description


Changes walkthrough

Relevant files
Configuration changes
app.module.ts
Import and Synchronize New Order Entities                               

server/src/app.module.ts
  • Added Order and ArbitrageOrder entities to the module imports and
    database synchronization list.
  • +4/-0     
    strategy.module.ts
    Strategy Module Updates for Orders Handling                           

    server/src/modules/strategy/strategy.module.ts
  • Included Order entity in module imports.
  • Added OrdersController to the module's controllers.
  • +9/-2     
    Enhancement
    arbitrage-order.entity.ts
    New Arbitrage Order Entity                                                             

    server/src/common/entities/arbitrage-order.entity.ts
  • Created a new entity ArbitrageOrder with fields for managing arbitrage
    orders, including user and client IDs, trading pair, exchanges,
    amounts, prices, profit, execution time, status, and strategy.
  • +44/-0   
    order.entity.ts
    New General Order Entity                                                                 

    server/src/common/entities/order.entity.ts
  • Introduced a new entity Order for handling general orders, including
    fields for user and client IDs, exchange, trading pair, side, amount,
    price, order ID, execution time, status, and strategy.
  • +40/-0   
    orders.controller.ts
    Orders Fetching Endpoints                                                               

    server/src/modules/strategy/orders.controller.ts
  • Implemented OrdersController with endpoints to fetch market-making and
    arbitrage orders for a user.
  • +23/-0   
    strategy.service.ts
    Enhancements in Strategy Service for Order Handling           

    server/src/modules/strategy/strategy.service.ts
  • Injected repositories for Order and ArbitrageOrder entities.
  • Enhanced trade execution methods to create and save order entities
    upon trade execution.
  • Added methods to fetch user-specific orders and arbitrage orders.
  • +93/-4   

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

    vercel[bot] commented 7 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 11, 2024 0:56am
    railway-app[bot] commented 7 months ago

    This PR is being deployed to Railway 🚅

    railway-app[bot] commented 7 months ago

    This PR is being deployed to Railway 🚅

    Mr.Market: ◻️ REMOVED

    github-actions[bot] commented 7 months ago

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

    github-actions[bot] commented 7 months ago

    PR Review

    ⏱️ Estimated effort to review [1-5] 3, because the PR introduces significant changes across multiple files, including new entities, service logic, and controller endpoints. The complexity of the changes, especially around the trading logic and database interactions, requires a thorough review to ensure correctness, performance, and security.
    🧪 Relevant tests No
    🔍 Possible issues Possible Bug: In `strategy.service.ts`, the `side` property is incorrectly set to 'buy' for a sell order entity creation. This could lead to incorrect order tracking and reporting.
    Performance Concern: The current implementation of order and arbitrage order creation within the trading functions might not be the most efficient, especially under high load. Consider batching or async processing to improve performance.
    🔒 Security concerns No
    Code feedback:
    relevant fileserver/src/modules/strategy/strategy.service.ts
    suggestion       Correct the `side` property for the sell order entity creation to 'sell'. This ensures accurate representation of the order's intent in the database. [important]
    relevant lineside: 'buy',

    relevant fileserver/src/common/entities/arbitrage-order.entity.ts
    suggestion       Consider adding index decorators to frequently queried columns like `userId`, `clientId`, and `pair` to improve query performance. [medium]
    relevant line@Column()

    relevant fileserver/src/modules/strategy/strategy.service.ts
    suggestion       Implement error handling for database operations (e.g., `save` method) to gracefully manage failures and ensure system stability. [important]
    relevant lineawait this.orderRepository.save(orderEntity);

    relevant fileserver/src/modules/strategy/strategy.service.ts
    suggestion       Optimize the arbitrage trading logic by considering parallel execution of buy and sell orders where possible, to reduce execution time and potentially increase profitability. [medium]
    relevant lineawait this.executeArbitrageTradeWithLimitOrders(


    ✨ 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 7 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Add a check for successful trade execution before saving the order entity. ___ **Consider checking for the execution result of the limit trade before creating and saving
    the order entity. This ensures that the order entity is only saved if the trade was
    successfully executed.** [server/src/modules/strategy/strategy.service.ts [367-392]](https://github.com/Hu-Fi/Mr.Market/pull/144/files#diff-413cb1b28e0d47a46768f97d10145a8e14d9e46b0a195768786127305916d944R367-R392) ```diff const order = await this.tradeService.executeLimitTrade({ ... -await this.orderRepository.save(orderEntity); +if (order && order.id) { + await this.orderRepository.save(orderEntity); +} ```
    Use enum types for status and strategy columns in the arbitrage order entity. ___ **Consider using enum types for the status and strategy columns to enforce valid values at
    the database level.** [server/src/common/entities/arbitrage-order.entity.ts [40-43]](https://github.com/Hu-Fi/Mr.Market/pull/144/files#diff-ff5a2e5f3af4797869b2a42e0e031f8e629b36f0e4e628d5819683c1a44c79a6R40-R43) ```diff -@Column() -status: string; // 'open', 'closed', 'canceled' +@Column({ + type: "enum", + enum: ['open', 'closed', 'canceled'], +}) +status: string; ... -@Column() -strategy: string; // 'arbitrage', 'market-making', etc. +@Column({ + type: "enum", + enum: ['arbitrage', 'market-making'], +}) +strategy: string; ```
    Make the orderId column non-nullable in the order entity. ___ **For consistency and to avoid potential errors, consider making the orderId column
    non-nullable. This ensures every order entity has a corresponding order ID from the
    exchange.** [server/src/common/entities/order.entity.ts [30]](https://github.com/Hu-Fi/Mr.Market/pull/144/files#diff-2aa5ad4319a48efa6de8b6cce7635377a524d840b69d00210ad0939064c0ea31R30-R30) ```diff -@Column() -orderId: string; // The order ID from the exchange +@Column({ nullable: false }) +orderId: string; ```
    Bug
    Correct the order side for sell orders when saving the order entity. ___ **For the side property in the order entity for sell orders, it should be set to 'sell'
    instead of 'buy'. This ensures the correct order side is recorded.** [server/src/modules/strategy/strategy.service.ts [427]](https://github.com/Hu-Fi/Mr.Market/pull/144/files#diff-413cb1b28e0d47a46768f97d10145a8e14d9e46b0a195768786127305916d944R427-R427) ```diff -side: 'buy', +side: 'sell', ```
    Fix the typo in the route for fetching market making orders. ___ **There's a typo in the route decorator for getting user orders. It should be 'marketmaking'
    instead of 'maretmaking'.** [server/src/modules/strategy/orders.controller.ts [12]](https://github.com/Hu-Fi/Mr.Market/pull/144/files#diff-0069d0715a0f071e72068a2530df2fa7b1663ab751cc3b8f9a90d343369b516fR12-R12) ```diff -@Get('/maretmaking/:userId') +@Get('/marketmaking/:userId') ```

    ✨ 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.