|
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', {
|
@@ -50,9 +50,43 @@ export const orderHandler = (argv: Arguments, isSell = false) => {
|
50 | 50 | if (argv.stream) {
|
51 | 51 | const subscription = loadXudClient(argv).placeOrder(request);
|
52 | 52 | 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 | + } |
54 | 68 | });
|
55 | 69 | } else {
|
56 |
| - loadXudClient(argv).placeOrderSync(request, callback(argv)); |
| 70 | + loadXudClient(argv).placeOrderSync(request, callback(argv, formatPlaceOrderOutput)); |
57 | 71 | }
|
58 | 72 | };
|
| 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