Skip to content

Commit

Permalink
feat(rpc): add owner parameter for listorders
Browse files Browse the repository at this point in the history
This commit adds an optional `owner` parameter for the `listorders` command while removing `include_own_orders_parmeter` and replacing its functionality.
The default value for the new parameter is `Both`, other options are `Own` and `Peer`.

The commit also fixes a bug that caused a situation where sell orders were displayed instead of buy orders when the limit was set.

Closes #1323
  • Loading branch information
krrprr committed Feb 25, 2020
1 parent d376e4c commit 8b0704e
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 41 deletions.
16 changes: 15 additions & 1 deletion docs/api.md

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

14 changes: 8 additions & 6 deletions lib/cli/commands/listorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ListOrdersRequest, ListOrdersResponse, Order } from '../../proto/xudrpc
import Table, { HorizontalTable } from 'cli-table3';
import colors from 'colors/safe';
import { satsToCoinsStr } from '../utils';
import { Owner } from '../../constants/enums';

type FormattedTradingPairOrders = {
pairId: string,
Expand Down Expand Up @@ -78,7 +79,7 @@ const displayTables = (orders: ListOrdersResponse.AsObject) => {
formatOrders(orders).forEach(displayOrdersTable);
};

export const command = 'listorders [pair_id] [include_own_orders] [limit]';
export const command = 'listorders [pair_id] [owner] [limit]';

export const describe = 'list orders from the order book';

Expand All @@ -87,10 +88,11 @@ export const builder = {
describe: 'trading pair for which to retrieve orders',
type: 'string',
},
include_own_orders: {
describe: 'whether to include own orders',
type: 'boolean',
default: true,
owner: {
describe: 'whether to include own, peer or both orders',
type: 'string',
choices: ['Both', 'Own', 'Peer'],
default: 'Both',
},
limit: {
describe: 'max number of orders to return',
Expand All @@ -103,7 +105,7 @@ export const handler = (argv: Arguments<any>) => {
const request = new ListOrdersRequest();
const pairId = argv.pair_id ? argv.pair_id.toUpperCase() : undefined;
request.setPairId(pairId);
request.setIncludeOwnOrders(argv.include_own_orders);
request.setOwner(Number(Owner[argv.owner]));
request.setLimit(argv.limit);
loadXudClient(argv).listOrders(request, callback(argv, displayTables));
};
3 changes: 2 additions & 1 deletion lib/cli/commands/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ListOrdersRequest, ListOrdersResponse, Order } from '../../proto/xudrpc
import Table, { HorizontalTable } from 'cli-table3';
import colors from 'colors/safe';
import { satsToCoinsStr } from '../utils';
import { Owner } from '../../constants/enums';

type FormattedOrderbook = {
pairId: string,
Expand Down Expand Up @@ -181,6 +182,6 @@ export const handler = (argv: Arguments<any>) => {
const request = new ListOrdersRequest();
const pairId = argv.pair_id ? argv.pair_id.toUpperCase() : undefined;
request.setPairId(pairId);
request.setIncludeOwnOrders(true);
request.setOwner(Number(Owner.Both));
loadXudClient(argv).listOrders(request, callback(argv, displayTables, displayJson));
};
6 changes: 6 additions & 0 deletions lib/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export enum OrderSide {
Sell,
}

export enum Owner {
Both,
Own,
Peer,
}

export enum XuNetwork {
// real coins.
MainNet = 'mainnet',
Expand Down
13 changes: 9 additions & 4 deletions lib/proto/xudrpc.swagger.json

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

13 changes: 10 additions & 3 deletions lib/proto/xudrpc_pb.d.ts

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

38 changes: 23 additions & 15 deletions lib/proto/xudrpc_pb.js

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

20 changes: 13 additions & 7 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrderSide, SwapClientType, SwapRole } from '../constants/enums';
import { OrderSide, SwapClientType, SwapRole, Owner } from '../constants/enums';
import { LndInfo } from '../lndclient/types';
import OrderBook from '../orderbook/OrderBook';
import { OrderSidesArrays } from '../orderbook/TradingPair';
Expand Down Expand Up @@ -358,8 +358,12 @@ class Service {
/**
* Get a map between pair ids and its orders from the order book.
*/
public listOrders = (args: { pairId: string, includeOwnOrders: boolean, limit: number }): Map<string, OrderSidesArrays<any>> => {
const { pairId, includeOwnOrders, limit } = args;
public listOrders = (
args: { pairId: string, owner: Owner | number, limit: number },
): Map<string, OrderSidesArrays<any>> => {
const { pairId, owner, limit } = args;
const includeOwnOrders = owner === Owner.Both || owner === Owner.Own;
const includePeerOrders = owner === Owner.Both || owner === Owner.Peer;

const result = new Map<string, OrderSidesArrays<any>>();

Expand All @@ -369,9 +373,11 @@ class Service {
sellArray: [],
};

const peerOrders = this.orderBook.getPeersOrders(pairId);
orders.buyArray = peerOrders.buyArray;
orders.sellArray = peerOrders.sellArray;
if (includePeerOrders) {
const peerOrders = this.orderBook.getPeersOrders(pairId);
orders.buyArray = peerOrders.buyArray;
orders.sellArray = peerOrders.sellArray;
}

if (includeOwnOrders) {
const ownOrders = this.orderBook.getOwnOrders(pairId);
Expand All @@ -386,7 +392,7 @@ class Service {

if (limit > 0) {
orders.buyArray = orders.buyArray.slice(0, limit);
orders.sellArray = orders.buyArray.slice(0, limit);
orders.sellArray = orders.sellArray.slice(0, limit);
}
return orders;
};
Expand Down
9 changes: 7 additions & 2 deletions proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,13 @@ message ListCurrenciesResponse {
message ListOrdersRequest {
// The trading pair for which to retrieve orders.
string pair_id = 1 [json_name = "pair_id"];
// Whether own orders should be included in result or not.
bool include_own_orders = 2 [json_name = "include_own_orders"];
enum Owner {
BOTH = 0;
OWN = 1;
PEER = 2;
}
// Whether only own, only peer or both orders should be included in result.
Owner owner = 2 [json_name = "owner"];
// The maximum number of orders to return from each side of the order book.
uint32 limit = 3 [json_name = "limit"];
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/Service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai, { expect } from 'chai';
import Xud from '../../lib/Xud';
import chaiAsPromised from 'chai-as-promised';
import Service from '../../lib/service/Service';
import { SwapClientType, OrderSide } from '../../lib/constants/enums';
import { SwapClientType, OrderSide, Owner } from '../../lib/constants/enums';
import { getTempDir } from '../utils';

chai.use(chaiAsPromised);
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('API Service', () => {
it('should get orders', async () => {
const args = {
pairId,
includeOwnOrders: true,
owner: Owner.Both,
limit: 0,
};
const orders = service.listOrders(args);
Expand Down
Loading

0 comments on commit 8b0704e

Please sign in to comment.