Skip to content

Commit

Permalink
[GB] make scientificToDecimal a global helper
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Sep 19, 2018
1 parent 767f225 commit a45a698
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
30 changes: 29 additions & 1 deletion exchange/exchangeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,36 @@ const isValidOrder = ({api, market, amount, price}) => {
}
}


// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
const scientificToDecimal = num => {
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
const zero = '0';
const parts = String(num).toLowerCase().split('e'); // split into coeff and exponent
const e = parts.pop(); // store the exponential part
const l = Math.abs(e); // get the number of zeros
const sign = e/l;
const coeff_array = parts[0].split('.');
if(sign === -1) {
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
} else {
const dec = coeff_array[1];
if(dec) {
l = l - dec.length;
}
num = coeff_array.join('') + new Array(l+1).join(zero);
}
} else {
// make sure we always cast to string
num = num + '';
}

return num;
}

module.exports = {
retry: retryInstance,
bindAll,
isValidOrder
isValidOrder,
scientificToDecimal
}
34 changes: 5 additions & 29 deletions exchange/wrappers/binance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const _ = require('lodash');

const Errors = require('../exchangeErrors');
const marketData = require('./binance-markets.json');
const retry = require('../exchangeUtils').retry;
const exchangeUtils = require('../exchangeUtils');
const retry = exchangeUtils.retry;
const scientificToDecimal = exchangeUtils.scientificToDecimal;

const Binance = require('binance');

Expand Down Expand Up @@ -264,37 +266,11 @@ Trader.prototype.round = function(amount, tickSize) {
amount /= precision;

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
amount = this.scientificToDecimal(amount);
amount = scientificToDecimal(amount);

return amount;
};

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
Trader.prototype.scientificToDecimal = function(num) {
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
const zero = '0';
const parts = String(num).toLowerCase().split('e'); // split into coeff and exponent
const e = parts.pop(); // store the exponential part
const l = Math.abs(e); // get the number of zeros
const sign = e/l;
const coeff_array = parts[0].split('.');
if(sign === -1) {
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
} else {
const dec = coeff_array[1];
if(dec) {
l = l - dec.length;
}
num = coeff_array.join('') + new Array(l+1).join(zero);
}
} else {
// make sure we always cast to string
num = num + '';
}

return num;
}

Trader.prototype.roundAmount = function(amount) {
return this.round(amount, this.market.minimalOrder.amount);
}
Expand Down Expand Up @@ -405,7 +381,7 @@ Trader.prototype.getOrder = function(order, callback) {
const reqData = {
symbol: this.pair,
// if this order was not part of the last 500 trades we won't find it..
limit: 500,
limit: 1000,
};

const handler = cb => this.binance.myTrades(reqData, this.handleResponse('getOrder', cb));
Expand Down

0 comments on commit a45a698

Please sign in to comment.