-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (150 loc) · 3.66 KB
/
index.js
File metadata and controls
153 lines (150 loc) · 3.66 KB
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
const fs = require("fs");
const EventEmitter = require("events");
exports.hello = function() {
console.log("hello world!");
}
exports.Oreki = class {
loadConfig(configPath) {
const jsonText = fs.readFileSync(configPath, {
encoding: "utf-8"
});
const json = JSON.parse(jsonText);
return json;
}
validateConfig(config) {
return true;
}
constructor(configPath) {
this.emitter = new EventEmitter();
const config = this.loadConfig(configPath);
if (!this.validateConfig(config)) {
return;
}
this.config = config
this.timer = null
this.db = require("./db")(config.db)
if (this.config.coinType === "lightning") {
this.lightning = require("./lightning")(config.lnd)
}
if (this.config.coinType === "ethereum") {
this.ethereum = require("./ethereum")(config.geth)
}
}
on(eventName, callback) {
this.emitter.on(eventName, callback);
}
async init() {
if (this.config.coinType === "lightning") {
try {
await this.lightning.unlock()
} catch(err) {
console.error(err)
}
}
try {
await this.db.initDB()
} catch(err) {
console.error(err)
return false
}
return true
}
start() {
if (this.timer === null) {
const that = this
this.timer = setInterval(function() {
if (that.config.coinType === "lightning") {
that.checkLightningTransaction.apply(that)
} else if (that.config.coinType === "ethereum") {
that.checkEthereumTransaction.apply(that)
}
}, 60 * 1000)
}
}
stop() {
if (this.timer !== null) {
clearInterval(this.timer)
}
}
async checkEthereumTransaction() {
let payments = null
try {
payments = await this.db.getPayments()
} catch(err) {
console.error(err)
return
}
for (let payment of payments) {
await this.ethereum.unlock(payment.payee)
const balance = await this.ethereum.getBalance(payment.payee)
if (payment.price > balance) {
payment.remain = payment.price - balance
payment.save
this.emitter.emit("insufficient", payment)
continue
}
payment.remain = 0
payment.paid = true
payment.save()
this.emitter.emit("paid", payment)
}
}
async checkLightningTransaction() {
let that = this
let invoices = null
try {
invoices = await this.lightning.listInvoices()
} catch(err) {
console.error(err)
return
}
let payments = null
try {
payments = await this.db.getPayments()
} catch(err) {
console.error(err)
return
}
payments.forEach(function(payment) {
const _invoices = invoices.filter(function(invoice) {
return invoice.payment_request === payment.payee
})
if (_invoices.length === 0) {
return
}
payment.remain = 0
payment.paid = true
payment.save()
that.emitter.emit("paid", payment)
})
}
async addPayment(userId, endpoint, point, price) {
let payee = null
try {
if (this.config.coinType === "lightning") {
payee = await this.lightning.addInvoice(price)
} else if (this.config.coinType === "ethereum") {
payee = await this.ethereum.createAddress()
}
} catch(err) {
console.error(err)
return null
}
let payment = null
try {
payment = await this.db.createPayment({
payee: payee,
user_id: userId,
endpoint: endpoint,
point: point,
price: price,
remain: price,
paid: false,
})
} catch(err) {
console.error(err)
return null
}
return payment
}
}