From e0b6ee24ffb2cd6eb90a998887f10a3b53652ef5 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Wed, 8 Apr 2020 09:04:12 -0400 Subject: [PATCH] feat(cli): automatically determine rpc host/port This enhances the cli to automatically attempt to find and read from the xud configuration file to determine the port and host for the xud gRPC service based on the configured network - which determines default port numbers - or configured port if specified. This prevents the user from having to specify the rpc port on `xucli` commands when using a non-mainnet xud instance or a non-default gRPC port via the config file. Closes #1451. --- bin/xucli | 7 +++++-- lib/cli/command.ts | 33 +++++++++++++++++++++++------- lib/cli/commands/addcurrency.ts | 4 ++-- lib/cli/commands/addpair.ts | 4 ++-- lib/cli/commands/ban.ts | 4 ++-- lib/cli/commands/buy.ts | 2 +- lib/cli/commands/connect.ts | 4 ++-- lib/cli/commands/create.ts | 2 +- lib/cli/commands/discovernodes.ts | 4 ++-- lib/cli/commands/executeswap.ts | 4 ++-- lib/cli/commands/getbalance.ts | 4 ++-- lib/cli/commands/getinfo.ts | 4 ++-- lib/cli/commands/getnodeinfo.ts | 4 ++-- lib/cli/commands/listcurrencies.ts | 4 ++-- lib/cli/commands/listorders.ts | 4 ++-- lib/cli/commands/listpairs.ts | 4 ++-- lib/cli/commands/listpeers.ts | 4 ++-- lib/cli/commands/listtrades.ts | 4 ++-- lib/cli/commands/openchannel.ts | 4 ++-- lib/cli/commands/orderbook.ts | 4 ++-- lib/cli/commands/removecurrency.ts | 4 ++-- lib/cli/commands/removeorder.ts | 4 ++-- lib/cli/commands/removepair.ts | 4 ++-- lib/cli/commands/restore.ts | 2 +- lib/cli/commands/sell.ts | 2 +- lib/cli/commands/shutdown.ts | 4 ++-- lib/cli/commands/streamorders.ts | 29 +++++++++++++------------- lib/cli/commands/tradinglimits.ts | 4 ++-- lib/cli/commands/unban.ts | 4 ++-- lib/cli/commands/unlock.ts | 4 ++-- lib/cli/placeorder.ts | 7 ++++--- 31 files changed, 100 insertions(+), 76 deletions(-) diff --git a/bin/xucli b/bin/xucli index dd06defdd..300d1d313 100755 --- a/bin/xucli +++ b/bin/xucli @@ -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', }, @@ -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, '') diff --git a/lib/cli/command.ts b/lib/cli/command.ts index fc07f7f49..37be8da60 100644 --- a/lib/cli/command.ts +++ b/lib/cli/command.ts @@ -1,27 +1,46 @@ 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) => { - const certPath = argv.tlscertpath || getDefaultCertPath(); +export const loadXudClient = async (argv: Arguments) => { + 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); - return new XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials); + return new XudClient(`${config.rpc.host}:${config.rpc.port}`, credentials); }; -export const loadXudInitClient = (argv: Arguments) => { - const certPath = argv.tlscertpath || getDefaultCertPath(); +export const loadXudInitClient = async (argv: Arguments) => { + 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); - return new XudInitClient(`${argv.rpchost}:${argv.rpcport}`, credentials); + return new XudInitClient(`${config.rpc.host}:${config.rpc.port}`, credentials); }; interface GrpcResponse { diff --git a/lib/cli/commands/addcurrency.ts b/lib/cli/commands/addcurrency.ts index 2439c1ec4..3d58d1053 100644 --- a/lib/cli/commands/addcurrency.ts +++ b/lib/cli/commands/addcurrency.ts @@ -28,11 +28,11 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/addpair.ts b/lib/cli/commands/addpair.ts index 12161c5a6..570b8d0ce 100644 --- a/lib/cli/commands/addpair.ts +++ b/lib/cli/commands/addpair.ts @@ -17,9 +17,9 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/ban.ts b/lib/cli/commands/ban.ts index 40eacfc36..48f4dfdfd 100644 --- a/lib/cli/commands/ban.ts +++ b/lib/cli/commands/ban.ts @@ -13,8 +13,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new BanRequest(); request.setNodeIdentifier(argv.node_identifier); - loadXudClient(argv).ban(request, callback(argv)); + (await loadXudClient(argv)).ban(request, callback(argv)); }; diff --git a/lib/cli/commands/buy.ts b/lib/cli/commands/buy.ts index ad02b35c4..488df472a 100644 --- a/lib/cli/commands/buy.ts +++ b/lib/cli/commands/buy.ts @@ -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); diff --git a/lib/cli/commands/connect.ts b/lib/cli/commands/connect.ts index dc2807a50..d5612a5f6 100644 --- a/lib/cli/commands/connect.ts +++ b/lib/cli/commands/connect.ts @@ -13,8 +13,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new ConnectRequest(); request.setNodeUri(argv.node_uri); - loadXudClient(argv).connect(request, callback(argv)); + (await loadXudClient(argv)).connect(request, callback(argv)); }; diff --git a/lib/cli/commands/create.ts b/lib/cli/commands/create.ts index 01479023a..ec2055772 100644 --- a/lib/cli/commands/create.ts +++ b/lib/cli/commands/create.ts @@ -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)); diff --git a/lib/cli/commands/discovernodes.ts b/lib/cli/commands/discovernodes.ts index a8e9af3d2..c8604f151 100644 --- a/lib/cli/commands/discovernodes.ts +++ b/lib/cli/commands/discovernodes.ts @@ -13,8 +13,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new DiscoverNodesRequest(); request.setNodeIdentifier(argv.node_identifier); - loadXudClient(argv).discoverNodes(request, callback(argv)); + (await loadXudClient(argv)).discoverNodes(request, callback(argv)); }; diff --git a/lib/cli/commands/executeswap.ts b/lib/cli/commands/executeswap.ts index 91935ce1e..6d4dabbbd 100644 --- a/lib/cli/commands/executeswap.ts +++ b/lib/cli/commands/executeswap.ts @@ -33,12 +33,12 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/getbalance.ts b/lib/cli/commands/getbalance.ts index 0058bbe11..79d0255d0 100644 --- a/lib/cli/commands/getbalance.ts +++ b/lib/cli/commands/getbalance.ts @@ -70,10 +70,10 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/getinfo.ts b/lib/cli/commands/getinfo.ts index 374bb35f3..b491e46f1 100644 --- a/lib/cli/commands/getinfo.ts +++ b/lib/cli/commands/getinfo.ts @@ -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)); }; diff --git a/lib/cli/commands/getnodeinfo.ts b/lib/cli/commands/getnodeinfo.ts index 5d00ac9ea..474e5d544 100644 --- a/lib/cli/commands/getnodeinfo.ts +++ b/lib/cli/commands/getnodeinfo.ts @@ -26,8 +26,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new GetNodeInfoRequest(); request.setNodeIdentifier(argv.node_identifier); - loadXudClient(argv).getNodeInfo(request, callback(argv, displayNodeInfo)); + (await loadXudClient(argv)).getNodeInfo(request, callback(argv, displayNodeInfo)); }; diff --git a/lib/cli/commands/listcurrencies.ts b/lib/cli/commands/listcurrencies.ts index df9e73fe9..8a2199422 100644 --- a/lib/cli/commands/listcurrencies.ts +++ b/lib/cli/commands/listcurrencies.ts @@ -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)); }; diff --git a/lib/cli/commands/listorders.ts b/lib/cli/commands/listorders.ts index ab24e710f..b75a517f8 100644 --- a/lib/cli/commands/listorders.ts +++ b/lib/cli/commands/listorders.ts @@ -101,11 +101,11 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/listpairs.ts b/lib/cli/commands/listpairs.ts index 4eff7a874..a652c79fe 100644 --- a/lib/cli/commands/listpairs.ts +++ b/lib/cli/commands/listpairs.ts @@ -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)); }; diff --git a/lib/cli/commands/listpeers.ts b/lib/cli/commands/listpeers.ts index 438ad954b..c44cb7cf8 100644 --- a/lib/cli/commands/listpeers.ts +++ b/lib/cli/commands/listpeers.ts @@ -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)); }; diff --git a/lib/cli/commands/listtrades.ts b/lib/cli/commands/listtrades.ts index 054b19db6..60e8a7966 100644 --- a/lib/cli/commands/listtrades.ts +++ b/lib/cli/commands/listtrades.ts @@ -46,8 +46,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new ListTradesRequest(); request.setLimit(argv.limit); - loadXudClient(argv).listTrades(request, callback(argv, displayTrades)); + (await loadXudClient(argv)).listTrades(request, callback(argv, displayTrades)); }; diff --git a/lib/cli/commands/openchannel.ts b/lib/cli/commands/openchannel.ts index 9cc9141c8..0eed5a7ae 100644 --- a/lib/cli/commands/openchannel.ts +++ b/lib/cli/commands/openchannel.ts @@ -22,10 +22,10 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new OpenChannelRequest(); request.setNodeIdentifier(argv.node_identifier); request.setCurrency(argv.currency.toUpperCase()); request.setAmount(coinsToSats(argv.amount)); - loadXudClient(argv).openChannel(request, callback(argv)); + (await loadXudClient(argv)).openChannel(request, callback(argv)); }; diff --git a/lib/cli/commands/orderbook.ts b/lib/cli/commands/orderbook.ts index c35bf715d..0913fa8a8 100644 --- a/lib/cli/commands/orderbook.ts +++ b/lib/cli/commands/orderbook.ts @@ -178,10 +178,10 @@ const displayJson = (orders: ListOrdersResponse.AsObject, argv: Arguments) console.log(JSON.stringify(jsonOrderbooks, undefined, 2)); }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/removecurrency.ts b/lib/cli/commands/removecurrency.ts index 7819dc464..85ee99a27 100644 --- a/lib/cli/commands/removecurrency.ts +++ b/lib/cli/commands/removecurrency.ts @@ -13,8 +13,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new RemoveCurrencyRequest(); request.setCurrency(argv.currency.toUpperCase()); - loadXudClient(argv).removeCurrency(request, callback(argv)); + (await loadXudClient(argv)).removeCurrency(request, callback(argv)); }; diff --git a/lib/cli/commands/removeorder.ts b/lib/cli/commands/removeorder.ts index 1178f7407..de5ed536d 100644 --- a/lib/cli/commands/removeorder.ts +++ b/lib/cli/commands/removeorder.ts @@ -17,11 +17,11 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { 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)); }; diff --git a/lib/cli/commands/removepair.ts b/lib/cli/commands/removepair.ts index a21b61dba..c257d4489 100644 --- a/lib/cli/commands/removepair.ts +++ b/lib/cli/commands/removepair.ts @@ -13,8 +13,8 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new RemovePairRequest(); request.setPairId(argv.pair_id.toUpperCase()); - loadXudClient(argv).removePair(request, callback(argv)); + (await loadXudClient(argv)).removePair(request, callback(argv)); }; diff --git a/lib/cli/commands/restore.ts b/lib/cli/commands/restore.ts index c8eb340ec..ad64c8f06 100644 --- a/lib/cli/commands/restore.ts +++ b/lib/cli/commands/restore.ts @@ -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)); diff --git a/lib/cli/commands/sell.ts b/lib/cli/commands/sell.ts index 879118762..2d887ff54 100644 --- a/lib/cli/commands/sell.ts +++ b/lib/cli/commands/sell.ts @@ -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); diff --git a/lib/cli/commands/shutdown.ts b/lib/cli/commands/shutdown.ts index 5819486ed..218b747c1 100644 --- a/lib/cli/commands/shutdown.ts +++ b/lib/cli/commands/shutdown.ts @@ -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)); }; diff --git a/lib/cli/commands/streamorders.ts b/lib/cli/commands/streamorders.ts index 45fcc3903..94a1aebbc 100644 --- a/lib/cli/commands/streamorders.ts +++ b/lib/cli/commands/streamorders.ts @@ -15,15 +15,17 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { - ensureConnection(argv, true); +export const handler = async (argv: Arguments) => { + await ensureConnection(argv, true); }; -let xud: XudClient; +let client: XudClient; -const ensureConnection = (argv: Arguments, printError?: boolean) => { - if (!xud) xud = loadXudClient(argv); - xud.waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => { +const ensureConnection = async (argv: Arguments, printError?: boolean) => { + if (!client) { + client = await loadXudClient(argv); + } + client.waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => { if (error) { if (printError) console.error(`${error.name}: ${error.message}`); setTimeout(ensureConnection.bind(undefined, argv), 3000); @@ -35,10 +37,9 @@ const ensureConnection = (argv: Arguments, printError?: boolean) => { }; const streamOrders = (argv: Arguments) => { - const xudClient = loadXudClient(argv); const ordersReqeust = new xudrpc.SubscribeOrdersRequest(); ordersReqeust.setExisting(argv.existing); - const ordersSubscription = loadXudClient(argv).subscribeOrders(ordersReqeust); + const ordersSubscription = client.subscribeOrders(ordersReqeust); ordersSubscription.on('data', (orderUpdate: xudrpc.OrderUpdate) => { if (orderUpdate.getOrder() !== undefined) { console.log(`Order added: ${JSON.stringify(orderUpdate.getOrder()!.toObject())}`); @@ -50,19 +51,19 @@ const streamOrders = (argv: Arguments) => { // adding end, close, error events only once, // since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough. ordersSubscription.on('end', reconnect.bind(undefined, argv)); - ordersSubscription.on('error', (err: Error) => { + ordersSubscription.on('error', async (err: Error) => { console.warn(`Unexpected error occured: ${err.message}, trying to reconnect`); - ensureConnection(argv); + await ensureConnection(argv); }); const swapsRequest = new xudrpc.SubscribeSwapsRequest(); swapsRequest.setIncludeTaker(true); - const swapsSubscription = xudClient.subscribeSwaps(swapsRequest); + const swapsSubscription = client.subscribeSwaps(swapsRequest); swapsSubscription.on('data', (swapSuccess: xudrpc.SwapSuccess) => { console.log(`Order swapped: ${JSON.stringify(swapSuccess.toObject())}`); }); - const swapFailuresSubscription = xudClient.subscribeSwapFailures(swapsRequest); + const swapFailuresSubscription = client.subscribeSwapFailures(swapsRequest); swapFailuresSubscription.on('data', (swapFailure: xudrpc.SwapFailure) => { console.log(`Swap failed: ${JSON.stringify(swapFailure.toObject())}`); }); @@ -72,7 +73,7 @@ const streamOrders = (argv: Arguments) => { swapFailuresSubscription.on('error', () => {}); }; -const reconnect = (argv: Arguments) => { +const reconnect = async (argv: Arguments) => { console.log('Stream has closed, trying to reconnect'); - ensureConnection(argv, false); + await ensureConnection(argv, false); }; diff --git a/lib/cli/commands/tradinglimits.ts b/lib/cli/commands/tradinglimits.ts index a7ec2d0e4..c0b848b57 100644 --- a/lib/cli/commands/tradinglimits.ts +++ b/lib/cli/commands/tradinglimits.ts @@ -51,11 +51,11 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new TradingLimitsRequest(); if (argv.currency) { request.setCurrency(argv.currency.toUpperCase()); } - loadXudClient(argv).tradingLimits(request, callback(argv, displayLimits)); + (await loadXudClient(argv)).tradingLimits(request, callback(argv, displayLimits)); }; diff --git a/lib/cli/commands/unban.ts b/lib/cli/commands/unban.ts index c4d50b973..301c216de 100644 --- a/lib/cli/commands/unban.ts +++ b/lib/cli/commands/unban.ts @@ -18,9 +18,9 @@ export const builder = { }, }; -export const handler = (argv: Arguments) => { +export const handler = async (argv: Arguments) => { const request = new UnbanRequest(); request.setNodeIdentifier(argv.node_identifier); request.setReconnect(argv.reconnect); - loadXudClient(argv).unban(request, callback(argv)); + (await loadXudClient(argv)).unban(request, callback(argv)); }; diff --git a/lib/cli/commands/unlock.ts b/lib/cli/commands/unlock.ts index 29b17f74a..e3e6ae82c 100644 --- a/lib/cli/commands/unlock.ts +++ b/lib/cli/commands/unlock.ts @@ -26,12 +26,12 @@ export const handler = (argv: Arguments) => { }); process.stdout.write('Enter master xud password: '); - rl.question('', (password) => { + rl.question('', async (password) => { process.stdout.write('\n'); rl.close(); const request = new UnlockNodeRequest(); request.setPassword(password); - 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.unlockNode(request, callback(argv, formatOutput)); diff --git a/lib/cli/placeorder.ts b/lib/cli/placeorder.ts index 166833260..8509f3afc 100644 --- a/lib/cli/placeorder.ts +++ b/lib/cli/placeorder.ts @@ -43,7 +43,7 @@ export const placeOrderBuilder = (argv: Argv, side: OrderSide) => { .example(`$0 ${command} 10 ZRX/GNT market`, `place a market order to ${command} 10 ZRX for GNT`); }; -export const placeOrderHandler = (argv: Arguments, side: OrderSide) => { +export const placeOrderHandler = async (argv: Arguments, side: OrderSide) => { const request = new PlaceOrderRequest(); const numericPrice = Number(argv.price); @@ -74,8 +74,9 @@ export const placeOrderHandler = (argv: Arguments, side: OrderSide) => { request.setReplaceOrderId(argv.replace_order_id); } + const client = await loadXudClient(argv); if (argv.stream) { - const subscription = loadXudClient(argv).placeOrder(request); + const subscription = client.placeOrder(request); let noMatches = true; subscription.on('data', (response: PlaceOrderEvent) => { if (argv.json) { @@ -108,7 +109,7 @@ export const placeOrderHandler = (argv: Arguments, side: OrderSide) => { console.error(err.message); }); } else { - loadXudClient(argv).placeOrderSync(request, callback(argv, formatPlaceOrderOutput)); + client.placeOrderSync(request, callback(argv, formatPlaceOrderOutput)); } };