coinbase / coinbase-pro-node

DEPRECATED — The official Node.js library for Coinbase Pro
Apache License 2.0
844 stars 316 forks source link

Orderbook sync has invalid state with crossed orders. #372

Closed loriopatrick closed 4 years ago

loriopatrick commented 5 years ago

Using version: 0.9.0

OrderbookSync produces state with cross orders.

Example code

const Big = require('big.js');
const CoinbasePro = require('coinbase-pro');

const markets = ['DAI-USDC', 'BAT-USDC', 'ETH-USD', 'ZRX-USD', 'BTC-USD'];
const orderbooks = new CoinbasePro.OrderbookSync();

setInterval(() => {
  markets.forEach(market_id => {
    const book = orderbooks.books[market_id];
    if (!book) {
      return;
    }

    const book_state = book.state();

    if (!book_state.bids.length || !book_state.asks.length) {
      return;
    }

    if (Big(book_state.bids[0].price).gt(book_state.asks[0].price)) {
      console.error('book is crossed');

      console.error('%j', market_id);
      console.error('%j\n%j', book_state.bids[0], book_state.asks[0]);
      console.error('%j\n%j', book_state.bids[book_state.bids.length - 1], book_state.asks[book_state.asks.length - 1]);
      process.exit(1);
    }
  });
}, 1000);

Output

book is crossed
"BTC-USD"
{"id":"138950da-6280-47ac-b22d-7a807efd5cf3","side":"buy","price":"10090.02","size":"0.19476695"}
{"id":"29ad4c66-83ea-4144-93b5-85bcce75269e","side":"sell","price":"10088.2","size":"0.49505608"}
{"id":"1a9b627e-e56f-47f6-aafd-9c3770f92c1a","side":"buy","price":"0.01","size":"280"}
{"id":"5d8e75e8-113f-4d86-a21b-784c43c03d9b","side":"sell","price":"8385505711.3","size":"0.01"}
drewrothstein commented 4 years ago

Hi, we are closing out PRs + Issues as this project is being archived.