Skip to content

Commit

Permalink
update btc-e dep, implement getOrder, fix askmike#526, fix askmike#776
Browse files Browse the repository at this point in the history
…& fix askmike#780
  • Loading branch information
askmike committed Jul 3, 2017
1 parent 9c50e13 commit fe647cf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
88 changes: 71 additions & 17 deletions exchanges/btce.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var log = require('../core/log')
var Trader = function(config) {
this.key = config.key;
this.secret = config.secret;
this.asset = config.asset;
this.currency = config.currency;
this.pair = [config.asset, config.currency].join('_').toLowerCase();
this.name = 'BTC-E';

Expand Down Expand Up @@ -35,10 +37,10 @@ Trader.prototype.buy = function(amount, price, callback) {
amount = this.round(amount);

var set = function(err, data) {
if(err)
if(err || !data || !data.return)
return log.error('unable to buy:', err);

callback(null, data.order_id);
callback(null, data.return.order_id);
}.bind(this);

// workaround for nonce error
Expand All @@ -51,10 +53,10 @@ Trader.prototype.sell = function(amount, price, callback) {
amount = this.round(amount);

var set = function(err, data) {
if(err)
if(err || !data || !data.return)
return log.error('unable to sell:\n\n', err);

callback(null, data.order_id);
callback(null, data.return.order_id);
};

// workaround for nonce error
Expand All @@ -80,19 +82,40 @@ Trader.prototype.retry = function(method, args) {
}

Trader.prototype.getPortfolio = function(callback) {
var args = _.toArray(arguments);
var calculate = function(err, data) {

if(err) {
if(err.message === 'invalid api key')
util.die('Your ' + this.name + ' API keys are invalid');

return this.retry(this.btce.getInfo, calculate);
return this.retry(this.getPortfolio, args);
}

var portfolio = [];
_.each(data.funds, function(amount, asset) {
portfolio.push({name: asset.toUpperCase(), amount: amount});
});
if(_.isEmpty(data))
err = 'no data';

if (err || !data.return || !data.return.funds)
return this.retry(this.getPortfolio, args);

var assetAmount = parseFloat( data.return.funds[this.asset.toLowerCase()] );
var currencyAmount = parseFloat( data.return.funds[this.currency.toLowerCase()] );

if(!_.isNumber(assetAmount) || _.isNaN(assetAmount)) {
log.error(`BTC-e did not return portfolio for ${this.asset}, assuming 0.`);
assetAmount = 0;
}

if(!_.isNumber(currencyAmount) || _.isNaN(currencyAmount)) {
log.error(`BTC-e did not return portfolio for ${this.currency}, assuming 0.`);
currencyAmount = 0;
}

var portfolio = [
{ name: this.asset, amount: assetAmount },
{ name: this.currency, amount: currencyAmount }
];

callback(err, portfolio);
}.bind(this);

Expand All @@ -107,10 +130,11 @@ Trader.prototype.getTicker = function(callback) {
if(err)
return this.retry(this.btce.ticker, [this.pair, set]);

var ticker = _.extend(data.ticker, {
var ticker = {
ask: data.ticker.buy,
bid: data.ticker.sell
});
};

callback(err, ticker);
}.bind(this);

Expand All @@ -135,15 +159,45 @@ Trader.prototype.checkOrder = function(order, callback) {
callback(false, true);
else
callback(err, !result[order]);
};
}.bind(this);

this.btce.orderList({}, _.bind(check, this));
this.btce.orderList({}, check);
}

Trader.prototype.cancelOrder = function(order) {
// TODO: properly test
var devNull = function() {}
this.btce.orderList(order, devNull);
Trader.prototype.getOrder = function(orderId, callback) {
console.log('getOrder', orderId);
var args = _.toArray(arguments);
var check = function(err, result) {
if(err) {
log.error('error on getOrder', err);
return this.retry(this.getOrder, args);
}

var order = null;

_.each(result.return, o => {
if(o.order_id === +orderId)
order = o;
});

if(!order)
return log.error('BTC-e did not provide the order');

var price = parseFloat(order.rate);
var amount = parseFloat(order.amount);
var date = moment.unix(order.timestamp);

callback(undefined, {price, amount, date});
}.bind(this);

this.btce.tradeHistory({pair: this.pair}, check);
}


Trader.prototype.cancelOrder = function(order, callback) {
this.btce.orderList(order, (err, result) => {
callback();
});
}

Trader.prototype.getTrades = function(since, callback, descending) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"bitstamp": "1.0.x",
"bitx": "^1.5.0",
"btc-china-fork": "0.0.6",
"btc-e": "0.0.x",
"btc-e": "^1.0.5",
"btc-markets": "0.0.6",
"cexio": "0.0.x",
"co-fs": "^1.2.0",
Expand Down

0 comments on commit fe647cf

Please sign in to comment.