|
1 | 1 | import { Arguments, Argv } from 'yargs';
|
2 | 2 | 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'; |
4 | 4 |
|
5 | 5 | export const orderBuilder = (argv: Argv, command: string) => argv
|
6 | 6 | .option('quantity', {
|
@@ -49,10 +49,59 @@ export const orderHandler = (argv: Arguments, isSell = false) => {
|
49 | 49 |
|
50 | 50 | if (argv.stream) {
|
51 | 51 | const subscription = loadXudClient(argv).placeOrder(request);
|
| 52 | + let noMatches = true; |
52 | 53 | 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 | + } |
54 | 73 | });
|
55 | 74 | } else {
|
56 |
| - loadXudClient(argv).placeOrderSync(request, callback(argv)); |
| 75 | + loadXudClient(argv).placeOrderSync(request, callback(argv, formatPlaceOrderOutput)); |
57 | 76 | }
|
58 | 77 | };
|
| 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