forked from legalizemath/mybosbotexample
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bos.js
1450 lines (1286 loc) · 50.2 KB
/
bos.js
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
/*
Wrapper for balanceofsatoshis installed globally
// Needs node v14+, node -v
Installed with `npm i -g balanceofsatoshis`
Tested with lnd-0.15.0-beta, BoS 12.16.3
Linked via `npm link balanceofsatoshis`
It's unofficial independent wrapper so if anything changes this can break.
(e.g. changes in ln-service or bos functions, parameter names, output)
I just add wrappers here on need-to basis and some parts become abandoned.
*/
import { fetchRequest
//, callRawApi
} from 'balanceofsatoshis/commands/index.js'
import fetch from 'balanceofsatoshis/node_modules/@alexbosworth/node-fetch/lib/index.js'
import { readFile } from 'fs'
import lnd from 'balanceofsatoshis/lnd/index.js'
import lnServiceRaw from 'balanceofsatoshis/node_modules/ln-service/index.js'
import {
adjustFees as bosAdjustFees,
getFeesChart as bosGetFeesChart,
getChainFeesChart as bosGetChainFeesChart,
getFeesPaid as bosGetFeesPaid
} from 'balanceofsatoshis/routing/index.js'
// import { rebalance as bosRebalance } from 'balanceofsatoshis/swaps/index.js'
import { manageRebalance as bosRebalance } from 'balanceofsatoshis/swaps/index.js'
import { getDetailedBalance as bosGetDetailedBalance } from 'balanceofsatoshis/balances/index.js'
import {
pushPayment as bosPushPayment,
reconnect as bosReconnect,
getPeers as bosGetPeers,
getForwards as bosGetForwards
} from 'balanceofsatoshis/network/index.js'
import { SocksProxyAgent } from 'balanceofsatoshis/node_modules/socks-proxy-agent/dist/index.js'
const { trunc, min, ceil, random } = Math
// reused authentication object or making new ones uses up a TON of memory
// re-initialize if node restarts with bos.initializeAuth()
let authed
// this method updates authentication object from global bos authentication
// WARNING:
// calling await lnd.authenticatedLnd({}) each time uses up some RAM
// recalling this method doesn't seem to let go of previous RAM
// # of times this is called must be kept low as each call leaks memory
const mylnd = async () => {
authed = (await lnd.authenticatedLnd({})).lnd
return authed
}
// max MB RAM script can use, above terminated for memory leak
const MAX_RAM_USE_MB = 250
// This ms delay is longest it will ever back off from retrying if auth fails
const MAX_RETRY_DELAY = 21 * 60 * 1000 // 21 minutes
// returns {closing_balance, offchain_balance, offchain_pending, onchain_balance, onchain_vbytes}
const getDetailedBalance = async (choices = {}, log = false) => {
try {
log && logDim(`${getDate()} bos.getDetailedBalance()`)
const res = await bosGetDetailedBalance({
lnd: authed ?? (await mylnd()), // required
...choices
})
log && console.log(`${getDate()} bos.getDetailedBalance() complete`, res)
return removeStyling(res)
} catch (e) {
console.error(`\n${getDate()} bos.getDetailedBalance() aborted:`, e?.message)
return {}
}
}
// returns {description, title, data: []}
const getFeesPaid = async (choices = {}, log = false) => {
try {
log && logDim(`${getDate()} bos.getFeesPaid()`)
const res = await bosGetFeesPaid({
lnds: [authed ?? (await mylnd())], // required
days: 30,
// is_most_forwarded_table: // ?
// is_most_fees_table: // ?
// is_network: // ?
// is_peer: // ?
...choices
})
log && console.log(`${getDate()} bos.getFeesPaid() complete`, res)
return res
} catch (e) {
console.error(`\n${getDate()} bos.getFeesPaid() aborted:`, e?.message)
return {}
}
}
// returns {description, title, data: []}
const getFeesChart = async (choices = {}, log = false) => {
try {
log && logDim(`${getDate()} bos.getFeesChart()`)
const res = await bosGetFeesChart({
lnds: [authed ?? (await mylnd())], // required
days: 30,
is_count: false,
fs: { getFile: readFile },
// via: <public key>
...choices
})
log && console.log(`${getDate()} bos.getFeesChart() complete`, res)
return res
} catch (e) {
console.error(`\n${getDate()} bos.getFeesChart() aborted:`, e?.message)
return {}
}
}
// pay an invoice/request (bolt-11)
/*
{
avoid: [<Avoid Forwarding Through String>]
[fs]: {
getFile: <Read File Contents Function> (path, cbk) => {}
}
[in_through]: <Pay In Through Node With Public Key Hex String>
lnd: <Authenticated LND API Object>
logger: <Winston Logger Object>
max_fee: <Max Fee Tokens Number>
max_paths: <Maximum Paths Number>
[message]: <Message String>
out: [<Out Through Peer With Public Key Hex String>]
request: <BOLT 11 Payment Request String>
}
*/
// returns {description, title, data: []}
const getChainFeesChart = async (choices = {}, log = false) => {
try {
log && logDim(`${getDate()} bos.getChainFeesChart()`)
const res = await bosGetChainFeesChart({
lnds: [authed ?? (await mylnd())], // required
days: 30,
is_monochrome: true,
request,
...choices
})
log && console.log(`${getDate()} bos.getChainFeesChart() complete`, res)
return res
} catch (e) {
console.error(`\n${getDate()} bos.getChainFeesChart() aborted:`, e?.message)
return { data: [] }
}
}
const forwards = async (choices = {}, log = false) => {
try {
log && logDim(`${getDate()} bos.forwards()`)
const res = await bosGetForwards({
lnd: authed ?? (await mylnd()), // required
fs: { getFile: readFile }, // required
days: 1,
// [from: public key]
// [to: public key]
...choices
})
log && console.log(`${getDate()} bos.forwards() complete`, res)
return res.peers
} catch (e) {
console.error(`\n${getDate()} bos.forwards() aborted:`, e?.message)
return []
}
}
const reconnect = async (log = false) => {
try {
log && logDim(`${getDate()} bos.reconnect()`)
const res = await bosReconnect({
lnd: authed ?? (await mylnd())
})
log && console.log(`${getDate()} bos.reconnect() complete`, res)
return res
} catch (e) {
console.error(`\n${getDate()} bos.reconnect() aborted:`, e?.message)
}
}
const rebalance = async (
{ fromChannel, toChannel, maxSats = 1, maxMinutes = 3, maxFeeRate = 1, avoid = [], retryAvoidsOnTimeout = 0 },
choices = {},
log = { details: false, progress: true }
) => {
try {
// change to internal key names, add overwrites in choices
const options = {
out_through: fromChannel, // public key
in_through: toChannel, // public key
max_rebalance: String(trunc(maxSats)), // sats
timeout_minutes: trunc(maxMinutes), // minutes
max_fee_rate: trunc(maxFeeRate), // max fee rate
max_fee: trunc(maxSats * 0.05), // 5% just in case
avoid,
// out_channels: [],
// in_outound: undefined,
// out_inbound: undefined,
...choices
}
log?.details && logDim(`${getDate()} bos.rebalance()`, JSON.stringify(options))
if (fromChannel === toChannel) throw new Error('fromChannel same as toChannel')
const res = await bosRebalance({
fs: { getFile: readFile }, // required
lnd: authed ?? (await mylnd()), // required
logger: logger(log),
out_channels: [], // seems necessary
...options
})
log?.progress && console.log('')
log?.details && console.log(`\n${getDate()} bos.rebalance() success:`, JSON.stringify(res))
const finalFeeRate = +res.rebalance[2]?.rebalance_fee_rate.match(/\((.*)\)/)[1]
// bos just shows down to sats
const finalAmount = trunc(+res.rebalance[2].rebalanced * 1e8)
const feeSpent = trunc(+res.rebalance[2].rebalance_fees_spent * 1e8)
return {
// failed: false,
fee_rate: finalFeeRate, // parts per million spent on fee
rebalanced: finalAmount, // total sats sent
msg: res, // bos response
arrived: finalAmount - feeSpent, // amount arrived at destination
sent: finalAmount, // total sats sent|spent from source
fee: feeSpent // sats paid for fee
// ppmSuggested: null
}
// e.g. {"fee_rate":250,"rebalanced":100025,"msg":{"rebalance":[{"increased_inbound_on":"ZCXZCXCZ","liquidity_inbound":"0.07391729","liquidity_outbound":"0.07607982"},{"decreased_inbound_on":"ASDASDASD","liquidity_inbound":"0.01627758","liquidity_outbound":"0.00722753"},{"rebalanced":"0.00100025","rebalance_fees_spent":"0.00000025","rebalance_fee_rate":"0.03% (250)"}]}}'
} catch (e) {
log?.progress && console.log('')
log?.details && console.error(`\n${getDate()} bos.rebalance() aborted:`, e?.message)
// if we're retrying on timeouts & avoid wasn't used, rerun again with avoid of low fees
if (retryAvoidsOnTimeout && e[1] === 'ProbeTimeout') {
retryAvoidsOnTimeout-- // 1 less retry left now
const oldAvoidPpm = +(avoid[0] || '').match(/FEE_RATE<(.+?)\//)?.[1] || 0
// each new retry moves avoid fee rate 25% closer to half max total fee rate
const newAvoidPpm = trunc(oldAvoidPpm * 0.75 + (maxFeeRate / 2) * 0.25)
const newAvoid = `FEE_RATE<${newAvoidPpm}/${toChannel}`
const pkToAlias = await getPublicKeyToAliasTable()
const alias = ca(pkToAlias[toChannel] || '')
// log?.details &&
logDim(
`${getDate()} Retrying bos.rebalance after ProbeTimeout error @ ${maxFeeRate} with --avoid FEE_RATE<${newAvoidPpm}` +
` to ${alias} ${toChannel.slice(0, 10)}. Retries left: ${retryAvoidsOnTimeout}`
)
// for simplicity will always overwrite or create first item in avoid array
if ((avoid[0] || '').includes('FEE_RATE<')) avoid[0] = newAvoid
else avoid.unshift(newAvoid)
// continue as retry
return await rebalance(
{ fromChannel, toChannel, maxSats, maxMinutes, maxFeeRate, avoid, retryAvoidsOnTimeout },
choices,
log
)
}
// provide suggested ppm if possible
const ppmSuggested = e[1] === 'RebalanceFeeRateTooHigh' ? +e[2].needed_max_fee_rate : null
return {
failed: true,
// fee_rate: null,
// rebalanced: null,
msg: e, // bos response
// arrived, // amount arrived at destination
// sent, // total sats sent|spent from source
// fee, // sats paid for fee
ppmSuggested // fee rate suggested
}
}
}
const send = async (
{
destination, // public key, kind of important
fromChannel = undefined, // public key
toChannel = undefined, // public key
sats = 1, // how much needs to arrive at destination
maxMinutes = 1,
maxFeeRate = undefined, // ppm, rounded up to next sat
// use smaller of these fee limits:
maxFee = undefined, // max fee sats, 1 sat fee per 1 sat arriving somewhere is default max
message = undefined, // string to send (reveals sender when used)
// retryAvoidsOnTimeout = 0
avoid = [],
isRebalance = false, // double checks in/out peers specified to avoid using same for both
is_omitting_message_from = false, // old default to include your key in messages
retryAvoidsOnTimeout = 0
},
log = { details: false, progress: true },
isRetry = false
) => {
try {
const unspecifiedFee = maxFee === undefined && maxFeeRate === undefined
if (unspecifiedFee) throw new Error('need to specify maxFeeRate or maxFee')
maxFee = maxFee ?? ceil(0.1 * sats) // 10% fallback if unspecified
maxFeeRate = maxFeeRate ?? trunc(((1.0 * maxFee) / sats) * 1e6)
const options = {
destination,
out_through: fromChannel,
in_through: toChannel,
amount: String(trunc(sats)),
timeout_minutes: trunc(maxMinutes),
// uses max fee (sats) only so calculated from max fee rate (ppm)
max_fee: min(
ceil((sats * maxFeeRate) / 1e6), // from fee rate rounded up to next sat
maxFee // from max fee in exact sats
),
message,
is_omitting_message_from
}
log?.details && logDim(`${getDate()} bos.send() to ${destination}`, JSON.stringify(options))
if (fromChannel === toChannel && toChannel !== undefined) throw new Error('fromChannel same as toChannel')
if (isRebalance && !(fromChannel && toChannel)) throw new Error('need to specify both "from" and "to" channels')
const res = await bosPushPayment({
lnd: authed ?? (await mylnd()),
logger: logger(log),
fs: { getFile: readFile }, // required
avoid, // required
is_dry_run: false, // required
quiz_answers: [], // required
request,
...options
})
log?.progress && console.log('')
log?.details && console.log(`\n${getDate()} bos.send() success:`, JSON.stringify(res))
const sent = +res.paid
const arrived = +res.paid - +res.fee
const totalFee = +res.fee
return {
// failed: false,
fee_rate: trunc(((1.0 * totalFee) / arrived) * 1e6),
msg: res, // bos info
arrived, // amount arrived at destination
sent, // total sent (spent) including fee
fee: totalFee
// ppmSuggested: null
}
// example of successful payment res
/*
{"fee":1,"id":"aaaaaaaaaaaaaccccccccccccddddddddddeeeeeeeeeeee","latency_ms":17543,"paid":1001,"preimage":"fffffffffffffgggggggggggggghhhhhhhhhhhhhhhhiiiiiiiiiiiii","relays":["030c3f19d742ca294a55c00376b3b355c3c90d61c6b6b39554dbc7ac19b141c14f","0260fab633066ed7b1d9b9b8a0fac87e1579d1709e874d28a0d171a1f5c43bb877","0340796fc55aec99d8f142659cd67e19080100a98ea14e8916525789b57e054eb3","03d1e805c38257b713340049745ff5a15d9ee5d733517a1d48a956815c9482055c"],"success":["696272x1444x1","679020x1484x0","687689x770x1","694601x1655x0"]}
*/
} catch (e) {
log?.progress && console.log('')
log?.details && console.error(`\n${getDate()} bos.send() aborted:`, e?.message)
// just max fee suggestions so convert to ppm
// e.g. [400,"MaxFeeLimitTooLow",{"needed_fee":167}]
// if someone JUST changed fee try again just 1 more time
if (!isRetry && e[1] === 'FeeInsufficient') {
logDim(`\n${getDate()} retrying bos.send just once after FeeInsufficient error`)
return await send(
{
destination,
fromChannel,
toChannel,
sats,
maxMinutes,
maxFeeRate,
maxFee,
message,
avoid,
isRebalance,
retryAvoidsOnTimeout
},
log,
true // mark it as a retry
)
}
// handle timeout retries if used, increment avoid filter each time
// towards half of max fee rate
if (retryAvoidsOnTimeout && e[1] === 'ProbeTimeout') {
// removed && avoid.length <= 1
retryAvoidsOnTimeout-- // 1 less retry left now
const oldAvoidPpm = +(avoid[0] || '').match(/FEE_RATE<(.+?)\//)?.[1] || 0
// each new retry moves avoid fee rate 25% closer to half max total fee rate
const newAvoidPpm = trunc(oldAvoidPpm * 0.75 + (maxFeeRate / 2) * 0.25)
const newAvoid = `FEE_RATE<${newAvoidPpm}/${toChannel}`
const pkToAlias = await getPublicKeyToAliasTable()
const alias = ca(pkToAlias[toChannel] || '')
logDim(
`${getDate()} Retrying bos.send after ProbeTimeout error @ ${maxFeeRate} with --avoid FEE_RATE<${newAvoidPpm}` +
` to ${alias} ${toChannel.slice(0, 10)}. Retries left: ${retryAvoidsOnTimeout}`
)
// for simplicity will always overwrite or create first item in avoid array
if ((avoid[0] || '').includes('FEE_RATE<')) avoid[0] = newAvoid
else avoid.unshift(newAvoid)
// continue as retry
return await send(
{
destination,
fromChannel,
toChannel,
sats,
maxMinutes,
maxFeeRate,
maxFee,
message,
avoid,
isRebalance,
retryAvoidsOnTimeout
},
log,
isRetry
)
}
// sometimes reputations get ruined by broken nodes, helps to reset those rarely
if (e[1] === 'UnexpectedSendPaymentFailure') {
// 1% chance, every 100 on avg
if (random() < 0.01) await callAPI('deleteforwardingreputations')
}
// failed
const suggestedFeeRate = e[1] === 'MaxFeeLimitTooLow' ? ceil(((1.0 * +e[2].needed_fee) / sats) * 1e6) : null
return {
failed: true,
// fee_rate,
msg: e,
// arrived,
// sent,
// fee,
ppmSuggested: suggestedFeeRate
}
}
}
// more accurate name as option
const keysend = send
// keysend specifically for rebalances so regular easier to use for actual sends
const keysendRebalance = (choices, logging, isRetry) => send({ ...choices, isRebalance: true }, logging, isRetry)
// returns new set fee
const setFees = async (peerPubKey, fee_rate, log = false) => {
try {
log && logDim(`${getDate()} bos.setFees()`)
const res = await bosAdjustFees({
fs: { getFile: readFile }, // required
lnd: authed ?? (await mylnd()),
logger: {}, // logger not used
to: [peerPubKey], // array of pubkeys to adjust fees towards
fee_rate: String(fee_rate) // pm rate to set
})
const newFee = res.rows[1][1]?.match(/\((.*)\)/)[1]
log && console.log(`${getDate()} bos.setFees()`, JSON.stringify(res), newFee)
return +newFee
} catch (e) {
console.error(`${getDate()} bos.setFees() aborted:`, e)
return {}
}
}
// helpful wrapper to re-use bos auth if not provided
// also handle errors via try/catch
// returns null on error, otherwise response || empty object
const lnServiceWrapped = {}
for (const cmd in lnServiceRaw) {
lnServiceWrapped[cmd] = async (arg1, ...otherArgs) => {
try {
// if lnd auth was provided use it, otherwise try using bos one
if (typeof arg1 === 'object' && 'lnd' in arg1) {
return (await lnServiceRaw[cmd](arg1, ...otherArgs)) || {}
} else {
const arg1_mod = { ...arg1, lnd: authed ?? (await mylnd()) }
return (await lnServiceRaw[cmd](arg1_mod, ...otherArgs)) || {}
}
} catch (e) {
const argsUsed = [
{ ...arg1, lnd: undefined },
...otherArgs
]
logDim(`${getDate()} wrapped lnService.${cmd}${JSON.stringify(argsUsed)} aborted.`, e?.message)
if (!e?.message) console.error(e)
return null
}
}
}
const lnService = lnServiceWrapped
// just calls lnService directly using bos authorization
// get method spelling, options, and expected output here:
// https://github.com/alexbosworth/ln-service/blob/master/README.md#all-methods
// instead of bos.callAPI('getChannels', { is_public: true })
// can also do bos.lnService.getChannels({ lnd, is_public: true })
// null on error
const callAPI = async (method, choices = {}, log = false) => {
try {
// for compatibility w/ old method, e.g. 'getpeers' in ln-service has to be 'getPeers'
[['getpeers', 'getPeers']].forEach(r => {
if (r[0] === method) method = r[1]
})
log && logDim(`${getDate()} lnService.${method}()`)
// handle bad calls
if (!(method in lnService)) throw new Error(`method ${method} doesn't exist in lnService`)
const res = await lnServiceRaw[method]({
lnd: authed ?? (await mylnd()),
...choices
})
return res || {}
// empty object if nothing good yet without caught errors
} catch (e) {
logDim(`${getDate()} lnService.${method}(), ${JSON.stringify(choices)}) aborted.`, e?.message)
if (!e?.message) console.error(e)
return null
}
}
const call = callAPI
const find = async (query, log = false) => {
try {
log && logDim(`${getDate()} bos.find('${query}')`)
return await lnd.findRecord({
lnd: authed ?? (await mylnd()),
query
})
} catch (e) {
console.error(`${getDate()} bos.find('${query}') aborted.`, e)
return null
}
}
/**
* Direct call to bos peers. Use default filters with peers() or show all with peers({}).
* See source reference for additional filters.
* Reference: https://github.com/alexbosworth/balanceofsatoshis/blob/master/network/get_peers.js
* @param {Object} choices
* @param {Boolean|undefined} [choices[].is_active = true] - show only if has active channels
* @param {Boolean|undefined} [choices[].is_public = true] - show only if has public channels
* @param {Boolean|undefined} [choices[].is_private = undefined] - show only if has private channels
* @param {Boolean|undefined} [choices[].is_offline = undefined] - show only offline peers
* @param {Boolean} [log = false] - log to console
* @returns (Object[]|null) - array of peers or null on error
*/
const peers = async (
choices = {
// defaults
is_active: true, // only connected peers
is_public: true // only public channels
},
log = false
) => {
try {
log && logDim(`${getDate()} bos.peers()`)
const res = await bosGetPeers({
fs: { getFile: readFile }, // required
lnd: authed ?? (await mylnd()), // required
omit: [], // required
...choices
})
const foundPeers =
res?.peers
// convert fee rate to just ppm
?.map(peer => ({
...peer,
inbound_fee_rate: +peer.inbound_fee_rate?.match(/\((.*)\)/)?.[1] || null
})) || null
log && console.log(`${getDate()} bos.peers()`, JSON.stringify(peers, fixJSON, 2))
return foundPeers
/* typical result
{
alias: 'some alias',
fee_earnings: undefined,
downtime_percentage: undefined,
first_connected: '7 months ago',
last_activity: undefined,
inbound_fee_rate: 153,
inbound_liquidity: 3852992,
is_forwarding: undefined,
is_inbound_disabled: undefined,
is_offline: undefined,
is_pending: undefined,
is_private: undefined,
is_small_max_htlc: undefined,
is_thawing: undefined,
outbound_liquidity: 1146107,
public_key: '555555555555555555555555555555555555555555555555'
}
*/
} catch (e) {
console.error(`${getDate()} bos.peers() aborted:`, e)
return null
}
}
// returns {pubkey: my_ppm_fee_rate}
const getFees = async (log = false) => {
try {
log && logDim(`${getDate()} bos.getFees()`)
const res = await bosAdjustFees({
fs: { getFile: readFile }, // required
lnd: authed ?? (await mylnd()),
logger: {}, // logger not used
to: [] // array of pubkeys to adjust fees towards
})
log && console.log(`${getDate()} bos.getFees() result:`, JSON.stringify(res, fixJSON, 2))
const myFees = res.rows
.slice(1) // remove table headers row
.reduce((feeRates, thisPeer) => {
// 3rd column is pubkey
const pubKey = thisPeer[2]
// 2nd column has fee ppm
feeRates[pubKey] = +thisPeer[1].match(/\((.*)\)/)[1]
return feeRates
}, {})
return myFees
} catch (e) {
console.error(`${getDate()} bos.getFees() aborted:`, e)
return null
}
}
// ------------- custom frequently used functions -------------
// get node info, https://github.com/alexbosworth/ln-service#getnode
const getNodeFromGraph = async ({ public_key, is_omitting_channels = true }, log = false) => {
log && logDim(`${getDate()} bos.getNodeFromGraph()`)
try {
const res = await callAPI('getNode', { public_key, is_omitting_channels })
return res
} catch (e) {
console.error(`${getDate()} bos.getNodeFromGraph() aborted:`, e)
return null
}
}
// token looks like adsfasfdsf:adsfsadfasdfasfasdfasfd-asdfsf
// chat_id looks like 1231231231
const sayWithTelegramBot = async ({ token, chat_id, message, proxy, parse_mode = 'HTML' }, log = false) => {
// parse_mode can be undefined, or 'MarkdownV2' or 'HTML'
// https://core.telegram.org/bots/api#html-style
const parseModeString = parse_mode ? `&parse_mode=${parse_mode}` : ''
try {
var endpoint = `https://api.telegram.org/bot${token}/sendMessage?chat_id=${chat_id}&text=${encodeURIComponent(message)}${parseModeString}`
var opts = new URL(endpoint)
if (proxy === "") {
log && logDim(`bos.sayWithTelegramBot()`)
} else {
opts.agent = new SocksProxyAgent(proxy)
log && logDim(`bos.sayWithTelegramBot() using proxy ${proxy}`)
log && logDim(`sayWithTelegramBot(): PROXY=${proxy} URL=${endpoint}`)
}
// const res = https.get(opts, function (res) {})
// return JSON.stringify(res, null, 2)
const res = await fetch(opts)
const fullResponse = await res.json()
log && logDim(`${getDate()} bos.sayWithTelegramBot() result:`, JSON.stringify(fullResponse, null, 2))
return fullResponse
/*
log && logDim(`${getDate()} bos.sayWithTelegramBot()`)
const res = await fetch(
`https://api.telegram.org/bot${token}/sendMessage?chat_id=${chat_id}` +
`&text=${encodeURIComponent(message)}${parseModeString}`
)
const fullResponse = await res.json()
log && logDim(`${getDate()} bos.sayWithTelegramBot() result:`, JSON.stringify(fullResponse, null, 2))
*/
} catch (e) {
console.error(`${getDate()} bos.sayWithTelegramBot() aborted:`, e)
return null
}
}
// bos call getForwards (and calls bos call getChannels)
// and returns by peer: {[public_keys]: [forwards]}
// or by time: [forwards]
// forwards look like this with my changes:
/*
{
created_at: '2021-09-10T14:31:44.000Z',
fee: 34,
fee_mtokens: 34253,
incoming_channel: '689868x588x1',
mtokens: 546018000,
outgoing_channel: '689686x689x1',
tokens: 546018,
created_at_ms: 1631284304000,
outgoing_peer: '03271338633d2d37b285dae4df40b413d8c6c791fbee7797bc5dc70812196d7d5c',
incoming_peer: '037cc5f9f1da20ac0d60e83989729a204a33cc2d8e80438969fadf35c1c5f1233b'
}
*/
const customGetForwardingEvents = async (
{
days = 1, // how many days ago to look back
byInPeer = false, // use in-peers as keys instead of out-peers
timeArray = false, // return as array of time points instead of object
max_minutes_search = 2 // safety if takes too long
} = {},
log = false
) => {
log && logDim(`${getDate()} bos.customGetForwardingEvents()`)
const started = Date.now()
const isRecent = t => Date.now() - Date.parse(t) < days * 24 * 60 * 60 * 1000
const byPeer = {}
const byTime = []
const pageSize = 5000
let page = 0
// need a table to convert short channel id's to public keys
const idToPublicKey = {}
// from existing channels
const getChannels = await callAPI('getChannels', {}, log)
getChannels.channels.forEach(channel => {
idToPublicKey[channel.id] = channel.partner_public_key
})
// also from closed channels which wouldn't be part of getChannels
const getClosedChannels = await callAPI('getClosedChannels', {}, log)
getClosedChannels.channels.forEach(channel => {
idToPublicKey[channel.id] = channel.partner_public_key
})
while (Date.now() - started < max_minutes_search * 60 * 1000) {
// get newer events
const thisOffset = `{"offset":${pageSize * page++},"limit":${pageSize}}`
const res = await callAPI('getForwards', { token: thisOffset })
log && logDim(`${getDate()} this offset: ${thisOffset}`)
const forwards = res.forwards || [] // new to old
if (forwards.length === 0) break // done
if (!isRecent(forwards[0].created_at)) continue // page too old
forwards.reverse() // old to new
for (const routed of forwards) {
if (!isRecent(routed.created_at)) continue // next item
const outPeer = idToPublicKey[routed.outgoing_channel] || 'unknown'
const inPeer = idToPublicKey[routed.incoming_channel] || 'unknown'
// switch to timestamp and public key
routed.created_at_ms = Date.parse(routed.created_at)
routed.outgoing_peer = outPeer
routed.incoming_peer = inPeer
routed.fee_mtokens = +routed.fee_mtokens || 0
routed.mtokens = +routed.mtokens
if (timeArray) {
byTime.push(routed)
continue
}
if (!byInPeer) {
if (byPeer[outPeer]) byPeer[outPeer].push(routed)
else byPeer[outPeer] = [routed]
} else {
if (byPeer[inPeer]) byPeer[inPeer].push(routed)
else byPeer[inPeer] = [routed]
}
}
}
if (!timeArray) return byPeer
return byTime
}
/*
returns payment events, new to old [] of
{
destination: <public key string>
created_at: <iso timestamp string>
created_at_ms: <ms time stamp> // converted to int
fee: <sats integer>
fee_mtokens: <msats integer> // converted to int
hops: [<public key strings>]
id: <payment id hex>
index: <number in database>
is_confirmed: true (?)
is_outgoing: true (?)
mtokens: <msats integer paid> // converted to int
request: (?)
secret: <secret string hex>
safe_fee: <int>
safe_tokens: <int>
tokens: <int>
hops_details: [ // just for simplify: true
{channel, channel_capacity, fee, fee_mtokens, forward, forward_mtokens, public_key, timeout}
// mtokens converted to integer too
]
attempts: // removed for simplify: true
}
*/
const customGetPaymentEvents = async (
{
days = 1, // how many days ago to look back
max_minutes_search = 2, // safety if takes too long
simplify = true, // remove failed attempts to save space
destination = undefined, // filter out by public key destination,
notDestination = undefined // filter out all payments to this destination
} = {},
log = false
) => {
log && logDim(`${getDate()} bos.customGetPaymentEvents()`)
const started = Date.now()
const isRecent = t => Date.now() - Date.parse(t) < days * 24 * 60 * 60 * 1000
const byTime = []
const pageSize = 5000
let nextOffset
const isTimedOut = () => {
const inTime = Date.now() - started < max_minutes_search * 60 * 1000
if (!inTime) {
console.error(`${getDate()} bos.customGetPaymentEvents() timed out`)
}
return !inTime
}
while (!isTimedOut()) {
// get newer events
const res = await callAPI('getPayments', !nextOffset ? { limit: pageSize } : { token: nextOffset }, log)
log && logDim(`${getDate()} this offset: ${nextOffset}, next offset: ${res.next}`)
nextOffset = res.next
const payments = res.payments || [] // new to old
if (payments.length === 0) break // done
if (!nextOffset) break // done
if (!isRecent(payments[0].created_at)) break // pages now too old
for (const paid of payments) {
// beyond days back
if (!isRecent(paid.created_at)) break
// skip unwanted destinations
if (destination && paid.destination !== destination) continue
if (notDestination && paid.destination === notDestination) continue
// unexpected messages
if (paid.request) {
log && console.log(`${getDate()} bos.customGetPaymentEvents() ??? request found`, paid)
}
if (paid.is_confirmed === false) {
log && console.log(`${getDate()} bos.customGetPaymentEvents() ??? unconfirmed found`, paid)
continue
}
// switch to timestamp and public key
paid.created_at_ms = Date.parse(paid.created_at)
paid.fee_mtokens = +paid.fee_mtokens || 0
paid.mtokens = +paid.mtokens
if (simplify) {
// just relevant successful attempt kept
paid.hops_details = paid.attempts
.filter(a => a.is_confirmed)[0]
.route.hops.map(h => ({
...h,
fee_mtokens: +h.fee_mtokens,
forward_mtokens: +h.forward_mtokens
}))
// get rid of giant fail-inclusive attempts list
delete paid.attempts
}
byTime.push(paid)
}
}
return byTime
}
/*
[{chain_address, cltv_delta, confirmed_at,
confirmed_index, created_at, description,
description_hash, expires_at, features,
id, index, is_canceled, is_confirmed,
is_held, is_private, is_push, mtokens,
payment, payments, received,
received_mtokens, request, secret,
tokens, created_at_ms, confirmed_at_ms}]
*/
const customGetReceivedEvents = async (
{
days = 1, // how many days ago to look back
max_minutes_search = 2, // safety if takes too long
idKeys = false // return object instead with ids as keys
} = {},
log = false
) => {
log && logDim(`${getDate()} bos.customGetReceivedEvents()`)
const started = Date.now()
const isRecent = t => Date.now() - Date.parse(t) < days * 24 * 60 * 60 * 1000
const byTime = []
const byId = {}
const pageSize = 5000
let nextOffset
const isTimedOut = () => {
const inTime = Date.now() - started < max_minutes_search * 60 * 1000
if (!inTime) {
console.error(`${getDate()} bos.customGetReceivedEvents() timed out`)
}
return inTime
}
while (isTimedOut()) {
// get newer events
const res = await callAPI('getInvoices', !nextOffset ? { limit: pageSize } : { token: nextOffset }, log)
log && logDim(`${getDate()} this offset: ${nextOffset}, next offset: ${res.next}`)
nextOffset = res.next
const payments = (res.invoices || []) // new to old
.filter(p => p.is_confirmed) // just care about completed
if (payments.length === 0) break // done
if (!nextOffset) break // done
// if entire page now too old
if (!isRecent(payments[0].confirmed_at)) break
for (const paid of payments) {
// end if created before cut-off AND confirmed before cut-off
if (!isRecent(paid.confirmed_at)) break
// switch to timestamp and public key
paid.created_at_ms = Date.parse(paid.created_at)
paid.confirmed_at_ms = Date.parse(paid.confirmed_at)
paid.received_mtokens = +paid.received_mtokens || 0
paid.mtokens = +paid.mtokens || 0
if (idKeys) byId[paid.id] = paid
else byTime.push(paid)
}
}
return idKeys ? byId : byTime
}
// gets node info and policies for every channel, slightly reformated
// if peer_key is provided, will only return channels with that peer
// if public_key not provided, will use this nodes public key
// byPublicKey will use peer public key as object keys and values will be arrach of all channels to that peer
// important: this seems to update much slower for your own node's new channels than callAPI('getFeeRates')
// uses https://github.com/alexbosworth/ln-service#getnode
const getNodeChannels = async ({ public_key, peer_key, byPublicKey = false } = {}) => {
try {
if (!public_key) public_key = (await callAPI('getIdentity')).public_key
const res = await callAPI('getNode', { public_key, is_omitting_channels: false })
// put remote public key directly into channels info
// instead of channels being a random array, convert to object where channel id is key
// instead of policies being array length 2 make it object with local: {}, and remote: {} data
// so getting remote fee rate would be
// res.channels[id].policy.remote.fee_rate
// and remote public key would be
// res.channels[id].public_key
const betterChannels = res.channels.reduce((edited, channel) => {
const outgoingPolicy = channel.policies.find(p => p.public_key === public_key)
const incomingPolicy = channel.policies.find(p => p.public_key !== public_key)
const remotePublicKey = incomingPolicy.public_key
// if specific peer for this node was requested, ignore all other peer keys
if (peer_key && peer_key !== remotePublicKey) return edited
const keyToUse = byPublicKey ? remotePublicKey : channel.id
if (byPublicKey) {
if (!edited[keyToUse]) edited[keyToUse] = []
// have to separate different channels to same public key in an array
const n = edited[keyToUse].push(channel) // returns new array length