-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmm-deposit-dlc-v2.clar
283 lines (238 loc) · 7.45 KB
/
mm-deposit-dlc-v2.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
(define-constant ERR-UNAUTHORIZED (err u601))
(define-constant ERR-INTERNAL-ERROR (err u602))
(define-constant ERR-INCORRECT-ID (err u501))
(define-constant ERR-INCORRECT-STATE (err u502))
(define-constant ERR-INCORRECT-DATA (err u401))
(define-constant ERR-NOT-ENOUGH-BALANCE (err u301))
(define-constant ERR-NOT-ENOUGH-TIME (err u302))
(define-constant CONTRACT-OWNER tx-sender)
(define-constant STX1 u1000000)
(define-constant MIN-DEPOSIT STX1)
(define-constant MIN-SERVICE-AMOUNT STX1)
(define-constant WITHDRAW-BLOCK-TIMEOUT u20)
(define-constant CANCEL-BLOCK-TIMEOUT u20)
(define-constant SERVICE-BLOCK-TIMEOUT u40000)
(define-constant SERVICE-COST-MAX-PCT u5)
(define-constant GAME-RESULT-NONE u0)
(define-constant GAME-RESULT-FIRST-WON u1)
(define-constant GAME-RESULT-SECOND-WON u2)
(define-constant GAME-RESULT-DRAW u3)
(define-constant GAME-RESULT-CANCELED u1000) ;; canceled
(define-data-var feePrincipal principal tx-sender)
(define-data-var feePct uint u500) ;; 5.0% (in pct * 100)
(define-map games
(buff 8)
{
cost: uint,
block: uint,
p1: principal,
p2: principal,
result: uint
}
)
(define-map players
principal
{
balance: uint,
block: uint,
}
)
(define-map serviceCost
principal
{
block: uint,
}
)
(define-read-only (get-service-cost-block (player principal))
(let
(
(serviceCostData (map-get? serviceCost player))
(block (get block serviceCostData))
)
(default-to u0 block)
)
)
(define-read-only (get-balance (player principal))
(let
(
(playerData (map-get? players player))
(balance (get balance playerData))
)
(default-to u0 balance)
)
)
(define-read-only (get-my-balance)
(let
(
(playerData (map-get? players tx-sender))
(balance (get balance playerData))
)
(default-to u0 balance)
)
)
(define-read-only (get-last-action-block (player principal))
(let
(
(playerData (map-get? players player))
(block (get block playerData))
)
(default-to u0 block)
)
)
(define-public (deposit (amount uint))
(let
(
(fee (calc-fee amount))
(amountMinusFee (- amount fee))
)
(asserts! (>= amount MIN-DEPOSIT) ERR-INCORRECT-DATA)
(try! (stx-transfer? amountMinusFee tx-sender (contract-address)))
(try! (stx-transfer? fee tx-sender (var-get feePrincipal)))
(map-set players tx-sender {
balance: (+ (get-balance tx-sender) amountMinusFee),
block: block-height
})
(ok true)
)
)
(define-public (withdraw (amount uint))
(let
(
(p tx-sender)
(player (unwrap! (map-get? players p) ERR-INCORRECT-ID))
(balance (get balance player))
(block (get block player))
)
(asserts! (>= balance amount) ERR-NOT-ENOUGH-BALANCE)
(asserts! (>= block-height (+ block WITHDRAW-BLOCK-TIMEOUT)) ERR-NOT-ENOUGH-TIME)
(unwrap! (as-contract (stx-transfer? amount tx-sender p)) ERR-INTERNAL-ERROR)
;; allow to call withdraw without limitations
(remove-from-player-balance p amount block)
(ok true)
)
)
(define-public (apply-service-cost (p principal))
(let
(
(player (unwrap! (map-get? players p) ERR-INCORRECT-ID))
(balance (get balance player))
(block (get block player))
(lastServiceBlock (get-service-cost-block p))
(maxBlock (max block lastServiceBlock))
(minBlock (min block lastServiceBlock))
(blockDeltaMax (- block-height maxBlock))
(blockDeltaMin (- block-height minBlock))
(cost (calc-service-cost balance blockDeltaMin))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)
(asserts! (>= balance MIN-SERVICE-AMOUNT) ERR-NOT-ENOUGH-BALANCE)
(asserts! (>= blockDeltaMax SERVICE-BLOCK-TIMEOUT) ERR-NOT-ENOUGH-TIME)
(unwrap! (as-contract (stx-transfer? cost tx-sender (var-get feePrincipal))) ERR-INTERNAL-ERROR)
(remove-from-player-balance p cost block)
(map-set serviceCost p {
block: block-height,
})
(ok true)
)
)
(define-public (cancel-game (uid (buff 8)))
(let
(
(game (unwrap! (map-get? games uid) ERR-INCORRECT-ID))
(result (get result game))
(blockDelta (- block-height (get block game)))
)
(if (>= blockDelta CANCEL-BLOCK-TIMEOUT)
(complete-game uid result)
ERR-NOT-ENOUGH-TIME
)
)
)
(define-public (start-game (uid (buff 8)) (p1 principal) (p2 principal) (cost uint))
(let
(
(player1Balance (get-balance p1))
(player2Balance (get-balance p2))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)
(asserts! (>= player1Balance cost) ERR-NOT-ENOUGH-BALANCE)
(asserts! (>= player2Balance cost) ERR-NOT-ENOUGH-BALANCE)
(remove-from-player-balance p1 cost block-height)
(remove-from-player-balance p2 cost block-height)
(map-set games uid {
block: block-height,
cost: cost,
p1: p1,
p2: p2,
result: GAME-RESULT-NONE
})
(ok true)
)
)
(define-public (set-result (uid (buff 8)) (result uint))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)
(complete-game uid result)
)
)
(define-private (remove-from-player-balance (p principal) (amount uint) (block uint))
(map-set players p {
balance: (- (get-balance p) amount),
block: block
})
)
(define-private (add-to-player-balance (p principal) (amount uint) (block uint))
(map-set players p {
balance: (+ (get-balance p) amount),
block: block
})
)
(define-private (complete-game (uid (buff 8)) (result uint))
(let
(
(game (unwrap! (map-get? games uid) ERR-INCORRECT-ID))
(totalReward (* (get cost game) u2))
(currentResult (get result game))
(p1 (get p1 game))
(p2 (get p2 game))
)
(asserts! (is-eq currentResult GAME-RESULT-NONE) ERR-INCORRECT-STATE)
(asserts! (not (is-eq result GAME-RESULT-NONE)) ERR-INCORRECT-DATA)
(send-rewards result totalReward p1 p2)
(map-set games uid (merge game {
result: result
}))
(ok true)
)
)
(define-private (send-rewards (result uint) (totalReward uint) (p1 principal) (p2 principal))
(let
(
(firstPlayerReward (if (is-eq result GAME-RESULT-FIRST-WON) totalReward (if (is-eq result GAME-RESULT-SECOND-WON) u0 (/ totalReward u2))))
(secondPlayerReward (- totalReward firstPlayerReward))
)
(if (> firstPlayerReward u0)
(add-to-player-balance p1 firstPlayerReward block-height)
true
)
(if (> secondPlayerReward u0)
(add-to-player-balance p2 secondPlayerReward block-height)
true
)
)
)
(define-private (calc-fee (amount uint))
(/ (* amount (var-get feePct)) u10000)
)
(define-private (calc-service-cost (amount uint) (blockDelta uint))
(let
(
(periods (/ blockDelta SERVICE-BLOCK-TIMEOUT))
(pct (min periods SERVICE-COST-MAX-PCT))
)
(/ (* pct amount) u100)
)
)
(define-private (contract-address) (as-contract tx-sender))
(define-private (max (a uint) (b uint)) (if (> a b) a b))
(define-private (min (a uint) (b uint)) (if (< a b) a b))