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/subscribealerts #1997

Merged
merged 32 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ba04193
feat: removeorder output changed to a more meaningful message (#1526)
rsercano Oct 23, 2020
1dc4425
Merge remote-tracking branch 'origin/master' into feature/removeorder…
rsercano Oct 27, 2020
8483e1e
fix(p2p): don't reconnect peers when pool closed (#1965)
sangaman Oct 28, 2020
aefdce8
feat(lnd): change gRPC client options
Oct 26, 2020
6599d88
fix(connext): not enough balance for closechannel (#1963)
sangaman Oct 28, 2020
d458745
feat: reserved capacity checks on PlaceOrder (#1949)
sangaman Oct 28, 2020
a0cc6d7
fix(cli): openchannel assertion error for string amount (#1950)
rsercano Nov 1, 2020
a1287dd
feat(swapclient): auto init wallets on xud unlock (#1973)
sangaman Nov 3, 2020
fc83823
feat(rpc): runtime addcurrency for lnd & connext (#1746)
sangaman Nov 5, 2020
721e098
refactor(rpc): rename reserved TradingLimits fields
sangaman Oct 21, 2020
17e40de
fix(rpc): no success if no channels to close (#1689) (#1942)
rsercano Nov 6, 2020
9633678
fix: tls certificate check on startup (#1510)
rsercano Oct 27, 2020
74a59dc
Merge pull request #1980 from ExchangeUnion/rename-trading-limits
Nov 6, 2020
d537206
fix: alias missing in streamorders (#1725) (#1962)
rsercano Nov 8, 2020
3fb5f18
Merge pull request #1966 from ExchangeUnion/feat/lnd-grpc-client-options
Nov 9, 2020
f0b2ac9
Merge pull request #1964 from ExchangeUnion/fix/check-tls-on-startup
Nov 9, 2020
98770f0
Rebased
rsercano Nov 9, 2020
5746516
fix(lnd): handling hold invoice check errors (#1969)
sangaman Nov 9, 2020
c1af693
Merge pull request #1958 from ExchangeUnion/feature/removeorder-outpu…
Nov 9, 2020
51c60dc
feat(lnd): SendPaymentV2
sangaman Oct 30, 2020
0218553
Merge pull request #1971 from ExchangeUnion/lnd/sendpaymentv2
sangaman Nov 10, 2020
1eabf6d
Revert "feat: removeorder output changed to a more meaningful message…
Nov 10, 2020
1105151
Merge pull request #1985 from ExchangeUnion/revert-1958-feature/remov…
Nov 10, 2020
7c6a226
fix: use regtest instead of regnet arg
sangaman Nov 10, 2020
b1020c1
fix(lnd): don't calculate negative capacities
sangaman Nov 10, 2020
298d1a1
Merge pull request #1988 from ExchangeUnion/fix/lnd-capacities-calc
sangaman Nov 11, 2020
874c4d5
Merge pull request #1987 from ExchangeUnion/fix/regtest-arg
sangaman Nov 11, 2020
a448e24
feat: new grpc call for subscribring alerts such as low balance (#864)
rsercano Nov 8, 2020
72fdb85
fixed reviews and rebased from master
rsercano Nov 18, 2020
43e7c8e
fix: changes removeorder output to a more meaningful message (#1526) …
rsercano Nov 18, 2020
fda6cb2
rebased
rsercano Nov 18, 2020
3e2e86c
Merge branch 'vect2' into feat/subscribealerts
raladev Nov 19, 2020
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
4 changes: 2 additions & 2 deletions bin/xud
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const { argv } = require('yargs')
type: 'boolean',
default: undefined,
},
regnet: {
describe: 'Whether to run XUD on regnet',
regtest: {
describe: 'Whether to run XUD on regtest',
type: 'boolean',
default: undefined,
},
Expand Down
86 changes: 82 additions & 4 deletions docs/api.md

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

12 changes: 6 additions & 6 deletions lib/Xud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ class Xud extends EventEmitter {
nodeKey = await NodeKey.generate();
await nodeKey.toFile(nodeKeyPath);
}

// we need to initialize connext every time xud starts, even in noencrypt mode
// the call below is in lieu of the UnlockNode/CreateNode call flow
await this.swapClientManager.initConnext(
nodeKey.childSeed(SwapClientType.Connext),
);
} else if (this.rpcServer) {
this.rpcServer.grpcService.locked = true;
const initService = new InitService(this.swapClientManager, nodeKeyPath, nodeKeyExists, this.config.dbpath);
Expand Down Expand Up @@ -194,12 +200,6 @@ class Xud extends EventEmitter {
// wait for components to initialize in parallel
await Promise.all(initPromises);

// We initialize Connext separately because it
// requires a NodeKey.
await this.swapClientManager.initConnext(
nodeKey.childSeed(SwapClientType.Connext),
);

// initialize pool and start listening/connecting only once other components are initialized
await this.pool.init();

Expand Down
16 changes: 14 additions & 2 deletions lib/cli/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const loadXudConfig = async (argv: Arguments<any>) => {
}
};

const getTlsCert = (certPath: string) => {
try {
return fs.readFileSync(certPath);
} catch (err) {
if (err.code === 'ENOENT') {
throw `tls cert could not be found at ${certPath}, it may take several seconds to be created on xud's first run`;
}

throw err;
}
};

/**
* A generic function to instantiate an XU client.
* @param argv the command line arguments
Expand All @@ -46,7 +58,7 @@ export const loadXudClient = async (argv: Arguments<any>) => {
await loadXudConfig(argv);

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

return new XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
Expand All @@ -56,7 +68,7 @@ export const loadXudInitClient = async (argv: Arguments<any>) => {
await loadXudConfig(argv);

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

return new XudInitClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/commands/openchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const builder = (argv: Argv) => argv
.example('$0 openchannel ETH 0.5', 'deposit 0.5 into an ETH Connext channel without specifying a remote node');

export const handler = async (argv: Arguments<any>) => {
if (isNaN(argv.amount)) {
throw 'amount must be a number';
}

const request = new OpenChannelRequest();
if (argv.node_identifier) {
request.setNodeIdentifier(argv.node_identifier);
Expand Down
18 changes: 15 additions & 3 deletions lib/cli/commands/removeorder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Arguments, Argv } from 'yargs';
import { RemoveOrderRequest } from '../../proto/xudrpc_pb';
import { RemoveOrderRequest, RemoveOrderResponse } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';
import { coinsToSats } from '../utils';
import { coinsToSats, satsToCoinsStr } from '../utils';

export const command = 'removeorder <order_id> [quantity]';

Expand All @@ -18,11 +18,23 @@ export const builder = (argv: Argv) => argv
.example('$0 removeorder 79d2cd30-8a26-11ea-90cf-439fb244cf44', 'remove an order by id')
.example('$0 removeorder 79d2cd30-8a26-11ea-90cf-439fb244cf44 0.1', 'remove 0.1 quantity from an order by id');

const displayOutput = (orderId: string, removeOrderResponse: RemoveOrderResponse.AsObject) => {
const removedCurrency = removeOrderResponse.pairId.split('/')[0];
if (removeOrderResponse.quantityOnHold === 0 && removeOrderResponse.remainingQuantity === 0) {
console.log(`Order ${orderId} successfully removed.`);
} else {
console.log(`
Order ${orderId} partially removed, remaining quantity: \
${satsToCoinsStr(removeOrderResponse.remainingQuantity)} ${removedCurrency}, \
on hold: ${satsToCoinsStr(removeOrderResponse.quantityOnHold)} ${removedCurrency}`);
}
};

export const handler = async (argv: Arguments<any>) => {
const request = new RemoveOrderRequest();
request.setOrderId(argv.order_id);
if (argv.quantity) {
request.setQuantity(coinsToSats(argv.quantity));
}
(await loadXudClient(argv)).removeOrder(request, callback(argv));
(await loadXudClient(argv)).removeOrder(request, callback(argv, displayOutput.bind(undefined, argv.order_id)));
};
27 changes: 3 additions & 24 deletions lib/cli/commands/streamorders.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ServiceError, status } from 'grpc';
import { Arguments, Argv } from 'yargs';
import { XudClient } from '../../proto/xudrpc_grpc_pb';
import * as xudrpc from '../../proto/xudrpc_pb';
import { setTimeoutPromise } from '../../utils/utils';
import { loadXudClient } from '../command';
import { onStreamError, waitForClient } from '../utils';

export const command = 'streamorders [existing]';

Expand All @@ -26,20 +25,8 @@ const ensureConnection = async (argv: Arguments, printError?: boolean) => {
if (!client) {
client = await loadXudClient(argv);
}
client.waitForReady(Date.now() + 3000, (error: Error | null) => {
if (error) {
if (error.message === 'Failed to connect before the deadline') {
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`);
process.exit(1);
}

if (printError) console.error(`${error.name}: ${error.message}`);
setTimeout(ensureConnection.bind(undefined, argv, printError), 3000);
} else {
console.log('Successfully connected, subscribing for orders');
streamOrders(argv);
}
});
waitForClient(client, argv, ensureConnection, streamOrders, printError);
};

const streamOrders = (argv: Arguments<any>) => {
Expand All @@ -57,15 +44,7 @@ const streamOrders = (argv: Arguments<any>) => {
// 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', async (err: ServiceError) => {
if (err.code === status.UNIMPLEMENTED) {
console.error("xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again");
process.exit(1);
}
console.warn(`Unexpected error occured: ${err.message}, reconnecting in 1 second`);
await setTimeoutPromise(1000);
await ensureConnection(argv);
});
ordersSubscription.on('error', onStreamError.bind(undefined, ensureConnection.bind(undefined, argv)));

const swapsRequest = new xudrpc.SubscribeSwapsRequest();
swapsRequest.setIncludeTaker(true);
Expand Down
51 changes: 51 additions & 0 deletions lib/cli/commands/subscribealerts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Arguments, Argv } from 'yargs';
import { XudClient } from '../../proto/xudrpc_grpc_pb';
import * as xudrpc from '../../proto/xudrpc_pb';
import { loadXudClient } from '../command';
import { AlertType } from '../../constants/enums';
import { onStreamError, waitForClient } from '../utils';

export const command = 'subscribealerts';

export const describe = 'subscribe alerts such as low balance';

export const builder = (argv: Argv) => argv
.option('pretty', {
type: 'boolean',
})
.example('$0 subscribealerts -j', 'prints alert payload in a JSON structure')
.example('$0 subscribealerts', 'prints alert message only');

export const handler = async (argv: Arguments) => {
await ensureConnection(argv, true);
};

let client: XudClient;

const ensureConnection = async (argv: Arguments, printError?: boolean) => {
if (!client) {
client = await loadXudClient(argv);
}

waitForClient(client, argv, ensureConnection, subscribeAlerts, printError);
};

const subscribeAlerts = (argv: Arguments<any>) => {
const request = new xudrpc.SubscribeAlertsRequest();
const alertsSubscription = client.subscribeAlerts(request);

alertsSubscription.on('data', (alert: xudrpc.Alert) => {
if (argv.json) {
console.log(JSON.stringify(alert, undefined, 2));
} else {
console.log(`${AlertType[alert.getType()]}: ${alert.getMessage()}`);
}
});
alertsSubscription.on('end', reconnect.bind(undefined, argv));
alertsSubscription.on('error', onStreamError.bind(undefined, ensureConnection.bind(undefined, argv)));
};

const reconnect = async (argv: Arguments) => {
console.log('Stream has closed, trying to reconnect');
await ensureConnection(argv, false);
};
Loading