-
Notifications
You must be signed in to change notification settings - Fork 7
/
LN-Invoice-Watch.js
160 lines (146 loc) · 3.86 KB
/
LN-Invoice-Watch.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
'use strict'
const async = require('async')
const { get } = require('lodash')
const { lnWorker } = require('./src/util/common-workers')
const { ORDER_STATES } = require('./src/Orders/Order')
const { Client: GrenacheClient } = require('blocktank-worker')
function getInvoice (order) {
return order.state === ORDER_STATES.CREATED
? order.ln_invoice
: get(order, 'renewal_quote.ln_invoice', {})
}
function getUpdatePayload (order, invoice) {
if (order.state === ORDER_STATES.CREATED) {
return {
state: ORDER_STATES.PAID,
amount_received: invoice.tokens
}
}
order.renewals.push({
previous_channel_expiry: order.channel_expiry_ts,
...order.renewal_quote
})
return {
renewals: order.renewals,
channel_expiry_ts: order.channel_expiry_ts
}
}
function getHodlOrders (cb) {
async.waterfall([
(next) => {
getOrder({}, next)
},
(orders, next) => {
async.mapLimit(orders, 5, (order, next) => {
const invoice = getInvoice(order)
lnWorker('getInvoice', { id: invoice.id }, (err, invoice) => {
if (err) return next(err)
if (invoice.is_held) {
return next(null, { order, invoice })
}
next(null, null)
})
}, (err, data) => {
if (err) return next(err)
next(null, data.filter(Boolean))
})
}
], cb)
}
function settleInvoice (order, cb) {
const txt = `remote: ${order.remote_balance} | local: ${order.local_balance} | total: ${order.local_balance + order.remote_balance}`
alert('info', 'payment', `Payment ${order._id} received. ${txt}`)
lnWorker('settleHodlInvoice', { secret: getInvoice(order).secret }, (err) => {
if (err) {
console.log('Failed to settle invoice')
console.log(err)
return cancelInvoice(order, cb)
}
cb(null, { settled: true })
})
}
function cancelInvoice (order, cb) {
console.log('Cancelling order invoice: ', order._id)
lnWorker('cancelInvoice', { id: getInvoice(order).id }, (err) => {
if (err) {
console.log('Failed to cancel invoice')
console.log(err)
return cb(err)
}
cb(null, { settled: true })
})
}
async function processHeldInvoice ({ order, invoice }, options = {}) {
console.log(`Invoice is being received and held : ${invoice.id}`)
async.waterfall([
(next) => {
settleInvoice(order, next)
},
(res, next) => {
if (!res.settled) return next(null, false)
updateOrder({
id: order._id,
update: getUpdatePayload(order, invoice)
}, next)
}
], (err) => {
if (err) {
console.log(`Failed to settle invoice: ${order._id}`)
console.log(err)
return
}
console.log(`Settled invoice: ${order._id}`)
})
}
function startWatch () {
let running = false
setInterval(() => {
if (running) {
return console.log('Still processing orders....')
}
running = true
getHodlOrders(async (err, data) => {
if (err) {
running = false
throw err
}
try {
console.log(`Processing ${data.length} invoices`)
await Promise.all(data.map((d) => processHeldInvoice(d)))
} catch (err) {
console.log('Failed to process orders')
console.log(err)
}
running = false
})
}, 5000)
}
function alert (level, tag, msg) {
return new Promise((resolve, reject) => {
gClient.send('svc:monitor:slack', [level, 'payment', msg], (err, data) => {
if (err) {
return reject(err)
}
resolve(data)
})
})
}
function updateOrder (args, cb) {
gClient.send('svc:get_order', {
method: 'updateOrder',
args: args
}, cb)
}
function getOrder (args, cb) {
gClient.send('svc:get_order', {
method: 'getPendingPaymentOrders',
args: args
}, (err, data) => {
if (err) {
return cb(err)
}
cb(null, data)
})
}
const gClient = new GrenacheClient()
startWatch()