# serum-machine

> Real-time market data API server for Serum DEX

Latest version **0.1.3** (published 2020-09-17) · MPL-2.0 license · 0 weekly downloads

## Install

```sh
npm install serum-machine
pnpm add serum-machine
yarn add serum-machine
bun add serum-machine
```

Provides the command `serum-machine`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2020-09-17 |
| First published | 2020-09-10 |
| Weekly downloads | 0 |
| License | MPL-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 11 |
| Unpacked size | 153 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 176 |
| Maintainers | tardis_thad |
| Keywords | serum dex, serum api, api client, solana, cryptocurrency api, exchange |

## Links

- npm: https://www.npmjs.com/package/serum-machine
- Repository: https://github.com/tardis-dev/serum-machine
- Issues: https://github.com/tardis-dev/serum-machine/issues
- npm.io page: https://npm.io/package/serum-machine

## Dependencies (11)

- [bn.js](https://npm.io/package/bn.js.md) ^5.1.3
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [yargs](https://npm.io/package/yargs.md) ^16.0.3
- [bintrees](https://npm.io/package/bintrees.md) ^1.0.2
- [is-docker](https://npm.io/package/is-docker.md) ^2.1.1
- [didyoumean2](https://npm.io/package/didyoumean2.md) ^4.1.0
- [@types/bn.js](https://npm.io/package/@types/bn.js.md) ^4.11.6
- [uWebSockets.js](https://npm.io/package/uWebSockets.js.md) github:uNetworking/uWebSockets.js#v18.4.0
- [@solana/web3.js](https://npm.io/package/@solana/web3.js.md) ^0.74.0
- [@types/bintrees](https://npm.io/package/@types/bintrees.md) ^1.0.2
- [@project-serum/serum](https://npm.io/package/@project-serum/serum.md) ^0.12.2

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2020-09-17
- 0.1.2 — 2020-09-17
- 0.1.1 — 2020-09-11
- 0.1.0 — 2020-09-10

## README

# Serum Machine

[![Version](https://img.shields.io/npm/v/serum-machine.svg)](https://www.npmjs.org/package/serum-machine)

Real-time market data API server for Serum DEX
<br/>

## Architecture

![architecture diagram](https://user-images.githubusercontent.com/51779538/92960443-ed6a4a00-f46d-11ea-9da8-2d4546db8a7d.png)


- server runs with multiple `Minions` worker threads* and single `Serum Producer` that runs in the main thread
- `Minions` are responsible for WebSockets subscriptions management that includes handling subscriptions requests and sending data to all connected clients
- `Serum Producer` is responsible for connecting to Serum Node RPC WS API and subscribing all relevant accounts changes (event & request queue, bids & asks) for all supported markets as well as producing market data messages that are then passed to minions and published as WebSocket messages to all subscribed clients

\* multi core support via [`worker_threads`](https://nodejs.org/api/worker_threads.html) is linux only feature which allows multiple threads to bind to the same port, see https://github.com/uNetworking/uWebSockets.js/issues/304 and https://lwn.net/Articles/542629/ - for other OSes there's only one worker thread running
<br/>
<br/>

## Installation options

- ### npx <sub>(requires Node.js >= 12 installed on host machine)</sub>

  That will start Serum Machine server running on port `8000`

  ```sh
  npx serum-machine
  ```

  If you'd like to switch to different Serum Node endpoint, change port or run with debug logs enabled, just add one of the available CLI options:

  ```sh
  npx serum-machine --endpoint https://solana-api.projectserum.com --debug --port 8080
  ```

  Run `npx serum-machine --help` to see all available startup options (node endpoint url, port etc.)
  <br/>
  <br/>

- ### npm <sub>(requires Node.js >= 12 installed on host machine)</sub>

  Installs `serum-machine` globally and runs it on port `8000`.

  ```sh
  npm install -g serum-machine
  serum-machine
  ```

  If you'd like to switch to different Serum Node endpoint, change port or run with debug logs enabled, just add one of the available CLI options:

  ```sh
  serum-machine --endpoint https://solana-api.projectserum.com --debug --port 8080
  ```

  Run `serum-machine --help` to see all available startup options (node endpoint url, port etc.)
  <br/>
  <br/>

- ### Docker
  Pulls and runs latest version of [`tardisdev/serum-machine` image](https://hub.docker.com/r/tardisdev/serum-machine). Serum Matchine server will available on host via `8000` port (for example [http://localhost:8000/v1/markets](http://localhost:8000/v1/markets)) with debug logs enabled (`TM_DEBUG` env var).
  ```sh
  docker run -p 8000:8000 -e "SM_ENDPOINT=https://solana-api.projectserum.com" -e "SM_DEBUG=true" -d tardisdev/serum-machine:latest
  ```
  <br/>
  <br/>

## WebSocket `/streams` endpoint

Allows subscribing to Serum DEX real-market data streams.

```js
const ws = new WebSocket('ws://localhost:8000/v1/streams')

ws.onmessage = (message) => {
  console.log(message)
}

ws.onopen = () => {
  const subscribePayload = {
    op: 'subscribe',
    channel: 'trades',
    markets: ['BTC/USDT', 'SRM/USDT']
  }

  ws.send(JSON.stringify(subscribePayload))
}
```

<br/>
<br/>

## HTTP endpoints

### `/markets`

Accepts no params and returns supported Serum markets.

#### Sample request & response

[http://localhost:8000/v1/markets](http://localhost:8000/v1/markets)

```json
[
  {
    "name": "ALEPH/USDT",
    "address": "EmCzMQfXMgNHcnRoFwAdPe1i2SuiSzMj1mx6wu3KN2uA",
    "programId": "4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn",
    "deprecated": false
  },
  {
    "name": "ALEPH/USDC",
    "address": "B37pZmwrwXHjpgvd9hHDAx1yeDsNevTnbbrN9W12BoGK",
    "programId": "4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn",
    "deprecated": false
  },
  {
    "name": "BTC/USDT",
    "address": "8AcVjMG2LTbpkjNoyq8RwysokqZunkjy3d5JDzxC6BJa",
    "programId": "4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn",
    "deprecated": false
  }
]
```

---
_Source: https://npm.io/package/serum-machine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
