-
Notifications
You must be signed in to change notification settings - Fork 40
/
eosbocai2222.hpp
388 lines (356 loc) · 12 KB
/
eosbocai2222.hpp
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
#include <algorithm>
#include <eosiolib/transaction.hpp>
#include "eosio.token.hpp"
#include "types.hpp"
#include "pradata.hpp"
class eosbocai2222 : public contract
{
public:
eosbocai2222(account_name self)
: contract(self),
_bets(_self, _self),
_users(_self, _self),
_fund_pool(_self, _self),
_tokens(_self, _self),
_global(_self, _self){};
// @abi action
void transfer(account_name from,
account_name to,
asset quantity,
string memo);
void onTransfer(account_name from,
account_name to,
extended_asset quantity,
string memo);
// @abi action
void reveal(const uint64_t &id);
// @abi action
void reveal1(const uint64_t &id);
// @abi action
void addtoken(account_name contract, asset quantity);
// @abi action
void init();
void apply(account_name code, action_name action);
private:
tb_bets _bets;
tb_uesrs _users;
tb_fund_pool _fund_pool;
tb_global _global;
tb_tokens _tokens;
void parse_memo(string memo,
uint8_t *roll_under,
account_name *referrer)
{
// remove space
memo.erase(std::remove_if(memo.begin(),
memo.end(),
[](unsigned char x) { return std::isspace(x); }),
memo.end());
size_t sep_count = std::count(memo.begin(), memo.end(), '-');
eosio_assert(sep_count == 3, "invalid memo");
size_t pos;
string container;
pos = sub2sep(memo, &container, '-', 0, true);
pos = sub2sep(memo, &container, '-', ++pos, true);
pos = sub2sep(memo, &container, '-', ++pos, true);
eosio_assert(!container.empty(), "no roll under");
*roll_under = stoi(container);
container = memo.substr(++pos);
if (container.empty())
{
*referrer = PRIZEPOOL;
}
else
{
*referrer = string_to_name(container.c_str());
}
}
asset compute_referrer_reward(const st_bet &bet) { return bet.amount * 2 / 1000; } //10% for ref
asset compute_dev_reward(const st_bet &bet) { return bet.amount * 4 / 1000; } // 20% for dev
asset compute_pool_reward(const st_bet &bet) { return bet.amount * 12 / 1000; } // 60% for pool
asset compute_fomopool_reward(const st_bet &bet) { return bet.amount * 2 / 1000; } // 10% for fomo pool
uint64_t next_id()
{
st_global global = _global.get_or_default();
global.current_id += 1;
_global.set(global, _self);
return global.current_id;
}
string referrer_memo(const st_bet &bet)
{
string memo = "bet id:";
string id = uint64_string(bet.id);
memo.append(id);
memo.append(" player: ");
string player = name{bet.player}.to_string();
memo.append(player);
memo.append(" referral reward! eosdice.vip");
return memo;
}
string winner_memo(const st_bet &bet)
{
string memo = "bet id:";
string id = uint64_string(bet.id);
memo.append(id);
memo.append(" player: ");
string player = name{bet.player}.to_string();
memo.append(player);
memo.append(" winner! eosdice.vip");
return memo;
}
void assert_quantity(const extended_asset &quantity)
{
auto itr = _tokens.find(quantity.contract + quantity.symbol);
eosio_assert(itr != _tokens.end(), "Non-existent token");
eosio_assert(quantity.is_valid(), "quantity invalid");
eosio_assert(quantity.amount >= itr->minAmout, "transfer quantity must be greater than minimum");
}
bool iseostoken(const extended_asset &quantity)
{
if ((quantity.contract == N(eosio.token)) && (quantity.symbol == EOS_SYMBOL))
{
return true;
}
return false;
}
void assert_roll_under(const uint8_t &roll_under, const extended_asset &quantity)
{
eosio_assert(roll_under >= 2 && roll_under <= 96,
"roll under overflow, must be greater than 2 and less than 96");
eosio_assert(
max_payout(roll_under, quantity) <= max_bonus(quantity),
"offered overflow, expected earning is greater than the maximum bonus");
}
void unlock(const asset &amount)
{
st_fund_pool pool = get_fund_pool();
pool.locked -= amount;
eosio_assert(pool.locked.amount >= 0, "fund unlock error");
_fund_pool.set(pool, _self);
}
void lock(const asset &amount)
{
st_fund_pool pool = get_fund_pool();
pool.locked += amount;
_fund_pool.set(pool, _self);
}
asset compute_payout(const uint8_t &roll_under, const extended_asset &offer)
{
return min(max_payout(roll_under, offer), max_bonus(offer));
}
asset max_payout(const uint8_t &roll_under, const asset &offer)
{
const double ODDS = 98 / ((double)roll_under - 1.0);
return asset(ODDS * offer.amount, offer.symbol);
}
asset max_bonus(const extended_asset &quantity) { return available_balance(quantity) / 10; } //Transfer balance to secure account
asset available_balance(const extended_asset &quantity)
{
auto token = eosio::token(quantity.contract);
const asset balance =
token.get_balance(_self, symbol_type(quantity.symbol).name());
asset available;
if (iseostoken(quantity))
{
const asset locked = get_fund_pool().locked;
available = balance - locked;
}
else
{
available = balance;
}
eosio_assert(available.amount >= 0, "fund pool overdraw");
return available;
}
st_fund_pool get_fund_pool()
{
st_fund_pool fund_pool{.locked = asset(0, EOS_SYMBOL)};
return _fund_pool.get_or_create(_self, fund_pool);
}
template <typename... Args>
void send_defer_action(Args &&... args)
{
transaction trx;
trx.actions.emplace_back(std::forward<Args>(args)...);
trx.delay_sec = 1;
trx.send(next_id(), _self, false);
}
uint64_t getDiceSupply()
{
auto eos_token = eosio::token(DICETOKEN);
auto supply = eos_token.get_supply(symbol_type(DICE_SYMBOL).name());
return supply.amount;
}
uint8_t random(account_name name, uint64_t game_id)
{
auto mixd = tapos_block_prefix() * tapos_block_num() + name + game_id - current_time();
const char *mixedChar = reinterpret_cast<const char *>(&mixd);
checksum256 result;
sha256((char *)mixedChar, sizeof(mixedChar), &result);
uint64_t random_num = *(uint64_t *)(&result.hash[0]) + *(uint64_t *)(&result.hash[8]) + *(uint64_t *)(&result.hash[16]) + *(uint64_t *)(&result.hash[24]);
return (uint8_t)(random_num % 100 + 1);
}
void issue_token(account_name to,
asset quantity,
string memo)
{
st_global global = _global.get_or_default();
auto supply = getDiceSupply();
auto nexthalve = global.nexthalve;
if ((DICESUPPLY - supply) <= nexthalve)
{
global.nexthalve = global.nexthalve * 95 / 100;
global.eosperdice = global.eosperdice * 3 / 4;
_global.set(global, _self);
}
asset sendAmout = asset(quantity.amount * global.eosperdice, DICE_SYMBOL);
action(permission_level{_self, N(active)},
DICETOKEN,
N(issue),
make_tuple(to, sendAmout, memo))
.send();
}
void iplay(account_name from, asset quantity)
{
tb_uesrs1 _users1(_self, from);
auto v = _users1.get_or_create(_self, st_user1{});
v.amount += quantity;
v.count += 1;
_users1.set(v, _self);
}
void fomo(const st_bet &bet)
{
st_global global = _global.get_or_default();
global.fomopool += compute_fomopool_reward(bet);
if (bet.amount.amount > global.fomopool.amount / 1000)
{
if (now() > global.endtime) //winner winner chicken dinner
{
action(permission_level{_self, N(active)},
N(eosio.token),
N(transfer),
std::make_tuple(_self, global.lastPlayer, global.fomopool / 2, std::string("Congratulations, win the fomo award! eosdice.vip ")))
.send();
global.fomopool /= 2;
}
global.lastPlayer = bet.player;
global.endtime = now() + 60 * 60;
}
_global.set(global, _self);
}
void save(const st_bet &bet)
{
_bets.emplace(_self, [&](st_bet &r) {
r.id = bet.id;
r.player = bet.player;
r.referrer = bet.referrer;
r.amount = bet.amount;
r.roll_under = bet.roll_under;
r.created_at = bet.created_at;
});
}
void remove(const uint64_t bet_id) { _bets.erase(_bets.find(bet_id)); }
st_bet find_or_error(const uint64_t &id)
{
auto itr = _bets.find(id);
eosio_assert(itr != _bets.end(), "bet not found");
return *itr;
}
void vipcheck(account_name from, asset quantity)
{
tb_uesrs1 _users1(_self, from);
auto v = _users1.get_or_create(_self, st_user1{});
uint64_t amount = v.amount.amount / 1e4;
asset checkout = asset(0, EOS_SYMBOL);
if (amount < 1000)
{
return;
}
else if (amount < 5000)
{
checkout = asset(quantity.amount * 1 / 10000, EOS_SYMBOL);
}
else if (amount < 10000)
{
checkout = asset(quantity.amount * 2 / 10000, EOS_SYMBOL);
}
else if (amount < 50000)
{
checkout = asset(quantity.amount * 3 / 10000, EOS_SYMBOL);
}
else if (amount < 100000)
{
checkout = asset(quantity.amount * 4 / 10000, EOS_SYMBOL);
}
else if (amount < 500000)
{
checkout = asset(quantity.amount * 5 / 10000, EOS_SYMBOL);
}
else if (amount < 1000000)
{
checkout = asset(quantity.amount * 7 / 10000, EOS_SYMBOL);
}
else if (amount < 5000000)
{
checkout = asset(quantity.amount * 9 / 10000, EOS_SYMBOL);
}
else if (amount < 10000000)
{
checkout = asset(quantity.amount * 11 / 10000, EOS_SYMBOL);
}
else if (amount < 50000000)
{
checkout = asset(quantity.amount * 13 / 10000, EOS_SYMBOL);
}
else
{
checkout = asset(quantity.amount * 15 / 10000, EOS_SYMBOL);
}
action(permission_level{_self, N(active)},
N(eosio.token),
N(transfer),
std::make_tuple(_self, from, checkout, std::string("for vip! eosdice.vip")))
.send();
}
void checkAccount1(account_name from)
{
auto db = prochain::rating_index(N(rating.pra), N(rating.pra));
auto me = db.find(from);
if (me != db.end())
{
auto account_type = me->account_type;
eosio_assert(account_type == 0, "Human only");
}
}
};
struct st_transfer
{
account_name from;
account_name to;
asset quantity;
string memo;
};
void eosbocai2222::apply(account_name code, action_name action)
{
auto &thiscontract = *this;
if (action == N(transfer))
{
auto transfer_data = unpack_action_data<st_transfer>();
onTransfer(transfer_data.from, transfer_data.to, extended_asset(transfer_data.quantity, code), transfer_data.memo);
return;
}
if (code != _self)
return;
switch (action)
{
EOSIO_API(eosbocai2222, (reveal)(init)(reveal1)(addtoken));
};
}
extern "C"
{
[[noreturn]] void apply(uint64_t receiver, uint64_t code, uint64_t action) {
eosbocai2222 p(receiver);
p.apply(code, action);
eosio_exit(0);
}
}