/
creature-racer-referral-pool-v5.clar
86 lines (71 loc) · 2.23 KB
/
creature-racer-referral-pool-v5.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
;; creature-racer-referral-pool
;; Referral pool contract
;;
;; =========
;; CONSTANTS
;; =========
;;
(define-constant contract-owner tx-sender)
;; Error definitions
;; -----------------
(define-constant err-forbidden (err u403))
(define-constant err-user-not-found (err u404))
(define-constant err-insufficient-funds (err u2002))
(define-constant err-invalid-withdrawal-count (err u6001))
;;
;; ==================
;; DATA MAPS AND VARS
;; ==================
;;
(define-map withdrawal-counters principal uint)
;; private functions
;;
;;
;; ================
;; PUBLIC FUNCTIONS
;; ================
;;
;; Get number of withdrawals of given user
(define-read-only (get-withdrawal-count (user principal))
(let (
(count (unwrap! (map-get? withdrawal-counters
user)
err-user-not-found))
)
(ok count)
)
)
;; get balance of the pool
(define-read-only (get-balance)
(stx-get-balance (as-contract tx-sender)))
;; Withdraw funds from pool to sender address.
;; amount - amount to withdraw
;; withdrawal-count - checksum for withdrawals
;; *-sig - argument signature issued by backend
;; This function can be called by sender who wants to withdraw
;; funds from the pool. Signatures issued by operator's private
;; key need to be passed
(define-public (withdraw (operator-sig (buff 65))
(sender-pk (buff 33))
(amount uint)
(withdrawal-count uint))
(let (
(sender tx-sender)
(balance (get-balance))
(wcnt (+ (default-to u0 (map-get? withdrawal-counters
sender))
u1))
)
(try! (contract-call? .creature-racer-admin-v5
verify-signature
operator-sig
sender-pk
(list amount withdrawal-count)))
(asserts! (>= balance amount) err-insufficient-funds)
(asserts! (is-eq withdrawal-count wcnt)
err-invalid-withdrawal-count)
(try! (as-contract (stx-transfer? amount tx-sender sender)))
(map-set withdrawal-counters sender wcnt)
(ok true)
)
)