From 4a01ac6c20757162cbd01f31bc93a4c964026a41 Mon Sep 17 00:00:00 2001 From: brucetus <31704711+brucetus@users.noreply.github.com> Date: Tue, 24 Apr 2018 18:23:13 -0700 Subject: [PATCH] Fix Bittrex error I fixed the order cancel error in issue #1514. I changed the cancelOrder and getOrder functions. I also changed 'fistRun' to 'firstRun' --- extensions/exchanges/bittrex/exchange.js | 90 +++++++++++------------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/extensions/exchanges/bittrex/exchange.js b/extensions/exchanges/bittrex/exchange.js index 977e2769d4..6af479a40e 100644 --- a/extensions/exchanges/bittrex/exchange.js +++ b/extensions/exchanges/bittrex/exchange.js @@ -7,9 +7,9 @@ var bittrex_authed = require('node-bittrex-api'), **/ module.exports = function bittrex(conf) { let recoverableErrors = new RegExp(/(ESOCKETTIMEOUT|ESOCKETTIMEDOUT|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ENOTFOUND|Invalid nonce|Rate limit exceeded|URL request error)/) - + let shownWarning = false - let fistRun = true + let firstRun = true let allowGetMarketCall=true let marketRefresh = 15000 @@ -31,7 +31,7 @@ module.exports = function bittrex(conf) { if (error.message) if (error.message.match(/Rate limit exceeded/)) { timeout = 10000 - } + } setTimeout(function () { exchange[method].apply(exchange, args) }, timeout) @@ -43,13 +43,13 @@ module.exports = function bittrex(conf) { if (err) { if (err.message && err.message.match(recoverableErrors)) { - + return retry(command, args, err) } - return callback(err, []) + return callback(err, []) } - - + + if (typeof data !== 'object') { console.log(`bittrex API ${command} had an abnormal response, quitting.`) return callback(null, []) @@ -103,7 +103,7 @@ module.exports = function bittrex(conf) { //accomplish back trades using 2 calls. //ticks and getMarket and create a hybrid result. var trades = [] - // first run do the full deal. + // first run do the full deal. // 2nd run. only return the last trades if (allowGetMarketCall != true) { @@ -111,7 +111,7 @@ module.exports = function bittrex(conf) { return null } - if (fistRun) + if (firstRun) { bittrex_public.getticks(args, function( data, err) { let res = handleErrors('getTrades', err, data, func_args, cb) @@ -126,13 +126,13 @@ module.exports = function bittrex(conf) { if (res) { let lastVal = 0 - + for (const key in Object.keys(data.result)) { - + var trade = data.result[key] if (isNaN(opts.from) || new Date(trade.T).getTime() > new Date(opts.from).getTime()) { let buySell = 'sell' - //todo: unsure about the >. if the price is greater than the last one should this one be a buy or sell. figure it out. + //todo: unsure about the >. if the price is greater than the last one should this one be a buy or sell. figure it out. if (parseFloat(trade.C) > lastVal) buySell = 'buy' trades.push({ trade_id: new Date(trade.T).getTime(), @@ -148,16 +148,16 @@ module.exports = function bittrex(conf) { } - + bittrex_public.getmarkethistory(args, function( data, err) { let res2 = handleErrors('getTrades', err, data, func_args, cb) - + if (res2) { for (const key in Object.keys(data.result)) { var trade = data.result[key] if (isNaN(opts.from) || new Date(trade.TimeStamp).getTime() > new Date(opts.from).getTime()) { - + trades.push({ //trade_id: trade.Id, trade_id: new Date(trade.TimeStamp).getTime(), @@ -171,7 +171,7 @@ module.exports = function bittrex(conf) { }) } } - fistRun = false + firstRun = false allowGetMarketCall = false setTimeout(()=>{allowGetMarketCall = true},marketRefresh) //make sure all times come out sorted correctly. there is a chance they can appear in the array out of order otherwise. @@ -181,21 +181,21 @@ module.exports = function bittrex(conf) { return 0 } ) - cb(null, trades) - + cb(null, trades) + } }) - + } }) } else { bittrex_public.getmarkethistory(args, function( data, err) { let res2 = handleErrors('getTrades', err, data, func_args, cb) - + if (res2) { - + for (const key in Object.keys(data.result)) { var trade = data.result[key] if (isNaN(opts.from) || new Date(trade.TimeStamp).getTime() > new Date(opts.from).getTime()) { @@ -210,12 +210,12 @@ module.exports = function bittrex(conf) { side: trade.OrderType || trade.OrderType == 'SELL' ? 'sell': 'buy' }) } - + } allowGetMarketCall = false - - + + setTimeout(()=>{allowGetMarketCall = true},marketRefresh) //Sorting at this point may be redundant. trades = trades.sort((a, b) => { @@ -225,18 +225,18 @@ module.exports = function bittrex(conf) { } ) cb(null, trades) - + } }) } - + }, getBalance: function (opts, cb) { var func_args = [].slice.call(arguments) bittrex_authed.getbalances(function( data,err ) { - + let res = handleErrors('getBalance', err, data, func_args, cb) var balance = { @@ -269,7 +269,7 @@ module.exports = function bittrex(conf) { } cb(null, balance) } - + }) }, @@ -327,14 +327,11 @@ module.exports = function bittrex(conf) { let args = { uuid: opts.order_id } - bittrex_authed.cancel(args, function( data,err ) { - - let res = handleErrors('cancelOrder', err, data, func_args, cb) - - - if (res) { - cb(null) + bittrex_authed.cancel(args, function (data, err) { + if (err) { + return retry('cancelOrder', func_args, err) } + cb() }) }, @@ -360,7 +357,7 @@ module.exports = function bittrex(conf) { data.message = err.message data.success = err.success data.result = err.result - } + } console.log('API Error') console.log(JSON.stringify(err)) if (err.message && err.message.match(recoverableErrors)) { @@ -378,11 +375,11 @@ module.exports = function bittrex(conf) { return cb(null, returnResult) } } - + if (typeof data !== 'object') { return cb(null, {}) } - + if(!data.success) { if (data.message && data.message.match(recoverableErrors)) { return retry('trade', func_args, data.message) @@ -390,7 +387,7 @@ module.exports = function bittrex(conf) { console.log(data.message) return cb(null, []) } - + var order = { id: data && data.result ? data.result.uuid : null, @@ -414,7 +411,7 @@ module.exports = function bittrex(conf) { if (opts.order_type === 'taker') { bittrex_authed.buymarket(params, fn) } - } + } if (type === 'sell') { if (opts.order_type === 'maker') { bittrex_authed.selllimit(params, fn) @@ -433,31 +430,26 @@ module.exports = function bittrex(conf) { exchange.trade('sell', opts, cb) }, - getOrder: function (opts, cb) { + getOrder: function(opts, cb) { var func_args = [].slice.call(arguments) var order = orders['~' + opts.order_id] if (!order) return cb(new Error('order not found in cache')) var params = { uuid: opts.order_id } - bittrex_authed.getorder(params, function (data,err) { + bittrex_authed.getorder(params, function(data, err) { let res = handleErrors('getOrder', err, data, func_args, cb) - - if (res) - { + if (res) { var orderData = data.result - if (!orderData) { return cb('Order not found') } - - if (orderData.IsOpen === false) { + if (orderData.QuantityRemaining == 0) { order.status = 'done' order.done_at = new Date().getTime() order.filled_size = parseFloat(orderData.Quantity) - parseFloat(orderData.QuantityRemaining) return cb(null, order) } - cb(null, order) } })