Skip to content

Commit

Permalink
feat(rpc): TradeHistory
Browse files Browse the repository at this point in the history
This replaces the partially implemented `ListTrades` call with the more
comprehensive and descriptive `TradeHistory`. This displays key info
about every trade we have made.

Closes #1232.
  • Loading branch information
sangaman committed May 29, 2020
1 parent 7ffa091 commit 79bed74
Show file tree
Hide file tree
Showing 25 changed files with 2,432 additions and 1,988 deletions.
85 changes: 46 additions & 39 deletions docs/api.md

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

8 changes: 4 additions & 4 deletions lib/Xud.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { AssertionError } from 'assert';
import { EventEmitter } from 'events';
import { promises as fs } from 'fs';
import path from 'path';
import { Subscription } from 'rxjs';
import bootstrap from './bootstrap';
import Config from './Config';
import { SwapClientType, XuNetwork } from './constants/enums';
import DB from './db/DB';
import GrpcServer from './grpc/GrpcServer';
import GrpcWebProxyServer from './grpc/webproxy/GrpcWebProxyServer';
Expand All @@ -15,11 +18,8 @@ import InitService from './service/InitService';
import Service from './service/Service';
import SwapClientManager from './swaps/SwapClientManager';
import Swaps from './swaps/Swaps';
import { UnitConverter } from './utils/UnitConverter';
import { AssertionError } from 'assert';
import { SwapClientType, XuNetwork } from './constants/enums';
import { createSimnetChannels } from './utils/simnet-connext-channels';
import { Subscription } from 'rxjs';
import { UnitConverter } from './utils/UnitConverter';

const version: string = require('../package.json').version;

Expand Down
54 changes: 0 additions & 54 deletions lib/cli/commands/listtrades.ts

This file was deleted.

89 changes: 89 additions & 0 deletions lib/cli/commands/tradehistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Table, { HorizontalTable } from 'cli-table3';
import colors from 'colors/safe';
import { Arguments, Argv } from 'yargs';
import { Role, Trade, TradeHistoryRequest, TradeHistoryResponse, OrderSide } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';
import { satsToCoinsStr, trim } from '../utils';

const HEADERS = [
colors.blue('Execution'),
colors.blue('Price'),
colors.blue('Role'),
colors.blue('Order Id'),
colors.blue('Order Id (Counterparty)'),
colors.blue('Swap Hash'),
colors.blue('Executed At'),
];

const displayTrades = (trades: TradeHistoryResponse.AsObject) => {
const table = new Table({ head: HEADERS }) as HorizontalTable;
trades.tradesList.forEach((trade: Trade.AsObject) => {
const [baseCurrency, quoteCurrency] = trade.pairId.split('/');
const counterparty = trade.counterparty?.alias || 'Self';
let role: string;
let orderId: string;
let counterpartyOrderId: string;
switch (trade.role) {
case Role.TAKER:
orderId = trim(trade.takerOrder!.id ?? '', 8);
counterpartyOrderId = trim(trade.makerOrder?.id ?? '', 8);
role = 'Taker';
break;
case Role.MAKER:
orderId = trim(trade.makerOrder!.id ?? '', 8);
counterpartyOrderId = 'N/A';
role = 'Maker';
break;
case Role.INTERNAL:
orderId = trim(trade.takerOrder?.id ?? '', 8);
counterpartyOrderId = trim(trade.makerOrder?.id ?? '', 8);
role = 'Internal';
break;
}
let side: string;
switch (trade.side) {
case OrderSide.BUY:
side = 'Buy';
break;
case OrderSide.SELL:
side = 'Sell';
break;
case OrderSide.BOTH:
side = trade.takerOrder!.side === OrderSide.BUY ? 'Buy' : 'Sell';
break;
}

const details = [
`${side} ${satsToCoinsStr(trade.quantity)} ${baseCurrency}`,
`${trade.price} ${quoteCurrency}`,
role,
orderId,
`${counterpartyOrderId} (${counterparty})`,
trim(trade.rHash, 6),
new Date(trade.executedAt).toLocaleString(),
];

table.push(details);
});
console.log(colors.underline(colors.bold('\Trades:')));
console.log(table.toString());
};

export const command = 'tradehistory [limit]';

export const describe = 'list completed trades';

export const builder = (argv: Argv) => argv
.option('limit', {
description: 'the maximum number of trades to display',
type: 'number',
default: 15,
})
.example('$0 tradehistory', 'list most recent trades')
.example('$0 tradehistory 50', 'list the 50 most recent trades');

export const handler = async (argv: Arguments<any>) => {
const request = new TradeHistoryRequest();
request.setLimit(argv.limit);
(await loadXudClient(argv)).tradeHistory(request, callback(argv, displayTrades));
};
2 changes: 2 additions & 0 deletions lib/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum OrderingDirection {
export enum OrderSide {
Buy,
Sell,
Both,
}

export enum Owner {
Expand Down Expand Up @@ -59,6 +60,7 @@ export const magicValsXuNetwork = {
export enum SwapRole {
Taker = 0,
Maker = 1,
Internal = 2,
}

export enum SwapPhase {
Expand Down
11 changes: 8 additions & 3 deletions lib/db/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Sequelize, { DataTypeAbstract, DefineAttributeColumnOptions, DefineAttributes } from 'sequelize';
import { ReputationEvent } from '../constants/enums';
import { Currency, Order, Pair } from '../orderbook/types';
import { Address, NodeConnectionInfo } from '../p2p/types';
import { SwapDeal } from '../swaps/types';
import { Currency, Pair, Order } from '../orderbook/types';
import { ReputationEvent } from '../constants/enums';

export type SequelizeAttributes<T extends { [key: string]: any }> = DefineAttributes & {
[P in keyof T]: string | DataTypeAbstract | DefineAttributeColumnOptions
Expand Down Expand Up @@ -67,12 +67,17 @@ export type TradeFactory = {
quantity: number,
};

export type TradeAttributes = TradeFactory;
export type TradeAttributes = TradeFactory & {
makerOrder?: OrderAttributes;
takerOrder?: OrderAttributes;
SwapDeal?: SwapDealAttributes;
};

export type TradeInstance = TradeAttributes & Sequelize.Instance<TradeAttributes> & {
getMakerOrder: Sequelize.BelongsToGetAssociationMixin<OrderInstance>;
getTakerOrder: Sequelize.BelongsToGetAssociationMixin<OrderInstance>;
getSwapDeal: Sequelize.BelongsToGetAssociationMixin<SwapDealInstance>;
createdAt: Date,
};

/* Node */
Expand Down
Loading

0 comments on commit 79bed74

Please sign in to comment.