-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbitcoin-unicorns.clar
113 lines (95 loc) · 3.31 KB
/
bitcoin-unicorns.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
;; bitcoin-unicorns
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
;; Non Fungible Token, using sip-009
(define-non-fungible-token bitcoin-unicorns uint)
;; Constants
(define-constant err-no-more-nfts u300)
(define-constant err-invalid-user u500)
(define-constant commission-address tx-sender)
;; Internal variables
(define-data-var mint-limit uint u600)
(define-data-var last-id uint u0)
(define-data-var commission uint u500)
(define-data-var total-price uint u95000000)
(define-data-var artist-address principal 'SP22BPG54TZGWTTWJ82AXS80K53V2Z0R3YC2FT7HX)
(define-data-var ipfs-root (string-ascii 80) "ipfs://ipfs/QmQs35MB9f8V8uQr4wuUH2iphLtFPQCRQnywaGzW4gPZJz/")
;; private functions
(define-private (mint (new-owner principal))
(let ((next-id (+ u1 (var-get last-id)))
(count (var-get last-id)))
(asserts! (< count (var-get mint-limit)) (err err-no-more-nfts))
(let
((total-commission (/ (* (var-get total-price) (var-get commission)) u10000))
(total-artist (- (var-get total-price) total-commission)))
(if (is-eq tx-sender (var-get artist-address))
(mint-helper new-owner next-id)
(if (is-eq tx-sender commission-address)
(begin
(mint-helper new-owner next-id))
(begin
(try! (stx-transfer? total-commission tx-sender commission-address))
(try! (stx-transfer? total-artist tx-sender (var-get artist-address)))
(mint-helper new-owner next-id))))
)
)
)
(define-private (mint-helper (new-owner principal) (next-id uint))
(match (nft-mint? bitcoin-unicorns next-id new-owner)
success
(begin
(var-set last-id next-id)
(ok true))
error (err error)))
;; public functions
(define-public (claim)
(mint tx-sender))
(define-public (claim-three)
(begin
(try! (mint tx-sender))
(try! (mint tx-sender))
(try! (mint tx-sender))
(ok true)
)
)
(define-public (set-artist-address (address principal))
(if (is-eq tx-sender commission-address)
(begin
(var-set artist-address address)
(ok true)
)
(err err-invalid-user)))
(define-public (set-price (price uint))
(if (is-eq tx-sender commission-address)
(begin
(var-set total-price price)
(ok true)
)
(err err-invalid-user)))
(define-public (set-ipfs-root (new-ipfs-root (string-ascii 80)))
(if (is-eq tx-sender commission-address)
(begin
(var-set ipfs-root new-ipfs-root)
(ok true)
)
(err err-invalid-user)))
(define-public (set-mint-limit (new-mint-limit uint))
(if (is-eq tx-sender commission-address)
(begin
(var-set mint-limit new-mint-limit)
(ok true)
)
(err err-invalid-user)))
(define-public (transfer (token-id uint) (sender principal) (recipient principal))
(if (and
(is-eq tx-sender sender))
(match (nft-transfer? bitcoin-unicorns token-id sender recipient)
success (ok success)
error (err error))
(err err-invalid-user)))
;; read-only functions
(define-read-only (get-owner (token-id uint))
(ok (nft-get-owner? bitcoin-unicorns token-id)))
(define-read-only (get-last-token-id)
(ok (var-get last-id)))
(define-read-only (get-token-uri (token-id uint))
(ok (some (concat (concat (var-get ipfs-root) "$TOKEN_ID") ".json"))))