Skip to content

Commit

Permalink
fix: changes removeorder output to a more meaningful message (#1526)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsercano committed Nov 10, 2020
1 parent 1105151 commit 733a3cd
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 266 deletions.
3 changes: 3 additions & 0 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions lib/cli/commands/removeorder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Arguments, Argv } from 'yargs';
import { RemoveOrderRequest } from '../../proto/xudrpc_pb';
import { RemoveOrderRequest, RemoveOrderResponse } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';
import { coinsToSats } from '../utils';
import { coinsToSats, satsToCoinsStr } from '../utils';

export const command = 'removeorder <order_id> [quantity]';

Expand All @@ -18,11 +18,23 @@ export const builder = (argv: Argv) => argv
.example('$0 removeorder 79d2cd30-8a26-11ea-90cf-439fb244cf44', 'remove an order by id')
.example('$0 removeorder 79d2cd30-8a26-11ea-90cf-439fb244cf44 0.1', 'remove 0.1 quantity from an order by id');

const displayOutput = (orderId: string, removeOrderResponse: RemoveOrderResponse.AsObject) => {
const removedCurrency = removeOrderResponse.pairId.split('/')[0];
if (removeOrderResponse.quantityOnHold === 0 && removeOrderResponse.remainingQuantity === 0) {
console.log(`Order ${orderId} successfully removed.`);
} else {
console.log(`
Order ${orderId} partially removed, remaining quantity: \
${satsToCoinsStr(removeOrderResponse.remainingQuantity)} ${removedCurrency}, \
on hold: ${satsToCoinsStr(removeOrderResponse.quantityOnHold)} ${removedCurrency}`);
}
};

export const handler = async (argv: Arguments<any>) => {
const request = new RemoveOrderRequest();
request.setOrderId(argv.order_id);
if (argv.quantity) {
request.setQuantity(coinsToSats(argv.quantity));
}
(await loadXudClient(argv)).removeOrder(request, callback(argv));
(await loadXudClient(argv)).removeOrder(request, callback(argv, displayOutput.bind(undefined, argv.order_id)));
};
7 changes: 5 additions & 2 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,12 @@ class GrpcService {
return;
}
try {
const quantityOnHold = this.service.removeOrder(call.request.toObject());
const { removedQuantity, remainingQuantity, onHoldQuantity, pairId } = this.service.removeOrder(call.request.toObject());
const response = new xudrpc.RemoveOrderResponse();
response.setQuantityOnHold(quantityOnHold);
response.setQuantityOnHold(onHoldQuantity);
response.setRemainingQuantity(remainingQuantity);
response.setRemovedQuantity(removedQuantity);
response.setPairId(pairId);
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
Expand Down
19 changes: 16 additions & 3 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,8 @@ class OrderBook extends EventEmitter {
const onHoldOrderLocalIds = [];

for (const localId of this.localIdMap.keys()) {
const onHoldIndicator = this.removeOwnOrderByLocalId(localId, true);
if (onHoldIndicator === 0) {
const { onHoldQuantity } = this.removeOwnOrderByLocalId(localId, true);
if (onHoldQuantity === 0) {
removedOrderLocalIds.push(localId);
} else {
onHoldOrderLocalIds.push(localId);
Expand All @@ -863,6 +863,9 @@ class OrderBook extends EventEmitter {
const order = this.getOwnOrderByLocalId(localId);

let remainingQuantityToRemove = quantityToRemove || order.quantity;
const orderQuantity = order.quantity;
let onHoldQuantity = order.hold;
let removedQuantity = 0;

if (remainingQuantityToRemove > order.quantity) {
// quantity to be removed can't be higher than order's quantity.
Expand All @@ -876,6 +879,7 @@ class OrderBook extends EventEmitter {
pairId: order.pairId,
quantityToRemove: remainingQuantityToRemove,
});
removedQuantity += remainingQuantityToRemove;
remainingQuantityToRemove = 0;
} else {
// we can't immediately remove the entire quantity because of a hold on the order.
Expand All @@ -890,6 +894,7 @@ class OrderBook extends EventEmitter {
pairId: order.pairId,
quantityToRemove: removableQuantity,
});
removedQuantity += removableQuantity;
remainingQuantityToRemove -= removableQuantity;
}

Expand All @@ -915,6 +920,8 @@ class OrderBook extends EventEmitter {

const cleanup = (quantity: number) => {
remainingQuantityToRemove -= quantity;
removedQuantity += quantity;
onHoldQuantity -= quantity;
this.logger.debug(`removed hold of ${quantity} on local order ${localId}, ${remainingQuantityToRemove} remaining`);
if (remainingQuantityToRemove === 0) {
// we can stop listening for swaps once all holds are cleared
Expand All @@ -927,7 +934,13 @@ class OrderBook extends EventEmitter {
this.swaps.on('swap.paid', paidHandler);
}

return remainingQuantityToRemove;
const remainingFromOrder = orderQuantity - removedQuantity;
return {
removedQuantity,
onHoldQuantity,
pairId: order.pairId,
remainingQuantity: remainingFromOrder > onHoldQuantity ? remainingFromOrder : 0,
};
}

private addOrderHold = (orderId: string, pairId: string, holdAmount?: number) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/proto/annotations_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/proto/xudp2p_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 82 additions & 1 deletion lib/proto/xudrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,12 @@ message RemoveOrderResponse {
// Any portion of the order that was on hold due to ongoing swaps at the time of the request
// and could not be removed until after the swaps finish.
uint64 quantity_on_hold = 1 [json_name = "hold"];
// Remaining portion of the order if it was a partial removal.
uint64 remaining_quantity = 2 [json_name = "remaining_quantity"];
// Successfully removed portion of the order.
uint64 removed_quantity = 3 [json_name = "removed_quantity"];
// Removed order's pairId. (e.g. ETH/BTC)
string pair_id = 4 [json_name = "pair_id"];
}

message RemoveAllOrdersRequest {}
Expand Down
Loading

0 comments on commit 733a3cd

Please sign in to comment.