-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformatter.js
381 lines (306 loc) Β· 12.1 KB
/
formatter.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
import MillisatParser from "./millisat-parser.js"
class Formatter {
// System
static help() {
return `π€ nostr-control π€
/help - show this help
/info - show node info
/channels - Get information about your channels
/funds - Get information about your funds
/invoice [amount_sats] [label] [description] - create an invoice
/pay [bolt11] - pay an invoice
/address - get a new address (Bech32)
/donate - tip me some β‘οΈ
/issue - report an issue
/verbose - show everything (including failed forwards)
/quiet - show only successful forwards (and payment related events)
/silent - show no notifications
/version - see the current version of nostr-control`
}
static unknown() {
return `π€ unknown command π€
type /help for a list of commands`
}
static issue() {
return `π€ report an issue π€
File an issue here: https://github.com/joelklabo/nostr-control/issues`
}
static donate() {
return `π€ donate π€
BTC βοΈ: bc1q4gahrs92m4fz6el39zk3zy546psgl3sndezz9l
LN Address β‘οΈ: [email protected]
Tips: https://klabo.blog/tip
Follow me on Nostr: npub19a86gzxctwtz68l8zld2u9y2fjvyyj4juyx8m5geylssrmfj27eqs22ckt
GitHub βοΈ: https://github.com/joelklabo/nostr-control`
}
static async funds(funds) {
const onChainFunds = MillisatParser.formatNumber(funds.chain_sat);
const channelFunds = MillisatParser.formatNumber(funds.channel_sat);
const totalFunds = MillisatParser.formatNumber(funds.chain_sat + funds.channel_sat);
return `π° funds π°
βοΈ: ${onChainFunds}
π: ${channelFunds}
total: ${totalFunds}`
}
static async channels(channels) {
const graphLength = 15; // Length of the line graph
let message = `π channel summary π\n`;
for (let channel of channels) {
const alias = await this.aliasCache.get(channel.scid) || channel.scid;
const spendable = MillisatParser.parseInput(channel.spendable, true);
const receivable = MillisatParser.parseInput(channel.receivable, true);
const total = MillisatParser.parseInput(channel.total, true);
const spendablePercentage = channel.spendable / channel.total;
let spendableGraphPosition = Math.round(spendablePercentage * graphLength);
// Ensure the vertical line is always present
if (spendableGraphPosition === 0) {
spendableGraphPosition = 1;
} else if (spendableGraphPosition >= graphLength) {
spendableGraphPosition = graphLength - 1;
}
let lineGraph = "";
for (let i = 0; i < graphLength; i++) {
if (i === spendableGraphPosition) {
lineGraph += "|";
} else {
lineGraph += "β";
}
}
message += `\n${alias}\n${lineGraph}\ntotal: ${total}\n`;
}
return message;
}
// RPC
// Example of getinfo response:
// "id": "03955cdc9ba1949429cc235cd9d8022d74fecb555009762ebaac6afddc4ea69d18",
// "alias": "HAPPYWAFFLE",
// "color": "03955c",
// "num_peers": 1,
// "num_pending_channels": 0,
// "num_active_channels": 2,
// "num_inactive_channels": 0,
// "address": [],
// "binding": [
// {
// "type": "ipv4",
// "address": "127.0.0.1",
// "port": 7171
// }
// ],
// "version": "v22.11.1",
// "blockheight": 563,
// "network": "regtest",
// "fees_collected_msat": 0,
// "lightning-dir": "/tmp/l1-regtest/regtest",
// "our_features": {
// "init": "08a000080269a2",
// "node": "88a000080269a2",
// "channel": "",
// "invoice": "02000000024100"
// }
static getinfo(info) {
const addressInfo = info.address.length > 0 ? `${info.id}@${info.address[0].address}:${info.address[0].port}` : "?";
return `π€ node info π€
${info.alias}
ID: ${info.id}
Address: ${addressInfo}
Color: ${info.color}
Peers: ${info.num_peers}
Active channels: ${info.num_active_channels}
Inactive channels: ${info.num_inactive_channels}
Pending channels: ${info.num_pending_channels}
Version: ${info.version}
Blockheight: ${info.blockheight}
Network: ${info.network}
Fees collected: ${MillisatParser.parseInput(info.fees_collected_msat, true)} β‘οΈ`
}
// Example of a channel_opened notification:
// "id": "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
// "funding_msat": 100000000,
// "funding_txid": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
// "channel_ready": false
static channel_opened(data) {
const info = data.channel_opened
return `π ${MillisatParser.parseInput(info.funding_msat, true)} β‘οΈ channel open π
with:
${info.id}`;
}
// Example of a channel_open_failed notification:
// "channel_id": "a2d0851832f0e30a0cf778a826d72f077ca86b69f72677e0267f23f63a0599b"
static channel_open_failed(data) {
const info = data.channel_open_failed
return `π channel open failed π
channel id:
${info.channel_id}`;
}
// Example of a channel_state_changed notification:
// "peer_id": "03bc9337c7a28bb784d67742ebedd30a93bacdf7e4ca16436ef3798000242b2251",
// "channel_id": "a2d0851832f0e30a0cf778a826d72f077ca86b69f72677e0267f23f63a0599b4",
// "short_channel_id" : "561820x1020x1",
// "timestamp":"2023-01-05T18:27:12.145Z",
// "old_state": "CHANNELD_NORMAL",
// "new_state": "CHANNELD_SHUTTING_DOWN",
// "cause" : "remote",
// "message" : "Peer closes channel"
static channel_state_changed(data) {
const info = data.channel_state_changed
return `π channel state changed π
peer:
${info.peer_id}
${info.old_state} -> ${info.new_state}`;
}
// Example of a connect notification:
// "id": "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432",
// "direction": "in",
// "address": "1.2.3.4:1234"
static connect(data) {
return `π€ connected π€
${data.id} ${data.direction == 'in' ? '(in)' : '(out)'}`;
}
// Example of a disconnect notification:
// "id": "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432"
static disconnect(info) {
return `π disconnected π
${info.id}`;
}
// Example of a invoice_payment notification:
// "label": "unique-label-for-invoice",
// "preimage": "0000000000000000000000000000000000000000000000000000000000000000",
// "msat": 10000msat
static invoice_payment(data) {
const info = data.invoice_payment
const msat = parseInt(info.msat.replace('msat', ''));
const sats = msat / 1000;
return `π§Ύ payment π§Ύ
${MillisatParser.parseInput(info.msat, true)} β‘οΈ`;
}
// Example of a invoice_creation notification:
// "label": "unique-label-for-invoice",
// "preimage": "0000000000000000000000000000000000000000000000000000000000000000",
// "msat": 10000msat
static invoice_creation(data) {
const info = data.invoice_creation
return `πΈ invoice created πΈ
${MillisatParser.parseInput(info.msat, true)} β‘οΈ`;
}
// Example of a warning notification:
// "level": "warn",
// "time": "1559743608.565342521",
// "source": "lightningd(17652): 0821f80652fb840239df8dc99205792bba2e559a05469915804c08420230e23c7c chan #7854:",
// "log": "Peer permanent failure in CHANNELD_NORMAL: lightning_channeld: sent ERROR bad reestablish dataloss msg"
static warning(info) {
return `π¨ warning π¨
${info.log}`;
}
// forward_event: {
// payment_hash:
// "5b7ae144baf46759dcda3b299695768f73542e746db822683470c8fdc22c8438",
// in_channel: "566x1x0",
// in_htlc_id: 1,
// out_channel: "1092x1x0",
// in_msat: 10001,
// out_msat: 10000,
// fee_msat: 1,
// status: "settled",
// style: "tlv",
// received_time: 1682310437.179,
// resolved_time: 1682310437.239,
// },
static async forward_event(data) {
const info = data.forward_event
const in_alias = await this.aliasCache.get(info.in_channel) || info.in_channel
const out_alias = await this.aliasCache.get(info.out_channel) || info.out_channel
return `π routed ${info.status == "local_failed" ? "N/A" : MillisatParser.parseInput(info.out_msat, true)} β‘οΈ π
${in_alias} > ${out_alias}
fee ${info.status == "local_failed" ? "N/A" : MillisatParser.parseInput(info.fee_msat, true)} β‘οΈ
status ${info.status}`;
}
// Example of a sendpay_success notification:
// "id": 1,
// "payment_hash": "5c85bf402b87d4860f4a728e2e58a2418bda92cd7aea0ce494f11670cfbfb206",
// "destination": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d",
// "amount_msat": 100000000, (69590514msat)
// "amount_sent_msat": 100001001, (69590514msat)
// "created_at": 1561390572,
// "status": "complete",
// "payment_preimage": "9540d98095fd7f37687ebb7759e733934234d4f934e34433d4998a37de3733ee"
static sendpay_success(data) {
return `π payment succeeded π`
}
// Example of a sendpay_failure notification:
// "code": 204,
// "message": "failed: WIRE_UNKNOWN_NEXT_PEER (reply from remote)",
// "data": {
// "id": 2,
// "payment_hash": "9036e3bdbd2515f1e653cb9f22f8e4c49b73aa2c36e937c926f43e33b8db8851",
// "destination": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d",
// "amount_msat": 100000000,
// "amount_sent_msat": 100001001,
// "created_at": 1561395134,
// "status": "failed",
// "erring_index": 1,
// "failcode": 16394,
// "failcodename": "WIRE_UNKNOWN_NEXT_PEER",
// "erring_node": "022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59",
// "erring_channel": "103x2x1",
// "erring_direction": 0
// }
static sendpay_failure(data) {
const info = data.sendpay_failure
const destination = info.data.destination
const message = info.message
return `π payment failed π
${destination} ${message}`;
}
// Example of a coin_movement notification:
// "version":2,
// "node_id":"03a7103a2322b811f7369cbb27fb213d30bbc0b012082fed3cad7e4498da2dc56b",
// "type":"chain_mvt",
// "account_id":"wallet",
// "originating_account": "wallet", // (`chain_mvt` only, optional)
// "txid":"0159693d8f3876b4def468b208712c630309381e9d106a9836fa0a9571a28722", // (`chain_mvt` only, optional)
// "utxo_txid":"0159693d8f3876b4def468b208712c630309381e9d106a9836fa0a9571a28722", // (`chain_mvt` only)
// "vout":1, // (`chain_mvt` only)
// "payment_hash": "xxx", // (either type, optional on both)
// "part_id": 0, // (`channel_mvt` only, optional)
// "credit_msat":2000000000,
// "debit_msat":0,
// "output_msat": 2000000000, // ('chain_mvt' only)
// "output_count": 2, // ('chain_mvt' only, typically only channel closes)
// "fees_msat": 382, // ('channel_mvt' only)
// "tags": ["deposit"],
// "blockheight":102, // 'chain_mvt' only
// "timestamp":1585948198,
// "coin_type":"bc"
static coin_movement(data) {
const info = data.coin_movement
return `πͺ coin movement: ${info.type} πͺ
credit: ${MillisatParser.parseInput(info.credit_msat, true)} β‘οΈ
debit: ${MillisatParser.parseInput(info.debit_msat, true)} β‘οΈ`;
}
// Example of a balance_snapshot (too complex for now):
static balance_snapshot(info) {
return `TODO: Balance snapshot event`;
}
// Example of block notifications:
// "hash": "000000000000000000034bdb3c01652a0aa8f63d32f949313d55af2509f9d245",
// "height": 753304
static block_added(data) {
const info = data.block
return `𧱠new block π§±
height: ${info.height}
hash: ${info.hash}`;
}
// Example of open_channel_peer_sigs notification:
// "channel_id": "252d1b0a1e5789...",
// "signed_psbt": "cHNidP8BAKgCAAAAAQ+y+61AQAAAAD9////AzbkHAAAAAAAFgAUwsyrFxwqW+natS7EG4JYYwJMVGZQwwAAAAAAACIAIKYE2s4YZ+RON6BB5lYQESHR9cA7hDm6/maYtTzSLA0hUMMAAAAAAAAiACBbjNO5FM9nzdj6YnPJMDU902R2c0+9liECwt9TuQiAzWYAAAAAAQDfAgAAAAABARtaSZufCbC+P+/G23XVaQ8mDwZQFW1vlCsCYhLbmVrpAAAAAAD+////AvJs5ykBAAAAFgAUT6ORgb3CgFsbwSOzNLzF7jQS5s+AhB4AAAAAABepFNi369DMyAJmqX2agouvGHcDKsZkhwJHMEQCIHELIyqrqlwRjyzquEPvqiorzL2hrvdu9EBxsqppeIKiAiBykC6De/PDElnqWw49y2vTqauSJIVBgGtSc+vq5BQd+gEhAg0f8WITWvA8o4grxNKfgdrNDncqreMLeRFiteUlne+GZQAAAAEBIICEHgAAAAAAF6kU2Lfr0MzIAmapfZqCi68YdwMqxmSHAQcXFgAUAfrZCrzWZpfiWSFkci3kqV6+4WUBCGsCRzBEAiBF31wbNWECsJ0DrPel2inWla2hYpCgaxeVgPAvFEOT2AIgWiFWN0hvUaK6kEnXhED50wQ2fBqnobsRhoy1iDDKXE0BIQPXRURck2JmXyLg2W6edm8nPzJg3qOcina/oF3SaE3czwz8CWxpZ2h0bmluZwEIexhVcpJl8ugM/AlsaWdodG5pbmcCAgABAAz8CWxpZ2h0bmluZwEIR7FutlQgkSoADPwJbGlnaHRuaW5nAQhYT+HjxFBqeAAM/AlsaWdodG5pbmcBCOpQ5iiTTNQEAA=="
static openchannel_peer_sigs(info) {
return `βοΈ open channel peer sigs
${info.channel_id}`;
}
// Example of shutdown notification:
static shutdown(info) {
return `π shutting down`;
}
}
export default Formatter;