/
bet.clar
682 lines (628 loc) · 24 KB
/
bet.clar
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
(define-constant ERR_BET_TYPE 1001)
(define-constant ERR_ROUND_NOT_RIGHT 1002)
(define-constant ERR_INVALID_COST 1003)
(define-constant ERR_EXCEED_MAX_PLAYERS_COUNT 1004)
(define-constant ERR_HAS_BET_CURRENT_ROUND 1005)
(define-constant ERR_INVALID_BET_VALUE 1006)
(define-constant ERR_BALANCE_UNENOUGH 1007)
(define-constant ERR_TRANSFER_STX_ERR 1008)
(define-constant ERR_ROUND_NOT_FOUND 1009)
(define-constant ERR_ROUND_NOT_CURRENT 1010)
(define-constant ERR_CANCAL_IN_CD 1011)
(define-constant ERR_NO_BET 1012)
(define-constant ERR_ALREADY_DRAW 1013)
(define-constant ERR_NO_AUTHORITY 1014)
;;
(define-constant OWNER tx-sender)
(define-constant BET_TYPE_LIST (list u1 u2 u3))
(define-constant MAX_BETS_EACH_SIDE u400)
(define-constant DRAW_BET_NUM_THRESHOLD u2) ;;TODO:u10
(define-constant HIT_SHARES u1000000)
(define-constant ONE_ROUND_DURATION_BLOCKS u1) ;;TODO u36 6 hours
(define-constant DRAW_CD_BLOCKS u1) ;;TODO u3 ;; 0.5 hour
(define-constant CANCEL_CD_BLOCKS u1) ;;TODO u48 ;; 8 hours
(define-constant UPDATE_PRICE_COST u5000000)
(define-constant BUFF_TO_BYTE (list
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f
0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f
0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f
0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f
0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f
0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f
0xa0 0xa1 0xa2 0xa3 0xa4 0xa5 0xa6 0xa7 0xa8 0xa9 0xaa 0xab 0xac 0xad 0xae 0xaf
0xb0 0xb1 0xb2 0xb3 0xb4 0xb5 0xb6 0xb7 0xb8 0xb9 0xba 0xbb 0xbc 0xbd 0xbe 0xbf
0xc0 0xc1 0xc2 0xc3 0xc4 0xc5 0xc6 0xc7 0xc8 0xc9 0xca 0xcb 0xcc 0xcd 0xce 0xcf
0xd0 0xd1 0xd2 0xd3 0xd4 0xd5 0xd6 0xd7 0xd8 0xd9 0xda 0xdb 0xdc 0xdd 0xde 0xdf
0xe0 0xe1 0xe2 0xe3 0xe4 0xe5 0xe6 0xe7 0xe8 0xe9 0xea 0xeb 0xec 0xed 0xee 0xef
0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8 0xf9 0xfa 0xfb 0xfc 0xfd 0xfe 0xff
))
(define-constant LIST_400 (list
u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25
u26 u27 u28 u29 u30 u31 u32 u33 u34 u35 u36 u37 u38 u39 u40 u41 u42 u43 u44 u45 u46 u47 u48 u49 u50
u51 u52 u53 u54 u55 u56 u57 u58 u59 u60 u61 u62 u63 u64 u65 u66 u67 u68 u69 u70 u71 u72 u73 u74 u75
u76 u77 u78 u79 u80 u81 u82 u83 u84 u85 u86 u87 u88 u89 u90 u91 u92 u93 u94 u95 u96 u97 u98 u99 u100
u101 u102 u103 u104 u105 u106 u107 u108 u109 u110 u111 u112 u113 u114 u115 u116 u117 u118 u119 u120 u121 u122 u123 u124 u125
u126 u127 u128 u129 u130 u131 u132 u133 u134 u135 u136 u137 u138 u139 u140 u141 u142 u143 u144 u145 u146 u147 u148 u149 u150
u151 u152 u153 u154 u155 u156 u157 u158 u159 u160 u161 u162 u163 u164 u165 u166 u167 u168 u169 u170 u171 u172 u173 u174 u175
u176 u177 u178 u179 u180 u181 u182 u183 u184 u185 u186 u187 u188 u189 u190 u191 u192 u193 u194 u195 u196 u197 u198 u199 u200
u201 u202 u203 u204 u205 u206 u207 u208 u209 u210 u211 u212 u213 u214 u215 u216 u217 u218 u219 u220 u221 u222 u223 u224 u225
u226 u227 u228 u229 u230 u231 u232 u233 u234 u235 u236 u237 u238 u239 u240 u241 u242 u243 u244 u245 u246 u247 u248 u249 u250
u251 u252 u253 u254 u255 u256 u257 u258 u259 u260 u261 u262 u263 u264 u265 u266 u267 u268 u269 u270 u271 u272 u273 u274 u275
u276 u277 u278 u279 u280 u281 u282 u283 u284 u285 u286 u287 u288 u289 u290 u291 u292 u293 u294 u295 u296 u297 u298 u299 u300
u301 u302 u303 u304 u305 u306 u307 u308 u309 u310 u311 u312 u313 u314 u315 u316 u317 u318 u319 u320 u321 u322 u323 u324 u325
u326 u327 u328 u329 u330 u331 u332 u333 u334 u335 u336 u337 u338 u339 u340 u341 u342 u343 u344 u345 u346 u347 u348 u349 u350
u351 u352 u353 u354 u355 u356 u357 u358 u359 u360 u361 u362 u363 u364 u365 u366 u367 u368 u369 u370 u371 u372 u373 u374 u375
u376 u377 u378 u379 u380 u381 u382 u383 u384 u385 u386 u387 u388 u389 u390 u391 u392 u393 u394 u395 u396 u397 u398 u399 u400
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; data maps and vars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-map bet-round-map
uint ;; bet-type
uint ;; round
)
(define-map bet-summary-map
{ bet-type: uint, round: uint }
{
start-at: uint, ;; round start block
start-time: uint, ;; round start time-stamp roughly
end-at: uint, ;; round end block
end-time: uint, ;; round end time-stamp roughly
player-num: uint, ;; player number cur round
v-num: uint, ;; bet v times (v means left side [1-50])
s-num: uint, ;; bet s times (s means right side [51-100])
rand-num: uint, ;; the result random number
total-shares: uint ;; total shares of the winners
}
)
(define-map bet-record-map
{ bet-type: uint, round: uint, index: uint } ;; index start from 1
{
player: principal,
bet-value: uint, ;; 100*v+s
shares: uint,
win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose
}
)
(define-map player-times-map
{ player: principal, bet-type: uint }
uint ;; times
)
(define-map player-record-map
{ player: principal, bet-type: uint, index: uint } ;; index start from 1
{
round: uint,
bet-value: uint, ;; 100*v+s
win-num: int ;; 0 means not draw yet, >0 means win, <0 means lose
}
)
(define-data-var price-list (list 3 uint) (list u1000000 u10000000 u100000000))
;; draw logic related
(define-data-var draw-bet-type uint u0)
(define-data-var draw-round uint u0)
(define-data-var draw-bet-price uint u0)
(define-data-var draw-rand-num uint u0)
(define-data-var draw-rand-is-v bool false)
(define-data-var draw-total-shares uint u0)
(define-data-var draw-award-pool-num uint u0)
(define-data-var draw-award-per-share uint u0)
(define-data-var tips (string-utf8 128) u"I WISH YOU WIN!")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; bet-round = 0 means always current round
(define-public (bet (bet-type uint) (bet-round uint) (bet-v uint) (bet-s uint) (stx-cost uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(player-times (default-to u0 (map-get? player-times-map { player: tx-sender, bet-type: bet-type })))
(player-record-opt-tuple (map-get? player-record-map { player: tx-sender, bet-type: bet-type, index: player-times }))
(bet-v-flag (and (>= bet-v u1) (<= bet-v u50)))
(bet-s-flag (and (>= bet-s u51) (<= bet-s u100)))
(bet-price (* (unwrap-panic (element-at (var-get price-list) (- bet-type u1))) (if (and bet-v-flag bet-s-flag) u2 u1)))
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round })))
(v-num (+ (get v-num bet-summary-tuple) (if bet-v-flag u1 u0)))
(s-num (+ (get s-num bet-summary-tuple) (if bet-s-flag u1 u0)))
(is-first-player (is-eq (get start-at bet-summary-tuple) u0))
(start-at (if is-first-player block-height (get start-at bet-summary-tuple)))
)
(asserts! (or (is-eq bet-round u0) (is-eq cur-round bet-round)) (err ERR_ROUND_NOT_RIGHT))
(asserts! (or (is-none player-record-opt-tuple) (not (is-eq (unwrap-panic (get round player-record-opt-tuple)) cur-round))) (err ERR_HAS_BET_CURRENT_ROUND))
(asserts! (and (<= v-num MAX_BETS_EACH_SIDE) (<= s-num MAX_BETS_EACH_SIDE)) (err ERR_EXCEED_MAX_PLAYERS_COUNT))
(asserts! (or (is-eq bet-v u0) (and (>= bet-v u1) (<= bet-v u50))) (err ERR_INVALID_BET_VALUE))
(asserts! (or (is-eq bet-s u0) (and (>= bet-s u51) (<= bet-s u100))) (err ERR_INVALID_BET_VALUE))
(asserts! (or bet-v-flag bet-s-flag) (err ERR_INVALID_BET_VALUE))
(asserts! (is-eq stx-cost bet-price) (err ERR_INVALID_COST))
(asserts! (>= (stx-get-balance tx-sender) bet-price) (err ERR_BALANCE_UNENOUGH))
;; deduct
(unwrap! (stx-transfer? bet-price tx-sender (as-contract tx-sender)) (err ERR_TRANSFER_STX_ERR))
(let
(
(bet-value (+ (* u100 bet-v) bet-s))
(start-time (if (is-eq (get start-time bet-summary-tuple) u0)
(default-to u0 (get-block-info? time start-at))
(get start-time bet-summary-tuple)))
(player-num (+ (get player-num bet-summary-tuple) u1))
)
;; note bet related map
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge bet-summary-tuple {
start-at: start-at,
start-time: start-time,
player-num: player-num,
v-num: v-num,
s-num: s-num
})
)
(map-set bet-record-map
{ bet-type: bet-type, round: cur-round, index: player-num }
{
player: tx-sender,
bet-value: bet-value,
shares: u0,
win-num: 0
}
)
;; note player related map
(map-set player-times-map
{ player: tx-sender, bet-type: bet-type }
(+ player-times u1)
)
(map-set player-record-map
{ player: tx-sender, bet-type: bet-type, index: (+ player-times u1) }
{
round: cur-round,
bet-value: bet-value,
win-num: 0
}
)
)
;; if (last-round has not draw)
;; if end-time hasn't be marked, try mark it.
;; if (has passed more than 0.5 hour since last-round-end-time) then (draw last round)
(let
(
(last-round (- cur-round u1))
(last-bet-summary-tuple-opt (map-get? bet-summary-map { bet-type: bet-type, round: last-round }))
)
(match last-bet-summary-tuple-opt last-bet-summary-tuple
(if (is-eq (get rand-num last-bet-summary-tuple) u0)
(begin
;; try mark end-time
(if
(and
(is-eq (get end-time last-bet-summary-tuple) u0)
(> block-height (get end-at last-bet-summary-tuple))
)
(map-set bet-summary-map
{ bet-type: bet-type, round: last-round }
(merge last-bet-summary-tuple {
end-time: (default-to u0 (get-block-info? time (get end-at last-bet-summary-tuple)))
})
)
true
)
;; try draw
(if (> block-height (+ (get end-at last-bet-summary-tuple) DRAW_CD_BLOCKS))
(draw bet-type last-round)
true
)
)
true
)
true
)
)
;; if (has passed 6 hours since round start) and (total bets >= 10) and (small-side-bet-count*2 > big-side-bet-count) then (end cur round, start next round)
(if
(and
(> block-height (+ start-at ONE_ROUND_DURATION_BLOCKS))
(>= (+ v-num s-num) DRAW_BET_NUM_THRESHOLD)
(> (* v-num u2) s-num)
(> (* s-num u2) v-num)
)
(let
(
(cur-bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: cur-round })))
)
(map-set bet-summary-map
{ bet-type: bet-type, round: cur-round }
(merge cur-bet-summary-tuple {
end-at: block-height
})
)
(restart-round bet-type (+ cur-round u1))
)
true
)
;;
(ok true)
)
)
(define-private (restart-round (bet-type uint) (round uint))
(begin
(map-set bet-round-map bet-type round)
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
{
start-at: u0,
start-time: u0,
end-at: u0,
end-time: u0,
player-num: u0,
v-num: u0,
s-num: u0,
rand-num: u0,
total-shares: u0
}
)
)
)
(define-private (draw (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
(player-num (get player-num bet-summary-tuple))
(rand-num (calc-round-rand-num bet-type round))
(is-v (< rand-num u51))
)
;;
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-rand-num rand-num)
(var-set draw-rand-is-v is-v)
(var-set draw-total-shares u0)
(var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1))))
(var-set draw-award-pool-num
(*
(var-get draw-bet-price)
(if is-v (get s-num bet-summary-tuple) (get v-num bet-summary-tuple))
)
)
;; add a log
(print "draw")
;; calc each player's shares
(filter calc-shares-loop LIST_400)
;; calc each award per share
(var-set draw-award-per-share (/ (var-get draw-award-pool-num) (var-get draw-total-shares)))
(map-set bet-summary-map
{ bet-type: bet-type, round: round }
(merge bet-summary-tuple {
rand-num: rand-num,
total-shares: (var-get draw-total-shares)
})
)
;; distribute award
(filter award-loop LIST_400)
;; remain is the fee
(unwrap! (as-contract (stx-transfer? (var-get draw-award-pool-num) tx-sender OWNER)) false)
;; finish
true
)
)
;;;;;;;; calc-shares-loop begin ;;;;;;;;
(define-private (calc-shares-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
(rand-num (var-get draw-rand-num))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v (/ bet-value u100))
(bet-s (mod bet-value u100))
(shares (
if (<= rand-num u50)
(if (is-eq bet-v u0)
u0
(if (> bet-v rand-num)
(/ HIT_SHARES (+ (- bet-v rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-v) u10))
)
)
(if (is-eq bet-s u0)
u0
(if (> bet-s rand-num)
(/ HIT_SHARES (+ (- bet-s rand-num) u10))
(/ HIT_SHARES (+ (- rand-num bet-s) u10))
)
)
)
)
)
(var-set draw-total-shares (+ (var-get draw-total-shares) shares))
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
shares: shares
})
)
false
)
false
)
)
)
;;;;;;;; calc-shares-loop end ;;;;;;;;
;;;;;;;; award-loop begin ;;;;;;;;
(define-private (award-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-price (var-get draw-bet-price))
(origin-award (* (get shares bet-record-tuple) (var-get draw-award-per-share)))
(award
(if (> origin-award bet-price)
(/ (* origin-award u98) u100)
origin-award
)
)
(player (get player bet-record-tuple))
(bet-value (get bet-value bet-record-tuple))
(is-bet-both-side (and (> bet-value u100) (> (mod bet-value u100) u0)))
(win-num
(if is-bet-both-side
(- (to-int award) (to-int bet-price))
(if (> origin-award u0) (to-int award) (- 0 (to-int bet-price)))
)
)
(player-times (unwrap-panic (map-get? player-times-map { player: player, bet-type: bet-type })))
(player-record (unwrap-panic (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times })))
)
(and
(> award u0)
(unwrap! (as-contract (stx-transfer? (+ award bet-price) tx-sender player)) false)
(var-set draw-award-pool-num (- (var-get draw-award-pool-num) award))
)
;;
(map-set bet-record-map
{ bet-type: bet-type, round: round, index: index }
(merge bet-record-tuple {
win-num: win-num
})
)
;;
(map-set player-record-map
{ player: player, bet-type: bet-type, index: player-times }
(merge player-record {
win-num: win-num
})
)
false
)
false
)
)
)
;;;;;;;; award-loop end ;;;;;;;;
(define-private (calc-round-rand-num (bet-type uint) (round uint))
(let
(
(end-block (get end-at (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round }))))
(vrf-rand-num (get-rand-uint-at-block (+ end-block DRAW_CD_BLOCKS)))
)
(+ (mod vrf-rand-num u100) u1)
)
)
;;;;;;;; borrow from citycoins begin ;;;;;;;;
;; (https://github.com/citycoins/citycoin/tree/main/contracts)
(define-private (buff-to-u8 (byte (buff 1)))
(unwrap-panic (index-of BUFF_TO_BYTE byte))
)
(define-private (buff-to-uint-le (word (buff 16)))
(get acc
(fold add-and-shift-uint-le (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15) { acc: u0, data: word })
)
)
(define-private (add-and-shift-uint-le (idx uint) (input { acc: uint, data: (buff 16) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (buff-to-u8 (unwrap-panic (element-at data idx))))
)
{
;; acc = byte * (2**(8 * (15 - idx))) + acc
acc: (+ (* byte (pow u2 (* u8 (- u15 idx)))) acc),
data: data
})
)
(define-private (lower-16-le (input (buff 32)))
(get acc
(fold lower-16-le-closure (list u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31) { acc: 0x, data: input })
)
)
(define-private (lower-16-le-closure (idx uint) (input { acc: (buff 16), data: (buff 32) }))
(let (
(acc (get acc input))
(data (get data input))
(byte (unwrap-panic (element-at data idx)))
)
{
acc: (unwrap-panic (as-max-len? (concat acc byte) u16)),
data: data
})
)
(define-read-only (get-rand-uint-at-block (block-index uint))
(buff-to-uint-le (lower-16-le (unwrap-panic (get-block-info? vrf-seed block-index))))
)
;;;;;;;; borrow from citycoins end ;;;;;;;;
;;;;;;;; cancel round begin ;;;;;;;;
(define-public (cancel (bet-type uint) (round uint))
(let
(
(cur-round (unwrap! (map-get? bet-round-map bet-type) (err ERR_BET_TYPE)))
(bet-summary-tuple (unwrap! (map-get? bet-summary-map { bet-type: bet-type, round: round }) (err ERR_ROUND_NOT_FOUND)))
)
(asserts! (is-eq cur-round round) (err ERR_ROUND_NOT_CURRENT))
(asserts! (> block-height (+ (get start-at bet-summary-tuple) CANCEL_CD_BLOCKS)) (err ERR_CANCAL_IN_CD))
(asserts! (> (get player-num bet-summary-tuple) u0) (err ERR_NO_BET))
(asserts! (is-eq (get rand-num bet-summary-tuple) u0) (err ERR_ALREADY_DRAW))
(ok (cancel-round bet-type round))
)
)
(define-private (cancel-round (bet-type uint) (round uint))
(let
(
(bet-summary-tuple (unwrap-panic (map-get? bet-summary-map { bet-type: bet-type, round: round })))
(player-num (get player-num bet-summary-tuple))
)
(var-set draw-bet-type bet-type)
(var-set draw-round round)
(var-set draw-bet-price (unwrap-panic (element-at (var-get price-list) (- bet-type u1))))
;; cancel each player
(filter cancel-loop LIST_400)
;; reset current round for each bet-type
(restart-round bet-type round)
)
)
(define-private (cancel-loop (index uint))
(let
(
(bet-type (var-get draw-bet-type))
(round (var-get draw-round))
)
(match (map-get? bet-record-map { bet-type: bet-type, round: round, index: index }) bet-record-tuple
(let
(
(bet-value (get bet-value bet-record-tuple))
(bet-v-flag (> bet-value u100))
(bet-s-flag (> (mod bet-value u100) u0))
(player (get player bet-record-tuple))
(player-times (default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type })))
)
;;
(map-delete bet-record-map { bet-type: bet-type, round: round, index: index })
;;
(if (> player-times u1)
(map-set player-times-map
{ player: player, bet-type: bet-type }
(- player-times u1)
)
(map-delete player-times-map { player: player, bet-type: bet-type })
)
(map-delete player-record-map { player: player, bet-type: bet-type, index: player-times })
;;
(unwrap! (as-contract (stx-transfer? (* (var-get draw-bet-price) (if (and bet-v-flag bet-s-flag) u2 u1)) tx-sender player)) false)
;;
false
)
false
)
)
)
;;;;;;;; cancel round end ;;;;;;;;
;;;;;;;; update price begin ;;;;;;;;
(define-public (update-price (in-price-list (list 3 uint)))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(unwrap! (stx-burn? UPDATE_PRICE_COST tx-sender) (err ERR_BALANCE_UNENOUGH))
(let
(
(cancel-ok-list (filter force-cancel-bet-type BET_TYPE_LIST))
)
(var-set price-list in-price-list)
(ok (is-eq (len cancel-ok-list) (len BET_TYPE_LIST)))
)
)
)
(define-private (force-cancel-bet-type (bet-type uint))
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(match (map-get? bet-summary-map { bet-type: bet-type, round: cur-round }) bet-summary-tuple
(if
(and
(> (get player-num bet-summary-tuple) u0)
(> (get start-at bet-summary-tuple) u0)
(is-eq (get rand-num bet-summary-tuple) u0)
)
(cancel-round bet-type cur-round)
true
)
true
)
)
)
;;;;;;;; update price end ;;;;;;;;
;;;;;;;; web use begin ;;;;;;;;
(define-public (update-tips (in-tips (string-utf8 128)))
(begin
(asserts! (is-eq OWNER tx-sender) (err ERR_NO_AUTHORITY))
(ok (var-set tips in-tips))
)
)
(define-read-only (get-summary (player-opt (optional principal)))
(ok {
summarys: (map get-cur-bet-summary BET_TYPE_LIST),
player-bet-infos: (map get-cur-player-bet-info BET_TYPE_LIST (list player-opt player-opt player-opt)),
price-list: (var-get price-list),
block-height: block-height,
tips: (var-get tips)
})
)
(define-read-only (get-cur-bet-summary (bet-type uint))
(let
(
(round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(ok {
round: round,
bet-summary: (map-get? bet-summary-map { bet-type: bet-type, round: round })
})
)
)
(define-read-only (get-bet-summary (bet-type uint) (round uint))
(map-get? bet-summary-map { bet-type: bet-type, round: round })
)
(define-read-only (get-player-times (player principal) (bet-type uint))
(default-to u0 (map-get? player-times-map { player: player, bet-type: bet-type }))
)
(define-read-only (get-cur-player-bet-info (bet-type uint) (player-opt (optional principal)))
(match player-opt player
(match (map-get? player-times-map { player: player, bet-type: bet-type }) player-times
(match (map-get? player-record-map { player: player, bet-type: bet-type, index: player-times }) player-record
(let
(
(cur-round (unwrap-panic (map-get? bet-round-map bet-type)))
)
(if (is-eq cur-round (get round player-record))
(ok player-record)
(err none)
)
)
(err none)
)
(err none)
)
(err none)
)
)
(define-read-only (get-player-bet-info (bet-type uint) (player principal) (index uint))
(map-get? player-record-map { player: player, bet-type: bet-type, index: index })
)
(define-read-only (get-bet-records (key-list (list 50 { bet-type: uint, round: uint, index: uint })))
(map get-bet-record key-list)
)
(define-read-only (get-bet-record (key { bet-type: uint, round: uint, index: uint }))
(map-get? bet-record-map key)
)
(define-read-only (get-balance)
(stx-get-balance (as-contract tx-sender))
)
;;;;;;;; web use end ;;;;;;;;
;;;;;;;; initialize begin ;;;;;;;;
(define-private (init-bet-type (bet-type uint))
(restart-round bet-type u1)
)
(map init-bet-type BET_TYPE_LIST)
;;;;;;;; initialize end ;;;;;;;;