-
Notifications
You must be signed in to change notification settings - Fork 0
/
umbril.js
147 lines (136 loc) · 4.97 KB
/
umbril.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
const axios = require("axios");
const config = require("./config.js");
let randomSeconds = Math.floor(Math.random() * 10);
module.exports.getJWTToken = function getJWTToken() {
return new Promise((resolve, reject) => {
axios
.post(`http://umbrel.local/manager-api/v1/account/login`, config.umbrilpassword)
.then((res) => resolve(res.data.jwt))
.catch((error) => reject(error.response.data));
});
};
function checkNumSatoshis(JWTToken, qrcode) {
return new Promise((resolve, reject) => {
axios
.get(`http://umbrel.local/api/v1/lnd/lightning/invoice?paymentRequest=${qrcode}`, {
headers: {
Authorization: `JWT ${JWTToken}`,
},
})
.then((res) => resolve(res.data.numSatoshis))
.catch((error) => reject(error.response.data));
});
}
function payInvoice(JWTToken, qrcode, amt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
axios
.post(
`http://umbrel.local/api/v1/lnd/lightning/payInvoice`,
{ amt: amt, paymentRequest: `${qrcode}` },
{
headers: {
Authorization: `JWT ${JWTToken}`,
},
}
)
.then((response) => resolve(response.data.paymentRoute.totalFees))
.catch((error) => {
//let errorBody = error.response.data
reject(error);
});
}, randomSeconds);
});
}
module.exports.lastDonation = function lastDonation(JWTToken) {
return new Promise((resolve, reject) => {
axios
.get(`http://umbrel.local/api/v1/lnd/lightning/invoices`, {
headers: {
Authorization: `JWT ${JWTToken}`,
},
})
.then((res) => {
let json = res.data;
let donationList = [];
//let highestSettleDate = [];
for (var i = 0; i < res.data.length; i++) {
var obj = json[i];
if (obj.memo && obj.settled) {
//highestSettleDate.push(obj.settleDate);
donationList.push({
memo: obj.memo,
value: obj.value,
settleDate: obj.settleDate,
});
}
}
//console.log(donationList)
let lastPersonDonated = latestDonor(donationList);
resolve(lastPersonDonated);
//console.log(lastPersonDonated)
})
.catch((err) => reject(err));
});
};
function latestDonor(items) {
let highestPriceSoFar = 0;
let nameOfHighestPriceSoFar;
for (const { memo, value, settleDate } of items) {
if (settleDate > highestPriceSoFar) {
highestPriceSoFar = settleDate;
nameOfHighestPriceSoFar = {
memo: memo,
value: value,
settleDate: settleDate,
};
}
}
return nameOfHighestPriceSoFar;
}
module.exports.makePayment = function makePayment(qrcode, payoutAmount, checkUserIDinPendingPayout) {
return new Promise(async (resolve, reject) => {
try {
//get JWT Token
let JWTToken = await this.getJWTToken();
console.log("JWT Token: ", JWTToken);
let sat = await checkNumSatoshis(JWTToken, qrcode);
if (sat <= payoutAmount && sat >= 10 && !checkUserIDinPendingPayout && sat <= config.maxTipAmount) {
//Pay invoice
console.log("Executing makePayment function ");
let executePaymentAndFee = await payInvoice(JWTToken, qrcode, 0);
console.log("Executing Done, going to resolve");
//returning fee we paid and sat amount
resolve({ fee: executePaymentAndFee, sat: sat });
} else if (sat == 0 && !checkUserIDinPendingPayout) {
//Pay invoice
console.log("Executing makePayment function with default 100 sats as we didnt detect invoice amount ");
let executePaymentAndFee = await payInvoice(JWTToken, qrcode, payoutAmount);
console.log("Executing Done, going to resolve");
//returning fee we paid and sat amount
resolve({ fee: executePaymentAndFee, sat: payoutAmount });
}
// Payout function--------
else if (sat <= payoutAmount && sat >= 10 && checkUserIDinPendingPayout) {
//Pay invoice
console.log("Payout Executing makePayment function ");
let executePaymentAndFee = await payInvoice(JWTToken, qrcode, 0);
console.log("Payout Executing Done, going to resolve");
//returning fee we paid and sat amount
resolve({ fee: executePaymentAndFee, sat: sat });
} else if (sat == 0 && checkUserIDinPendingPayout) {
//Pay invoice
console.log("Payout Executing makePayment function with default 1000 sats as we didnt detect invoice amount ");
let executePaymentAndFee = await payInvoice(JWTToken, qrcode, payoutAmount);
console.log("Payout Executing Done, going to resolve");
//returning fee we paid and sat amount
resolve({ fee: executePaymentAndFee, sat: payoutAmount });
} else {
reject(`Invalid sat amount: (${sat}), please try again with the correct amount of Sats.`);
}
} catch (err) {
console.log(err);
reject(err);
}
});
};