Skip to content

Commit 773fea9

Browse files
committed
feat(cli): format buy & sell output
This attempts to make the cli output for the `buy` and `sell` commands more human-readable and succint. Closes #620.
1 parent 582fa7a commit 773fea9

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

lib/cli/utils.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Arguments, Argv } from 'yargs';
22
import { callback, loadXudClient } from './command';
3-
import { PlaceOrderRequest, PlaceOrderEvent, OrderSide } from '../proto/xudrpc_pb';
3+
import { PlaceOrderRequest, PlaceOrderEvent, OrderSide, PlaceOrderResponse, Order, SwapResult } from '../proto/xudrpc_pb';
44

55
export const orderBuilder = (argv: Argv, command: string) => argv
66
.option('quantity', {
@@ -50,9 +50,43 @@ export const orderHandler = (argv: Arguments, isSell = false) => {
5050
if (argv.stream) {
5151
const subscription = loadXudClient(argv).placeOrder(request);
5252
subscription.on('data', (response: PlaceOrderEvent) => {
53-
console.log(JSON.stringify(response.toObject(), undefined, 2));
53+
if (argv.json) {
54+
console.log(JSON.stringify(response.toObject(), undefined, 2));
55+
} else {
56+
console.log('stweeem');
57+
const internalMatch = response.getInternalMatch();
58+
const swapResult = response.getSwapResult();
59+
const remainingOrder = response.getRemainingOrder();
60+
if (internalMatch) {
61+
formatInternalMatch(internalMatch.toObject());
62+
} else if (swapResult) {
63+
formatSwapResult(swapResult.toObject());
64+
} else if (remainingOrder) {
65+
formatRemainingOrder(remainingOrder.toObject());
66+
}
67+
}
5468
});
5569
} else {
56-
loadXudClient(argv).placeOrderSync(request, callback(argv));
70+
loadXudClient(argv).placeOrderSync(request, callback(argv, formatPlaceOrderOutput));
5771
}
5872
};
73+
74+
const formatPlaceOrderOutput = (response: PlaceOrderResponse.AsObject) => {
75+
response.internalMatchesList.forEach(formatInternalMatch);
76+
response.swapResultsList.forEach(formatSwapResult);
77+
if (response.remainingOrder) {
78+
formatRemainingOrder(response.remainingOrder);
79+
}
80+
};
81+
82+
const formatInternalMatch = (order: Order.AsObject) => {
83+
console.log(`matched ${order.quantity} quantity @ ${order.price} with own order ${order.id}`);
84+
};
85+
86+
const formatSwapResult = (swapResult: SwapResult.AsObject) => {
87+
console.log(`swapped ${swapResult.quantity} with peer order ${swapResult.orderId}`);
88+
};
89+
90+
const formatRemainingOrder = (remainingOrder: Order.AsObject) => {
91+
console.log(`new order ${remainingOrder.id} entered the order book with ${remainingOrder.quantity} quantity`);
92+
};

0 commit comments

Comments
 (0)