Code Monkey home page Code Monkey logo

hydro-sdk-backend's Introduction

Hydro SDK Backend

CircleCI Go Report Card

The Hydro SDK is a collection of golang language packages. You can use it to build a Dapp application backend based on the Hydro contract quickly. It can help to communicate with Ethereum node, match orders, monitor Ethereum results and so on. Some general data structures are also provided.

This project cannot be used alone. You need to add your own application logic. The following projects are built on top of this SDK.

Break down to each package

sdk

The main function of this package is to define the interface to communicate with a blockchain. We have implemented Ethereum communication codes based on this interface spec. So as long as the interface is implemented for a blockchain, hydro SDK backend can be used on top it.This makes it possible to support multi-chain environments easily.

common

We put some common data structures and interface definitions into this package for sharing with other projects.

engine

The engine maintains a series of market orderbooks. It is responsible for handling all placing orders and cancel requests. Requests in each market are processed serially, and multiple markets are concurrent.

The engine in this package only maintains the orderbook based on the received message and returns the result of the operation. It is not responsible for persisting these changes, nor for pushing messages to users. Persistent data and push messages are business logic and should be done by the upper application.

watcher

Blockchain Watcher is responsible for monitoring blockchain changes. Whenever a new block is generated, it gets all the transactions in that block. And pass each transaction to a specific method to deal with. This method requires you to register with the RegisterHandler function. You can process the transactions you are interested in as needed and skip unrelated transactions.

websocket

The Websocket package allows you to easily launch a websocket server. The server is channel based. Users can join multiple channels and can leave at any time.

The websocket server should have a message source. Every message read from the source will be broadcast to that channel. All users in the channel will receive this message.

If you want to make some special logic and not just broadcast the message. This can be done by creating your own channel.

Any structure that implements the IChannel interface can be registered to the websocket server.

There are already a customized channel called MarketChannel in this package. It keep maintaining the newest order book in memory.
If a new user joins this channel, it sends a snapshot of current market order book to the user. After receive a new event from source, it will update the order book in memory, then push the change event to all subscribers.

import (
    github.com/hydroprotocol/hydor-sdk-backend/common
    github.com/hydroprotocol/hydor-sdk-backend/websocket
)

// new a source queue
queue, _ := common.InitQueue(&common.RedisQueueConfig{
    Name:   common.HYDRO_WEBSOCKET_MESSAGES_QUEUE_KEY,
    Ctx:    ctx,
    Client: redisClient,
})

// new a websockert server
wsServer := websocket.NewWSServer("localhost:3002", queue)

websocket.RegisterChannelCreator(
    common.MarketChannelPrefix,
    websocket.NewMarketChannelCreator(&websocket.DefaultHttpSnapshotFetcher{
        ApiUrl: os.Getenv("HSK_API_URL"),
    }),
)

// Start the server
// It will block the current process to listen on the `addr` your provided. 
wsServer.Start()

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details

hydro-sdk-backend's People

Contributors

davidqhr avatar diveinto avatar keevon avatar kevinxzhang avatar mingmin-101 avatar wlchn avatar

Watchers

 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.