forked from DeBankDeFi/tokendapppub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokendapppub.cpp
464 lines (383 loc) · 18.3 KB
/
tokendapppub.cpp
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
#include "tokendapppub.hpp"
void tokendapppub::detail(string tokenname, string dappname, string logo, string website,
string social, string community, string medium, string github, account_name contract, string intro) {
symbol_name name = _string_to_symbol_name(tokenname.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
eosio_assert(intro.size() <= 1024, "memo has more than 1024 bytes");
}
void tokendapppub::create(account_name issuer, asset maximum_supply) {
require_auth(issuer);
auto sym = maximum_supply.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
eosio_assert( maximum_supply.is_valid(), "invalid supply");
eosio_assert( maximum_supply.amount > 0, "max-supply must be positive");
tb_games game_sgt(_self, sym.name());
eosio_assert(game_sgt.exists(), "game not found by this symbol name");
st_game game = game_sgt.get();
eosio_assert(game.symbol == sym, "symbol precision mismatch");
eosio_assert(game.owner == issuer, "issuer is not the owner of this token");
eosio_assert(game.base_stake - game.deserved_option + game.base_option == maximum_supply.amount, "invalid maximum supply");
stats statstable(_self, sym.name());
auto existing = statstable.find(sym.name());
eosio_assert(existing == statstable.end(), "token with symbol already exists" );
statstable.emplace(issuer, [&]( auto& s) {
s.supply.symbol = maximum_supply.symbol;
s.max_supply = maximum_supply;
s.issuer = issuer;
});
}
void tokendapppub::issue(account_name to, asset quantity, string memo) {
auto sym = quantity.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
eosio_assert( memo.size() <= 256, "memo has more than 256 bytes" );
auto sym_name = sym.name();
stats statstable( _self, sym_name );
auto existing = statstable.find( sym_name );
eosio_assert( existing != statstable.end(), "token with symbol does not exist, create token before issue" );
const auto& st = *existing;
require_auth( st.issuer );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must issue positive quantity" );
eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
eosio_assert( quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");
statstable.modify( st, 0, [&]( auto& s ) {
s.supply += quantity;
});
}
void tokendapppub::reg(account_name from, string memo) {
require_auth(from);
eosio_assert(memo.length() <= 7, "invalid memo format");
symbol_name name = string_to_symbol(0, memo.c_str()) >> 8;
eosio_assert(!check_action_locked(name, N(reg)) || check_bypass_lock(name, N(reg), from), "reg action locked by owner");
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol name");
accounts from_player(_self, from);
auto player_itr = from_player.find(name);
if (player_itr == from_player.end()) {
from_player.emplace(from, [&](auto& rt){
rt.balance = asset(0, game_sgt.get().symbol);
});
}
}
void tokendapppub::buy(account_name from, account_name to, asset quantity, string memo) {
if ((from == _self) || (to != _self) || (from == BANK_RESERVES) || (from == IBANK_ACCOUNT)) {
return;
}
eosio_assert(quantity.symbol == CORE_SYMBOL, "must pay with CORE token");
if (memo.find("-profit") != string::npos) {
auto first_separator_pos = memo.find("-profit");
eosio_assert(first_separator_pos != string::npos, "invalid memo format for profit");
string name_str = memo.substr(0, first_separator_pos);
eosio_assert(name_str.length() <= 7, "invalid symbol name");
symbol_name name = string_to_symbol(0, name_str.c_str()) >> 8;
game_profit(name, quantity.amount);
return;
}
string referrer_account_str = "";
symbol_name name;
string referrer_str = "-referrer:";
if (memo.find(referrer_str) != string::npos) {
auto first_separator_pos = memo.find(referrer_str);
string name_str = memo.substr(0, first_separator_pos);
eosio_assert(name_str.length() <= 7, "invalid symbol name");
name = string_to_symbol(0, name_str.c_str()) >> 8;
referrer_account_str = memo.substr(first_separator_pos+referrer_str.length());
eosio_assert(referrer_account_str.length() <= 12, "invalid referrer account name");
} else {
eosio_assert(memo.length() <= 7, "invalid memo format");
name = string_to_symbol(0, memo.c_str()) >> 8;
}
eosio_assert(!check_action_locked(name, N(buy)) || check_bypass_lock(name, N(buy), from), "buy action locked by owner");
int32_t fee = 0;
tb_refer refer_sgt(_self, name);
if (refer_sgt.exists()) {
fee = refer_sgt.get().fee(quantity.amount);
eosio_assert(quantity.amount - fee > 0, "refer fee must be less than amount of eos");
if (fee > 0) {
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "game not found by this symbol name");
account_name referrer_account;
if (referrer_account_str == "") {
referrer_account = game_sgt.get().owner;
} else {
referrer_account = string_to_name(referrer_account_str.c_str());
if (referrer_account == from) {
referrer_account = game_sgt.get().owner;
}
if (referrer_account != game_sgt.get().owner) {
tb_ref ref_sgt(_self, name);
if (ref_sgt.exists() && !ref_sgt.get().opened()) {
if (!check_referrer_in_whitelist(name, referrer_account)) {
referrer_account = game_sgt.get().owner;
}
}
}
}
action(
permission_level{_self, N(active)},
N(eosio.token),
N(transfer),
make_tuple(_self, referrer_account, asset(fee), string("tokendapppub refer fee https://dapp.pub"))
).send();
}
}
asset stake_quantity = game_buy(name, quantity.amount - fee);
accounts from_player(_self, from);
auto player_itr = from_player.find(name);
if (player_itr == from_player.end()) {
from_player.emplace(from, [&](auto& rt){
rt.balance = stake_quantity;
});
} else {
from_player.modify(player_itr, 0, [&](auto& rt){
rt.balance += stake_quantity;
});
}
action(
permission_level{_self, N(active)},
_self,
N(receipt),
make_tuple(from, string("buy"), quantity, stake_quantity, asset(fee))
).send();
}
void tokendapppub::sell(account_name from, asset quantity) {
require_auth(from);
accounts from_player(_self, from);
auto player_itr = from_player.find(quantity.symbol.name());
eosio_assert(player_itr != from_player.end(), "account not found");
eosio_assert(quantity.symbol == player_itr->balance.symbol, "symbol precision mismatch");
eosio_assert((quantity.amount > 0) && (quantity.amount <= player_itr->balance.amount), "invalid amount");
eosio_assert(!check_action_locked(quantity.symbol.name(), N(sell)) || check_bypass_lock(quantity.symbol.name(), N(sell), from), "sell action locked by owner");
asset eos_quantity, all_quantity;
tie(eos_quantity, all_quantity) = game_sell(quantity.symbol.name(), quantity.amount);
eosio_assert(eos_quantity.amount > 0, "selled eos amount should be greater than 0");
action(
permission_level{_self, N(active)},
N(eosio.token),
N(transfer),
make_tuple(_self, from, eos_quantity, string("tokendapppub withdraw https://dapp.pub"))
).send();
from_player.modify(player_itr, 0, [&](auto& rt){
rt.balance -= quantity;
});
if (player_itr->balance.amount == 0) {
from_player.erase(player_itr);
}
action(
permission_level{_self, N(active)},
_self,
N(receipt),
make_tuple(from, string("sell"), quantity, all_quantity, all_quantity-eos_quantity)
).send();
}
void tokendapppub::consume(account_name from, asset quantity, string memo) {
require_auth(from);
accounts from_player(_self, from);
auto player_itr = from_player.find(quantity.symbol.name());
eosio_assert(player_itr != from_player.end(), "player not found");
eosio_assert((quantity.amount > 0) && (quantity.amount <= player_itr->balance.amount), "not enough balance to consume");
eosio_assert(quantity.symbol == player_itr->balance.symbol, "symbol precision mismatch");
eosio_assert(!check_action_locked(quantity.symbol.name(), N(consume)) || check_bypass_lock(quantity.symbol.name(), N(consume), from), "consume action locked by owner");
game_consume(quantity.symbol.name(), quantity.amount);
from_player.modify(player_itr, 0, [&](auto& rt){
rt.balance -= quantity;
});
if (player_itr->balance.amount == 0) {
from_player.erase(player_itr);
}
}
void tokendapppub::claim(string name_str, bool sell) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
asset stake_quantity = game_claim(name);
accounts from_player(_self, game.owner);
auto player_itr = from_player.find(name);
if (player_itr == from_player.end()) {
from_player.emplace(game.owner, [&](auto& rt){
rt.balance = stake_quantity;
});
} else {
from_player.modify(player_itr, 0, [&](auto& rt){
rt.balance += stake_quantity;
});
}
if (sell) {
player_itr = from_player.find(name);
eosio_assert(player_itr != from_player.end(), "WTF!");
this->sell(game.owner, player_itr->balance);
}
}
void tokendapppub::transfer(account_name from, account_name to, asset quantity, string memo) {
eosio_assert(from != to, "cannot transfer to self");
require_auth(from);
eosio_assert(is_account(to), "to account does not exist");
auto sym = quantity.symbol.name();
tb_games game_sgt(_self, sym);
eosio_assert(game_sgt.exists(), "game not found by this symbol name");
st_game game = game_sgt.get();
eosio_assert(quantity.is_valid(), "invalid quantity");
eosio_assert(quantity.amount > 0, "must transfer positive quantity");
eosio_assert(quantity.symbol == game.symbol, "symbol precision mismatch");
eosio_assert(memo.size() <= 256, "memo has more than 256 bytes");
eosio_assert(!check_action_locked(sym, N(transfer)) || check_bypass_lock(sym, N(transfer), from, to), "transfer action locked by owner");
tb_trans trans_sgt(_self, sym);
if (trans_sgt.exists() && !trans_sgt.get().transactable()) {
eosio_assert(check_in_whitelist(sym, from) || check_in_whitelist(sym, to), "should only transfer token with account in the whitelist");
}
require_recipient( from );
require_recipient( to );
accounts from_player(_self, from);
auto player_itr = from_player.find(quantity.symbol.name());
eosio_assert(player_itr != from_player.end(), "no balance object found by from account");
eosio_assert(player_itr->balance.amount >= quantity.amount, "overdrawn balance" );
from_player.modify(player_itr, 0, [&](auto& rt){
rt.balance -= quantity;
});
if (player_itr->balance.amount == 0) {
from_player.erase(player_itr);
}
accounts to_player(_self, to);
auto to_player_itr = to_player.find(quantity.symbol.name());
if (to_player_itr == to_player.end()) {
to_player.emplace(from, [&](auto& rt){
rt.balance = quantity;
});
} else {
to_player.modify(to_player_itr, 0, [&](auto& rt){
rt.balance += quantity;
});
}
}
void tokendapppub::destroy(string name_str) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
eosio_assert(game.base_stake == game.stake, "all stake should be retrieved before erasing game");
game_sgt.remove();
stats statstable(_self, name);
auto existing = statstable.find(name);
// some token issued before this contract compatible with eosio.token
if (existing != statstable.end()) {
statstable.erase(existing);
}
tb_refer refer_sgt(_self, name);
if (refer_sgt.exists()) {
refer_sgt.remove();
}
}
void tokendapppub::hellodapppub(asset base_eos_quantity, asset maximum_stake, asset option_quantity,
uint32_t lock_up_period,
uint8_t base_fee_percent, uint8_t init_fee_percent, uint64_t refer_fee, uint32_t start_time) {
require_auth(GOD_ACCOUNT);
new_game(GOD_ACCOUNT, base_eos_quantity, maximum_stake, option_quantity, lock_up_period, base_fee_percent, init_fee_percent, start_time);
set_refer_fee(maximum_stake.symbol.name(), refer_fee, GOD_ACCOUNT);
SEND_INLINE_ACTION(*this, create, {GOD_ACCOUNT, N(active)}, {GOD_ACCOUNT, maximum_stake});
SEND_INLINE_ACTION(*this, issue, {GOD_ACCOUNT, N(active)}, {GOD_ACCOUNT, maximum_stake, string("")});
}
void tokendapppub::newtoken(account_name from, asset base_eos_quantity, asset maximum_stake, asset option_quantity,
uint32_t lock_up_period,
uint8_t base_fee_percent, uint8_t init_fee_percent, uint64_t refer_fee, uint32_t start_time) {
require_auth(from);
asset fee = NEW_GAME_CONSOME;
if (maximum_stake.symbol.name_length() <= 3) {
fee = NEW_GAME_CONSOME * pow(10, 4 - maximum_stake.symbol.name_length());
}
this->consume(from, fee, "consume for new token");
new_game(from, base_eos_quantity, maximum_stake, option_quantity, lock_up_period, base_fee_percent, init_fee_percent, start_time);
set_refer_fee(maximum_stake.symbol.name(), refer_fee, from);
SEND_INLINE_ACTION(*this, create, {from, N(active)}, {from, maximum_stake});
SEND_INLINE_ACTION(*this, issue, {from, N(active)}, {from, maximum_stake, string("")});
}
void tokendapppub::setreferfee(string name_str, uint64_t refer_fee) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
set_refer_fee(name, refer_fee, game.owner);
}
void tokendapppub::settrans(string name_str, uint64_t trans) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
set_trans(name, trans, game.owner);
}
void tokendapppub::setsellfee(string name_str, uint64_t base_fee_percent, uint64_t init_fee_percent) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
eosio_assert((base_fee_percent >= 0) && (base_fee_percent <= 99), "invalid fee percent");
eosio_assert((init_fee_percent >= base_fee_percent) && (init_fee_percent <=99), "invalid init fee percent");
game.base_fee_percent = base_fee_percent;
game.init_fee_percent = init_fee_percent;
game_sgt.set(game, game.owner);
}
void tokendapppub::addtowl(string name_str, account_name agent) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
add_agent_to_whitelist(name, agent, game.owner);
}
void tokendapppub::setref(string name_str, uint64_t ref) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
set_ref(name, ref, game.owner);
}
void tokendapppub::addreftowl(string name_str, account_name referrer) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
add_referrer_to_whitelist(name, referrer, game.owner);
}
void tokendapppub::lock(string name_str, vector<action_name> actions) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
set_lock_actions(name, actions, game.owner);
}
void tokendapppub::setactionwl(string name_str, action_name action, vector<account_name> from, vector<account_name> to) {
symbol_name name = _string_to_symbol_name(name_str.c_str());
tb_games game_sgt(_self, name);
eosio_assert(game_sgt.exists(), "token not found by this symbol_name");
st_game game = game_sgt.get();
require_auth(game.owner);
set_action_whitelist(name, action, from, to, game.owner);
}
void tokendapppub::receipt(account_name from, string type, asset in, asset out, asset fee) {
require_auth(_self);
}
extern "C" {
void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
tokendapppub thiscontract(receiver);
if((code == N(eosio.token)) && (action == N(transfer))) {
execute_action(&thiscontract, &tokendapppub::buy);
return;
}
if (code != receiver) return;
switch (action) {
EOSIO_API(tokendapppub, (setactionwl)(lock)(setref)(addreftowl)(addtowl)(settrans)(setreferfee)(detail)(issue)(create)(reg)(receipt)(transfer)(sell)(consume)(destroy)(claim)(newtoken)(hellodapppub))
};
eosio_exit(0);
}
}