-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
1058 lines (949 loc) · 37.7 KB
/
app.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const async = require('async');
const base58 = require('bs58check');
const config = require('./config/config');
const fs = require('fs');
const moment = require('moment');
const mysql = require('mysql');
const request = require('request');
const util = require('util');
if (config.debug) {
require('request-debug')(request);
}
// URLS
const baseUrl = 'https://oauth.reddit.com';
const rateUrl = 'https://api.lbry.io/lbc/exchange_rate';
const tokenUrlFormat = 'https://%s:%[email protected]/api/v1/access_token';
const tipRegex = /(\$[\d\.]+|[\d\.]+( usd| lbc))/ig;
const gildRegex = new RegExp('gild (u|\/u)\/lbryian|(u|\/u)\/lbryian gild', 'ig');
// Other globals
const commentKind = 't1';
const privateMessageKind = 't4';
let globalAccessToken;
let accessTokenTime;
// Load message templates
const messageTemplates = {};
const templateNames = [
'onbalance',
'ondeposit',
'ondeposit.completed',
'ongild',
'ongild.insufficientfunds',
'onsendtip',
'onsendtip.insufficientfunds',
'onsendtip.invalidamount',
'onwithdraw',
'onwithdraw.amountltefee',
'onwithdraw.insufficientfunds',
'onwithdraw.invalidaddress',
'onwithdraw.invalidamount'
];
for (let i = 0; i < templateNames.length; i++) {
const name = templateNames[i];
messageTemplates[name] = fs.readFileSync(`templates/${name}.txt`, { encoding: 'utf8' });
}
// Connect to the database
let db;
const initSqlConnection = () => {
const _db = mysql.createConnection({
host: config.mariadb.host,
user: config.mariadb.username,
password: config.mariadb.password,
database: config.mariadb.database,
charset: 'utf8mb4',
timezone: 'Z'
});
_db.on('error', (err) => {
if (err.code === 2006 || ['PROTOCOL_CONNECTION_LOST', 'PROTOCOL_PACKETS_OUT_OF_ORDER', 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'].indexOf(err.code) > -1) {
_db.destroy();
db = initSqlConnection();
}
});
return _db;
};
db = initSqlConnection();
const loadAccessToken = (callback) => {
if (fs.existsSync(config.accessTokenPath)) {
const token = fs.readFileSync(config.accessTokenPath, { encoding: 'utf8' });
return callback(null, String(token));
}
return callback(null, null);
};
const oauth = (callback) => {
const url = util.format(tokenUrlFormat, config.clientId, config.clientSecret);
request.post(url, { form: { grant_type: 'password', username: config.username, password: config.password} }, (err, res, body) => {
if (err) {
return callback(err, null);
}
let accessToken = null;
try {
const response = JSON.parse(body);
accessToken = response.access_token;
accessTokenTime = moment();
if (accessToken && accessToken.trim().length > 0) {
fs.writeFileSync(config.accessTokenPath, accessToken);
}
} catch (e) {
return callback(e, null);
}
return callback(null, accessToken);
});
};
const retrieveUnreadMessages = (accessToken, callback) => {
const url = util.format('%s/message/unread?limit=100', baseUrl);
request.get({ url: url, headers: { 'User-Agent': 'lbryian/1.0.0 Node.js (by /u/lbryian)', 'Authorization': 'Bearer ' + accessToken } }, (err, res, body) => {
if (err) {
return callback(err);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
if (response.error) {
return callback(new Error(response.message));
}
console.log(`Got ${response.data.children.length} unread messages.`);
return callback(null, response.data.children);
});
};
const createOrGetUserId = (username, callback) => {
async.waterfall([
(cb) => {
db.query('SELECT Id FROM Users WHERE LOWER(Username) = ?', [username.toLowerCase()], cb);
},
(res, fields, cb) => {
if (res.length === 0) {
// user does not exist, create the user
return cb(null, 0);
}
return cb(null, res[0].Id);
},
(userId, cb) => {
if (userId === 0) {
return db.query('INSERT INTO Users (Username, Created) VALUES (?, UTC_TIMESTAMP())', [username], (err, res) => {
if (err) {
return cb(err, null);
}
return cb(null, res.insertId);
});
}
return cb(null, userId);
}
], callback);
};
const processCompletedDeposits = (callback) => {
const delay = 2000;
async.waterfall([
(cb) => {
db.query('SELECT C.DepositId, D.Amount, U.Username, U.Balance FROM CompletedDepositConfirmations C JOIN Deposits D ON D.Id = C.DepositId JOIN Users U ON U.Id = C.UserId', cb);
},
(res, fields, cb) => {
if (res.length > 0) {
return async.eachSeries(res, (completedDeposit, ecb) => {
sendPMUsingTemplate('ondeposit.completed', { how_to_use_url: config.howToUseUrl, amount: completedDeposit.Amount, balance: completedDeposit.Balance },
'Deposit completed!', completedDeposit.Username, (err) => {
if (err) {
return setTimeout(ecb, delay, err);
}
// remove the entry from the DB
return db.query('DELETE FROM CompletedDepositConfirmations WHERE DepositId = ?', [completedDeposit.DepositId], (ierr) => {
if (ierr) {
return setTimeout(ecb, delay, ierr);
}
// success
return setTimeout(ecb, delay, null, true);
});
});
// TODO: Implement inserting messages into a pending message queue instead
}, (err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
}
return cb(null, true);
}
], callback);
};
const getBalance = (userId, callback) => {
db.query('SELECT Balance FROM Users WHERE Id = ?', [userId], (err, res) => {
if (err) {
return callback(err, null);
}
return callback(0, res.length === 0 ? 0 : res[0].Balance);
});
};
const generateDepositAddress = (callback) => {
request.post({ url: config.lbrycrd.rpcurl, json: { method: 'getnewaddress', params: [config.lbrycrd.account] } }, (err, resp, body) => {
if (err || body.error) {
return callback(err || body.error, null);
}
return callback(null, body.result);
});
};
const getDepositAddress = (userId, callback) => {
let newAddress = false;
async.waterfall([
(cb) => {
db.query('SELECT DepositAddress FROM Users WHERE Id = ?', [userId], cb);
},
(res, fields, cb) => {
const address = res.length > 0 ? res[0].DepositAddress : null;
if (!address || address.trim().length === 0) {
newAddress = true;
return generateDepositAddress(cb);
}
return cb(null, address);
},
(address, cb) => {
if (newAddress) {
return db.query('UPDATE Users SET DepositAddress = ? WHERE Id = ?', [address, userId], (err) => {
if (err) {
return cb(err, null);
}
return cb(null, address);
});
}
return cb(null, address);
}
], callback);
};
const sendTip = (sender, recipient, amount, tipdata, callback) => {
console.log(`sending ${amount} LBC from ${sender} to ${recipient}`);
const data = {};
async.waterfall([
(cb) => {
// Start DB transaction
db.beginTransaction((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
},
(started, cb) => {
// start a transaction
// check the sender's balance
createOrGetUserId(sender, cb);
},
(senderId, cb) => {
data.senderId = senderId;
getBalance(senderId, cb);
},
(senderBalance, cb) => {
// balance is less than amount to tip, or the difference after sending the tip is negative
if (senderBalance < amount || (senderBalance - amount) < 0) {
return sendPMUsingTemplate('onsendtip.insufficientfunds',
{
how_to_use_url: config.howToUseUrl,
recipient: `u/${recipient}`,
amount: amount,
amount_usd: ['$', parseFloat(tipdata.amountUsd).toFixed(2)].join(''),
balance: senderBalance
}, 'Insufficient funds to send tip', tipdata.message.data.author, () => {
markMessageRead(tipdata.message.data.name, () => {
cb(new Error('Insufficient funds'), null);
});
});
}
return db.query('UPDATE Users SET Balance = Balance - ? WHERE Id = ?', [amount, data.senderId], cb);
},
(res, fields, cb) => {
// Update the recipient's balance
createOrGetUserId(recipient, cb);
},
(recipientId, cb) => {
data.recipientId = recipientId;
db.query('UPDATE Users SET Balance = Balance + ? WHERE Id = ?', [amount, recipientId], cb);
},
(res, fields, cb) => {
// save the message
const msgdata = tipdata.message.data;
db.query( ['INSERT INTO Messages (AuthorId, Type, FullId, RedditId, ParentRedditId, Subreddit, Body, Context, RedditCreated, Created) ',
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())'].join(''),
[data.senderId,
tipdata.message.kind === privateMessageKind ? 1 : 2,
msgdata.name,
msgdata.id,
msgdata.parent_id,
msgdata.subreddit,
msgdata.body,
msgdata.context,
moment.utc(msgdata.created_utc * 1000).format('YYYY-MM-DD HH:mm:ss')
], cb);
},
(res, fields, cb) => {
// save the tip information
db.query( ['INSERT INTO Tips (MessageId, SenderId, RecipientId, Amount, AmountUsd, ParsedAmount, Created) ',
'VALUES (?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())'].join(''),
[res.insertId,
data.senderId,
data.recipientId,
amount,
tipdata.amountUsd,
tipdata.parsedAmount,
], cb);
},
(res, fields, cb) => {
// reply to the source message with message template after successful commit
const amountUsdStr = parseFloat(tipdata.amountUsd).toFixed(2);
replyMessageUsingTemplate('onsendtip', { recipient: `u/${recipient}`, tip: `${amount} LBC ($${amountUsdStr})`, how_to_use_url: config.howToUseUrl},
tipdata.message.data.name, cb);
},
(success, cb) => {
// Mark the message as read
markMessageRead(tipdata.message.data.name, cb);
},
(success, cb) => {
// commit the transaction
db.commit((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
}
], (err) => {
if (err) {
return db.rollback(() => {
callback(err, null);
});
}
// success
return callback(null, true);
});
};
const convertUsdToLbc = (amount, callback) => {
request.get({ url: rateUrl }, (err, res, body) => {
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
if (!response.data || !response.data.lbc_usd) {
return callback(new Error('Could not retrieve the LBC/USD conversion rate.'));
}
const rateUsd = parseFloat(response.data.lbc_usd);
if (isNaN(rateUsd) || rateUsd === 0) {
return callback(new Error('Invalid LBC/USD rate retrieved.'));
}
const amountLbc = (amount / rateUsd).toFixed(8);
return callback(null, amountLbc);
});
};
const convertLbcToUsd = (amount, callback) => {
request.get({ url: rateUrl }, (err, res, body) => {
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
if (!response.data || !response.data.lbc_usd) {
return callback(new Error('Could not retrieve the LBC/USD conversion rate.'));
}
const rateUsd = parseFloat(response.data.lbc_usd);
if (isNaN(rateUsd) || rateUsd === 0) {
return callback(new Error('Invalid LBC/USD rate retrieved.'));
}
const amountLbc = (amount * rateUsd).toFixed(2);
return callback(null, amountLbc);
});
};
const gildThing = (thingFullId, callback) => {
const url = `${baseUrl}/api/v1/gold/gild/${thingFullId}`;
request.post({ url, headers: { 'User-Agent': config.userAgent, 'Authorization': 'Bearer ' + globalAccessToken } }, (err, res, body) => {
if (err) {
return callback(err, null);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
//return callback(e, null);
}
if (response && (response.json.ratelimit > 0 || response.json.errors.length > 0)) {
return callback(new Error('Rate limited.'), null);
}
// success
return callback(null, true);
});
};
const markMessageRead = (messageFullId, callback) => {
const url = `${baseUrl}/api/read_message`;
request.post({ url, form: { id: messageFullId }, headers: { 'User-Agent': config.userAgent, 'Authorization': 'Bearer ' + globalAccessToken } }, (err, res, body) => {
if (err) {
console.log(err);
return callback(err, null);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
// success
return callback(null, true);
});
};
const sendPMUsingTemplate = (template, substitutions, subject, recipient, callback) => {
if (!messageTemplates[template]) {
return callback(new Error(`Message template ${template} not found.`));
}
let messageText = messageTemplates[template];
for (let variable in substitutions) {
if (substitutions.hasOwnProperty(variable)) {
const re = new RegExp(['{', variable, '}'].join(''), 'ig');
messageText = messageText.replace(re, substitutions[variable]);
}
}
// send the message
const url = `${baseUrl}/api/compose`;
request.post({
url,
form: { api_type: 'json', text: messageText, subject, to: recipient },
headers: { 'User-Agent': config.userAgent, 'Authorization': 'Bearer ' + globalAccessToken }
}, (err, res, body) => {
if (err) {
return callback(err, null);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
if (response.json.ratelimit > 0 ||
response.json.errors.length > 0) {
return callback(new Error('Rate limited.'), null);
}
// success
return callback(null, true);
});
};
const replyMessageUsingTemplate = (template, substitutions, sourceMessageFullId, callback) => {
if (!messageTemplates[template]) {
return callback(new Error(`Message template ${template} not found.`));
}
let messageText = messageTemplates[template];
for (let variable in substitutions) {
if (substitutions.hasOwnProperty(variable)) {
const re = new RegExp(['{', variable, '}'].join(''), 'ig');
messageText = messageText.replace(re, substitutions[variable]);
}
}
// send the message
const url = `${baseUrl}/api/comment`;
request.post({
url,
form: { api_type: 'json', text: messageText, thing_id: sourceMessageFullId },
headers: { 'User-Agent': config.userAgent, 'Authorization': 'Bearer ' + globalAccessToken }
}, (err, res, body) => {
if (err) {
return callback(err, null);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
if (!response.json) {
return callback(new Error('Invalid response.'), null);
}
if (response.json.ratelimit > 0 ||
response.json.errors.length > 0) {
return callback(new Error('Rate limited.'), null);
}
// success
return callback(null, true);
});
};
const getMessageAuthor = (thingId, accessToken, callback) => {
const url = util.format('%s/api/info?id=%s', baseUrl, thingId);
request.get({ url: url, headers: { 'User-Agent': config.userAgent, 'Authorization': 'Bearer ' + globalAccessToken } }, (err, res, body) => {
if (err) {
return callback(err, null);
}
let response;
try {
response = JSON.parse(body);
} catch (e) {
return callback(e, null);
}
// possible 500 error
if (!response || !response.data || !response.data.children) {
return callback(new Error('Could not retrieve the message author info.'), null);
}
return callback(null, (response.data.children.length > 0) ? response.data.children[0].data.author : null);
});
};
const sendGild = (sender, recipient, amount, gilddata, callback) => {
console.log(`gilding ${recipient} with ${amount} LBC worth ${gilddata.amountUsd} from ${sender}`);
const data = {};
async.waterfall([
(cb) => {
// Start DB transaction
db.beginTransaction((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
},
(started, cb) => {
// start a transaction
// check the sender's balance
createOrGetUserId(sender, cb);
},
(senderId, cb) => {
data.senderId = senderId;
getBalance(senderId, cb);
},
(senderBalance, cb) => {
// balance is less than amount required for gilding, or the difference after sending the tip is negative
if (senderBalance < amount || (senderBalance - amount) < 0) {
return sendPMUsingTemplate('ongild.insufficientfunds',
{
how_to_use_url: config.howToUseUrl,
recipient: `u/${recipient}`,
amount: amount,
amount_usd: ['$', parseFloat(gilddata.amountUsd).toFixed(2)].join(''),
balance: senderBalance
}, 'Insufficient funds', gilddata.message.data.author, () => {
markMessageRead(gilddata.message.data.name, () => {
cb(new Error('Insufficient funds'), null);
});
});
}
return db.query('UPDATE Users SET Balance = Balance - ? WHERE Id = ?', [amount, data.senderId], cb);
},
(res, fields, cb) => {
createOrGetUserId(recipient, cb);
},
(recipientId, cb) => {
data.recipientId = recipientId;
// save the message
const msgdata = gilddata.message.data;
db.query( ['INSERT INTO Messages (AuthorId, Type, FullId, RedditId, ParentRedditId, Subreddit, Body, Context, RedditCreated, Created) ',
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())'].join(''),
[data.senderId,
gilddata.message.kind === privateMessageKind ? 1 : 2,
msgdata.name,
msgdata.id,
msgdata.parent_id,
msgdata.subreddit,
msgdata.body,
msgdata.context,
moment.utc(msgdata.created_utc * 1000).format('YYYY-MM-DD HH:mm:ss')
], cb);
},
(res, fields, cb) => {
// save the tip information
db.query( ['INSERT INTO Tips (MessageId, SenderId, RecipientId, Amount, AmountUsd, ParsedAmount, IsGild, Created) ',
'VALUES (?, ?, ?, ?, ?, ?, 1, UTC_TIMESTAMP())'].join(''),
[res.insertId,
data.senderId,
data.recipientId,
amount,
gilddata.amountUsd,
['$', config.gildPrice.toFixed(2)].join(''),
], cb);
},
(res, fields, cb) => {
// send the gild
gildThing(gilddata.message.data.parent_id, cb);
},
(success, cb) => {
// reply to the source message with message template after successful commit
const amountUsdStr = parseFloat(gilddata.amountUsd).toFixed(2);
replyMessageUsingTemplate('ongild', { sender: `u/${sender}`, recipient: `u/${recipient}`, gild_amount: `${amount} LBC ($${amountUsdStr})`, how_to_use_url: config.howToUseUrl},
gilddata.message.data.name, cb);
},
(success, cb) => {
// Mark the message as read
markMessageRead(gilddata.message.data.name, cb);
},
(success, cb) => {
// commit the transaction
db.commit((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
}
], (err) => {
if (err) {
return db.rollback(() => {
callback(err, null);
});
}
// success
return callback(null, true);
});
};
const doGild = function(message, callback) {
async.waterfall([
(cb) => {
getMessageAuthor(message.data.parent_id, globalAccessToken, cb);
},
(recipient, cb) => {
const sender = message.data.author;
if (sender !== recipient) {
return cb(null, { message, recipient, sender, amountUsd: config.gildPrice });
}
return cb(null, null);
},
(gilddata, cb) => {
if (gilddata && gilddata.amountUsd > 0) {
return convertUsdToLbc(gilddata.amountUsd, (err, convertedAmount) => {
if (err) {
return cb(err);
}
gilddata.amountLbc = convertedAmount;
return cb(null, gilddata);
});
}
return cb(null, null);
},
(data, cb) => {
if (data) {
return sendGild(data.sender, data.recipient, data.amountLbc, data, cb);
}
return cb(null, null);
}
], (err) => {
if (err) {
callback(err, null);
}
markMessageRead(message.data.name, callback);
});
};
const doSendTip = (body, message, callback) => {
/**
* accepted matched strings:
* "1 usd" or "1 lbc" or "$1"
*/
// Use regex matching
let amountUsd = 0;
let amountLbc = 0;
let matchedString = '';
const match = String(message.data.body).match(tipRegex);
if (match && match.length > 0) {
matchedString = match[0];
if (matchedString.indexOf(' ') > -1) {
const parts = matchedString.split(' ', 2);
const amount = parseFloat(parts[0]);
const unit = parts[1].toLowerCase();
if (isNaN(amount) || amount <= 0 || ['usd', 'lbc'].indexOf(unit) === -1) {
// invalid amount or unit
return sendPMUsingTemplate('onsendtip.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for send tip', message.data.author, () => {
markMessageRead(message.data.name, callback);
});
}
if (unit === 'lbc') {
amountLbc = amount;
} else {
amountUsd = amount;
}
} else {
amountUsd = parseFloat(matchedString.substring(1));
if (isNaN(amountUsd) || amountUsd <= 0) {
return sendPMUsingTemplate('onsendtip.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for send tip', message.data.author, () => {
markMessageRead(message.data.name, callback);
});
}
}
}
if (amountLbc > 0 || amountUsd > 0) {
const parsedAmount = matchedString;
// get the author of the parent message
return async.waterfall([
(cb) => {
getMessageAuthor(message.data.parent_id, globalAccessToken, cb);
},
(recipient, cb) => {
const sender = message.data.author;
if (sender !== recipient) {
return cb(null, { amountLbc, amountUsd, message, recipient, sender, parsedAmount });
}
return cb(null, null);
},
(tipdata, cb) => {
if (tipdata) {
if (tipdata.amountUsd > 0) {
return convertUsdToLbc(tipdata.amountUsd, (err, convertedAmount) => {
if (err) {
return cb(err);
}
tipdata.amountLbc = convertedAmount;
return cb(null, tipdata);
});
} else if (tipdata.amountLbc > 0 && (!tipdata.amountUsd || tipdata.amountUsd === 0)) {
return convertLbcToUsd(tipdata.amountLbc, (err, convertedAmount) => {
if (err) {
return cb(err);
}
tipdata.amountUsd = convertedAmount;
return cb(null, tipdata);
});
}
}
return cb(null, null);
},
(data, cb) => {
if (data) {
return sendTip(data.sender, data.recipient, data.amountLbc, data, cb);
}
}
], (err) => {
if (err) { callback(err, null); }
markMessageRead(message.data.name, callback);
});
}
// not a valid or recognised message, simply mark the message as read
return markMessageRead(message.data.name, callback);
};
const doSendBalance = (message, callback) => {
async.waterfall([
(cb) => {
createOrGetUserId(message.data.author, cb);
},
(authorId, cb) => {
getBalance(authorId, cb);
},
(balance, cb) => {
// send message with balance
replyMessageUsingTemplate('onbalance', { how_to_use_url: config.howToUseUrl, amount: balance }, message.data.name, cb);
},
(success, cb) => {
// mark messge as read
markMessageRead(message.data.name, cb);
}
], (err) => {
if (err) {
return callback(err, null);
}
// success
return callback(null, true);
});
};
const sendLbcToAddress = (address, amount, callback) => {
request.post({ url: config.lbrycrd.rpcurl, json: { method: 'sendfrom', params: [config.lbrycrd.account, address, amount] } }, (err, resp, body) => {
if (err || body.error) {
return callback(err || body.error, null);
}
return callback(null, body.result);
});
};
const doWithdrawal = (amount, address, message, callback) => {
const data = {};
async.waterfall([
(cb) => {
// Start DB transaction
db.beginTransaction((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
},
// prevent withdrawal to deposit address
(started, cb) => {
createOrGetUserId(message.data.author, cb);
},
(authorId, cb) => {
data.userId = authorId;
getDepositAddress(authorId, cb);
},
(depositAddress, cb) => {
if (address === depositAddress) {
return cb(new Error('Attempt to withdraw to deposit address.'), null);
}
return getBalance(data.userId, cb);
},
(balance, cb) => {
// check sufficient balance
if (balance < amount || balance - amount < 0) {
return sendPMUsingTemplate('onwithdraw.insufficientfunds', { how_to_use_url: config.howToUseUrl, amount: amount, balance: balance },
'Insufficient funds for withdrawal', message.data.author, () => {
markMessageRead(message.data.name, () => {
cb(new Error('Insufficient funds'), null);
});
});
}
// Update the balance
db.query('UPDATE Users SET Balance = Balance - ? WHERE Id = ?', [amount, data.userId], cb);
},
(res, fields, cb) => {
// Send the transaction on the blockchain
sendLbcToAddress(address, amount, cb);
},
(txhash, cb) => {
data.txhash = txhash;
// Insert the withdrawal entry
db.query('INSERT INTO Withdrawals (UserId, TxHash, Amount, Created) VALUES (?, ?, ?, UTC_TIMESTAMP())', [data.userId, txhash, amount], cb);
},
(res, fields, cb) => {
// commit the transaction
db.commit((err) => {
if (err) {
return cb(err, null);
}
return cb(null, true);
});
},
(success, cb) => {
// mark messge as read
markMessageRead(message.data.name, cb);
},
(success, cb) => {
// send a reply
replyMessageUsingTemplate('onwithdraw', { how_to_use_url: config.howToUseUrl, address: address, amount: amount, txid: data.txhash }, message.data.name, cb);
}
], (err) => {
if (err) {
return db.rollback(() => {
callback(err, null);
});
}
// success
return callback(null, true);
});
};
const doSendDepositAddress = (message, callback) => {
async.waterfall([
(cb) => {
createOrGetUserId(message.data.author, cb);
},
(authorId, cb) => {
getDepositAddress(authorId, cb);
},
(address, cb) => {
// send message with balance
replyMessageUsingTemplate('ondeposit', { how_to_use_url: config.howToUseUrl, address: address }, message.data.name, cb);
},
(success, cb) => {
// mark messge as read
markMessageRead(message.data.name, cb);
}
], (err) => {
if (err) {
return callback(err, null);
}
// success
return callback(null, true);
});
};
// Commands
// balance (PM)
// deposit (PM)
// tip (Comment): <amount> <unit> u/lbryian
// withdraw (PM): withdraw <amount> <address>
const processMessage = function(message, callback) {
if (!message.kind || !message.data) {
//console.log('Invalid message encountered.');
return callback(new Error('Invalid message specified for processing.'));
}
const body = String(message.data.body).trim();
if (message.kind === privateMessageKind) {
// balance, deposit or withdraw
// Check the command
if ('balance' === body.toLowerCase()) {
// do balance check
return doSendBalance(message, callback);
} else if ('deposit' === body.toLowerCase()) {
// send deposit address
return doSendDepositAddress(message, callback);
} else {
// withdrawal
const parts = body.split(' ');
if (parts.length !== 3 ||
parts[0].toLowerCase() !== 'withdraw') {
// invalid message, ignore
//console.log("Invalid Message=" + body);
return markMessageRead(message.data.name, callback);
}
const amount = parseFloat(parts[1]);
if (isNaN(amount) || amount < 0) {
// TODO: send a message that the withdrawal amount is invalid
return sendPMUsingTemplate('onwithdraw.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for withdrawal', message.data.author, () => {
markMessageRead(message.data.name, callback);
});
}
if (amount <= config.lbrycrd.txfee) {
return sendPMUsingTemplate('onwithdraw.amountltefee', { how_to_use_url: config.howToUseUrl, amount: amount, fee: config.lbrycrd.txfee },
'Withdrawal amount less than minimum fee', message.data.author, () => {
markMessageRead(message.data.name, callback);
});
}
// base58 check the address
const address = parts[2];
try {
base58.decode(address);
} catch(e) {
return sendPMUsingTemplate('onwithdraw.invalidaddress', { how_to_use_url: config.howToUseUrl }, 'Invalid address for withdrawal', message.data.author, () => {
markMessageRead(message.data.name, callback);
});
}
return doWithdrawal(amount, address, message, callback);
}
return markMessageRead(message.data.name, callback);
}