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
|
import config from "$config";
import db from "$db";
import { emit } from "$lib/sockets";
import store from "./store";
import bolt11 from "bolt11";
import { Op } from "@sequelize/core";
import {
computeConversionFee,
conversionFeeReceiver
} from "../routes/lightning/conversionFee";
import lnd from "$lib/lnd";
import ln from "$lib/ln";
import { payViaPaymentRequest } from "lightning";
import { l, warn, err } from "$lib/logging";
export default async (amount, memo, hash, user) => {
l("attempting lightning payment", user.username, amount, hash);
let payreq = bolt11.decode(hash);
let routingInfo = payreq.tags.find(t => t.tagName === "routing_info");
if (
routingInfo &&
routingInfo.data.find(d => d.fee_proportional_millionths > 10000)
) {
warn("fee rate too high", d.fee_proportional_millionths);
throw new Error(
"Couldn't find a suitable route to pay to your destination"
);
}
if (!amount || payreq.satoshis > amount) amount = payreq.satoshis;
amount = parseInt(amount);
if (store.seen.includes(hash)) {
warn("attempted to pay a paid invoice", user.username);
throw new Error("Invoice has been paid, can't pay again");
}
let error, fee, total, preimage, payment;
let conversionFee = computeConversionFee(amount);
let conversionFeeDeduction;
await db.transaction(async transaction => {
l("starting lightning send transaction");
let account;
if (user.account.asset === config.liquid.btcasset)
account = await db.Account.findOne({
where: {
id: user.account.id,
pubkey: null
},
lock: transaction.LOCK.UPDATE,
transaction
});
else
account = await db.Account.findOne({
where: {
user_id: user.id,
asset: config.liquid.btcasset,
pubkey: null,
balance: {
[Op.gte]: amount
}
},
lock: transaction.LOCK.UPDATE,
transaction
});
conversionFeeDeduction = Math.min(account.lightning_credits, conversionFee);
total = amount + conversionFee - conversionFeeDeduction;
console.log("a t lc cf cfd", amount, total, account.lightning_credits, conversionFee, conversionFeeDeduction);
if (!account || account.balance < total) {
warn(
"user attempting to send more than they have",
user.id,
amount,
conversionFee
);
throw new Error("Insufficient funds");
}
await account.decrement(
{ balance: total, lightning_credits: conversionFeeDeduction },
{ transaction }
);
await account.reload({ transaction });
l("debited account", account.id, total);
let tokens;
if (!payreq.satoshis) tokens = amount;
let m;
if (config.lna.lnd) {
m = await payViaPaymentRequest({
lnd,
request: hash,
max_fee: Math.min(10000, account.balance)
});
if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`);
if (m.payment_error) throw new Error(m.payment_error);
({ fee, secret, tokens } = m);
preimage = secret.toString("hex");
} else {
let { msatoshi, msatoshi_sent, payment_preimage } = await ln.pay(
hash,
tokens ? `${tokens}sats` : undefined
);
preimage = payment_preimage;
fee = parseInt((msatoshi_sent - msatoshi) / 1000);
}
if (store.seen.includes(preimage)) {
warn("duplicate payment detected", preimage);
throw new Error("Duplicate payment detected");
}
store.seen.push(preimage);
store.seen.push(hash);
});
await db.transaction(async transaction => {
l("starting lightning fee transaction");
let account;
if (user.account.asset === config.liquid.btcasset)
account = await db.Account.findOne({
where: {
id: user.account.id,
pubkey: null
},
lock: transaction.LOCK.UPDATE,
transaction
});
else
account = await db.Account.findOne({
where: {
user_id: user.id,
asset: config.liquid.btcasset,
pubkey: null,
balance: {
[Op.gte]: fee
}
},
lock: transaction.LOCK.UPDATE,
transaction
});
l("deducting fee", fee);
await account.decrement({ balance: fee }, { transaction });
await account.reload({ transaction });
if (conversionFeeDeduction) {
conversionFee -= conversionFeeDeduction;
}
l("conversion fee", conversionFee);
let receiverAccount = await db.Account.findOne({
where: {
"$user.username$": conversionFeeReceiver
},
include: [
{
model: db.User,
as: "user"
}
],
lock: transaction.LOCK.UPDATE,
transaction
});
let fee_payment;
let fee_payment_id = null;
if (conversionFee) {
await receiverAccount.increment(
{ balance: conversionFee },
{ transaction }
);
await receiverAccount.reload({ transaction });
fee_payment = await db.Payment.create(
{
amount: conversionFee,
fee: 0,
memo: "Lightning conversion fee",
account_id: receiverAccount.id,
user_id: receiverAccount.user_id,
rate: store.rates[receiverAccount.user.currency],
currency: receiverAccount.user.currency,
confirmed: true,
received: true,
network: "COINOS"
},
{ transaction }
);
fee_payment_id = fee_payment.id;
}
payment = await db.Payment.create(
{
amount: -total,
account_id: account.id,
user_id: user.id,
hash,
memo,
preimage,
rate: store.rates[user.currency],
currency: user.currency,
confirmed: true,
network: "lightning",
fee,
fee_payment_id
},
{ transaction }
);
l("created payment record", payment.id);
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
payment.fee_payment = fee_payment && fee_payment.get({ plain: true });
emit(user.username, "account", payment.account);
emit(user.username, "payment", payment);
l("sent lightning", user.username, amount, total, fee);
});
return payment;
};
|