Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Bitfinex WS Fixes: Reconnect and crash on previously non-traded assets/currency #474

Merged
merged 3 commits into from
Aug 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions extensions/exchanges/bitfinex/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module.exports = function container (get, set, clear) {
var c = get('conf')
var s = {options: minimist(process.argv)}
var so = s.options

var ws_connecting = false
var ws_connected = false
var ws_timeout = 60000
var ws_retry = 10000
var ws_wait_on_apikey_error = 60000 * 5

var pair, public_client, ws_client

var ws_trades = []
var ws_balance = []
var ws_orders = []
var ws_ticker = []
var ws_orders = []
var ws_ticker = []
var ws_hb = []
var ws_walletCalcDone

Expand All @@ -35,7 +35,7 @@ module.exports = function container (get, set, clear) {
} else if (trades[0] === "te") {
return
}

trades.forEach(function (trade) {
newTrade = {
trade_id: Number(trade.ID),
Expand All @@ -46,13 +46,13 @@ module.exports = function container (get, set, clear) {
}
ws_trades.push(newTrade)
})

Copy link
Contributor

@nedievas nedievas Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

if (ws_trades.length > 1010)
ws_trades.shift()
}

function wsUpdateTicker (pair, ticker) {
ws_ticker = ticker
ws_ticker = ticker
}

function wsMessage (message) {
Expand All @@ -65,7 +65,7 @@ module.exports = function container (get, set, clear) {
if (message[0] != "undefined")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove at 64

ws_hb[message[0]] = Date.now()
}

function wsUpdateOrder (ws_order) {
cid = ws_order[2]

Expand Down Expand Up @@ -94,9 +94,9 @@ module.exports = function container (get, set, clear) {
order.price = ws_order[16]
order.price_avg = ws_order[17]

ws_orders['~' + cid] = order
ws_orders['~' + cid] = order
}

function wsUpdateOrderCancel (ws_order) {
cid = ws_order[2]

Expand All @@ -114,15 +114,15 @@ module.exports = function container (get, set, clear) {

wsUpdateOrder(ws_order)
}

function wsUpdateReqOrder (error) {
if (error[6] === 'ERROR' && error[7].match(/^Invalid order: not enough .* balance for/)) {
cid = error[4][2]
ws_orders['~' + cid].status = 'rejected'
ws_orders['~' + cid].reject_reason = 'balance'
}
}

function updateWallet (wallets) {
if (typeof(wallets[0]) !== "object") wallets = [wallets]

Expand Down Expand Up @@ -201,7 +201,7 @@ module.exports = function container (get, set, clear) {
ws_connected = false

ws_client = new BFX(c.bitfinex.key, c.bitfinex.secret, {version: 2, transform: true}).ws

ws_client
.on('open', wsOpen)
.on('close', wsClose)
Expand All @@ -217,16 +217,16 @@ module.exports = function container (get, set, clear) {
.on('ou', wsUpdateOrder)
.on('oc', wsUpdateOrderCancel)

setTimeout(function() {
setInterval(function() {
wsConnect()
}, ws_retry)
}
}

function joinProduct (product_id) {
return product_id.split('-')[0] + '' + product_id.split('-')[1]
}

function retry (method, args, cb) {
setTimeout(function () {
exchange[method].call(exchange, args, cb)
Expand All @@ -238,24 +238,24 @@ module.exports = function container (get, set, clear) {
exchange[method].call(exchange, args, cb)
}, 50)
}

function encodeQueryData(data) {
let ret = []
for (let d in data)
ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]))
return ret.join('&')
}

var exchange = {
name: 'bitfinex',
historyScan: 'backward',
makerFee: 0.1,
takerFee: 0.2,

getProducts: function () {
return require('./products.json')
},

getTrades: function (opts, cb) {
if (!pair) { pair = joinProduct(opts.product_id) }

Expand Down Expand Up @@ -292,7 +292,7 @@ module.exports = function container (get, set, clear) {
}
})
cb(null, trades)
})
})
} else {
// We're live now (i.e. opts.from is set), use websockets
if (!ws_client) { wsClient() }
Expand All @@ -301,7 +301,7 @@ module.exports = function container (get, set, clear) {
cb(null, trades)
}
},

getBalance: function (opts, cb) {
if (!pair) { pair = joinProduct(opts.asset + '-' + opts.currency) }

Expand Down Expand Up @@ -353,8 +353,8 @@ module.exports = function container (get, set, clear) {
}
else {
balance = {}
balance.currency = n(ws_balance[opts.currency].balance).format('0.00000000')
balance.asset = n(ws_balance[opts.asset].balance).format('0.00000000')
balance.currency = ws_balance[opts.currency].balance ? n(ws_balance[opts.currency].balance).format('0.00000000') : n(0).format('0.00000000')
balance.asset = ws_balance[opts.asset].balance ? n(ws_balance[opts.asset].balance).format('0.00000000') : n(0).format('0.00000000')

balance.currency_hold = ws_balance[opts.currency].available ? n(ws_balance[opts.currency].balance).subtract(ws_balance[opts.currency].available).format('0.00000000') : n(0).format('0.00000000')
balance.asset_hold = ws_balance[opts.asset].available ? n(ws_balance[opts.asset].balance).subtract(ws_balance[opts.asset].available).format('0.00000000') : n(0).format('0.00000000')
Expand All @@ -365,11 +365,11 @@ module.exports = function container (get, set, clear) {
cb(null, balance)
}
},

getQuote: function (opts, cb) {
cb(null, { bid : String(ws_ticker.BID), ask : String(ws_ticker.ASK) })
},

cancelOrder: function (opts, cb) {
order = ws_orders['~' + opts.order_id]
ws_orders['~' + opts.order_id].reject_reason = "zenbot cancel"
Expand All @@ -395,7 +395,7 @@ module.exports = function container (get, set, clear) {
}
cb()
},

trade: function (action, opts, cb) {
if (!pair) { pair = joinProduct(opts.product_id) }
var symbol = 't' + pair
Expand Down Expand Up @@ -429,7 +429,7 @@ module.exports = function container (get, set, clear) {
filled_size: 0,
ordertype: opts.order_type
}

var ws_order = [
0,
'on',
Expand Down Expand Up @@ -461,22 +461,22 @@ module.exports = function container (get, set, clear) {

return cb(null, order)
},

buy: function (opts, cb) {
exchange.trade('buy', opts, cb)
},

sell: function (opts, cb) {
exchange.trade('sell', opts, cb)
},

getOrder: function (opts, cb) {
var order = ws_orders['~' + opts.order_id]

if (order.status === 'rejected' && order.reject_reason === 'post only') {
if (order.status === 'rejected' && order.reject_reason === 'post only') {
return cb(null, order)
} else if (order.status === 'rejected' && order.reject_reason === 'zenbot canceled') {
return cb(null, order)
return cb(null, order)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

if (order.status == "done") {
Expand All @@ -486,7 +486,7 @@ module.exports = function container (get, set, clear) {

cb(null, order)
},

// return the property used for range querying.
getCursor: function (trade) {
return (trade.time || trade)
Expand Down