Skip to content

Commit

Permalink
improve bytetrade & remove integerDiv / integerMod / integerPow
Browse files Browse the repository at this point in the history
  • Loading branch information
frosty00 committed Jul 5, 2021
1 parent 977258c commit c4666da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 63 deletions.
14 changes: 0 additions & 14 deletions js/base/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,20 +1487,6 @@ module.exports = class Exchange {
}
}

// the following functions take and return numbers represented as strings
// this is useful for arbitrary precision maths that floats lack
integerDivide (a, b) {
return new BN (a).div (new BN (b))
}

integerModulo (a, b) {
return new BN (a).mod (new BN (b))
}

integerPow (a, b) {
return new BN (a).pow (new BN (b))
}

reduceFeesByCurrency (fees) {
const reduced = {};
for (let i = 0; i < fees.length; i++) {
Expand Down
55 changes: 32 additions & 23 deletions js/bytetrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const Exchange = require ('./base/Exchange');
const { ExchangeError, ArgumentsRequired, BadRequest, AuthenticationError, DDoSProtection, BadResponse } = require ('./base/errors');
const { TRUNCATE, NO_PADDING, DECIMAL_PLACES } = require ('./base/functions/number');
const Precise = require ('./base/Precise');

// ---------------------------------------------------------------------------

Expand Down Expand Up @@ -116,6 +117,9 @@ module.exports = class bytetrade extends Exchange {
'transaction already in network': BadRequest, // same transaction submited
'invalid argument': BadRequest,
},
'options': {
'orderExpiration': 31536000000, // one year
},
});
}

Expand Down Expand Up @@ -628,12 +632,15 @@ module.exports = class bytetrade extends Exchange {
const baseCurrency = this.currency (market['base']);
const amountTruncated = this.amountToPrecision (symbol, amount);
const amountChain = this.toWei (amountTruncated, baseCurrency['precision']);
const amountChainString = this.numberToString (amountChain);
const quoteId = market['quoteId'];
const quoteCurrency = this.currency (market['quote']);
const priceRounded = this.priceToPrecision (symbol, price);
const priceChain = this.toWei (priceRounded, quoteCurrency['precision']);
const priceChainString = this.numberToString (priceChain);
const now = this.milliseconds ();
const expiration = this.milliseconds ();
const expiryDelta = this.safeInteger (this.options, 'orderExpiration', 31536000000);
const expiration = this.milliseconds () + expiryDelta;
let datetime = this.iso8601 (now);
datetime = datetime.split ('.')[0];
let expirationDatetime = this.iso8601 (expiration);
Expand All @@ -644,7 +651,7 @@ module.exports = class bytetrade extends Exchange {
const totalFeeRate = this.safeString (params, 'totalFeeRate', 8);
const chainFeeRate = this.safeString (params, 'chainFeeRate', 1);
const fee = this.safeString (params, 'fee', defaultFee);
const eightBytes = this.integerPow ('2', '64');
const eightBytes = Precise.stringPow ('2', '64');
const allByteStringArray = [
this.numberToBE (1, 32),
this.numberToLE (Math.floor (now / 1000), 4),
Expand All @@ -660,10 +667,10 @@ module.exports = class bytetrade extends Exchange {
this.numberToLE (typeNum, 1),
this.numberToLE (normalSymbol.length, 1),
this.stringToBinary (this.encode (normalSymbol)),
this.numberToLE (this.integerDivide (amountChain, eightBytes), 8),
this.numberToLE (this.integerModulo (amountChain, eightBytes), 8),
this.numberToLE (this.integerDivide (priceChain, eightBytes), 8),
this.numberToLE (this.integerModulo (priceChain, eightBytes), 8),
this.numberToLE (Precise.stringDiv (amountChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (amountChainString, eightBytes), 8),
this.numberToLE (Precise.stringDiv (priceChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (priceChainString, eightBytes), 8),
this.numberToLE (0, 2),
this.numberToLE (Math.floor (now / 1000), 4),
this.numberToLE (Math.floor (expiration / 1000), 4),
Expand Down Expand Up @@ -693,10 +700,10 @@ module.exports = class bytetrade extends Exchange {
this.numberToLE (typeNum, 1),
this.numberToLE (normalSymbol.length, 1),
this.stringToBinary (this.encode (normalSymbol)),
this.numberToLE (this.integerDivide (amountChain, eightBytes), 8),
this.numberToLE (this.integerModulo (amountChain, eightBytes), 8),
this.numberToLE (this.integerDivide (priceChain, eightBytes), 8),
this.numberToLE (this.integerModulo (priceChain, eightBytes), 8),
this.numberToLE (Precise.stringDiv (amountChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (amountChainString, eightBytes), 8),
this.numberToLE (Precise.stringDiv (priceChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (priceChainString, eightBytes), 8),
this.numberToLE (0, 2),
this.numberToLE (Math.floor (now / 1000), 4),
this.numberToLE (Math.floor (expiration / 1000), 4),
Expand Down Expand Up @@ -764,7 +771,7 @@ module.exports = class bytetrade extends Exchange {
};
const response = await this.publicPostTransactionCreateorder (request);
const timestamp = this.milliseconds ();
const statusCode = this.safe_string (response, 'code');
const statusCode = this.safeString (response, 'code');
const status = (statusCode === '0') ? 'open' : 'failed';
return {
'info': response,
Expand Down Expand Up @@ -951,7 +958,7 @@ module.exports = class bytetrade extends Exchange {
};
const response = await this.publicPostTransactionCancelorder (request);
const timestamp = this.milliseconds ();
const statusCode = this.safe_string (response, 'code');
const statusCode = this.safeString (response, 'code');
const status = (statusCode === '0') ? 'canceled' : 'failed';
return {
'info': response,
Expand Down Expand Up @@ -984,6 +991,7 @@ module.exports = class bytetrade extends Exchange {
const currency = this.currency (code);
const amountTruncate = this.decimalToPrecision (amount, TRUNCATE, currency['info']['basePrecision'] - currency['info']['transferPrecision'], DECIMAL_PLACES, NO_PADDING);
const amountChain = this.toWei (amountTruncate, currency['precision']);
const amountChainString = this.numberToString (amountChain);
const assetType = parseInt (currency['id']);
const now = this.milliseconds ();
const expiration = now;
Expand All @@ -995,7 +1003,7 @@ module.exports = class bytetrade extends Exchange {
const defaultDappId = 'Sagittarius';
const message = this.safeString (params, 'message', '');
const dappId = this.safeString (params, 'dappId', defaultDappId);
const eightBytes = this.integerPow ('2', '64');
const eightBytes = Precise.stringPow ('2', '64');
const byteStringArray = [
this.numberToBE (1, 32),
this.numberToLE (Math.floor (now / 1000), 4),
Expand All @@ -1010,8 +1018,8 @@ module.exports = class bytetrade extends Exchange {
this.numberToLE (toAccount.length, 1),
this.stringToBinary (this.encode (toAccount)),
this.numberToLE (assetType, 4),
this.numberToLE (this.integerDivide (amountChain, eightBytes), 8),
this.numberToLE (this.integerModulo (amountChain, eightBytes), 8),
this.numberToLE (Precise.stringDiv (amountChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (amountChainString, eightBytes), 8),
this.numberToLE (1, 1),
this.numberToLE (message.length, 1),
this.stringToBinary (this.encode (message)),
Expand Down Expand Up @@ -1054,7 +1062,7 @@ module.exports = class bytetrade extends Exchange {
};
const response = await this.publicPostTransactionTransfer (request);
const timestamp = this.milliseconds ();
const statusCode = this.safe_string (response, 'code');
const statusCode = this.safeString (response, 'code');
let status = '';
if (statusCode === '0') {
status = 'submit success';
Expand Down Expand Up @@ -1269,7 +1277,8 @@ module.exports = class bytetrade extends Exchange {
const coinId = currency['id'];
const amountTruncate = this.decimalToPrecision (amount, TRUNCATE, currency['info']['basePrecision'] - currency['info']['transferPrecision'], DECIMAL_PLACES, NO_PADDING);
const amountChain = this.toWei (amountTruncate, currency['info']['externalPrecision']);
const eightBytes = this.integerPow ('2', '64');
const amountChainString = this.numberToString (amountChain);
const eightBytes = Precise.stringPow ('2', '64');
let assetFee = 0;
let byteStringArray = [];
if (operationId === 26) {
Expand All @@ -1288,11 +1297,11 @@ module.exports = class bytetrade extends Exchange {
this.numberToLE (address.length, 1),
this.stringToBinary (this.encode (address)),
this.numberToLE (parseInt (coinId), 4),
this.numberToLE (this.integerDivide (amountChain, eightBytes), 8),
this.numberToLE (this.integerModulo (amountChain, eightBytes), 8),
this.numberToLE (Precise.stringDiv (amountChainString, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (amountChainString, eightBytes), 8),
this.numberToLE (1, 1),
this.numberToLE (this.integerDivide (assetFee, eightBytes), 8),
this.numberToLE (this.integerModulo (assetFee, eightBytes), 8),
this.numberToLE (Precise.stringDiv (assetFee, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (assetFee, eightBytes), 8),
this.numberToLE (0, 1),
this.numberToLE (1, 1),
this.numberToLE (dappId.length, 1),
Expand Down Expand Up @@ -1321,8 +1330,8 @@ module.exports = class bytetrade extends Exchange {
this.numberToLE (middleAddress.length, 1),
this.stringToBinary (this.encode (middleAddress)),
this.numberToLE (parseInt (coinId), 4),
this.numberToLE (this.integerDivide (amountChain, eightBytes), 8),
this.numberToLE (this.integerModulo (amountChain, eightBytes), 8),
this.numberToLE (Precise.stringDiv (amountChain, eightBytes, 0), 8),
this.numberToLE (Precise.stringMod (amountChain, eightBytes), 8),
this.numberToLE (0, 1),
this.numberToLE (1, 1),
this.numberToLE (dappId.length, 1),
Expand Down
2 changes: 1 addition & 1 deletion js/probit.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ module.exports = class probit extends Exchange {
timestamp = parseInt (timestamp / 1000);
const firstSunday = 259200; // 1970-01-04T00:00:00.000Z
const difference = timestamp - firstSunday;
const numWeeks = this.integerDivide (difference, duration);
const numWeeks = Math.floor (difference / duration);
let previousSunday = this.sum (firstSunday, numWeeks * duration);
if (after) {
previousSunday = this.sum (previousSunday, duration);
Expand Down
12 changes: 0 additions & 12 deletions php/Exchange.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions python/ccxt/base/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,19 +2136,6 @@ def base16_to_binary(s):
def binary_to_base16(s):
return Exchange.decode(base64.b16encode(s)).lower()

# python supports arbitrarily big integers
@staticmethod
def integer_divide(a, b):
return int(a) // int(b)

@staticmethod
def integer_pow(a, b):
return int(a) ** int(b)

@staticmethod
def integer_modulo(a, b):
return int(a) % int(b)

def sleep(self, milliseconds):
return time.sleep(milliseconds / 1000)

Expand Down

0 comments on commit c4666da

Please sign in to comment.