/
buy-mia-v2.clar
78 lines (64 loc) · 2.33 KB
/
buy-mia-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
;; contract written by Asteria of Syvita
;; rights to this contract are reserved for the Syvita Guild
(define-constant MIAMICOIN_TOKEN 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token)
(define-constant POOL_ADDRESS 'SP2F8X5AT0726E8B7GGXDADHD53ARM5927SJN3TQ6)
(define-constant ERR_UNAUTHORIZED u1)
(define-data-var price uint u15000) ;; price in uSTX for 1 MIA
(define-public (sell-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to contract
(try! (transfer-mia amount contract-caller (as-contract tx-sender)))
(ok true)
)
)
(define-public (exit-mia (amount uint))
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (buy-mia (amount uint))
(begin
(asserts! (not (is-auth-pool)) (err ERR_UNAUTHORIZED))
;; transfer stx to deployer
(try! (stx-transfer? (* amount (var-get price)) contract-caller POOL_ADDRESS))
;; send MIA to caller
(try! (transfer-mia amount (as-contract tx-sender) contract-caller))
(ok true)
)
)
(define-public (change-price (newPrice uint)) ;; price in uSTX
(begin
(asserts! (is-auth-pool) (err ERR_UNAUTHORIZED))
;; update price of 1 MIA
(var-set price newPrice)
(ok true)
)
)
(define-read-only (get-price)
(ok (var-get price))
)
(define-read-only (get-remaining)
(ok (contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance (as-contract tx-sender)))
)
(define-read-only (get-contract-stx-balance)
(stx-get-balance (as-contract tx-sender))
)
(define-read-only (get-pool-mia-balance)
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance POOL_ADDRESS)
)
(define-read-only (get-pool-stx-balance)
(stx-get-balance POOL_ADDRESS)
)
(define-private (is-auth-pool)
(is-eq contract-caller POOL_ADDRESS)
)
(define-private (transfer-mia (amount uint) (from principal) (to principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token transfer amount from to none)
)
(define-private (get-balance (user principal))
(contract-call? 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-token get-balance user)
)