-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
346 lines (282 loc) · 9.75 KB
/
server.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
const request = require('request')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors');
const paymentModule = require('iota-payment')
const low = require('lowdb')
const FileAsync = require('lowdb/adapters/FileAsync')
const fetch = require('node-fetch');
const { PROVIDER, URL, APP_PORT, IOTAADDRESS } = require('./config.json');
const Mam = require('@iota/mam');
const { asciiToTrytes } = require('@iota/converter')
let {isValidChecksum} = require('@iota/checksum');
const generateSeed = require('iota-generate-seed');
let mamState;
const PORT = APP_PORT || 3000
// Create server
const app = express()
app.use(cors());
app.use(bodyParser.json())
let options = {
api: true,
websockets: true
}
let server = paymentModule.createServer(app, options)
//Create an event handler which is called, when a payment was successfull
let onPaymentSuccess = function (payment) {
console.log(`Payment received:`, payment);
handleDonation(payment)
// .then((response) => {
// db.set('config.state', response.state)
// .write()
// db.get('snapshots')
// .push(response.snapshot)
// .last()
// .assign({ id: Date.now().toString() })
// .write()
// })
}
// Listen to the "paymentSuccess" event and call function
paymentModule.onEvent('paymentSuccess', onPaymentSuccess);
// Create database instance and start server
const adapter = new FileAsync('db.json')
low(adapter)
.then(db => {
// Routes
// GET /projects/:id
app.get('/projects/:id', (req, res) => {
const post = db.get('projects')
.find({ id: req.params.id })
.value()
res.send(post)
})
// GET /root
app.get('/', (req, res) => {
const root = db.get('config.root')
.value()
res.send(root)
})
// GET /projects
app.get('/projects', (req, res) => {
console.log("URL", URL)
fetch(URL)
.then(res => res.json())
.then(json => {
console.log("json", json)
res.send(json)
});
})
// GET /payments
app.get('/payments', (req, res) => {
paymentModule.getPayments().then(payments => {
res.send(payments)
})
.catch(err => {
console.log(err)
})
})
// GET /payouts/:nodeId
app.get('/payouts/:projectId', (req, res) => {
let nodeId = req.params.nodeId
paymentModule.getPayouts().then(payouts => {
payouts = payouts.filter(p => typeof p.data != 'undefined')
payouts = payouts.filter(payout => payout.data.projectId == projectId).reverse();
res.send(payouts)
})
.catch(err => {
console.log(err)
})
})
// Initialise MAM State
let seed = db.get('config.seed').value()
console.log("seed", seed)
if (seed) {
mamState = Mam.init(PROVIDER, seed)
let old_state = db.get('config.state').value()
if (old_state) {
updateMamState(old_state);
}
} else {
seed = generateSeed()
db.set('config.seed', seed)
.write()
mamState = Mam.init(PROVIDER, seed)
db.set('config.root', Mam.getRoot(mamState))
.write()
}
// Set db default values
return db.defaults({ projects: [], config: {} }).write()
})
.then(() => {
server.listen(PORT, () => console.log('Server listening on port ' + PORT))
})
const fetchData = async () => {
let response = await fetch(URL);
let json = await response.json();
return json
}
const updateMamState = newMamState => (mamState = newMamState);
// Publish to tangle
const publishToMAM = async data => {
// Create MAM Payload - STRING OF TRYTES
const trytes = asciiToTrytes(JSON.stringify(data))
const message = Mam.create(mamState, trytes)
// Save new mamState
updateMamState(message.state);
// Attach the payload
let x = await Mam.attach(message.payload, message.address, 3, 14)
return message
}
const handleDonation = async (payment) => {
try{
let data
while(typeof data == 'undefined'){
try{
console.log("fetch new projects Data");
// TODO: GET NEW PROJET DONATION ADDRESSES
data = await fetchData()
}catch(e){}
await new Promise(resolve => setTimeout(resolve, 20000))
}
console.log("projects", data)
console.log("projects count", data.length)
// make payout
//check if own address is valid
if(validAddress(IOTAADDRESS)){
//set address for invalid entries
data.map(e=> {
if(!validAddress(e.donation_address)){
e.address = IOTAADDRESS
}
})
}
// 1. get node_with_addresses
const all_project_with_addresses = data.filter((project) => validAddress(project.donation_address));
//trim spaces
for(project of all_project_with_addresses){
project.donation_address = project.donation_address.trim()
}
console.log("all_project_with_addresses count", all_project_with_addresses.length)
//remove spent addresses
let addresses = all_project_with_addresses.map(e => e.donation_address.slice(0, 81))
let spentStatus = await wereAddressesSpentFrom(addresses)
let projects_with_addresses = all_project_with_addresses.filter((obj, index) => spentStatus[index] == false)
console.log("projects_with_addresses count", projects_with_addresses.length)
// 2. calculate iota for each project
let total_iotas = payment.txInfo.value
//calculate iota, assign rounded iota value and calculate remaining iotas
let assigned_iotas = 0
let average_iota = Math.floor(total_iotas / projects_with_addresses.length)
console.log("average_iota:", average_iota);
projects_with_addresses.forEach(function (object) {
object.iotas = average_iota
assigned_iotas = object.iotas + assigned_iotas;
})
let remaining = total_iotas - assigned_iotas
console.log("total_iotas:", total_iotas);
console.log("assigned_iotas:", assigned_iotas);
console.log("remaining", remaining);
console.log("projects_with_addresses", projects_with_addresses);
console.log("projects_with_addresses", projects_with_addresses.length);
// send remaining tokens to standart address
//send payouts
let tag = 'PROJECTS9PAYOUT'
for(project of projects_with_addresses){
try {
if(project.iotas>0){
let payout = await paymentModule.sendPayout({
address: project.address,
value: project.iotas,
message: `einfachIOTA Project donation payout!\nProject: "${project.name}".`,
tag,
data:{projectUrl:project.eco_url}
})
console.log(`Payout with ${payout.value} created for project (${project.name}). Address: ${payout.address}`);
//wait 1 second
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (e) {
console.log(e)
}
}
try {
if(remaining>0){
let payout = await paymentModule.sendPayout({
address: IOTAADDRESS,
value: remaining,
message: `einfachIOTA Project donation remaining payout!.`,
tag,
data:{remaining_payout:remaining}
})
console.log(`remaining Payout with ${payout.value} created (${IOTAADDRESS}).`);
//wait 1 second
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (e) {
console.log(e)
}
// return {}
// // Hash data
// const hash = sha256(JSON.stringify(data))
// let mam_message = {
// timestamp: Date.now(),
// data_hash: hash
// }
// let mam = await publishToMAM(mam_message)
// let snapshot = {
// ...mam_message,
// root: mam.root
// }
// return { snapshot: snapshot, state: mam.state }
} catch(err){
console.error("Error in handleDonation", err);
}
}
function wereAddressesSpentFrom(addresses, provider) {
return new Promise(async (resolve, reject) => {
try {
var command = {
command: 'wereAddressesSpentFrom',
addresses: addresses
}
var options = {
url: PROVIDER,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-IOTA-API-Version': '1',
'Content-Length': Buffer.byteLength(JSON.stringify(command))
},
json: command
}
request(options, function (error, response, data) {
if (!error && response.statusCode == 200) {
resolve(data.states)
}
})
} catch (e) {
reject(e)
}
})
}
function validAddress(address){
if (typeof address == 'string') {
address = address.trim()
} else {
return false
}
try {
if (!isValidChecksum(address)) {
return false
}
} catch (e) {
console.error(e)
return false
}
//check last trit for Kerl address if value transfer
if (!/[E-V]/.test(address.slice(80, 81))) {
return true
} else {
return false
}
}