Giter Club home page Giter Club logo

order-book's Introduction

Hello! Thanks for your time on this Simple orderbook project

Getting start

  1. yarn / npm install
  2. yarn start
  3. yarn test for testing

Key points

  1. React.js for Orderbook UI
  2. Establish socket connection and get bids and asks data.
  3. Merge the push data to the orderbook
  4. Edge cases handle: automatic reconnect/resubscription, fallbacks with api, error handlers.
  5. Memory performance review
  6. Some e2e test cases with cypress.

Structure

  1. Rematch for Redux management, which handled all the data update logic.

The orderbook model should look like this:

export const orderbook = {
  state: INIT_STATE, // initial state of the orderbook
  reducers: {}, // reducers
  effects: (dispatch) => ({
    // effects handlers
    initSnapshot() {},
    udpateAsks() {}
    updateBids() {}
  }),
};

Use the Provider to wrap our Orderbook component and pass the store prop.

<Provider store={store}>
  <OrderbookContainer />
</Provider>

When we use the event handler to manage the data, we dispatch the data

subscriber.on("subscribed", function() {
  const { asks, bids } = ctx.data;
  dispatch.orderbook.initSnapshot({
    asks,
    bids,
  });
});

The benefit of using Rematch is to avoid writing boilerplate code when using the Redux

  1. /hooks for managing event handlers
export const useSocketData = () => {
  /**
   * Event handlers
   * 1. ws connect handler
   * 2. data subscribe handler
   * 3. error handler
   */
};
  1. /orderbook for UI rendering

  2. centrifugal.js for websocket management

  3. Generating the jwt_token:

from websocket import WebSocketApp
from rabbitx import const
from rabbitx.client import Client, WSClient, WSClientCallback

if __name__ == '__main__':
    private_key = 'PRIVATE_KEY'
 # change this to your private key
    testnet=True # change this to False if using on mainnet
    if testnet:
        client = Client(api_url=const.TESTNET_URL, private_key=private_key)
    else:
        client = Client(api_url=const.URL, private_key=private_key)
    x=client.onboarding.onboarding()

    print(x)

In this project, I put the generated token in the .env file. However, it's a better option to get jwt_token from an endpoint.

  1. cypress for some e2e tests. They may not be so thourough

Challenges

  • I spent quite some time for python environment setting... But finally run onboarding.py successfully on venv with poetry
  • It took some time on the socket data update logic. It was hard to debug at the beginning and couldn't calculate the level Total correctly
  • The frequent updated components should be an performance issue since we couldn't delay the update when new data comes in.

Areas for imporvements

  • Add the fallbacks if websocket is down, such as http_stream, socketJS and sse
  • Add the market depth, making the Orderbook more complete.
  • The calculation inside the orderbook model effects would be overloaded when the push data comes frequently. It's probably a performance issue. Web worker could be a good solution to handle the complex calculation in the background thread and send back the result to main thread.
  • Use styled-system for responsive components
  • Error reports to some platform like Sentry.io for errors tracking
  • Should refactor the code into Typescript

order-book's People

Stargazers

CLQuantizer avatar

Watchers

Dylanwoo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.