Skip to content

Commit

Permalink
feat(rpc): ListCurrencies enhancement
Browse files Browse the repository at this point in the history
Closes #1067.
  • Loading branch information
rsercano authored and sangaman committed Sep 11, 2019
1 parent 4edce06 commit 3d716b6
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 146 deletions.
48 changes: 24 additions & 24 deletions docs/api.md

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

4 changes: 2 additions & 2 deletions lib/cli/commands/addcurrency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Arguments } from 'yargs';
import { callback, loadXudClient } from '../command';
import { AddCurrencyRequest } from '../../proto/xudrpc_pb';
import { Currency } from '../../proto/xudrpc_pb';
import { SwapClientType } from '../../constants/enums';

export const command = 'addcurrency <currency> <swap_client> [decimal_places] [token_address]';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const builder = {
};

export const handler = (argv: Arguments) => {
const request = new AddCurrencyRequest();
const request = new Currency();
request.setCurrency(argv.currency.toUpperCase());
request.setSwapClient(Number(SwapClientType[argv.swap_client]));
request.setTokenAddress(argv.token_address);
Expand Down
48 changes: 48 additions & 0 deletions lib/cli/commands/listcurrencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { callback, loadXudClient } from '../command';
import { Arguments } from 'yargs';
import colors from 'colors/safe';
import Table, { HorizontalTable } from 'cli-table3';
import { ListCurrenciesRequest, ListCurrenciesResponse } from '../../proto/xudrpc_pb';
import { SwapClientType } from '../../constants/enums';

const HEADERS = [
colors.blue('Ticker'),
colors.blue('Digits'),
colors.blue('Token Address'),
colors.blue('Swap Client'),
];

const formatCurrencies = (currencies: ListCurrenciesResponse.AsObject) => {
const formatted: any[] = [];
currencies.currenciesList.forEach((currency) => {
const element = [];
element.push(currency.currency, currency.decimalPlaces, currency.tokenAddress, SwapClientType[currency.swapClient]);
formatted.push(element);
});
return formatted;
};

const createTable = () => {
const table = new Table({
head: HEADERS,
}) as HorizontalTable;
return table;
};

const displayTable = (response: ListCurrenciesResponse.AsObject) => {
const table = createTable();

formatCurrencies(response).forEach((currency) => {
table.push(currency);
});
console.log(colors.underline(colors.bold('\nCurrencies:')));
console.log(table.toString());
};

export const command = 'listcurrencies';

export const describe = 'list available currencies';

export const handler = (argv: Arguments) => {
loadXudClient(argv).listCurrencies(new ListCurrenciesRequest(), callback(argv, displayTable));
};
16 changes: 12 additions & 4 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { errorCodes as lndErrorCodes } from '../lndclient/errors';
import { LndInfo } from '../lndclient/types';
import { SwapSuccess, SwapFailure } from '../swaps/types';
import { SwapFailureReason } from '../constants/enums';
import { TradeInstance, OrderInstance } from '../db/types';
import { TradeInstance, OrderInstance, CurrencyInstance } from '../db/types';

/**
* Creates an xudrpc Order message from an [[Order]].
Expand Down Expand Up @@ -227,7 +227,7 @@ class GrpcService {
/**
* See [[Service.addCurrency]]
*/
public addCurrency: grpc.handleUnaryCall<xudrpc.AddCurrencyRequest, xudrpc.AddCurrencyResponse> = async (call, callback) => {
public addCurrency: grpc.handleUnaryCall<xudrpc.Currency, xudrpc.AddCurrencyResponse> = async (call, callback) => {
try {
await this.service.addCurrency(call.request.toObject());
const response = new xudrpc.AddCurrencyResponse();
Expand Down Expand Up @@ -499,9 +499,17 @@ class GrpcService {
*/
public listCurrencies: grpc.handleUnaryCall<xudrpc.ListCurrenciesRequest, xudrpc.ListCurrenciesResponse> = (_, callback) => {
try {
const listCurrenciesResponse = this.service.listCurrencies();
const currencies = this.service.listCurrencies();
const response = new xudrpc.ListCurrenciesResponse();
response.setCurrenciesList(listCurrenciesResponse);

currencies.forEach((currency: CurrencyInstance) => {
const resultCurrency = new xudrpc.Currency();
resultCurrency.setDecimalPlaces(currency.decimalPlaces);
resultCurrency.setCurrency(currency.id);
resultCurrency.setTokenAddress(currency.tokenAddress);
resultCurrency.setSwapClient(currency.swapClient as number);
response.getCurrenciesList().push(resultCurrency);
});

callback(null, response);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class OrderBook extends EventEmitter {
}

public get currencies() {
return this.currencyInstances.keys();
return this.currencyInstances;
}

constructor({ logger, models, thresholds, pool, swaps, nosanityswaps, nobalancechecks, nomatching = false }:
Expand Down
52 changes: 26 additions & 26 deletions lib/proto/xudrpc.swagger.json

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

20 changes: 10 additions & 10 deletions lib/proto/xudrpc_grpc_pb.d.ts

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

Loading

0 comments on commit 3d716b6

Please sign in to comment.