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

Fix Bittrex error #1563

Merged
merged 1 commit into from
Apr 25, 2018
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
90 changes: 41 additions & 49 deletions extensions/exchanges/bittrex/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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, [])
Expand Down Expand Up @@ -103,15 +103,15 @@ 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)
{
cb(null, [])
return null
}

if (fistRun)
if (firstRun)
{
bittrex_public.getticks(args, function( data, err) {
let res = handleErrors('getTrades', err, data, func_args, cb)
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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.
Expand All @@ -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()) {
Expand All @@ -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) => {
Expand All @@ -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 = {
Expand Down Expand Up @@ -269,7 +269,7 @@ module.exports = function bittrex(conf) {
}
cb(null, balance)
}

})
},

Expand Down Expand Up @@ -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()
})
},

Expand All @@ -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)) {
Expand All @@ -378,19 +375,19 @@ 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)
}
console.log(data.message)
return cb(null, [])
}


var order = {
id: data && data.result ? data.result.uuid : null,
Expand All @@ -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)
Expand All @@ -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)
}
})
Expand Down