-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.cpp
More file actions
390 lines (356 loc) · 11.7 KB
/
contract.cpp
File metadata and controls
390 lines (356 loc) · 11.7 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
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
#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>
#include <eosio/system.hpp>
using namespace eosio;
using namespace std;
#include <chrono>
CONTRACT mycontract : public contract
{
public:
using contract::contract;
TABLE config
{
uint64_t id;
string description;
int64_t intvalue;
time_point timevalue;
uint64_t primary_key() const
{
return id;
}
};
using config_table = eosio::multi_index<"configs"_n, config>;
TABLE slots
{
name user;
uint32_t slots;
uint32_t slotsused;
string sec;
uint64_t primary_key() const
{
return user.value;
}
};
using slot_table = eosio::multi_index<"slots"_n, slots>;
TABLE uploads
{
uint64_t id;
name user;
time_point createdat;
string thumbipfshash;
string uploadipfshash;
uint64_t primary_key() const
{
return id;
}
};
using upload_table = eosio::multi_index<"uploads"_n, uploads>;
uint64_t configid_freeslotsperuser = 1;
uint64_t configid_freeslotsperday = 2;
uint64_t configid_freeslotsusedtoday = 3;
uint64_t configid_freeslotslastreset = 4;
uint64_t configid_freeslotsusedsum = 5;
uint64_t configid_slotsusedsum = 6;
uint64_t configid_priceforslot = 10;
int64_t config_freeslotsperuser = 1;
int64_t config_freeslotsperday = 50;
int64_t config_freeslotsusedtoday = 0;
int64_t config_freeslotslastreset = 0;
int64_t config_freeslotsusedsum = 0;
int64_t config_slotsusedsum = 0;
int64_t config_priceforslot = 1000;
ACTION init()
{
require_auth(get_self());
config_table configs(get_self(), get_self().value);
auto itr = configs.find(configid_freeslotsperuser);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_freeslotsperuser;
row.description = "Free slots per user";
row.intvalue = config_freeslotsperuser; });
}
itr = configs.find(configid_freeslotsperday);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_freeslotsperday;
row.description = "Free slots per day";
row.intvalue = config_freeslotsperday; });
}
itr = configs.find(configid_freeslotsusedtoday);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_freeslotsusedtoday;
row.description = "Free slots used today";
row.intvalue = config_freeslotsusedtoday; });
}
itr = configs.find(configid_freeslotslastreset);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_freeslotslastreset;
row.description = "Free slots last reset";
row.timevalue = eosio::current_time_point(); });
}
itr = configs.find(configid_freeslotsusedsum);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_freeslotsusedsum;
row.description = "Free slots used sum";
row.intvalue = config_freeslotsusedsum; });
}
itr = configs.find(configid_slotsusedsum);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_slotsusedsum;
row.description = "Slots used sum";
row.intvalue = config_slotsusedsum; });
}
itr = configs.find(configid_priceforslot);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = configid_priceforslot;
row.description = "Price for slot";
row.intvalue = config_priceforslot;
row.timevalue = eosio::current_time_point(); });
}
print("Contract initialized");
}
ACTION clearconfig()
{
require_auth(get_self());
config_table configs(get_self(), get_self().value);
// Clear config table, scope is self
auto itr = configs.begin();
while (itr != configs.end())
{
itr = configs.erase(itr);
}
}
ACTION setconfig(uint64_t id, string description, int64_t intvalue, time_point timevalue)
{
require_auth(get_self());
config_table configs(get_self(), get_self().value);
auto itr = configs.find(id);
if (itr == configs.end())
{
configs.emplace(get_self(), [&](auto &row)
{
row.id = id;
row.description = description;
row.intvalue = intvalue;
row.timevalue = timevalue; });
}
else
{
configs.modify(itr, get_self(), [&](auto &row)
{
row.description = description;
row.intvalue = intvalue;
row.timevalue = timevalue; });
}
}
ACTION getslot(name accountname, string pubkey)
{
require_auth(accountname);
// Check if there a free slot available
checkresetforfreeslots();
config_table configs(get_self(), get_self().value);
auto itr_config = configs.find(configid_freeslotsusedtoday);
auto itr_config_sum = configs.find(configid_freeslotsusedsum);
if (itr_config != configs.end())
{
int64_t freeslotsusedtoday = itr_config->intvalue;
if (freeslotsusedtoday >= config_freeslotsperday)
{
eosio::check(false, "No free slots available for today");
}
}
slot_table slots(get_self(), get_self().value);
// Check if user is already exists in the table
auto itr_slots = slots.find(accountname.value);
bool userexist = !(itr_slots == slots.end());
if (userexist)
{
if (itr_slots->slots >= 1)
{
// Check if user has already a slot. No need to add another slot
// eosio::check(false, "User have slot/s available");
// Set pubkey
slots.modify(itr_slots, get_self(), [&](auto &row)
{ row.sec = pubkey; });
}
else
{
// User has no slot available, add a slot
slots.modify(itr_slots, get_self(), [&](auto &row)
{
row.slots += 1;
row.sec = pubkey; });
// Increment free slots used today and total free slots used
configs.modify(itr_config, get_self(), [&](auto &row)
{ row.intvalue += config_freeslotsperuser; });
if (itr_config_sum != configs.end())
{
configs.modify(itr_config_sum, get_self(), [&](auto &row)
{ row.intvalue += config_freeslotsperuser; });
}
}
}
else
{
// User does not exist in the table, add a new user
slots.emplace(get_self(), [&](auto &row)
{
row.user = accountname;
row.slots = config_freeslotsperuser;
row.slotsused = 0;
row.sec = pubkey; });
// Increment free slots used today and total free slots used
configs.modify(itr_config, get_self(), [&](auto &row)
{ row.intvalue += config_freeslotsperuser; });
if (itr_config_sum != configs.end())
{
configs.modify(itr_config_sum, get_self(), [&](auto &row)
{ row.intvalue += config_freeslotsperuser; });
}
}
}
void checkresetforfreeslots()
{
config_table configs(get_self(), get_self().value);
auto itr = configs.find(configid_freeslotslastreset);
if (itr != configs.end())
{
time_point lastresetday = itr->timevalue;
time_point currentday = eosio::current_time_point();
int64_t diff = currentday.time_since_epoch().count() - lastresetday.time_since_epoch().count();
if (diff > 86400 * 1000000)
{
resetslots();
}
}
}
void resetslots()
{
config_table configs(get_self(), get_self().value);
auto itr = configs.find(configid_freeslotsusedtoday);
if (itr != configs.end())
{
configs.modify(itr, get_self(), [&](auto &row)
{
row.intvalue = 0;
row.timevalue = eosio::current_time_point(); });
}
auto itr_last = configs.find(configid_freeslotslastreset);
if (itr_last != configs.end())
{
configs.modify(itr_last, get_self(), [&](auto &row)
{ row.timevalue = eosio::current_time_point(); });
}
}
ACTION setsec(name accountname, string pubkey)
{
require_auth(accountname);
// Check if user is allowed to set sec
slot_table slots(get_self(), get_self().value);
auto itr_slots = slots.find(accountname.value);
eosio::check(itr_slots != slots.end(), "User does not exist in the table");
if (itr_slots->slots < 1)
{
eosio::check(false, "User does not have enough slots to set sec");
}
slots.modify(itr_slots, get_self(), [&](auto &row)
{ row.sec = pubkey; });
}
ACTION useslot(name accountname, string ipfshash, string thumbipfshash)
{
// only contract owner can use this action
require_auth(get_self());
slot_table slots(get_self(), get_self().value);
auto itr_slots = slots.find(accountname.value);
eosio::check(itr_slots != slots.end(), "User does not exist in the table");
eosio::check(itr_slots->slots > 0, "User does not have enough slots to use");
slots.modify(itr_slots, get_self(), [&](auto &row)
{
row.slots -= 1;
row.slotsused += 1;
});
upload_table uploads(get_self(), get_self().value);
uploads.emplace(get_self(), [&](auto &row)
{
row.id = uploads.available_primary_key();
row.user = accountname;
row.createdat = eosio::current_time_point();
row.thumbipfshash = thumbipfshash;
row.uploadipfshash = ipfshash; });
// Increment free slots used today
config_table configs(get_self(), get_self().value);
auto itr_config = configs.find(configid_slotsusedsum);
if (itr_config != configs.end())
{
configs.modify(itr_config, get_self(), [&](auto &row)
{ row.intvalue += 1; });
}
}
[[eosio::on_notify("eosio.token::transfer")]] void buyslot(name from, name to, eosio::asset quantity, std::string memo)
{
if (to != get_self())
{
return;
}
const std::string prefix = "buyslot";
if (memo.rfind(prefix, 0) == 0)
{
std::string rest = memo.substr(prefix.size());
size_t start = rest.find_first_not_of(" \t");
size_t end = rest.find_last_not_of(" \t");
eosio::check(start != std::string::npos && end != std::string::npos && end >= start, "Invalid memo format");
std::string username_str = rest.substr(start, end - start + 1);
eosio::check(username_str.find_first_of(" \t") == std::string::npos, "Invalid memo format");
eosio::name username = name(username_str);
eosio::check(is_account(username), "User does not exist");
config_table configs(get_self(), get_self().value);
auto price_itr = configs.find(configid_priceforslot);
eosio::check(price_itr != configs.end(), "Price configuration missing");
eosio::asset expected_price(price_itr->intvalue, quantity.symbol);
eosio::check(quantity == expected_price,
(std::string("Invalid quantity. You need to send ") + expected_price.to_string() + " to buy slot.").c_str());
// Check if user is already exists in the table
slot_table slots(get_self(), get_self().value);
auto itr_slots = slots.find(username.value);
bool userexists = itr_slots != slots.end();
if (userexists)
{
slots.modify(itr_slots, get_self(), [&](auto &row)
{ row.slots += 1; });
}
else
{
slots.emplace(get_self(), [&](auto &row)
{
row.user = username;
row.slots = 1;
row.slotsused = 0;
row.sec = ""; });
}
}
else
{
return;
}
}
};