Skip to content

Commit 3d716b6

Browse files
rsercanosangaman
authored andcommitted
feat(rpc): ListCurrencies enhancement
Closes #1067.
1 parent 4edce06 commit 3d716b6

File tree

12 files changed

+208
-146
lines changed

12 files changed

+208
-146
lines changed

docs/api.md

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli/commands/addcurrency.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Arguments } from 'yargs';
22
import { callback, loadXudClient } from '../command';
3-
import { AddCurrencyRequest } from '../../proto/xudrpc_pb';
3+
import { Currency } from '../../proto/xudrpc_pb';
44
import { SwapClientType } from '../../constants/enums';
55

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

3131
export const handler = (argv: Arguments) => {
32-
const request = new AddCurrencyRequest();
32+
const request = new Currency();
3333
request.setCurrency(argv.currency.toUpperCase());
3434
request.setSwapClient(Number(SwapClientType[argv.swap_client]));
3535
request.setTokenAddress(argv.token_address);

lib/cli/commands/listcurrencies.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { callback, loadXudClient } from '../command';
2+
import { Arguments } from 'yargs';
3+
import colors from 'colors/safe';
4+
import Table, { HorizontalTable } from 'cli-table3';
5+
import { ListCurrenciesRequest, ListCurrenciesResponse } from '../../proto/xudrpc_pb';
6+
import { SwapClientType } from '../../constants/enums';
7+
8+
const HEADERS = [
9+
colors.blue('Ticker'),
10+
colors.blue('Digits'),
11+
colors.blue('Token Address'),
12+
colors.blue('Swap Client'),
13+
];
14+
15+
const formatCurrencies = (currencies: ListCurrenciesResponse.AsObject) => {
16+
const formatted: any[] = [];
17+
currencies.currenciesList.forEach((currency) => {
18+
const element = [];
19+
element.push(currency.currency, currency.decimalPlaces, currency.tokenAddress, SwapClientType[currency.swapClient]);
20+
formatted.push(element);
21+
});
22+
return formatted;
23+
};
24+
25+
const createTable = () => {
26+
const table = new Table({
27+
head: HEADERS,
28+
}) as HorizontalTable;
29+
return table;
30+
};
31+
32+
const displayTable = (response: ListCurrenciesResponse.AsObject) => {
33+
const table = createTable();
34+
35+
formatCurrencies(response).forEach((currency) => {
36+
table.push(currency);
37+
});
38+
console.log(colors.underline(colors.bold('\nCurrencies:')));
39+
console.log(table.toString());
40+
};
41+
42+
export const command = 'listcurrencies';
43+
44+
export const describe = 'list available currencies';
45+
46+
export const handler = (argv: Arguments) => {
47+
loadXudClient(argv).listCurrencies(new ListCurrenciesRequest(), callback(argv, displayTable));
48+
};

lib/grpc/GrpcService.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { errorCodes as lndErrorCodes } from '../lndclient/errors';
1212
import { LndInfo } from '../lndclient/types';
1313
import { SwapSuccess, SwapFailure } from '../swaps/types';
1414
import { SwapFailureReason } from '../constants/enums';
15-
import { TradeInstance, OrderInstance } from '../db/types';
15+
import { TradeInstance, OrderInstance, CurrencyInstance } from '../db/types';
1616

1717
/**
1818
* Creates an xudrpc Order message from an [[Order]].
@@ -227,7 +227,7 @@ class GrpcService {
227227
/**
228228
* See [[Service.addCurrency]]
229229
*/
230-
public addCurrency: grpc.handleUnaryCall<xudrpc.AddCurrencyRequest, xudrpc.AddCurrencyResponse> = async (call, callback) => {
230+
public addCurrency: grpc.handleUnaryCall<xudrpc.Currency, xudrpc.AddCurrencyResponse> = async (call, callback) => {
231231
try {
232232
await this.service.addCurrency(call.request.toObject());
233233
const response = new xudrpc.AddCurrencyResponse();
@@ -499,9 +499,17 @@ class GrpcService {
499499
*/
500500
public listCurrencies: grpc.handleUnaryCall<xudrpc.ListCurrenciesRequest, xudrpc.ListCurrenciesResponse> = (_, callback) => {
501501
try {
502-
const listCurrenciesResponse = this.service.listCurrencies();
502+
const currencies = this.service.listCurrencies();
503503
const response = new xudrpc.ListCurrenciesResponse();
504-
response.setCurrenciesList(listCurrenciesResponse);
504+
505+
currencies.forEach((currency: CurrencyInstance) => {
506+
const resultCurrency = new xudrpc.Currency();
507+
resultCurrency.setDecimalPlaces(currency.decimalPlaces);
508+
resultCurrency.setCurrency(currency.id);
509+
resultCurrency.setTokenAddress(currency.tokenAddress);
510+
resultCurrency.setSwapClient(currency.swapClient as number);
511+
response.getCurrenciesList().push(resultCurrency);
512+
});
505513

506514
callback(null, response);
507515
} catch (err) {

lib/orderbook/OrderBook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class OrderBook extends EventEmitter {
9090
}
9191

9292
public get currencies() {
93-
return this.currencyInstances.keys();
93+
return this.currencyInstances;
9494
}
9595

9696
constructor({ logger, models, thresholds, pool, swaps, nosanityswaps, nobalancechecks, nomatching = false }:

lib/proto/xudrpc.swagger.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/proto/xudrpc_grpc_pb.d.ts

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)