-
Notifications
You must be signed in to change notification settings - Fork 13
/
venice-visuals.clar
255 lines (225 loc) · 6.63 KB
/
venice-visuals.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;; https://explorer.stacks.co/txid/0x80eb693e5e2a9928094792080b7f6d69d66ea9cc881bc465e8d9c5c621bd4d07?chain=mainnet
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(use-trait commission-trait .commission-trait.commission)
(define-non-fungible-token venice-visuals uint)
;; constants
(define-constant ERR-NOT-AUTHORIZED u401)
(define-constant ERR-ALL-MINTED u101)
(define-constant ERR-NOT-FOUND u102)
(define-constant ERR-LISTING u103)
(define-constant ERR-WRONG-COMMISSION u104)
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ITEM-COUNT u100)
;; variables
(define-data-var metadata-frozen bool false)
(define-data-var venice-counter uint u0)
(define-data-var venice-index uint u0)
(define-data-var token-uri (string-ascii 256) "ipfs://QmWAJBFiq5TFTDawymE2kj2fXSfimZDzMRt49joatFTs4o/{id}.json")
(define-data-var cost-per-mint uint u35000000)
(define-data-var marketplace-commission uint u5000000)
(define-data-var creator-address principal 'SP1NYHBF7GNF9CE7P5KB27VZTHK3V8XANTMXNHD2)
(define-data-var rotation uint u1)
(define-map token-count principal uint)
(define-map market uint {price: uint, commission: principal})
;; public functions
(define-public (mint)
(let (
(count (var-get venice-counter))
)
(asserts! (<= count ITEM-COUNT) (err ERR-ALL-MINTED))
(try! (mint-next))
(ok true)
)
)
(define-public (mint-three)
(begin
(try! (mint))
(try! (mint))
(try! (mint))
(ok true)
)
)
(define-public (mint-five)
(begin
(try! (mint))
(try! (mint))
(try! (mint))
(try! (mint))
(try! (mint))
(ok true)
)
)
(define-private (mint-next)
(let (
(count (var-get venice-counter))
(idx (var-get venice-index))
(random-venice-id
(if (is-eq (var-get rotation) u0)
(+ u1 idx)
(- ITEM-COUNT (- count idx))
)
)
(current-balance (get-balance tx-sender))
)
(match (stx-transfer? (var-get cost-per-mint) tx-sender (as-contract tx-sender))
success (begin
(try! (nft-mint? venice-visuals random-venice-id tx-sender))
(var-set venice-counter (+ count u1))
(if (is-eq u0 (var-get rotation))
(begin
(var-set rotation u1)
(var-set venice-index (+ u1 (var-get venice-index)))
)
(var-set rotation u0)
)
(try!
(as-contract
(stx-transfer? (- (var-get cost-per-mint) (var-get marketplace-commission)) (as-contract tx-sender) (var-get creator-address))
)
)
(map-set token-count tx-sender (+ current-balance u1))
(ok random-venice-id)
)
error (err error)
)
)
)
(define-public (burn (index uint))
(let (
(current-balance (get-balance tx-sender))
)
(asserts! (is-sender-owner index) (err ERR-NOT-AUTHORIZED))
(match (nft-burn? venice-visuals index tx-sender)
success (begin
(map-set token-count tx-sender (- current-balance u1))
(ok true)
)
error (err error)
)
)
)
(define-private (trnsfr (id uint) (sender principal) (recipient principal))
(match (nft-transfer? venice-visuals 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)
)
)
;; SIP009: Transfer token to a specified principal
(define-public (transfer (id uint) (sender principal) (recipient principal))
(begin
(asserts! (is-eq tx-sender sender) (err ERR-NOT-AUTHORIZED))
(asserts! (is-none (map-get? market id)) (err ERR-LISTING))
(trnsfr id sender recipient)
)
)
(define-read-only (get-last-token-id)
(ok (var-get venice-counter))
)
(define-public (set-cost-per-mint (value uint))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set cost-per-mint value))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (set-marketplace-commission (value uint))
(if (is-eq tx-sender CONTRACT-OWNER)
(ok (var-set marketplace-commission value))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (freeze-metadata)
(if (or
(is-eq tx-sender CONTRACT-OWNER)
(is-eq tx-sender (var-get creator-address))
)
(ok (var-set metadata-frozen true))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (set-token-uri (value (string-ascii 256)))
(begin
(asserts! (is-eq false (var-get metadata-frozen)) (err ERR-NOT-AUTHORIZED))
(if (or
(is-eq tx-sender CONTRACT-OWNER)
(is-eq tx-sender (var-get creator-address))
)
(ok (var-set token-uri value))
(err ERR-NOT-AUTHORIZED)
)
)
)
(define-public (set-creator-address (address principal))
(if (or
(is-eq tx-sender CONTRACT-OWNER)
(is-eq tx-sender (var-get creator-address))
)
(ok (var-set creator-address address))
(err ERR-NOT-AUTHORIZED)
)
)
(define-read-only (get-token-uri (id uint))
(ok (some (var-get token-uri)))
)
(define-read-only (get-owner (index uint))
(ok (nft-get-owner? venice-visuals index))
)
(define-read-only (get-balance (account principal))
(default-to u0
(map-get? token-count account)
)
)
(define-public (transfer-stx (address principal) (amount uint))
(if (is-eq tx-sender CONTRACT-OWNER)
(as-contract (stx-transfer? amount (as-contract tx-sender) address))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (list-in-ustx (id uint) (price uint) (comm <commission-trait>))
(let (
(listing {price: price, commission: (contract-of comm)})
)
(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 <commission-trait>))
(let (
(owner (unwrap! (nft-get-owner? venice-visuals id) (err ERR-NOT-FOUND)))
(listing (unwrap! (map-get? market id) (err ERR-LISTING)))
(price (get price listing))
)
(asserts! (is-eq (contract-of comm) (get commission listing)) (err ERR-WRONG-COMMISSION))
(try! (stx-transfer? price tx-sender owner))
(try! (contract-call? comm pay id price))
(try! (trnsfr id owner tx-sender))
(map-delete market id)
(print {a: "buy-in-ustx", id: id})
(ok true)
)
)
;; private functions
(define-private (is-sender-owner (id uint))
(let (
(owner (unwrap! (nft-get-owner? venice-visuals id) false))
)
(or (is-eq tx-sender owner) (is-eq contract-caller owner))
)
)