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

Spot ui #82

Closed zed-wong closed 5 months ago

zed-wong commented 6 months ago

For displaying price

For clicking the confirm buy/sell button

Check order status

zed-wong commented 6 months ago

The whole process of placing an order would look like this:

  1. Connect to the wallet and display the balance of the corresponding pair of transactions
  2. Select the trading pair
  3. Select the order type (Market/Limit).
  4. Select Buy/Sell
  5. Enter the amount
  6. Click on the 'buy BTC' button to place the order, check that the entered quantity is within the minimum and maximum quantities and that there is sufficient liquidity.
  7. Confirm order dialog pops up
  8. Click the confirm order button to call Mixin payment
  9. poll the backend getOrderById API with trace ID, check the order then jump to the order details page, timeout if not found within 1 minute.

What we lack now:

  1. After connecting wallet, AfterMixinOauth function will read user balance, we need to get the balance from inside wallet.ts userAssets store corresponding to asset id, e.g. BTC/USDT, when the type is Buy, we need to show the balance of USDT, and Sell shows the balance of BTC.

  1. check if the quantity is within the min and max quantities, this backend doesn't have a return field yet, we can get it from the value of the corresponding pair on the corresponding exchange in ccxt. This field can be added to @Get('/tickers/pairs') in marketdata.controller.ts as getSupportedPairs in marketdata.service.ts

The check for sufficient liquidity is implemented by using the method in exchange.service.ts to read the balance of the corresponding pair on the exchange.

This can be done by adding a field to getSupportedPairs so that the front-end can directly read the information it needs when switching between pairs, but this will expose the balance of the API key because it must return the balance of the corresponding asset on the exchange to do a quantity check, and should prevent the user from placing an order if there is insufficient quantity on the exchange. It is also possible to make a separate endpoint for checking the balance, so that the quantity is passed in and only whether there is enough balance to support the transaction is returned.

@Get('/tickers/pairs') getSupportedPairs This API is used to get the list of supported pairs for the frontend, and is called when we select and switch pairs in /spot/okx/BTC-USDT.


  1. We will need to subtract fee from Estimated price in confirm order dialog. The current Estimated price is a value that directly get from exchange. which is needed to subtract our platform trading fee and our rebalance cost. The trading fee is a value in customConfig.service.ts inside readSpotFee function. Now it's 0.2%.

The rebalance cost is a bit of complicated. For different token there will be different fee and the fee is dynamic. We need to get the value from mixin api and bigone api. We may add a field to getSupportedPairs endpoint for rebalance cost calculation.

It would take massive amount of time to find the best and the most precise way for calculating the rebalance cost. We can skip this for now and just reduce the platform trading fee from the Estimated price.


  1. getOrderById API is not tested yet, redirect is also not tested yet, we need to test placedOrderUpdater() in exchange.service.ts to make sure that the order state in our database will be successfully updated to the latest values.

@mj-blockydevs @arianejasuwienas

zed-wong commented 6 months ago

@mj-blockydevs As we don't have enough time for fine tuning the massive amount of details:

  1. we skip it and work on it after the alpha release. leave it as hardcoded as now.
  2. we skip it and work on it after the alpha release.
  3. we set the estimated price to subtract 0.2% fees (which in practice is to add 0.2%)
  4. we focus on getting one exchange to work. such as okx.
zed-wong commented 6 months ago

@mj-blockydevs

the whole process of placing an order would be like:

  1. we open /spot, default OKX BTC/USDT, buy and market order
  2. enter amount of USDT
  3. click on Buy BTC
  4. click on Confirm order
  5. pay the amount of USDT with mixin
  6. show the loading spinner within the Confirm order button
  7. Fetching from backend API by traceId (orderId), timeout 1 min.
  8. Once order is found, redirect to details page (such as /spot/history/2fd3e077-5ded-4624-87e1-4cc1f7b78931)
  9. Keep fetching details and update states until success in details page.
  10. The backend (exchange.service.ts) place order(placeOrder()) in exchange.
  11. The backend (exchange.service.ts) scheduler(placedOrderUpdater()) update the order state.
  12. Once the order succeed, the scheduler would send event to mixin.listener.ts and release token to user.

we will need to mainly focus on steps 7-12