-
Notifications
You must be signed in to change notification settings - Fork 13
/
alphabet.clar
159 lines (128 loc) · 5.54 KB
/
alphabet.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
;; alphabet
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
;;(impl-trait .nft-trait.nft-trait)
(define-non-fungible-token alphabet uint)
(define-constant DEPLOYER tx-sender)
(define-constant ERR-NOT-AUTHORIZED u101)
(define-constant ERR-INVALID-USER u102)
(define-constant ERR-LISTING u103)
(define-constant ERR-WRONG-COMMISSION u104)
(define-constant ERR-NOT-FOUND u105)
(define-constant ERR-NFT-MINT u106)
(define-constant ERR-CONTRACT-LOCKED u107)
(define-data-var last-id uint u0)
(define-data-var artist-address principal 'SP26GZCVY8FYHNZ6C73W68TCFJHS8F8C9E772XX7X)
(define-data-var locked bool false)
(define-map cids uint (string-ascii 64))
(define-public (lock-contract)
(begin
(asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-NOT-AUTHORIZED))
(var-set locked true)
(ok true)))
(define-public (set-artist-address (address principal))
(begin
(asserts! (or (is-eq tx-sender (var-get artist-address)) (is-eq tx-sender DEPLOYER)) (err ERR-INVALID-USER))
(ok (var-set artist-address address))))
(define-public (burn (token-id uint))
(begin
(asserts! (is-owner token-id tx-sender) (err ERR-NOT-AUTHORIZED))
(nft-burn? alphabet token-id tx-sender)))
(define-private (is-owner (token-id uint) (user principal))
(is-eq user (unwrap! (nft-get-owner? alphabet token-id) false)))
(define-public (transfer (token-id uint) (sender principal) (recipient principal))
(begin
(asserts! (is-eq tx-sender sender) (err ERR-INVALID-USER))
(nft-transfer? alphabet token-id sender recipient)))
(define-read-only (get-owner (token-id uint))
(ok (nft-get-owner? alphabet 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 "ipfs://" (unwrap-panic (map-get? cids token-id))))))
(define-public (claim (uris (list 25 (string-ascii 64))))
(mint-many uris))
(define-private (mint-many (uris (list 25 (string-ascii 64))))
(let
(
(token-id (+ (var-get last-id) u1))
(art-addr (var-get artist-address))
(id-reached (fold mint-many-iter uris token-id))
(current-balance (get-balance tx-sender))
)
(asserts! (or (is-eq tx-sender DEPLOYER) (is-eq tx-sender art-addr)) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (var-get locked) false) (err ERR-CONTRACT-LOCKED))
(var-set last-id (- id-reached u1))
(map-set token-count tx-sender (+ current-balance (- id-reached token-id)))
(ok id-reached)))
(define-private (mint-many-iter (hash (string-ascii 64)) (next-id uint))
(begin
(unwrap! (nft-mint? alphabet next-id tx-sender) next-id)
(map-set cids next-id hash)
(+ next-id u1)))
;; NON-CUSTODIAL FUNCTIONS START
(use-trait commission-trait 'SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335.commission-trait.commission)
(define-map token-count principal uint)
(define-map market uint {price: uint, commission: principal})
(define-read-only (get-balance (account principal))
(default-to u0
(map-get? token-count account)))
(define-private (trnsfr (id uint) (sender principal) (recipient principal))
(match (nft-transfer? alphabet id sender recipient)
success
(let
((sender-balance (get-balance sender))
(recipient-balance (get-balance recipient)))
(map-set token-count
sender
(- sender-balance u1))
(map-set token-count
recipient
(+ recipient-balance u1))
(ok success))
error (err error)))
(define-private (is-sender-owner (id uint))
(let ((owner (unwrap! (nft-get-owner? alphabet id) false)))
(or (is-eq tx-sender owner) (is-eq contract-caller owner))))
(define-read-only (get-listing-in-ustx (id uint))
(map-get? market id))
(define-public (list-in-ustx (id uint) (price uint) (comm-trait <commission-trait>))
(let ((listing {price: price, commission: (contract-of comm-trait)}))
(asserts! (is-sender-owner id) (err ERR-NOT-AUTHORIZED))
(map-set market id listing)
(print (merge listing {a: "list-in-ustx", id: id}))
(ok true)))
(define-public (unlist-in-ustx (id uint))
(begin
(asserts! (is-sender-owner id) (err ERR-NOT-AUTHORIZED))
(map-delete market id)
(print {a: "unlist-in-ustx", id: id})
(ok true)))
(define-public (buy-in-ustx (id uint) (comm-trait <commission-trait>))
(let ((owner (unwrap! (nft-get-owner? alphabet id) (err ERR-NOT-FOUND)))
(listing (unwrap! (map-get? market id) (err ERR-LISTING)))
(price (get price listing)))
(asserts! (is-eq (contract-of comm-trait) (get commission listing)) (err ERR-WRONG-COMMISSION))
(try! (stx-transfer? price tx-sender owner))
(try! (pay-royalty price))
(try! (contract-call? comm-trait pay id price))
(try! (trnsfr id owner tx-sender))
(map-delete market id)
(print {a: "buy-in-ustx", id: id})
(ok true)))
(define-data-var royalty-percent uint u500)
(define-read-only (get-royalty-percent)
(ok (var-get royalty-percent)))
(define-private (pay-royalty (price uint))
(let (
(royalty (/ (* price (var-get royalty-percent)) u10000))
)
(if (> (var-get royalty-percent) u0)
(try! (stx-transfer? royalty tx-sender (var-get artist-address)))
(print false)
)
(ok true)))
;; NON-CUSTODIAL FUNCTIONS END
(try! (nft-mint? alphabet u1 'SP26GZCVY8FYHNZ6C73W68TCFJHS8F8C9E772XX7X))
(map-set token-count 'SP26GZCVY8FYHNZ6C73W68TCFJHS8F8C9E772XX7X (+ (get-balance 'SP26GZCVY8FYHNZ6C73W68TCFJHS8F8C9E772XX7X) u1))
(map-set cids u1 "QmaCTWqQ6eozLJNCXEWB4i24BWpHSHPT7BTS6v2hooFczc/json/1.json")
(var-set last-id u1)