Node.JS Client ‘2.0.0.beta-1' Release - The Bitfinex Blog
16798
post-template-default,single,single-post,postid-16798,single-format-standard,bridge-core-3.0.6,et_bloom,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,qode-content-sidebar-responsive,qode-child-theme-ver-1.0.0,qode-theme-ver-29.3,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.10.0,vc_responsive

Node.JS Client ‘2.0.0.beta-1′ Release

The first update to our 2.0.0.beta Node API library is ready, and comes with many fixes, tweaks, and new features to support the recent updates to our REST and WebSocket v2 APIs.

New CLI Commands

Alongside changes to the library internals, this release includes a set of command-line helpers for querying your account data in tabular form:

Bitfinex Node API library

The available commands use the API key & secret provided in an .env file in the library’s root folder. To use them, create a file in your local copy of the library with the following format:

API_KEY=...
API_SECRET=...

Then, run any one of the available commands:

npm run tickers - fetch the latest ticker data for all symbols
npm run symbols - fetch the latest symbol data npm run order-history <ETHUSD> - query your order history for a symbol, start/end/limit currently hardcoded npm run trade-history <ETHUSD> - queries trades, see order-history npm run positions - view open positions npm run positions pl - as above, but with P/L information npm run balances - view wallet balances npm run balances <USD> - as above, but with estimated value npm run orders - view open orders npm run cancel-orders - cancel all open orders (via ws2) npm run status - view platform status

The results are returned within CLI tables where possible, and can be passed through utilities like grep for further processing:

Bitfinex Node API library

All commands except for ‘cancel-orders’ perform read-only operations; ‘cancel-orders’ itself spawns a WSv2 client to fetch your account order snapshot before proceeding to cancel all orders included within it.

In the future we will add new commands and further refine their output, in order to allow you to perform basic account operations directly from your nearest console interface.

New REST Endpoints

We’ve added support for closing positions & querying the platform status (under maintenance or operative) to the RESTv1 & RESTv2 classes, along with full query param support (start, end, limit, sort) for the RESTv2 candles endpoint helper.

const rest = bfx.rest(2)

// Close position with ID 42
rest.closePosition({ position_id: 42 }).then(() => {
  // position closed
})

// Query platform status
rest.status().then(status => {
  // status is 0 for maintenance, 1 for operative
})

// Query candles with start/end/limit
rest.candles({
  timeframe: '1m',
  symbol: 'tETHUSD',
  query: {
    start: Date.now() - (24 * 60 * 60 * 1000),
    end: Date.now(),
    limit: 1000
  }
}).then((candles) => {
  // process received candles
})

 

Improved WSv2 Order Listener Filtering

All WSv2 listener helpers for order-related messages (onOrderUpdate, etc) now support filtering by order id, gid, cid, and symbol. This allows you to bind a listener for a specific order CID to only receive updates for that order.

const ws = bfx.ws(2)
const cid = Date.now() // filter by client ID const cbGID = Date.now() // group listeners together // Listen for updates to our order ws.onOrderUpdate({ cid, cbGID }, (ou) => { // handle order updates }) ws.onOrderNew({ cid, cgGID }, (on) => { // handle order creation }) // remove listeners when the order closes ws.onOrderClosed({ cid, cbGID }, (oc) => { ws.removeListeners(cbGID) }) // Create the order const o = new Order({ type: 'EXCHANGE LIMIT', symbol: 'tETHUSD', price: 500, amount: 2, cid }, ws) o.submit().then(() => { console.log('order submitted') })

As before the listeners can be grouped together with a callback ID (cbID), and removed all at once.

Order Book Checksums

The OrderBook model now provides a checksum() method, which can be used to verify checksums received from the WSv2 API. A static checksumArr() helper is also available for array-format order books.

const { OrderBook } = require('bitfinex-api-node/lib/models')
const ob = new OrderBook({
  bids: [[6000, 1, 1], [5900, 1, 2]],
  asks: [[6100, 1, -3], [6200, 1, -4]]
})

// Outputs the equivalent of CRC.str('6000:1:6100:-3:5900:2:6200:-4')
console.log(ob.checksum())

// Or, with an array-format order book
console.log(OrderBook.checksumArr([
  [6000, 1, 1],
  [5900, 1, 2],
  [6100, 1, -3],
  [6200, 1, -4]
]))

Along with these helpers, the WSv2 client now automatically verifies checksums for managed order books if checksums have been enabled. If automatic verification fails, an error event is emitted.

WSv2 Order Flags

Support for the new order flags has been added, along with a map of valid flag values on the Order model itself. Flags can now be modified via dedicated getters/setters on the Order model, with a combined flags field value maintained internally.

// if 'o' is an order:
o.setHidden(true)
o.setPostOnly(false)
o.setReduceOnly(true)
o.setPositionClose(false)
o.setNoVariableRates(true)

console.log(o.isHidden()) // true
console.log(o.isPostOnly()) // false
console.log(o.isReduceOnly()) // true
console.log(o.isPositionClose()) // false
console.log(o.includesVariableRates()) // false
console.log(o.flags) // 525376, or 64 & 1024 & 524288

 

WSv2 Atomic Order Updates

The Order class now supports the new atomic update API via an update() method, which accepts & prepares an update change-set. The method returns a Promise, which resolves after receiving an update confirmation from the server.

const o = new Order({
  symbol: 'tEOSUSD',
  type: Order.type.EXCHANGE_LIMIT,
  price: 20,
  amount: 200
}, ws)

o.submit().then(() => {
  return o.update({ price: 23 })
}).then(() => {
  console.log('set order price to 23')
}).then(() => {
  return o.update({ delta: 100 })
}).then(() => {
  console.log('increased order amount to 300')
})

For more information on both order flags & atomic updates, see the April API dev update.

Fixes & Tweaks

Besides these new features, several bugs have been found & fixed and core parts of the library, such as the model system, have been refactored. Moving forwards, we will release new versions of the library more often and with smaller change-sets in order to keep the npm registry version up to date.

We apologise to those of you that reached out via our Github issue tracker as a result of using an outdated version of the library, and we thank you for your patience during the wait.

For now, upgrade your node client to v2.0.0.beta-1 and let us know what you think!


Stay up to date with Bitfinex on TwitterLinkedIn & Telegram.

Most of our development libraries are open-source! If you are interested in learning more about the Bitfinex development libraries, visit our Github

Join us on our mission to create the industry’s most innovative digital asset trading platform.

Tags: