Skip to content

Commit d958cd5

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 92987be commit d958cd5

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

lib/cli/utils.ts

+52-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', {
@@ -49,10 +49,59 @@ export const orderHandler = (argv: Arguments, isSell = false) => {
4949

5050
if (argv.stream) {
5151
const subscription = loadXudClient(argv).placeOrder(request);
52+
let noMatches = true;
5253
subscription.on('data', (response: PlaceOrderEvent) => {
53-
console.log(JSON.stringify(response.toObject(), undefined, 2));
54+
if (argv.json) {
55+
console.log(JSON.stringify(response.toObject(), undefined, 2));
56+
} else {
57+
const internalMatch = response.getInternalMatch();
58+
const swapResult = response.getSwapResult();
59+
const remainingOrder = response.getRemainingOrder();
60+
if (internalMatch) {
61+
noMatches = false;
62+
formatInternalMatch(internalMatch.toObject());
63+
} else if (swapResult) {
64+
noMatches = false;
65+
formatSwapResult(swapResult.toObject());
66+
} else if (remainingOrder) {
67+
if (noMatches) {
68+
console.log('no matches found');
69+
}
70+
formatRemainingOrder(remainingOrder.toObject());
71+
}
72+
}
5473
});
5574
} else {
56-
loadXudClient(argv).placeOrderSync(request, callback(argv));
75+
loadXudClient(argv).placeOrderSync(request, callback(argv, formatPlaceOrderOutput));
5776
}
5877
};
78+
79+
const formatPlaceOrderOutput = (response: PlaceOrderResponse.AsObject) => {
80+
const { internalMatchesList, swapResultsList, remainingOrder } = response;
81+
if (internalMatchesList.length === 0 && swapResultsList.length === 0) {
82+
console.log('no matches found');
83+
} else {
84+
internalMatchesList.forEach(formatInternalMatch);
85+
swapResultsList.forEach(formatSwapResult);
86+
}
87+
if (remainingOrder) {
88+
formatRemainingOrder(remainingOrder);
89+
}
90+
};
91+
92+
const formatInternalMatch = (order: Order.AsObject) => {
93+
const baseCurrency = getBaseCurrency(order.pairId);
94+
console.log(`matched ${order.quantity} ${baseCurrency} @ ${order.price} with own order ${order.id}`);
95+
};
96+
97+
const formatSwapResult = (swapResult: SwapResult.AsObject) => {
98+
const baseCurrency = getBaseCurrency(swapResult.pairId);
99+
console.log(`swapped ${swapResult.quantity} ${baseCurrency} with peer order ${swapResult.orderId}`);
100+
};
101+
102+
const formatRemainingOrder = (order: Order.AsObject) => {
103+
const baseCurrency = getBaseCurrency(order.pairId);
104+
console.log(`remaining ${order.quantity} ${baseCurrency} entered the order book as ${order.id}`);
105+
};
106+
107+
const getBaseCurrency = (pairId: string) => pairId.substring(0, pairId.indexOf('/'));

0 commit comments

Comments
 (0)