-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
641 lines (617 loc) · 28.1 KB
/
start.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
/*jslint node: true */
"use strict";
var conf = require('trustnote-common/conf.js');
var eventBus = require('trustnote-common/event_bus.js');
var headlessWallet = require('trustnote-headless');
var desktopApp = require('trustnote-common/desktop_app.js');
var db = require('trustnote-common/db.js');
var network = require('./network.js');
var sendLockups = require('./lockup.js');
var util = require('./util.js');
const SESSION_TIMEOUT = 600*1000;
const langs = ['english', 'English', 'EN', 'en', 'chinese', '中文', 'CN', 'cn'];
var assocSessions = {};
var users_status={};
function resumeSession(from_address){
if (!assocSessions[from_address])
assocSessions[from_address] = {};
assocSessions[from_address].ts = Date.now();
}
function sendMessageToDevice(device_address, text){
var device = require('trustnote-common/device.js');
device.sendMessageToDevice(device_address, 'text', text);
}
function sendGreetingCN(from_address){
// get lockup service information
network.getLockupMenu('/financial/home.htm', function(res, error, status_code) {
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code);
return;
}
if(!res){
return sendMessageToDevice(from_address, '该活动未开启,敬请期待')
}
// updateLockupMenu(res);
var greeting_res = '';
var lockup_count = res.length;
greeting_res += '当前共有'+lockup_count+'种套餐,每期都需要提前抢购,抢购时间结束或额度完成,则募集结束。 参与活动的用户还将获得一定数量的TFans(通证符号为TFS),收益和TFS到账时间为解锁后第二天 ,周末及节假日顺延。\n';
network.getActivityStatus('/financial-lockup/participate.htm', function(res2, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code);
return;
}
var total_users = res2["total_user"];
var total_amount = res2["total_amount"];
greeting_res += '当前共有 --'+ util.formatNumbers(total_users)+'人-- 参与, 已抢购 --'+util.formatNumbers(total_amount)+'MN-- ,欢迎选择套餐参与\n\n';
res.map(function(lockup) {
greeting_res+='['+lockup["financialName"]+lockup["financialRate"]*100+'%]';
greeting_res+='(command:#';
greeting_res+=lockup["financialBenefitsId"];
greeting_res+=')\n';
})
// greeting_res+='\n输入套餐id查询套餐状态';
sendMessageToDevice(from_address, greeting_res);
// sendMessageToDevice(from_address, DEFAULT_GREETING);
assocSessions[from_address].greeting_ts = Date.now();
})
});
}
function sendGreeting(from_address){
// get lockup service information
network.getLockupMenu('/financial/home.htm', function(res, error, status_code) {
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code);
return;
}
if(!res){
return sendMessageToDevice(from_address, 'TTT High Interest Saver is not online yet, stay tuned!')
}
// updateLockupMenu(res);
var greeting_res = '7~360 days term deposit, choose a term suits you, no account or application fees, guaranteed rate of return.\n\nTerm, Interest Paid at Maturity\n';
network.getActivityStatus('/financial-lockup/participate.htm', function(res2, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, error code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code);
return;
}
var total_users = res2["total_user"];
var total_amount = res2["total_amount"];
res.map(function(lockup) {
greeting_res+='['+lockup["financialName"].match(/\d+/g)+' days, '+lockup["financialRate"]*100+'% p.a.]';
greeting_res+='(command:#';
greeting_res+=lockup["financialBenefitsId"];
greeting_res+=')\n';
})
greeting_res+='\nAs a promotion, all participants will also receive a certain number of TFans token (symbol: TFS) as a reward.\n\nYour principal, interest and TFS will be paid on the second business day following the day when the term ends.\n\n';
greeting_res+='Limited offer only, '+total_users+' participants saved '+util.formatNumbers(total_amount)+'MN TTT already, take your deposit opportunities now before they are all sold!';
sendMessageToDevice(from_address, greeting_res);
// sendMessageToDevice(from_address, DEFAULT_GREETING);
assocSessions[from_address].greeting_ts = Date.now();
})
});
}
function purgeOldSessions(){
console.log('purging old sessions');
var cutoff_ts = Date.now() - SESSION_TIMEOUT;
for (var from_address in assocSessions)
if (assocSessions[from_address].ts < cutoff_ts)
delete assocSessions[from_address];
}
setInterval(purgeOldSessions, SESSION_TIMEOUT);
eventBus.on('headless_wallet_ready', function(){
// get lockup service information
if (!conf.admin_email || !conf.from_email){
console.log("please specify admin_email and from_email in your "+desktopApp.getAppDataDir()+'/conf.json');
process.exit(1);
}
network.getLockupMenu('/financial/home.htm', function(menu, error, status_code) {
if (error) {
console.log(error)
return;
} else if (status_code) {
console.log('Status code: ' + status_code);
return;
}
// updateLockupMenu(menu);
});
});
function getUserLang(from_address, callback) {
db.query("select lang from states where from_address=?", [from_address], function(rows){
if(rows.length===0) return callback('en')
return callback(rows[0].lang)
})
}
function updateUserLang(from_address, lang, callback) {
db.query("select lang from states where from_address=?", [from_address], function(rows){
if(rows.length>0){
db.query("update states set lang=? where from_address=?", [lang, from_address], function(){
callback()
})
return
}
db.query("insert or ignore into states (lang, from_address) values (?,?)", [lang, from_address], function() {
callback()
})
})
}
eventBus.on('paired', function(from_address){
console.log('paired '+from_address);
if (headlessWallet.isControlAddress(from_address))
headlessWallet.handlePairing(from_address);
resumeSession(from_address);
sendMessageToDevice(from_address, 'Welcome to the TTT High Interest Saver, a deposit service provides high interest with variable terms. Please select your language:—[English](command:English)\n(or enter English/english/EN/en)\n—[中文](command:中文)\n(or enter中文/CN/cn)');
});
eventBus.on('text', function(from_address, text){
console.log('text from '+from_address+': '+text);
if (headlessWallet.isControlAddress(from_address))
headlessWallet.handleText(from_address, text);
resumeSession(from_address);
text = text.trim();
// for debug, to test client's and bot's connection status
// if (text.match(/hello/i))
// return sendMessageToDevice(from_address, DEFAULT_GREETING);
if (text.match(/^语言|Language$/i)) {
return sendMessageToDevice(from_address, 'Welcome to the TTT High Interest Saver, a deposit service provides high interest with variable terms. Please select your language:\n—[English](command:English)\n(or enter English/english/EN/en)\n—[中文](command:中文)\n(or enter中文/CN/cn)')
}
if (langs.indexOf(text)>=0){
updateUserLang(from_address, text, function() {
switch(text) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
sendGreetingCN(from_address);
break;
default:
sendGreeting(from_address);
break;
}
return;
})
return;
}
getUserLang(from_address, function(language){
if (text.match(/^(锁仓激励服务|LockupServices)/i)){
switch(language) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
sendGreetingCN(from_address);
break;
default:
sendGreeting(from_address);
break;
}
return;
}
// pre-purchase lockup
if (text.match(/^[A-Z2-7]{32}#\d+$/)) {
// get user's address
var address = text.match(/\b[A-Z2-7]{32}\b/)[0];
var arrNumbers = text.split("#");
// get lockup term
var lockupId = arrNumbers[1];
network.getLockupInfo('/financial-benefits/push_benefitid.htm', lockupId, function(info, error, status_code){
switch(language) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500')
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'请[重新发起流程](command:锁仓激励服务)');
return;
}
if (!info){
return sendMessageToDevice(from_address, '活动暂未开启,请参与[其他套餐](command:锁仓激励服务)');
}
// get unlock date
var unlock_date = info["interestEndTime"];
var panicStarttime = info["panicStartTime"]
var panicEndtime = info["panicEndTime"]
// validate activity date
if(Date.now() <= panicStarttime){
return sendMessageToDevice(from_address, "该活动未开启,敬请期待");
}
if(Date.now() >= panicEndtime){
return sendMessageToDevice(from_address, "该活动已结束,请参与[其他套餐](command:锁仓激励服务)");
}
users_status[from_address] = {
"address": address,
"lockupId": lockupId,
"unlock_date": unlock_date,
"language": language
}
if (info["minAmount"] == info["purchaseLimit"]){
users_status[from_address]["amount"] = info["minAmount"];
var myAddress = users_status[from_address]["address"]
var myLockupId = users_status[from_address]["lockupId"];
var myMinAmount = info["minAmount"];
return sendLockups.prePurchaseLockup(from_address, myAddress, myMinAmount, myLockupId, myMinAmount, language);
}
return sendMessageToDevice(from_address, '请输入抢购数量(例如:25000MN)');
break;
default:
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500')
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'请[重新发起流程](command:锁仓激励服务)');
return;
}
if (!info){
return sendMessageToDevice(from_address, 'This term is not online yet, please choose [other terms](command:LockupServices)');
}
// get unlock date
var unlock_date = info["interestEndTime"];
var panicStarttime = info["panicStartTime"]
var panicEndtime = info["panicEndTime"]
// validate activity date
if(Date.now() <= panicStarttime){
return sendMessageToDevice(from_address, "该活动未开启,敬请期待");
}
if(Date.now() >= panicEndtime){
return sendMessageToDevice(from_address, "This term is over, please choose [other terms](command:LockupServices)");
}
users_status[from_address] = {
"address": address,
"lockupId": lockupId,
"unlock_date": unlock_date
}
if (info["minAmount"] == info["purchaseLimit"]){
users_status[from_address]["amount"] = info["minAmount"];
var myAddress = users_status[from_address]["address"]
var myLockupId = users_status[from_address]["lockupId"];
var myMinAmount = info["minAmount"];
return sendLockups.prePurchaseLockup(from_address, myAddress, myMinAmount, myLockupId, myMinAmount, language);
}
return sendMessageToDevice(from_address, 'Please enter your deposit amont(e.g. 25000MN)');
break;
}
})
// check if lockup is available
return;
}
if (text.match(/^\d+(MN)?$/i)) {
switch(language) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
var amount = text.match(/^\d+/)[0];
if (!users_status[from_address]) {
sendMessageToDevice(from_address, '请[先选择套餐](command:锁仓激励服务)');
return sendGreeting(from_address);
}
var myAddress = users_status[from_address]["address"];
var myLockupId = parseInt(users_status[from_address]["lockupId"]);
if(!myAddress || !myLockupId){
return sendMessageToDevice(from_address, '数据缺失,请[重新发起流程](command:锁仓激励服务)');
}
if(isNaN(myLockupId)){
return sendMessageToDevice(from_address, '套餐ID错误,请[重新发起流程](command:锁仓激励服务)');
}
network.getLockupInfo('/financial-benefits/push_benefitid.htm', myLockupId, function(info, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'请[重新发起流程](command:锁仓激励服务)');
return;
}
if(!info){
return sendMessageToDevice(from_address, '活动暂未开启,请参与[其他套餐](command:锁仓激励服务)');
}
var myMinAmount = info["minAmount"];
var myMaxAmount = info["purchaseLimit"]!=null ? info["purchaseLimit"] : Infinity;
var remain = info["remainLimit"]!=null ? info["remainLimit"] : Infinity;
if (amount<myMinAmount) {
return sendMessageToDevice(from_address, '最低金额不能小于'+myMinAmount+'MN,请重新输入');
}
if (amount>myMaxAmount) {
return sendMessageToDevice(from_address, '最高金额不能大于'+myMaxAmount+'MN,请重新输入');
}
if (amount>remain){
return sendMessageToDevice(from_address, '剩余额度不足,该套餐剩余额度为:'+( remain>=0 ? remain : 0 )+'MN,请选择更低的购买额度');
}
users_status[from_address]={};
return sendLockups.prePurchaseLockup(from_address, myAddress, amount, myLockupId, myMaxAmount, language);
});
break;
default:
var amount = text.match(/^\d+/)[0];
if (!users_status[from_address]) {
sendMessageToDevice(from_address, '请[先选择套餐](command:锁仓激励服务)');
return sendGreeting(from_address);
}
var myAddress = users_status[from_address]["address"];
var myLockupId = parseInt(users_status[from_address]["lockupId"]);
if(!myAddress || !myLockupId){
return sendMessageToDevice(from_address, 'Missing some data, please [retry](command:LockupServices)');
}
if(isNaN(myLockupId)){
return sendMessageToDevice(from_address, 'Wrong Lockup ID, please [retry](command:LockupSerivces)');
}
network.getLockupInfo('/financial-benefits/push_benefitid.htm', myLockupId, function(info, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'please [retry](command:LockupSerivces)');
return;
}
if(!info){
return sendMessageToDevice(from_address, 'This term is over, please choose [other terms](command:LockupServices)');
}
var myMinAmount = info["minAmount"];
var myMaxAmount = info["purchaseLimit"]!=null ? info["purchaseLimit"] : Infinity;
var remain = info["remainLimit"]!=null ? info["remainLimit"] : Infinity;
if (amount<myMinAmount) {
return sendMessageToDevice(from_address, 'Minimum amount is '+myMinAmount+'MN, please re-enter');
}
if (amount>myMaxAmount) {
return sendMessageToDevice(from_address, 'Maximum amount is'+myMaxAmount+'MN, please re-enter');
}
if (amount>remain){
return sendMessageToDevice(from_address, '剩余额度不足,该套餐剩余额度为:'+( remain>=0 ? remain : 0 )+'MN,请选择更低的购买额度');
}
users_status[from_address]={};
return sendLockups.prePurchaseLockup(from_address, myAddress, amount, myLockupId, myMaxAmount, language);
});
break;
}
return;
}
// return lockup detail
if (text.match(/^#\d+$/)){
switch(language) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
var lockupId = text.match(/\d+/);
network.getLockupInfo('/financial-benefits/push_benefitid.htm', lockupId, function(info, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'请[重新发起流程](command:锁仓激励服务)');
return;
}
if (!info){
return sendMessageToDevice(from_address, '活动暂未开启,请参与[其他套餐](command:锁仓激励服务)');
}
if(info["activityStatus"] == "抢购已结束" && info["nextPanicStartTime"]!=null && info["nextPanicStartTime"]!=0 && Date.now()>info["nextPanicStartTime"]){
return sendMessageToDevice(from_address, '活动已过期,请重新发起[锁仓激励服务](command:锁仓激励服务)')
}
var lockupDetail = ('产品名称: ' + info["productName"] +'\n\n');
lockupDetail += ('抢购时间: ' + util.timestampToDate(info["panicStartTime"]) + ' -- ' + util.timestampToDate(info["panicEndTime"]) +'\n');
lockupDetail += ('计息时间: ' + util.timestampToDate(info["interestStartTime"]) + ' -- '+ util.timestampToDate(info["interestEndTime"]) +'\n');
lockupDetail += ('解锁时间: ' + util.timestampToDate(info["unlockTime"]) +'\n');
if(info["panicTotalLimit"]!=null){
lockupDetail += ('\n抢购总额度: ' + util.formatNumbers(info["panicTotalLimit"]) + 'MN\n');
} else {
lockupDetail += '\n抢购总额度: 无上限\n';
}
lockupDetail += ('起购额度: ' + util.formatNumbers(info["minAmount"]) + 'MN\n');
if(info["purchaseLimit"]!=null){
lockupDetail += ('限购额度: ' + util.formatNumbers(info["purchaseLimit"]) + 'MN\n');
} else {
lockupDetail += '限购额度: 无上限\n';
}
if (info["remainLimit"]!=null){
lockupDetail += ('剩余额度: ' + (info["remainLimit"] ? util.formatNumbers(info["remainLimit"]) : 0) + 'MN\n');
}
lockupDetail += '获得TFS数量:收益*' + info["tFans"] +'\n';
// validate activity date
if(info["activityStatus"] == "未开启"){
lockupDetail += ('\n状态: 未开启 \n'); // test: _blue_ -blue- +red+ formal: __blue__ --blue-- ++red++
} else if(info["activityStatus"] == "抢购已结束"){
// remove expired lockup
db.query('delete from user_status where lockupId=?', [lockupId], function(){
console.log('Remove expired lockup, lockupId: '+lockupId);
});
lockupDetail += ('\n状态: ++抢购已结束++ \n'); // _blue_ -blue- +red+
} else {
lockupDetail += ('\n状态: --抢购进行中-- \n'); // _blue_ -blue- +red+
}
if(info["nextPanicStartTime"] && info["nextPanicEndTime"]){
lockupDetail += '\n下期抢购时间: '
lockupDetail += (util.timestampToDate(info["nextPanicStartTime"]) + ' -- ' + util.timestampToDate(info["nextPanicEndTime"]));
} else {
lockupDetail += '\n下期抢购时间: 敬请期待'
}
sendMessageToDevice(from_address, lockupDetail);
if(info["activityStatus"]=="抢购进行中"){
sendMessageToDevice(from_address, '请点击[#'+ lockupId +'](command:#' + lockupId +')#开始抢购');
}
})
break;
default:
var lockupId = text.match(/\d+/);
network.getLockupInfo('/financial-benefits/push_benefitid.htm', lockupId, function(info, error, status_code){
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Status code: ' + status_code +'please [retry](command:LockupSerivces)');
return;
}
if (!info){
return sendMessageToDevice(from_address, 'This term is not online yet, please choose [other terms](command:LockupServices)');
}
if(info["activityStatus"] == "抢购已结束" && info["nextPanicStartTime"]!=null && info["nextPanicStartTime"]!=0 && Date.now()>info["nextPanicStartTime"]){
return sendMessageToDevice(from_address, 'This term is over, please choose [other terms](command:LockupServices)')
}
var lockupDetail = ('Product Name: ' + info["productName"] +'\n\n');
lockupDetail += ('Opening Time: ' + util.timestampToDateEN(info["panicStartTime"]) + ' -- ' + util.timestampToDateEN(info["panicEndTime"]) +'\n');
lockupDetail += ('Interest Period: ' + util.timestampToDateEN(info["interestStartTime"]) + ' -- '+ util.timestampToDateEN(info["interestEndTime"]) +'\n');
lockupDetail += ('Maturity Date: ' + util.timestampToDateEN(info["unlockTime"]) +'\n');
if(info["panicTotalLimit"]!=null){
lockupDetail += ('\nTotal Amount: ' + util.formatNumbers(info["panicTotalLimit"]) + 'MN\n');
} else {
lockupDetail += '\nTotal Amount: no limit\n';
}
lockupDetail += ('Minimum Amount: ' + util.formatNumbers(info["minAmount"]) + 'MN\n');
if(info["purchaseLimit"]!=null){
lockupDetail += ('Maximum: ' + util.formatNumbers(info["purchaseLimit"]) + 'MN\n');
} else {
lockupDetail += 'Maximum: on limit\n';
}
if (info["remainLimit"]!=null){
lockupDetail += ('Available for deposit: ' + (info["remainLimit"] ? util.formatNumbers(info["remainLimit"]) : 0) + 'MN\n');
}
lockupDetail += 'The number of TFS reward: income*' + info["tFans"] +'\n';
// validate activity date
if(info["activityStatus"] == "未开启"){
lockupDetail += ('\Status: unopened \n'); // test: _blue_ -blue- +red+ formal: __blue__ --blue-- ++red++
} else if(info["activityStatus"] == "抢购已结束"){
// remove expired lockup
db.query('delete from user_status where lockupId=?', [lockupId], function(){
console.log('Remove expired lockup, lockupId: '+lockupId);
});
lockupDetail += ('\Status: ++sold++ ++out++ \n'); // _blue_ -blue- +red+
} else {
lockupDetail += ('\Status: --ongoing-- \n'); // _blue_ -blue- +red+
}
if(info["nextPanicStartTime"] && info["nextPanicEndTime"]){
lockupDetail += '\nNext term: '
lockupDetail += (util.timestampToDateEN(info["nextPanicStartTime"]) + ' -- ' + util.timestampToDateEN(info["nextPanicEndTime"]));
} else {
lockupDetail += '\nNext term is coming soon!'
}
sendMessageToDevice(from_address, lockupDetail);
if(info["activityStatus"]=="抢购进行中"){
sendMessageToDevice(from_address, 'Please click [#'+ lockupId +'](command:#' + lockupId +')# to start deposit');
}
})
break;
}
return;
};
sendMessageToDevice(from_address, 'The information you entered cannot be identified, Please [retry.](command:LockupServices)\n');
})
});
// validate commission and create lockup address
eventBus.on('received_payment', function(from_address, amount, asset){
// validate commission and create shared address
if(asset!=='base' || amount!==100000) {
return;
}
// 查找未完成的合约
db.query('select * from user_status where from_address=? and sent=0 order by create_ts desc', [from_address], function(rows){
if(rows.length===0){
return;
}
var address = rows[0].address;
var amount = rows[0].amount;
var lockupId = rows[0].lockupId;
if(!lockupId){
return sendMessageToDevice(from_address, '未选择锁仓激励服务');
}
network.getLockupInfo('/financial-benefits/push_benefitid.htm', lockupId, function(info, error, status_code, code){
var unlock_date;
var panicStarttime;
var panicEndtime;
var remailLimit;
getUserLang(from_address, function(language){
console.log('===info===: '+info)
switch(language) {
case 'chinese':
case '中文':
case 'cn':
case 'CN':
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Lockup: '+lockupId +'Status code: ' + status_code +'请[重新发起流程](command:锁仓激励服务)');
return;
}
if(!info){
console.log('Error: '+ code + '#' + info+ '#' + lockupId + '不存在');
return sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code:' + code + '#' + info+ '#' + lockupId + 'and [retry](command:LockupServices)');
}
unlock_date = info["unlockTime"];
panicStarttime = info["panicStartTime"];
panicEndtime = info["panicEndTime"];
remailLimit = info["remainLimit"] == null ? Infinity : info["remainLimit"];
// validate activity date
if(Date.now() <= panicStarttime){
return sendMessageToDevice(from_address, "该活动未开启,敬请期待");
}
if(Date.now() >= panicEndtime || remailLimit<=0){
// remove expired lockup
db.query('delete from user_status where lockupId=?', [lockupId], function(){
console.log('Remove expired lockup, lockupId: '+lockupId);
});
return sendMessageToDevice(from_address, "该活动已结束,请参与[其他套餐](command:锁仓激励服务)");
}
console.log('===device_address===: '+from_address+' will purchase lock up')
// create and store shared address, send result to user and server
sendLockups.purchaseLockup(from_address, address, amount, lockupId, unlock_date, language)
break;
default:
if (error) {
console.log('Error: ', error);
sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code: 500');
return;
} else if (status_code) {
sendMessageToDevice(from_address, 'Lockup: '+lockupId +'Status code: ' + status_code +'please [retry](command:LockupSerivces)');
return;
}
if(!info){
console.log('Error: '+ code + '#' + info+ '#' + lockupId + 'DoesnotExist');
return sendMessageToDevice(from_address, 'This Services seems to have some problems, please contect Trustnote staff, code:' + code + '#' + info+ '#' + lockupId + 'and [retry](command:LockupServices)');
}
unlock_date = info["unlockTime"];
panicStarttime = info["panicStartTime"];
panicEndtime = info["panicEndTime"];
remailLimit = info["remainLimit"] == null ? Infinity : info["remainLimit"];
// validate activity date
if(Date.now() <= panicStarttime){
return sendMessageToDevice(from_address, "该活动未开启,敬请期待");
}
if(Date.now() >= panicEndtime || remailLimit<=0){
// remove expired lockup
db.query('delete from user_status where lockupId=?', [lockupId], function(){
console.log('Remove expired lockup, lockupId: '+lockupId);
});
return sendMessageToDevice(from_address, "This term is over, please choose [other terms](command:LockupServices)");
}
console.log('===device_address===: '+from_address+' will purchase lock up')
// create and store shared address, send result to user and server
sendLockups.purchaseLockup(from_address, address, amount, lockupId, unlock_date, language)
break;
}
})
return;
});
});
});
module.exports = headlessWallet;