Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): automatically determine rpc host/port #1462

Merged
1 commit merged into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bin/xucli
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ require('yargs')
},
rpcport: {
alias: 'p',
default: 8886,
describe: 'RPC service port',
type: 'number',
},
rpchost: {
alias: 'h',
default: 'localhost',
describe: 'RPC service hostname',
type: 'string',
},
Expand All @@ -33,6 +31,11 @@ require('yargs')
type: 'boolean',
default: false,
},
xudir: {
alias: 'x',
describe: 'Data directory for xud',
type: 'string',
},
})
.commandDir('../dist/cli/commands/', { recurse: true })
.demandCommand(1, '')
Expand Down
39 changes: 34 additions & 5 deletions lib/cli/command.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
import fs from 'fs';
import grpc, { status } from 'grpc';
import path from 'path';
import { Arguments } from 'yargs';
import Config from '../Config';
import { XudClient, XudInitClient } from '../proto/xudrpc_grpc_pb';
import { getDefaultCertPath } from './utils';

/**
* A generic function to instantiate an XU client.
* @param argv the command line arguments
*/
export const loadXudClient = (argv: Arguments<any>) => {
const certPath = argv.tlscertpath || getDefaultCertPath();
export const loadXudClient = async (argv: Arguments<any>) => {
const config = new Config();
await config.load({
xudir: argv.xudir,
rpc: {
port: argv.rpcport,
host: argv.rpchost,
},
});

const certPath = argv.tlscertpath || path.join(config.xudir, 'tls.cert');
const cert = fs.readFileSync(certPath);
const credentials = grpc.credentials.createSsl(cert);

// in case port and host args were not set, we update them to the values we
// determined by loading the config
argv.rpcport = config.rpc.port;
argv.rpchost = config.rpc.host;

return new XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
};

export const loadXudInitClient = (argv: Arguments<any>) => {
const certPath = argv.tlscertpath || getDefaultCertPath();
export const loadXudInitClient = async (argv: Arguments<any>) => {
const config = new Config();
await config.load({
xudir: argv.xudir,
rpc: {
port: argv.rpcport,
host: argv.rpchost,
},
});

const certPath = argv.tlscertpath || path.join(config.xudir, 'tls.cert');
const cert = fs.readFileSync(certPath);
const credentials = grpc.credentials.createSsl(cert);

// in case port and host args were not set, we update them to the values we
// determined by loading the config
argv.rpcport = config.rpc.port;
argv.rpchost = config.rpc.host;

return new XudInitClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/cli/commands/addcurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new Currency();
request.setCurrency(argv.currency.toUpperCase());
request.setSwapClient(Number(SwapClientType[argv.swap_client]));
request.setTokenAddress(argv.token_address);
request.setDecimalPlaces(argv.decimal_places);
loadXudClient(argv).addCurrency(request, callback(argv));
(await loadXudClient(argv)).addCurrency(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/addpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new AddPairRequest();
request.setBaseCurrency(argv.base_currency.toUpperCase());
request.setQuoteCurrency(argv.quote_currency.toUpperCase());
loadXudClient(argv).addPair(request, callback(argv));
(await loadXudClient(argv)).addPair(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new BanRequest();
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).ban(request, callback(argv));
(await loadXudClient(argv)).ban(request, callback(argv));
};
2 changes: 1 addition & 1 deletion lib/cli/commands/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const describe = 'place a buy order';

export const builder = (argv: Argv) => placeOrderBuilder(argv, OrderSide.BUY);

export const handler = (argv: Arguments) => placeOrderHandler(argv, OrderSide.BUY);
export const handler = async (argv: Arguments) => placeOrderHandler(argv, OrderSide.BUY);
4 changes: 2 additions & 2 deletions lib/cli/commands/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new ConnectRequest();
request.setNodeUri(argv.node_uri);
loadXudClient(argv).connect(request, callback(argv));
(await loadXudClient(argv)).connect(request, callback(argv));
};
2 changes: 1 addition & 1 deletion lib/cli/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ a single password provided below.
return;
}

const client = loadXudInitClient(argv);
const client = await loadXudInitClient(argv);
// wait up to 3 seconds for rpc server to listen before call in case xud was just started
client.waitForReady(Date.now() + 3000, () => {
client.createNode(request, callback(argv, formatOutput));
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/commands/discovernodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new DiscoverNodesRequest();
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).discoverNodes(request, callback(argv));
(await loadXudClient(argv)).discoverNodes(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/executeswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new ExecuteSwapRequest();
request.setOrderId(argv.order_id);
request.setPairId(argv.pair_id);
if (argv.quantity) {
request.setQuantity(coinsToSats(argv.quantity));
}
loadXudClient(argv).executeSwap(request, callback(argv, displaySwapSuccess));
(await loadXudClient(argv)).executeSwap(request, callback(argv, displaySwapSuccess));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/getbalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new GetBalanceRequest();
if (argv.currency) {
request.setCurrency(argv.currency.toUpperCase());
}
loadXudClient(argv).getBalance(request, callback(argv, displayBalances));
(await loadXudClient(argv)).getBalance(request, callback(argv, displayBalances));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/getinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ export const command = 'getinfo';

export const describe = 'get general info from the local xud node';

export const handler = (argv: Arguments) => {
loadXudClient(argv).getInfo(new GetInfoRequest(), callback(argv, displayGetInfo));
export const handler = async (argv: Arguments) => {
(await loadXudClient(argv)).getInfo(new GetInfoRequest(), callback(argv, displayGetInfo));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/getnodeinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new GetNodeInfoRequest();
request.setNodeIdentifier(argv.node_identifier);
loadXudClient(argv).getNodeInfo(request, callback(argv, displayNodeInfo));
(await loadXudClient(argv)).getNodeInfo(request, callback(argv, displayNodeInfo));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/listcurrencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ export const command = 'listcurrencies';

export const describe = 'list available currencies';

export const handler = (argv: Arguments) => {
loadXudClient(argv).listCurrencies(new ListCurrenciesRequest(), callback(argv, displayTable));
export const handler = async (argv: Arguments) => {
(await loadXudClient(argv)).listCurrencies(new ListCurrenciesRequest(), callback(argv, displayTable));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/listorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new ListOrdersRequest();
const pairId = argv.pair_id ? argv.pair_id.toUpperCase() : undefined;
request.setPairId(pairId);
request.setOwner(Number(Owner[argv.owner]));
request.setLimit(argv.limit);
loadXudClient(argv).listOrders(request, callback(argv, displayTables));
(await loadXudClient(argv)).listOrders(request, callback(argv, displayTables));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/listpairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export const command = 'listpairs';

export const describe = 'get order book\'s available pairs';

export const handler = (argv: Arguments) => {
loadXudClient(argv).listPairs(new ListPairsRequest(), callback(argv, displayPairs));
export const handler = async (argv: Arguments) => {
(await loadXudClient(argv)).listPairs(new ListPairsRequest(), callback(argv, displayPairs));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/listpeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ export const command = 'listpeers';

export const describe = 'list connected peers';

export const handler = (argv: Arguments) => {
loadXudClient(argv).listPeers(new ListPeersRequest(), callback(argv, displayTables));
export const handler = async (argv: Arguments) => {
(await loadXudClient(argv)).listPeers(new ListPeersRequest(), callback(argv, displayTables));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/listtrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new ListTradesRequest();
request.setLimit(argv.limit);
loadXudClient(argv).listTrades(request, callback(argv, displayTrades));
(await loadXudClient(argv)).listTrades(request, callback(argv, displayTrades));
};
5 changes: 3 additions & 2 deletions lib/cli/commands/openchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new OpenChannelRequest();
request.setNodeIdentifier(argv.node_identifier);
request.setCurrency(argv.currency.toUpperCase());
request.setAmount(coinsToSats(argv.amount));
request.setPushAmount(coinsToSats(argv.amount));
loadXudClient(argv).openChannel(request, callback(argv));

(await loadXudClient(argv)).openChannel(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ const displayJson = (orders: ListOrdersResponse.AsObject, argv: Arguments<any>)
console.log(JSON.stringify(jsonOrderbooks, undefined, 2));
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new ListOrdersRequest();
const pairId = argv.pair_id ? argv.pair_id.toUpperCase() : undefined;
request.setPairId(pairId);
request.setOwner(Number(Owner.Both));
loadXudClient(argv).listOrders(request, callback(argv, displayTables, displayJson));
(await loadXudClient(argv)).listOrders(request, callback(argv, displayTables, displayJson));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/removecurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new RemoveCurrencyRequest();
request.setCurrency(argv.currency.toUpperCase());
loadXudClient(argv).removeCurrency(request, callback(argv));
(await loadXudClient(argv)).removeCurrency(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/removeorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new RemoveOrderRequest();
request.setOrderId(argv.order_id);
if (argv.quantity) {
request.setQuantity(coinsToSats(argv.quantity));
}
loadXudClient(argv).removeOrder(request, callback(argv));
(await loadXudClient(argv)).removeOrder(request, callback(argv));
};
4 changes: 2 additions & 2 deletions lib/cli/commands/removepair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const builder = {
},
};

export const handler = (argv: Arguments<any>) => {
export const handler = async (argv: Arguments<any>) => {
const request = new RemovePairRequest();
request.setPairId(argv.pair_id.toUpperCase());
loadXudClient(argv).removePair(request, callback(argv));
(await loadXudClient(argv)).removePair(request, callback(argv));
};
2 changes: 1 addition & 1 deletion lib/cli/commands/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ a single password provided below.
return;
}

const client = loadXudInitClient(argv);
const client = await loadXudInitClient(argv);
// wait up to 3 seconds for rpc server to listen before call in case xud was just started
client.waitForReady(Date.now() + 3000, () => {
client.restoreNode(request, callback(argv, formatOutput));
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/commands/sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const describe = 'place a sell order';

export const builder = (argv: Argv) => placeOrderBuilder(argv, OrderSide.SELL);

export const handler = (argv: Arguments) => placeOrderHandler(argv, OrderSide.SELL);
export const handler = async (argv: Arguments) => placeOrderHandler(argv, OrderSide.SELL);
4 changes: 2 additions & 2 deletions lib/cli/commands/shutdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const command = 'shutdown';

export const describe = 'gracefully shutdown local xud node';

export const handler = (argv: Arguments) => {
loadXudClient(argv).shutdown(new ShutdownRequest(), callback(argv));
export const handler = async (argv: Arguments) => {
(await loadXudClient(argv)).shutdown(new ShutdownRequest(), callback(argv));
};
Loading