-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacking-action.clar
87 lines (75 loc) · 2.14 KB
/
stacking-action.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
;; title: Stacking Action
;; version: 1.0.0
;; summary: A simple test of delegation through a contract.
;; description: NOT AUDITED - USE AT YOUR OWN RISK
;; constants
(define-constant ADMIN tx-sender)
(define-constant SELF (as-contract tx-sender))
(define-constant ERR_UNAUTHORIZED (err u1000))
(define-constant ERR_INVALID_PARAMS (err u1001))
;; public functions
(define-public (stack-stx (amount uint) (to principal) (poxVer (buff 1)) (poxHash (buff 20)) (until (optional uint)))
(begin
(try! (is-authorized))
(print {
event: "stack-stx",
amount: amount,
caller: contract-caller,
delegate: to,
pox-addr: { version: poxVer, hashbytes: poxHash},
sender: tx-sender,
until: until
})
(match (as-contract (contract-call? 'SP000000000000000000002Q6VF78.pox delegate-stx amount to until (some { version: poxVer, hashbytes: poxHash})))
success (ok success)
err (err (to-uint err))
)
)
)
(define-public (unstack-stx)
(begin
(try! (is-authorized))
(print {
event: "unstack-stx",
caller: contract-caller,
sender: tx-sender
})
(match (as-contract (contract-call? 'SP000000000000000000002Q6VF78.pox revoke-delegate-stx))
success (ok success)
err (err (to-uint err))
)
)
)
(define-public (deposit-stx (sender principal) (amount uint))
(begin
(asserts! (and (> amount u0) (>= (stx-get-balance sender) amount)) ERR_INVALID_PARAMS)
(print {
event: "deposit-stx",
amount: amount,
caller: contract-caller,
sender: tx-sender
})
(stx-transfer? amount sender SELF)
)
)
(define-public (withdraw-stx (to principal) (amount uint))
(begin
(try! (is-authorized))
(asserts! (and (> amount u0) (>= (get-balance) amount)) ERR_INVALID_PARAMS)
(print {
event: "withdraw-stx",
amount: amount,
caller: contract-caller,
sender: tx-sender
})
(as-contract (stx-transfer? amount SELF to))
)
)
;; read only functions
(define-read-only (get-balance)
(stx-get-balance SELF)
)
;; private functions
(define-private (is-authorized)
(ok (asserts! (is-eq contract-caller ADMIN) ERR_UNAUTHORIZED))
)