WebSockets for EOS-based on-chain exchange
With the launch of eosfinex right around the corner, we’re pleased to give a sneak peek into the mechanics powering the exchange.
eosfinex is a decentralised exchange built on EOS which will run fully on-chain — custody, order book, and matching engine are all part of the EOS contracts.
In the below article, we will take a look at the additional WebSocket protocols which we are offering to traders, as well as using the gateway in practise.
EOS On WebSockets
Usually, EOS contracts offer an HTTP-based API, which will be one way of trading on eosfinex. In addition to this, we will also support low latency WebSockets. WebSockets allow users to react quickly to drastic market movements and are at the same time a web standard.
The WebSocket protocol for eosfinex is based on the v2 protocol that is in use at Bitfinex. To make trading in the decentralised environment possible, we added a few changes.
We’ve designed the gateway to be capable of high-performance, and to make it simple for existing Bitfinex users to use it.
The following section shows a trade update message from the eosfinex WebSocket gateway.
Looks pretty familiar to our API v2 messages, doesn’t it?
1 ] ]
Auth & Pre-Signing
To be able to trade via WebSockets, we have to connect to the WebSocket gateway. The gateway itself runs as a native EOS plugin, on an EOS node next to the contract. The trader presigns the orders with their private key before sending it to the gateway.
The gateway then applies it to the contract. This way, no private keys ever leave the traders machine.
Below is a sample message, sent from the client to the gateway, which would submit a trade:
By nature, all updates are public for an on-chain exchange. But how can one decide which trade update message belongs to the right user? This is an important question required for sending the right message to their socket.
For this reason we have added a special endpoint to our contract. The name of the EOS action is
validate. Sunbeam, our WebSocket library uses it as part of an auth process.
By calling auth(), Sunbeam sends a locally signed message to the websocket gateway. The message sent to the validate action itself is empty and does not trigger anything.
The gateway only uses it to verify the user by comparing the signed message with their public keys on-chain. With an successful auth, we receive “private” wallet messages, trades and order updates.
Below you will find a brief demonstration of Scatter:
If you are curious as to how Sunbeam functions, feel free to take a look below. It is fully open-sourced under Apache License 2.0.
We hope you enjoyed this quick preview and are excited as us about the upcoming launch of eosfinex.