Skip to content

Commit c037f66

Browse files
authored
feat(cli): add price max precision checks for orders creation (#1385)
Date: Tue Feb 4 19:49:35 2020 +0300 Changes to be committed: modified: lib/cli/placeorder.ts modified: lib/cli/utils.ts modified: lib/service/Service.ts modified: npm-shrinkwrap.json modified: package.json
1 parent a4e1a26 commit c037f66

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Diff for: lib/cli/placeorder.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Arguments, Argv } from 'yargs';
22
import { Order, OrderSide, PlaceOrderEvent, PlaceOrderRequest, PlaceOrderResponse, SwapFailure, SwapSuccess } from '../proto/xudrpc_pb';
33
import { callback, loadXudClient } from './command';
44
import { coinsToSats, satsToCoinsStr } from './utils';
5+
import { checkDecimalPlaces } from '../utils/utils';
56

67
export const placeOrderBuilder = (argv: Argv, side: OrderSide) => {
78
const command = side === OrderSide.BUY ? 'buy' : 'sell';
89
argv.option('quantity', {
910
type: 'number',
11+
describe: 'the quantity to trade',
1012
})
1113
.option('pair_id', {
1214
type: 'string',
@@ -62,6 +64,12 @@ export const placeOrderHandler = (argv: Arguments<any>, side: OrderSide) => {
6264
return;
6365
}
6466

67+
if (checkDecimalPlaces(numericPrice)) {
68+
process.exitCode = 1;
69+
console.error('price cannot have more than 12 decimal places');
70+
return;
71+
}
72+
6573
if (argv.replace_order_id) {
6674
request.setReplaceOrderId(argv.replace_order_id);
6775
}

Diff for: lib/service/Service.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TradingLimits } from '../swaps/SwapClient';
1111
import Swaps from '../swaps/Swaps';
1212
import { ResolveRequest, SwapFailure, SwapSuccess } from '../swaps/types';
1313
import { parseUri, toUri, UriParts } from '../utils/uriUtils';
14-
import { sortOrders, toEip55Address } from '../utils/utils';
14+
import { checkDecimalPlaces, sortOrders, toEip55Address } from '../utils/utils';
1515
import commitHash from '../Version';
1616
import errors from './errors';
1717

@@ -58,6 +58,9 @@ const argChecks = {
5858
POSITIVE_AMOUNT: ({ amount }: { amount: number }) => { if (amount <= 0) throw errors.INVALID_ARGUMENT('amount must be greater than 0'); },
5959
POSITIVE_QUANTITY: ({ quantity }: { quantity: number }) => { if (quantity <= 0) throw errors.INVALID_ARGUMENT('quantity must be greater than 0'); },
6060
PRICE_NON_NEGATIVE: ({ price }: { price: number }) => { if (price < 0) throw errors.INVALID_ARGUMENT('price cannot be negative'); },
61+
PRICE_MAX_DECIMAL_PLACES: ({ price }: { price: number }) => {
62+
if (checkDecimalPlaces(price)) throw errors.INVALID_ARGUMENT('price cannot have more than 12 decimal places');
63+
},
6164
VALID_CURRENCY: ({ currency }: { currency: string }) => {
6265
if (currency.length < 2 || currency.length > 5 || !currency.match(/^[A-Z0-9]+$/)) {
6366
throw errors.INVALID_ARGUMENT('currency must consist of 2 to 5 upper case English letters or numbers');
@@ -447,6 +450,7 @@ class Service {
447450
const { pairId, price, quantity, orderId, side, replaceOrderId, immediateOrCancel } = args;
448451
argChecks.PRICE_NON_NEGATIVE(args);
449452
argChecks.POSITIVE_QUANTITY(args);
453+
argChecks.PRICE_MAX_DECIMAL_PLACES(args);
450454
argChecks.HAS_PAIR_ID(args);
451455

452456
if (replaceOrderId) {

Diff for: lib/utils/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { promisify } from 'util';
99
import { Order, Pair } from '../orderbook/types';
1010
import p2pErrors from '../p2p/errors';
1111

12+
const MAX_DECIMAL_PLACES = 12;
13+
1214
/**
1315
* Gets the external IP of the node.
1416
*/
@@ -105,6 +107,12 @@ export const groupBy = (arr: object[], keyGetter: (item: any) => string | number
105107
return ret;
106108
};
107109

110+
/** Returns true if number has more than MAX_DECIMAL_PLACES, false otherwise. */
111+
export const checkDecimalPlaces = (digits: number) => {
112+
const fixed = Number(digits.toFixed(MAX_DECIMAL_PLACES));
113+
return fixed < digits;
114+
};
115+
108116
/**
109117
* Get current time in unix time (milliseconds).
110118
*/

0 commit comments

Comments
 (0)