This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfaucet.js
206 lines (155 loc) · 5.73 KB
/
faucet.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
"use strict";
var desktopApp = require('ocore/desktop_app.js');
var conf = require('ocore/conf.js');
var db = require('ocore/db.js');
var eventBus = require('ocore/event_bus.js');
var headlessWallet = require('headless-obyte');
var validationUtils = require("ocore/validation_utils.js");
var notifications = require('./notifications.js');
var wallet_id;
var arrPeers = [];
const coolDown = 24 * 3600 * 1000; //user is allowed to receive token again after this time
var assets = {
'Tangos': {
hash: '0Qki2BWSJ80dMN58Dq1rrJugaYyFndxkZloLJby+olU=',
qty: 1100000,
txtAmount: '1100000'
},
'Tingos': {
hash: 'O1DbJWbZJfKhjZQYH5RrdRQ2ojMuo2WiaKbjIWSWd4E=',
qty: 9900000,
txtAmount: '9900000'
},
'Zingos': {
hash: 'FPboi/y+Vo008aCHgPsEpSDQe1dogkKH0e1Z/iCcH84=',
qty: 1200077,
txtAmount: '12000.77'
},
'Zangos': {
hash: '3qacTCGOkrzYRyekq8yRIT/Pc30Pb/BcmTEHDY/aTz0=',
qty: 9900099,
txtAmount: '99000.99'
},
'Credits': {
hash: 'o/VVwWXXGpb/XZ1fHh0m/SSgyNWydcYKPk6ScsJ4WRM=',
qty: 1000,
txtAmount: '1000'
}
};
eventBus.on('headless_wallet_ready', function() {
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);
}
headlessWallet.readSingleWallet(function(_wallet_id) {
wallet_id = _wallet_id;
});
setInterval(function() {
for (var index in arrPeers) {
arrPeers[index].transactionsCount = 0;
arrPeers[index].assetsSent = [];
}
},
coolDown
);
});
function prepareBalanceText(handleBalanceText) {
var Wallet = require('ocore/wallet.js');
Wallet.readBalance(wallet_id, function(assocBalances) {
var arrLines = [];
for (var asset in assocBalances) {
var total = assocBalances[asset].stable + assocBalances[asset].pending;
var units = (asset === 'base') ? ' bytes' : (' of ' + asset);
var line = total + units;
if (assocBalances[asset].pending)
line += ' (' + assocBalances[asset].pending + ' pending)';
arrLines.push(line);
}
handleBalanceText(arrLines.join("\n"));
});
}
function processTxt(from_address, text) {
var device = require('ocore/device.js');
if (!arrPeers[from_address]) {
arrPeers[from_address] = {};
arrPeers[from_address].assetsSent = [];
}
if (text === 'balance') {
prepareBalanceText(function(balances) {
device.sendMessageToDevice(from_address, 'text', balances);
});
}
if (text === 'cancel') {
arrPeers[from_address].step = 'home';
}
if (assets[text] && arrPeers[from_address].assetsSent.indexOf(text) == -1) {
if (Object.keys(assets).length > arrPeers[from_address].assetsSent.length) {
arrPeers[from_address].assetToSend = text;
arrPeers[from_address].step = 'giveAdress';
device.sendMessageToDevice(from_address, 'text', 'Insert your address to receive ' + text + " (click on '...' bottom left)\n➡" + getTxtCommandButton("cancel"));
} else {
device.sendMessageToDevice(from_address, 'text', "Sorry you've already received enough tokens.");
}
return;
}
if (arrPeers[from_address].step == 'giveAdress') {
if (validationUtils.isValidAddress(text.trim())) {
headlessWallet.issueChangeAddressAndSendPayment(assets[arrPeers[from_address].assetToSend].hash, assets[arrPeers[from_address].assetToSend].qty, text.trim(), from_address, function() {
arrPeers[from_address].step = 'yesNo';
arrPeers[from_address].address = text.trim();
arrPeers[from_address].assetsSent.push(arrPeers[from_address].assetToSend);
notifications.notifyAdmin("Someone received " + arrPeers[from_address].assetToSend, from_address + " received " + arrPeers[from_address].assetToSend);
if (Object.keys(assets).length > arrPeers[from_address].assetsSent.length) {
device.sendMessageToDevice(from_address, 'text', 'Would you like to receive another token? \n➡ ' + getTxtCommandButton("yes") + ' \n➡ ' + getTxtCommandButton("no"));
return;
} else {
sendThankYouMsg(from_address);
arrPeers[from_address].step = 'home';
return;
}
});
} else {
device.sendMessageToDevice(from_address, 'text', 'Incorrect address, try again \n➡' + getTxtCommandButton("cancel"));
return;
}
return;
}
if (arrPeers[from_address].step == 'yesNo') {
if (text.trim() == 'yes') {
arrPeers[from_address].step = 'home';
} else {
sendThankYouMsg(from_address);
arrPeers[from_address].step = 'home';
return;
}
}
var assetsList = '';
if (Object.keys(assets).length > arrPeers[from_address].assetsSent.length) {
for (var index in assets) {
if (arrPeers[from_address].assetsSent.indexOf(index) == -1) {
assetsList += getTxtCommandButton(assets[index].txtAmount + ' ' + index, index) + "\n";
}
}
device.sendMessageToDevice(from_address, 'text', 'Tangos and Tingos are new tokens on the Obyte platform with zero monetary value, for your zero-risk practice with textcoin, smart contracts etc. Zangos and Zingos are like Tangos and Tingos but with decimal points. See https://wiki.obyte.org/Fun-coins for all details.\n\n Select the token you want to receive:\n' + assetsList);
} else {
device.sendMessageToDevice(from_address, 'text', "Sorry, you've already received enough tokens");
}
}
function sendThankYouMsg(from_address) {
var device = require('ocore/device.js');
setTimeout(function() {
device.sendMessageToDevice(from_address, 'text', 'Thank you very much. Now have fun playing with textcoins, smart contracts etc.');
}, 300);
}
eventBus.on('paired', function(from_address) {
processTxt(from_address, 'hi');
});
eventBus.on('text', function(from_address, text) {
processTxt(from_address, text);
});
function getTxtCommandButton(label, command) {
var text = "";
var _command = command ? command : label;
text += "[" + label + "]" + "(command:" + _command + ")";
return text;
}