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

CEXIO error handling #783

Merged
merged 2 commits into from
Dec 4, 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
63 changes: 24 additions & 39 deletions extensions/exchanges/cexio/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function container (get, set, clear) {
if (!c.cexio || !c.cexio.username || !c.cexio.key || c.cexio.key === 'YOUR-API-KEY') {
throw new Error('please configure your CEX.IO credentials in ' + path.resolve(__dirname, 'conf.js'))
}
var nonce = Math.floor(new Date().getTime() / 1000)
var nonce = new Date().getTime() / 1000
authed_client = new CEX(c.cexio.username, c.cexio.key, c.cexio.secret).rest
authed_client.nonce = function () { return nonce++ }
}
Expand Down Expand Up @@ -61,7 +61,8 @@ module.exports = function container (get, set, clear) {
var client = publicClient()
var pair = joinProduct(opts.product_id)
client.trade_history(pair, args, function (err, body) {
if (err || typeof body === 'undefined' || body === 'error: Rate limit exceeded') return retry('getTrades', func_args, err)
if (typeof body === 'string' && body.match(/error/)) console.log(('\ngetTrades ' + body).red)
if (err || (typeof body === 'string' && body.match(/error/))) return retry('getTrades', func_args, body)
var trades = body.map(function (trade) {
return {
trade_id: Number(trade.tid),
Expand All @@ -79,7 +80,8 @@ module.exports = function container (get, set, clear) {
var func_args = [].slice.call(arguments)
var client = authedClient()
client.account_balance(function (err, body) {
if (err || typeof body === 'undefined' || body === 'error: Nonce must be incremented' || body === 'error: Rate limit exceeded') return retry('getBalance', func_args, err)
if (typeof body === 'string' && body.match(/error/)) console.log(('\ngetBalance ' + body).red)
if (err || (typeof body === 'string' && body.match(/error/))) return retry('getBalance', func_args, body)
var balance = { asset: 0, currency: 0 }
balance.currency = n(body[opts.currency].available).add(body[opts.currency].orders).format('0.00000000')
balance.currency_hold = n(body[opts.currency].orders).format('0.00000000')
Expand All @@ -94,7 +96,8 @@ module.exports = function container (get, set, clear) {
var client = publicClient()
var pair = joinProduct(opts.product_id)
client.ticker(pair, function (err, body) {
if (err || typeof body === 'undefined') return retry('getQuote', func_args, err)
if (typeof body === 'string' && body.match(/error/)) console.log(('\ngetQuote ' + body).red)
if (err || (typeof body === 'string' && body.match(/error/))) return retry('getQuote', func_args, body)
cb(null, { bid: String(body.bid), ask: String(body.ask) })
})
},
Expand All @@ -103,30 +106,36 @@ module.exports = function container (get, set, clear) {
var func_args = [].slice.call(arguments)
var client = authedClient()
client.cancel_order(opts.order_id, function (err, body) {
//if (body === 'Order canceled') return cb()
if (typeof body === 'string' && body.match(/error/)) console.log(('\ncancelOrder ' + body).red)
if (err) return retry('cancelOrder', func_args, err)
cb()
})
},

buy: function (opts, cb) {
trade: function (action, opts, cb) {
var func_args = [].slice.call(arguments)
var client = authedClient()
var pair = joinProduct(opts.product_id)
if (opts.order_type === 'taker') {
delete opts.price
delete opts.post_only
opts.size = n(opts.size).multiply(opts.orig_price).value() // CEXIO estimates asset size and uses free currency to performe margin buy
if (action === 'buy') {
opts.size = n(opts.size).multiply(opts.orig_price).value() // CEXIO estimates asset size and uses free currency to performe margin buy
}
opts.type = 'market'
}
client.place_order(pair, 'buy', opts.size, opts.price, opts.type, function (err, body) {
client.place_order(pair, action, opts.size, opts.price, opts.type, function (err, body) {
if (typeof body === 'string' && body.match(/error/)) console.log(('\ntrade ' + body).red)
if (err || (typeof body === 'string' && body.match(/error/) && body !== 'error: Error: Place order error: Insufficient funds.')) return retry('trade', func_args, body)
if (body === 'error: Error: Place order error: Insufficient funds.') {
var order = {
status: 'rejected',
reject_reason: 'balance'
}
return cb(null, order)
}
if (err) return retry('buy', func_args, err)
if (err) return retry('trade', func_args, err)
order = {
id: body && (body.complete === false || body.message) ? body.id : null,
status: 'open',
Expand All @@ -142,45 +151,21 @@ module.exports = function container (get, set, clear) {
})
},

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

sell: function (opts, cb) {
var func_args = [].slice.call(arguments)
var client = authedClient()
var pair = joinProduct(opts.product_id)
if (opts.order_type === 'taker') {
delete opts.price
delete opts.post_only
opts.type = 'market'
}
client.place_order(pair, 'sell', opts.size, opts.price, opts.type, function (err, body) {
if (body === 'error: Error: Place order error: Insufficient funds.') {
var order = {
status: 'rejected',
reject_reason: 'balance'
}
return cb(null, order)
}
if (err) return retry('buy', func_args, err)
order = {
id: body && (body.complete === false || body.message) ? body.id : null,
status: 'open',
price: opts.price,
size: opts.size,
post_only: !!opts.post_only,
created_at: new Date().getTime(),
filled_size: '0',
ordertype: opts.order_type
}
orders['~' + body.id] = order
cb(null, order)
})
exchange.trade('sell', opts, cb)
},

getOrder: function (opts, cb) {
var func_args = [].slice.call(arguments)
var order = orders['~' + opts.order_id]
var client = authedClient()
client.get_order_details(opts.order_id, function (err, body) {
if (err || body === 'error: Invalid Order ID') return retry('getOrder', func_args, err)
if (typeof body === 'string' && body.match(/error/)) console.log(('\ngetOrder ' + body).red)
if (err || (typeof body === 'string' && body.match(/error/))) return retry('getOrder', func_args, body)
if (body.status === 'c') {
order.status = 'rejected'
order.reject_reason = 'canceled'
Expand Down