forked from alexbosworth/balanceofsatoshis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bos
executable file
·1833 lines (1749 loc) · 75.7 KB
/
bos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
const {lstat} = require('fs');
const {mkdir} = require('fs');
const {readdir} = require('fs');
const {readFile} = require('fs');
const {rmdir} = require('fs');
const {spawn} = require('child_process');
const {statSync} = require('fs');
const {unlink} = require('fs');
const {writeFile} = require('fs');
const importLazy = require('import-lazy')(require);
const fetch = importLazy('@alexbosworth/node-fetch');
const fiat = importLazy('@alexbosworth/fiat');
const inquirer = importLazy('inquirer');
const lnService = importLazy('ln-service');
const lnSync = importLazy('ln-sync');
const paidServices = importLazy('paid-services');
const prog = require('@alexbosworth/caporal');
const commandConstants = require('./commands/constants');
const {accountingCategories} = commandConstants;
const balances = importLazy('./balances');
const chain = importLazy('./chain');
const commands = importLazy('./commands');
const display = importLazy('./display');
const encryption = importLazy('./encryption');
const lnd = importLazy('./lnd');
const network = importLazy('./network');
const nodes = importLazy('./nodes');
const {peerSortOptions} = commandConstants;
const peers = importLazy('./peers');
const {priceProviders} = commandConstants;
const {rateProviders} = commandConstants;
const responses = importLazy('./responses');
const routing = importLazy('./routing');
const services = importLazy('./services');
const {swapTypes} = commandConstants;
const swaps = importLazy('./swaps');
const telegram = importLazy('./telegram');
const wallets = importLazy('./wallets');
const {version} = importLazy('./package');
const {BOOL} = prog;
const collect = arr => [].concat(...[arr]).filter(n => !!n);
const {exit} = process;
const flatten = arr => [].concat(...arr);
const {FLOAT} = prog;
const hexMatch = /^[0-9a-f]+$/i;
const {INT} = prog;
const {keys} = Object;
const lndForNode = (logger, node) => lnd.authenticatedLnd({logger, node});
const months = [...Array(12).keys()].map(n => ++n);
const {REPEATABLE} = prog;
const {STRING} = prog;
const yearMatch = /^\d{4}$/;
prog
.version(version)
// Get accounting information
.command('accounting', 'Get an accounting rundown')
.argument('<category>', 'Report category', keys(accountingCategories))
.help(`Categories: ${keys(accountingCategories).join(', ')}`)
.help(`Rate providers: ${rateProviders.join(', ')}`)
.help('Privacy note: this requests tx related data from third parties')
.option('--csv', 'Output a CSV')
.option('--disable-fiat', 'Avoid looking up fiat conversions for records')
.option('--month <month>', 'Show only records for specific month', months)
.option('--node <node_name>', 'Get details from named node')
.option('--rate-provider <rate_provider>', 'Rate provider', rateProviders)
.option('--year <year>', 'Show only records for specified year', yearMatch)
.action((args, options, logger) => {
const table = !!options.csv ? null : 'rows';
return new Promise(async (resolve, reject) => {
try {
return balances.getAccountingReport({
category: args.category,
is_csv: !!options.csv,
is_fiat_disabled: options.disableFiat,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
month: options.month,
node: options.node,
rate_provider: options.rateProvider,
request: commands.simpleRequest,
year: options.year,
},
responses.returnObject({logger, reject, resolve, table}));
} catch (err) {
return reject(err);
}
});
})
// Advertise to other nodes on the network
.command('advertise', 'Broadcast advertisement')
.help('use --filter conditions to limit broadcast scope: capacity > 1*m')
.help('--filter variables: CAPACITY/CHANNELS_COUNT/K/M')
.help('Default filter scope: channels_count > 9')
.help('To only advertise to direct peers use --max-hops 0')
.help('To avoid advertising to direct peers use --min-hops 1')
.option('--budget <budget>', 'Spending amount to allow for advertising', INT)
.option('--dryrun', 'Avoid actually sending advertisements')
.option('--filter <expression>', 'Require node match condition', REPEATABLE)
.option('--max-hops <max_hops>', 'Maximum number of relaying nodes', INT)
.option('--message <message>', 'Custom advertisement message')
.option('--min-hops <min_hops>', 'Minimum number of relaying nodes', INT)
.option('--node <node_name>', 'Advertise via saved node')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return await services.advertise({
logger,
budget: options.budget || undefined,
filters: flatten([options.filter].filter(n => !!n)),
is_dry_run: !!options.dryrun,
lnd: (await lndForNode(logger, options.node)).lnd,
message: options.message,
max_hops: options.maxHops,
min_hops: options.minHops,
});
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Direct autopilot to mirror one or more nodes on the network
.command('autopilot', 'Enable autopilot')
.visible(false)
.argument('<status>', 'Status of autopilot', ['off', 'on'])
.help('Autopilot status is either on or off')
.help('Mirroring and urls require lnd --autopilot.heuristic=externalscore:1')
.option('--dryrun', 'Show scoring without changing autopilot settings')
.option('--mirror <pubkey>', 'Mirror channels of node', REPEATABLE)
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Set autopilot on saved node')
.option('--url <url>', 'Follow nodes from a scoring URL', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return network.setAutopilot({
is_dry_run: !!options.dryrun,
is_enabled: args.status === 'on',
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
mirrors: flatten([options.mirror].filter(n => !!n)),
node: options.node,
request: commands.simpleRequest,
urls: flatten([options.url].filter(n => !!n)),
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Get local balance information
.command('balance', 'Get total tokens')
.help('Sums balances on-chain, in channels, and pending, plus commit fees')
.option('--above <tokens>', 'Return tokens above watermark', INT)
.option('--below <tokens>', 'Return tokens below watermark', INT)
.option('--confirmed', 'Return confirmed funds only')
.option('--detailed', 'Return detailed balance information')
.option('--node <node_name>', 'Node to get balance for')
.option('--offchain', 'List only off-chain tokens')
.option('--onchain', 'List only on-chain tokens')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
const {lnd} = await lndForNode(logger, options.node);
// Exit early when detailed balance details are requested
if (!!options.detailed) {
return balances.getDetailedBalance({
lnd,
is_confirmed: options.confirmed,
},
responses.returnObject({logger, reject, resolve}));
}
return balances.getBalance({
lnd,
above: options.above,
below: options.below,
is_confirmed: !!options.confirmed,
is_offchain_only: !!options.offchain,
is_onchain_only: !!options.onchain,
},
responses.returnNumber({logger, reject, resolve, number: 'balance'}));
} catch (err) {
return reject(err);
}
});
})
// Broadcast a chain transaction
.command('broadcast', 'Submit a signed transaction to the mempool')
.argument('<tx>', 'Signed raw transaction')
.option('--description <description>', 'Describe the transaction being sent')
.option('--node <node_name>', 'Node to submit transaction on')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return lnSync.broadcastTransaction({
logger,
description: options.description,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
transaction: args.tx,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Call API directly
.command('call', 'Make a raw API call and to get a raw API response')
.help('If you do not specify a method it will list the supported methods')
.argument('[method]', 'Method to call')
.option('--node <node_name>', 'Saved node to use for call')
.option('--param <param>', 'query encoded name=value', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return commands.callRawApi({
logger,
ask: (n, cbk) => inquirer.prompt([n]).then(n => cbk(null, n)),
lnd: (await lndForNode(logger, options.node)).lnd,
method: args.method,
params: flatten([options.param].filter(n => !!n)),
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Get the number of days until the RPC cert expires
.command('cert-validity-days', 'Number of days until the cert is invalid')
.option('--below <number_of_days>', 'Return number of days below mark', INT)
.option('--node <node_name>', 'Node to check cert on')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
return lnd.getCertValidityDays({
logger,
below: options.below,
node: options.node,
},
responses.returnNumber({logger, reject, resolve, number: 'days'}));
});
})
// Deposit coins
.command('chain-deposit', 'Deposit coins in the on-chain wallet')
.argument('[amount]', 'Amount to receive', INT)
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Node to deposit coins to')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return chain.getDepositAddress({
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
tokens: args.amount,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Get the current chain fee rates
.command('chainfees', 'Get the current chain fee estimates')
.help('Lookup chain fee estimates at various confirm targets')
.option('--blocks <depth>', 'Blocks confirm target depth to estimate to')
.option('--file <path>', 'Write the output to a JSON file at desired path')
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Node to get chain fees view from')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return chain.getChainFees({
blocks: options.blocks,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
},
responses.returnObject({
logger,
reject,
resolve,
file: options.file,
write: writeFile,
}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Change the capacity of an existing channel
.command('change-channel-capacity', 'Change the capacity of a channel')
.help('Remote node must be prepared to receive this type of request')
.help('The remote node should also run this same command after you propose')
.option('--node <node_name>', 'Use saved node details instead of local node')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
const {lnd} = await lndForNode(logger, options.node);
const saved = await nodes.manageSavedNodes({
logger,
spawn,
ask: (n, cbk) => inquirer.prompt([n]).then(res => cbk(res)),
fs: {
writeFile,
getDirectoryFiles: readdir,
getFile: readFile,
getFileStatus: lstat,
makeDirectory: mkdir,
removeDirectory: rmdir,
removeFile: unlink,
},
is_including_lnd_api: true,
lock_credentials_to: [],
network: (await lnSync.getNetwork({lnd})).network,
});
return paidServices.changeChannelCapacity({
lnd,
logger,
ask: (n, cbk) => inquirer.prompt([n]).then(res => cbk(res)),
nodes: saved.nodes,
},
responses.returnObject({exit, logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Show a chart of chain fees paid
.command('chart-chain-fees', 'Get a chart of chain fee expenses')
.help('Show chart of mining fee expenditure over time')
.help('Privacy note: this requests tx data from third parties')
.option('--days <days>', 'Chart over the past number of days', INT, 60)
.option('--no-color', 'Disable colors')
.option('--node <node_name>', 'Chain fees from saved node(s)', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return routing.getChainFeesChart({
days: options.days,
is_monochrome: !!options.noColor,
lnds: (await lnd.getLnds({logger, nodes: options.node})).lnds,
request: commands.simpleRequest,
},
responses.returnChart({logger, reject, resolve, data: 'data'}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Show a chart of fees earned
.command('chart-fees-earned', 'Get a chart of earned routing fees')
.argument('[via]', 'Routing fees earned via a specified node or tag')
.help('Show the routing fees earned')
.option('--count', 'Show count of forwards instead of fees earned')
.option('--days <days>', 'Chart fees over the past number of days', INT, 60)
.option('--forwarded', 'Show amount forwarded instead of fees earned')
.option('--node <node_name>', 'Get saved node fees earned', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return routing.getFeesChart({
days: options.days,
fs: {getFile: readFile},
is_count: options.count,
is_forwarded: options.forwarded,
lnds: (await lnd.getLnds({logger, nodes: options.node})).lnds,
via: args.via || undefined,
},
responses.returnChart({logger, reject, resolve, data: 'data'}));
} catch (err) {
return reject(err);
}
});
})
// Show a chart of routing fees paid
.command('chart-fees-paid', 'Get a chart of paid routing fees')
.help('Show the routing fees paid to forwarding nodes')
.help('--rebalances can return results much more quickly')
.option('--days <days>', 'Chart fees over the past number of days', INT, 60)
.option('--in <public_key>', 'Fees paid on routes in node with public key')
.option('--most-fees', 'View table of fees paid per node')
.option('--most-forwarded', 'View table of forwarded per node')
.option('--network', 'Show only non-peers in table view')
.option('--node <node_name>', 'Get fees chart for saved node(s)', REPEATABLE)
.option('--out <public_key>', 'Fees paid on routes out peer with public key')
.option('--peers', 'Show only peers in table view')
.option('--rebalances', 'Only consider fees paid in self-to-self transfers')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
const data = 'data';
const table = 'rows';
const asTable = responses.returnObject({logger, reject, resolve, table});
const chart = responses.returnChart({data, logger, reject, resolve});
try {
return routing.getFeesPaid({
days: options.days,
fs: {getFile: readFile},
in: options.in,
is_most_fees_table: options.mostFees,
is_most_forwarded_table: options.mostForwarded,
is_network: options.network,
is_peer: options.peers,
is_rebalances_only: options.rebalances,
lnds: (await lnd.getLnds({logger, nodes: options.node})).lnds,
out: options.out,
},
(options.mostFees || options.mostForwarded) ? asTable : chart);
} catch (err) {
return reject(err);
}
});
})
// Chart earnings from payments received
.command('chart-payments-received', 'Get a chart of received payments')
.help('Show chart for settled invoices from external parties')
.option('--days <days>', 'Chart over the past number of days', INT, 60)
.option('--node <node_name>', 'Get payments from saved node(s)', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return wallets.getReceivedChart({
days: options.days,
lnds: (await lnd.getLnds({logger, nodes: options.node})).lnds,
},
responses.returnChart({logger, reject, resolve, data: 'data'}));
} catch (err) {
return reject(err);
}
});
})
// Clean out failed payments
.command('clean-failed-payments', 'Clean out past failed payment data')
.help('Remove old failed payment data for probes and other failed payments')
.option('--dryrun', 'Avoid actually deleting the failed payment record')
.option('--node <node_name>', 'Clean failed payments on a saved node')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return wallets.cleanFailedPayments({
logger,
is_dry_run: !!options.dryrun,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Determine the outcomes of channel closings
.command('closed', 'Get the status of a channel closings')
.help('Channel closes with chain-transaction derived resolution details')
.help('Privacy note: this requests tx data from third parties')
.option('--limit [limit]', 'Limit of closings to get', INT, 20)
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Get channel closes from saved node')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return chain.getChannelCloses({
limit: options.limit,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
request: commands.simpleRequest,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Export LND credentials
.command('credentials', 'Export local credentials')
.help('Output encrypted remote access credentials. Use with "nodes --add"')
.option('--cleartext', 'Output remote access credentials without encryption')
.option('--days <days>', 'Expiration days for credentials', INT, 365)
.option('--method <method_name>', 'White-list specific method', REPEATABLE)
.option('--node <node_name>', 'Get credentials for a saved node')
.option('--nospend', 'Credentials do not include spending privileges')
.option('--readonly', 'Credentials only include read permissions')
.action((args, options, logger) => {
return new Promise((resolve, reject) => {
return lnd.getCredentials({
logger,
ask: (n, cbk) => inquirer.prompt([n]).then(res => cbk(res)),
expire_days: options.days,
is_cleartext: options.cleartext,
is_nospend: options.nospend,
is_readonly: options.readonly,
methods: flatten([options.method].filter(n => !!n)),
node: options.node,
},
responses.returnObject({logger, reject, resolve}));
});
})
// Decrypt a message
.command('decrypt', 'Decrypt data using the node key')
.visible(false)
.argument('<encrypted>', 'Encrypted message')
.help('Decrypt a message encrypted to the node key or to another node key')
.option('--node <node_name>', 'Node to decrypt with')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return encryption.decryptWithNode({
encrypted: args.encrypted,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Delete all payments
.command('delete-payments-history', 'Delete all records of past payments')
.visible(false)
.option('--node <node_name>', 'Node to delete all past payments on')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return lnService.deletePayments({
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Encrypt a message
.command('encrypt', 'Encrypt data using the node key')
.visible(false)
.help('Encrypt a message to the node key or to another node key')
.option('--node <node_name>', 'Node to encrypt with')
.option('--message <message>', 'Text message to encrypt')
.option('--to <to>', 'Encrypt message to another node')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return encryption.encryptToNode({
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
message: options.message,
to: options.to,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Fan out utxos
.command('fanout', 'Fan out utxos')
.visible(false)
.argument('<size>', 'UTXO minimum size', INT)
.argument('<count>', 'Desired number of total utxos', INT)
.help('Make a bunch of utxos by making a tx with a bunch of outputs')
.option('--confirmed', 'Only consider confirmed existing utxos')
.option('--dryrun', 'Execute a fan-out dry run')
.option('--feerate <feerate>', 'Feerate in per vbyte rate', INT)
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Node to do fan out for')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return chain.splitUtxos({
count: args.count,
is_confirmed: !!options.confirmed,
is_dry_run: !!options.dryrun,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
size: args.size,
tokens_per_vbyte: options.feerate,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Show and set outbound fee rates
.command('fees', 'Show and adjust outbound fee rates')
.help('List out fee rates, fix problems with routing policies, set out fees')
.help('When setting fee, if channels pending, will wait for confirm to set')
.help('Set-fee-rate can use formulas: https://formulajs.info/functions/')
.help('Specify PERCENT(0.00) to set the fee as a fraction of routed amount')
.help('Specify BIPS() to set the fee as parts per thousand')
.help('You can use INBOUND and OUTBOUND in formulas for IF formulas')
.help('You can use INBOUND_FEE_RATE to mirror an inbound fee')
.help('You can use FEE_RATE_OF_<PUBKEY> to reference other node rates')
.option('--node <node_name>', 'Saved node (not peer to set fees on)')
.option('--set-cltv-delta <count>', 'Set the number of blocks for CLTV', INT)
.option('--set-fee-rate <rate>', 'Fee in parts per million or use a formula')
.option('--to <peer>', 'Peer key/alias/tag to set fees', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return routing.adjustFees({
logger,
cltv_delta: options.setCltvDelta,
fee_rate: options.setFeeRate,
fs: {getFile: readFile},
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
to: flatten([options.to].filter(n => !!n)),
},
responses.returnObject({logger, reject, resolve, table: 'rows'}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Query the node to search for something
.command('find', 'Find a record')
.help('Look for something in the node db that matches a query')
.argument('<query>', 'Query for a record')
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Node to find record on')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return lnd.findRecord({
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
query: args.query,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Get forwards
.command('forwards', 'Show recent forwarding earnings')
.help('Peers where routing has taken place from inbound and outbound sides')
.help('Sorts: earned_in/earned_out/earned_total/inbound/liquidity/outbound')
.option('--complete', 'Show complete set of records in non table view')
.option('--days <days>', 'Number of past days to evaluate', INT)
.option('--in <from>', 'Forwards that originated from a specific peer')
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Node to get forwards for')
.option('--out <to>', 'Forwards that sent out to a specified peer')
.option('--sort <type>', 'Sort forward-active peers by earnings/liquidity')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
const {lnd} = await lndForNode(logger, options.node);
const table = !!options.complete ? undefined : 'rows';
return network.getForwards({
lnd,
days: options.days,
from: (await lnSync.findKey({lnd, query: options.in})).public_key,
fs: {getFile: readFile},
is_monochrome: !!options.noColor,
is_table: !options.complete,
sort: options.sort || undefined,
to: (await lnSync.findKey({lnd, query: options.out})).public_key,
},
responses.returnObject({logger, reject, resolve, table}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Fund and sign a chain transaction
.command('fund', 'Make a signed transaction spending on-chain funds')
.help('Use LND UTXOs to craft a signed raw transaction sending to addresses')
.help('Specify <address> <amount> <address> <amount> for addresses, amounts')
.help('Amounts support formulas, use MAX to reference selected UTXOs total')
.help('--utxo can be specified multiple times to spend multiple UTXOs')
.argument('<address_amount...>', 'Address and amount to send')
.option('--dryrun', 'Avoid locking up UTXOs')
.option('--fee-rate <fee>', 'Per vbyte fee rate for on-chain tx fee', INT)
.option('--node <node_name>', 'Node to spend coins')
.option('--select-utxos', 'Specify UTXOs to spend interactively from a list')
.option('--utxo <outpoint>', 'Spend a specific tx_id:vout', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return chain.fundTransaction({
logger,
addresses: args.addressAmount.filter((n, i) => !(i % 2)),
amounts: args.addressAmount.filter((n, i) => i % 2),
ask: (n, cbk) => inquirer.prompt([n]).then(res => cbk(res)),
fee_tokens_per_vbyte: options.feeRate,
is_dry_run: !!options.dryrun,
is_selecting_utxos: options.selectUtxos,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
utxos: flatten([options.utxo].filter(n => !!n)),
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Gateway service
.command('gateway', 'Request gateway for https://ln-operator.github.io/')
.help('Start LND gateway server listening for Web UI access')
.help('Using the --remote option generates credentials for a remote gateway')
.option('--node <node_name>', 'Node for gateway')
.option('--port <port>', 'Port for gateway', INT, 4805)
.option('--remote <url>', 'Output credentials for a remote gateway')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return lnd.gateway({
logger,
credentials: await lnd.lndCredentials({node: options.node}),
port: options.port,
remote: options.remote,
});
} catch (err) {
return reject(err);
}
});
})
// Give a peer some tokens
.command('gift', 'Give a direct peer some free funds off-chain')
.visible(false)
.help('Send some funds to a connected peer')
.argument('<target>', 'Peer to give funds to')
.argument('<amount>', 'Tokens to give', INT)
.option('--node <node_name>', 'Source node to use to pay gift')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
const number = 'gave_tokens';
try {
return network.sendGift({
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
to: args.target,
tokens: args.amount,
},
responses.returnNumber({logger, number, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Get the edges of a given node
.command('graph', 'List out the connections a node has with other nodes')
.argument('<alias_or_public_key>', 'Node in the graph to look up')
.help('--filter variables: AGE/CAPACITY/HOPS/IN_FEE_RATE/OUT_FEE_RATE')
.help('Example: --filter "age<7*144" for connections in the last week')
.option('--filter <formula>', 'Filter formula to apply', REPEATABLE)
.option('--node <node_name>', 'Node to use for lookup')
.option('--sort <sort_connections_by', 'Sort peers by field')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
const {lnd} = await lndForNode(logger, options.node);
return network.getGraphEntry({
lnd,
logger,
filters: flatten([options.filter].filter(n => !!n)),
fs: {getFile: readFile},
query: args.aliasOrPublicKey.trim(),
sort: options.sort,
},
responses.returnObject({logger, reject, resolve, table: 'rows'}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Intercept inbound channel requests and set requirements for inbound opens
.command('inbound-channel-rules', 'Enforce rules for inbound channels')
.help('Rules should be written evaluating to TRUE to accept a channel')
.help('Example rule: --rule "CAPACITY > 100000"')
.help('For formulas: CAPACITIES are the sizes of the peer public channels')
.help('For formulas: CAPACITY is the size of the requested channel open')
.help('For formulas: CHANNEL_AGES are the block ages of public channels')
.help('For formulas: FEE_RATES are the outbound fee rates for the peer')
.help('For formulas: LOCAL_BALANCE is the gifted amount from the peer')
.help('For formulas: PUBLIC_KEY is the public key of the requesting peer')
.option('--coop-close-address', 'Request using a cooperative close address')
.option('--node <node_name>', 'Saved node to reject inbound channels on')
.option('--reason <message>', 'Message to return when rejecting a request')
.option('--rule <formula>', 'Freeform rule for inbound channel', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return await peers.interceptInboundChannels({
logger,
address: options.coopCloseAddress || undefined,
lnd: (await lndForNode(logger, options.node)).lnd,
reason: options.reason,
rules: flatten([options.rule].filter(n => !!n)),
});
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Intercept forwarding requests, enforce requirements on acceptance
.command('limit-forwarding', 'Enforce rules for routing payments')
.help('Setting --only-allow will disable all forwards except only allowed')
.help('--only-allow option can be repeated for multiple forwards')
.option('--node <node_name>', 'Saved node to enforce rules on')
.option('--disable-forwards', 'Disable all forwards')
.option('--max-hours-since-last-block <h>', 'Require fresh blocks', INT, 5)
.option('--max-new-pending-per-hour <h>', 'Limit held HTLCs', INT)
.option('--min-channel-confirmations <confs>', 'Minimum channel confs', INT)
.option('--only-allow <pair>', 'only forward fromKey/toKey', REPEATABLE)
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return await peers.limitForwarding({
logger,
is_disabling_all_forwards: options.disableForwards || undefined,
lnd: (await lndForNode(logger, options.node)).lnd,
max_hours_since_last_block: options.maxHoursSinceLastBlock,
max_new_pending_per_hour: options.maxNewPendingPerHour,
min_channel_confirmations: options.minChannelConfirmations,
only_allow: flatten([options.onlyAllow].filter(n => !!n)),
});
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Get inbound liquidity information: available inbound off-chain tokens
.command('inbound-liquidity', 'Get inbound liquidity size')
.option('--above <tokens>', 'Return amount above watermark', INT)
.option('--below <tokens>', 'Return amount above watermark', INT)
.option('--max-fee-rate <fee_rate>', 'Maximum fee rate to consider', INT)
.option('--node <node_name>', 'Node to get inbound liquidity')
.option('--top', 'Top percentile inbound liquidity in an individual channel')
.option('--with <node_key_or_tag>', 'Liquidity with a specific node/tag')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return balances.getLiquidity({
above: options.above || undefined,
below: options.below || undefined,
fs: {getFile: readFile},
is_top: options.top || undefined,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
max_fee_rate: options.maxFeeRate || undefined,
request: commands.simpleRequest,
with: options.with,
},
responses.returnNumber({logger, reject, resolve, number: 'balance'}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Increase inbound liquidity
.command('increase-inbound-liquidity', 'Increase node inbound liquidity')
.help('Spend down a channel to get inbound. Fee is an estimate, may be more')
.help('If you want to control chain fee increases, use show-raw-recoveries')
.help('Formulas supported for --amount like 5*m or 0.05*BTC for 5 million')
.option('--address <out_address>', 'Out chain address to send funds out to')
.option('--api-key <api_key>', 'Pre-paid API key to use')
.option('--amount <amount>', 'Amount to increase inbound', STRING, '500*k')
.option('--avoid <pubkey/chan/tag>', 'Avoid forwarding through', REPEATABLE)
.option('--confs <confs>', 'Confs to consider reorg safe', INT, 1)
.option('--dryrun', 'Only show cost estimate for increase')
.option('--fast', 'Request swap server avoid batching delay')
.option('--max-deposit <max_deposit>', 'Maximum deposit amount', INT, 5e4)
.option('--max-fee <max_fee>', 'Maximum estimated fee tokens', INT, 3e4)
.option('--max-hours <max_hours>', 'Maximum hours to wait', INT, 65)
.option('--max-paths <max_paths>', 'Maximum paths to attempt', INT, 1)
.option('--no-color', 'Mute all colors')
.option('--node <node_name>', 'Increase inbound liquidity on saved node')
.option('--recovery <recovery>', 'Recover in-progress swap')
.option('--service-socket', 'Specify a custom swap service address')
.option('--spend-address', 'Send an exact amount to a specific address')
.option('--spend-amount', 'Exact amount to send to a specific address')
.option('--show-raw-recovery', 'Show raw recovery transactions')
.option('--with <peer>', 'Public key of peer to increase liquidity from')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return swaps.swapOut({
fetch,
logger,
api_key: options.apiKey || undefined,
avoid: flatten([options.avoid].filter(n => !!n)),
confs: options.confs,
fs: {getFile: readFile},
is_fast: options.fast || false,
is_raw_recovery_shown: options.showRawRecovery || undefined,
is_dry_run: options.dryrun || false,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
max_deposit: options.maxDeposit,
max_fee: options.maxFee,
max_paths: options.maxPaths || undefined,
max_wait_blocks: Math.ceil((options.maxHours) * 60 / 10),
node: options.node || undefined,
out_address: options.address || undefined,
peer: options.with || undefined,
recovery: options.recovery,
request: commands.fetchRequest({fetch}),
socket: options.serviceSocket || undefined,
spend_address: options.spendAddress || undefined,
spend_tokens: options.spendAmount || undefined,
timeout: 1000 * 60 * 60 * 10,
tokens: display.parseAmount({amount: options.amount}).tokens,
},
responses.returnObject({exit, logger, reject, resolve}));
} catch (err) {
return reject(logger.error({err}));
}
});
})
// Increase outbound liquidity
.command('increase-outbound-liquidity', 'Move on-chain funds off-chain')
.help('Open a new channel to add more off-chain liquidity')
.option('--amount <amount>', 'Amount to assign to new channel capacity', INT)
.option('--dryrun', 'Avoid actually opening a channel')
.option('--fee-rate <fee_rate>', 'Use specific fee rate (per vbyte)', FLOAT)
.option('--node <node_name>', 'Increase outbound liquidity on saved node')
.option('--private', 'Mark new channel as private')
.option('--set-fee-rate <ppm>', 'Fee in parts per million or use a formula')
.option('--with <peer_public_key>', 'Select a specific peer to open with')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
return network.openChannel({
logger,
chain_fee_rate: options.feeRate || undefined,
fs: {getFile: readFile},
is_dry_run: options.dryrun,
is_private: options.private,
lnd: (await lnd.authenticatedLnd({logger, node: options.node})).lnd,
peer: options.with || undefined,
request: commands.simpleRequest,
set_fee_rate: options.setFeeRate || undefined,
tokens: options.amount,
},
responses.returnObject({logger, reject, resolve}));
} catch (err) {
return reject(err);
}
});
})
// Get the price for liquidity
.command('liquidity-cost', 'Get the price of liquidity')
.visible(false)
.argument('<type>', 'Liquidity direction', swapTypes)
.argument('<amount>', 'Amount of liquidity to get quote for', INT)
.argument('<api-key>', 'Swap API key to use', hexMatch)
.option('--above <tokens>', 'Return amount above watermark', INT)
.option('--fast', 'Avoid any server batching wait time')
.option('--node <node_name>', 'Node to get liquidity cost')
.option('--service-socket', 'Specify a custom swap service address')
.action((args, options, logger) => {
return new Promise(async (resolve, reject) => {
try {
const {lnd} = await lndForNode(logger, options.node);
const {metadata, service} = await swaps.getPaidService({
fetch,
lnd,
logger,
socket: options.serviceSocket,
token: args.apiKey,