Repository navigation
Expand file tree
/
Copy pathbyzantion-market-v3.clar
More file actions
386 lines (345 loc) · 13.9 KB
/
Copy pathbyzantion-market-v3.clar
File metadata and controls
386 lines (345 loc) · 13.9 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(define-constant ERR-NOT-AUTHORIZED u401)
(define-constant ERR-BID-NOT-HIGH-ENOUGH u100)
(define-constant ERR-ITEM-NOT-FOR-SALE u101)
(define-constant ERR-ITEM-PRICE-TOO-LOW u102)
(define-constant CONTRACT-OWNER tx-sender)
(define-map collections { id: uint } {
name: (string-ascii 256),
artist: principal,
address: (optional principal),
commission: uint,
royalty: uint,
royalty-address: (optional principal)
})
(define-map item-for-sale { collection-id: uint, item-id: uint } { seller: (optional principal), price: uint })
(define-map items-by-seller { collection-id: uint, seller: principal } { ids: (list 2500 uint) })
(define-map item-bids { collection-id: uint, item-id: uint } { buyer: principal, offer: uint })
(define-map listed-item-ids { collection-id: uint } { ids: (list 5000 uint) })
(define-data-var last-collection-id uint u0)
(define-data-var removing-item-id uint u0)
;; Private functions
(define-private (remove-item-listing (collection-id uint) (item-id uint) (sender principal))
(if true
(let (
(collection-entry (get-collection-by-id collection-id))
(item-ids (unwrap-panic (get-item-ids-by-seller collection-id sender)))
(all-item-ids (get-listed-item-ids collection-id))
)
(var-set removing-item-id item-id)
(map-set listed-item-ids { collection-id: collection-id }
{ ids: (unwrap-panic (as-max-len? (filter remove-item-id all-item-ids) u5000)) }
)
(map-set items-by-seller { collection-id: collection-id, seller: sender }
{ ids: (unwrap-panic (as-max-len? (filter remove-item-id item-ids) u2500)) }
)
(ok true)
)
(err u0)
)
)
(define-private (remove-item-id (item-id uint))
(if (is-eq item-id (var-get removing-item-id))
false
true
)
)
;; Public functions
(define-public (add-collection
(name (string-ascii 256))
(address principal)
(commission uint)
(royalty uint)
(royalty-address principal)
)
(begin
(asserts!
(or
(is-eq tx-sender CONTRACT-OWNER)
)
(err ERR-NOT-AUTHORIZED)
)
(map-set collections { id: (var-get last-collection-id)} {
name: name,
artist: tx-sender,
address: (some address),
commission: (if (is-eq tx-sender CONTRACT-OWNER)
commission
u250
),
royalty: royalty,
royalty-address: (some royalty-address)
})
(var-set last-collection-id (+ u1 (var-get last-collection-id)))
(ok true)
)
)
(define-public (list-item (collection <nft-trait>) (collection-id uint) (item-id uint) (price uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item-owner (unwrap-panic (contract-call? collection get-owner item-id)))
(item-ids (unwrap-panic (get-item-ids-by-seller collection-id tx-sender)))
(all-item-ids (get-listed-item-ids collection-id))
)
(asserts! (is-eq tx-sender (unwrap-panic item-owner)) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(asserts! (> price u0) (err ERR-ITEM-PRICE-TOO-LOW))
(map-set item-for-sale { collection-id: collection-id, item-id: item-id } { seller: (some tx-sender), price: price })
(map-set items-by-seller { collection-id: collection-id, seller: tx-sender }
{ ids: (unwrap-panic (as-max-len? (append item-ids item-id) u2500)) }
)
(map-set listed-item-ids { collection-id: collection-id }
{ ids: (unwrap-panic (as-max-len? (append all-item-ids item-id) u5000)) }
)
(print {
type: "marketplace",
action: "list-item",
data: { collection-id: collection-id, item-id: item-id, seller: tx-sender, price: price }
})
(contract-call? collection transfer item-id tx-sender (as-contract tx-sender))
)
)
(define-public (change-price (collection <nft-trait>) (collection-id uint) (item-id uint) (price uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item (get-item-for-sale collection-id item-id))
(sender tx-sender)
)
(asserts! (is-some (get seller item)) (err ERR-ITEM-NOT-FOR-SALE))
(asserts! (is-eq sender (unwrap-panic (get seller item))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(asserts! (> price u0) (err ERR-ITEM-PRICE-TOO-LOW))
(map-set item-for-sale { collection-id: collection-id, item-id: item-id } { seller: (some tx-sender), price: price })
(print {
type: "marketplace",
action: "change-price",
data: { collection-id: collection-id, item-id: item-id, seller: sender, price: price }
})
(ok true)
)
)
(define-public (unlist-item (collection <nft-trait>) (collection-id uint) (item-id uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item (get-item-for-sale collection-id item-id))
(sender tx-sender)
(bid (get-item-bid collection-id item-id))
)
(asserts! (is-some (get seller item)) (err ERR-ITEM-NOT-FOR-SALE))
(asserts! (is-eq sender (unwrap-panic (get seller item))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(try! (remove-item-listing collection-id item-id sender))
(map-delete item-for-sale { collection-id: collection-id, item-id: item-id })
(if (> (get offer bid) u0)
(begin
(try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid))))
(map-delete item-bids { collection-id: collection-id, item-id: item-id })
)
true
)
(print {
type: "marketplace",
action: "unlist-item",
data: { collection-id: collection-id, item-id: item-id }
})
(as-contract (contract-call? collection transfer item-id (as-contract tx-sender) sender))
)
)
(define-public (bid-item (collection-id uint) (item-id uint) (amount uint))
(let (
(item (get-item-for-sale collection-id item-id))
(bid (get-item-bid collection-id item-id))
)
(asserts! (> amount (get offer bid)) (err ERR-BID-NOT-HIGH-ENOUGH))
(match (stx-transfer? amount tx-sender (as-contract tx-sender))
success (begin
(if (> (get offer bid) u0)
(begin
(try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid))))
(map-delete item-bids { collection-id: collection-id, item-id: item-id })
)
true
)
(map-set item-bids { collection-id: collection-id, item-id: item-id } { buyer: tx-sender, offer: amount })
(print {
type: "marketplace",
action: "bid-item",
data: { collection-id: collection-id, item-id: item-id, buyer: tx-sender, offer: amount }
})
(ok amount)
)
error (err error)
)
)
)
(define-public (withdraw-bid (collection-id uint) (item-id uint))
(let (
(item (get-item-for-sale collection-id item-id))
(bid (get-item-bid collection-id item-id))
(sender tx-sender)
)
(asserts! (is-eq tx-sender (get buyer bid)) (err ERR-NOT-AUTHORIZED))
(match (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) sender))
success (begin
(map-delete item-bids { collection-id: collection-id, item-id: item-id })
(print {
type: "marketplace",
action: "withdraw-bid",
data: { collection-id: collection-id, item-id: item-id, buyer: tx-sender }
})
(ok (get offer bid))
)
error (err error)
)
)
)
(define-public (accept-bid (collection <nft-trait>) (collection-id uint) (item-id uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item (get-item-for-sale collection-id item-id))
(bid (get-item-bid collection-id item-id))
(commission (/ (* (get offer bid) (get commission collection-entry)) u10000))
(royalty (/ (* (get offer bid) (get royalty collection-entry)) u10000))
)
(asserts! (is-eq tx-sender (unwrap-panic (get seller item))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(try! (as-contract (stx-transfer? commission (as-contract tx-sender) CONTRACT-OWNER)))
(if (is-some (get royalty-address collection-entry))
(try! (as-contract (stx-transfer? royalty (as-contract tx-sender) (unwrap-panic (get royalty-address collection-entry)))))
true
)
(try! (as-contract (stx-transfer? (- (- (get offer bid) commission) royalty) (as-contract tx-sender) (unwrap-panic (get seller item)))))
(try! (remove-item-listing collection-id item-id (unwrap-panic (get seller item))))
(map-delete item-for-sale { collection-id: collection-id, item-id: item-id })
(try! (as-contract (contract-call? collection transfer item-id (as-contract tx-sender) (get buyer bid))))
(print {
type: "marketplace",
action: "accept-bid",
data: { collection-id: collection-id, item-id: item-id }
})
(ok true)
)
)
(define-public (buy-item (collection <nft-trait>) (collection-id uint) (item-id uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item-owner (unwrap-panic (contract-call? collection get-owner item-id)))
(item (get-item-for-sale collection-id item-id))
(sender tx-sender)
(commission (/ (* (get price item) (get commission collection-entry)) u10000))
(royalty (/ (* (get price item) (get royalty collection-entry)) u10000))
(bid (get-item-bid collection-id item-id))
)
(asserts! (not (is-eq tx-sender (unwrap-panic item-owner))) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (get seller item)) (err ERR-ITEM-NOT-FOR-SALE))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(try! (stx-transfer? commission sender CONTRACT-OWNER))
(if (not (is-eq sender (unwrap-panic (get seller item))))
(try! (stx-transfer? (- (- (get price item) commission) royalty) sender (unwrap-panic (get seller item))))
true
)
(if (> (get offer bid) u0)
(begin
(try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid))))
(map-delete item-bids { collection-id: collection-id, item-id: item-id })
)
true
)
(if (and (is-some (get royalty-address collection-entry)) (> royalty u0))
(try! (stx-transfer? royalty sender (unwrap-panic (get royalty-address collection-entry))))
true
)
(try! (remove-item-listing collection-id item-id (unwrap-panic (get seller item))))
(map-delete item-for-sale { collection-id: collection-id, item-id: item-id })
(try! (as-contract (contract-call? collection transfer item-id (as-contract tx-sender) sender)))
(print {
type: "marketplace",
action: "buy-item",
data: { collection-id: collection-id, item-id: item-id }
})
(ok true)
)
)
(define-public (admin-unlist (collection <nft-trait>) (collection-id uint) (item-id uint))
(let (
(collection-entry (get-collection-by-id collection-id))
(item (get-item-for-sale collection-id item-id))
(bid (get-item-bid collection-id item-id))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
(asserts! (is-some (get seller item)) (err ERR-ITEM-NOT-FOR-SALE))
(asserts! (is-eq (contract-of collection) (unwrap-panic (get address collection-entry))) (err ERR-NOT-AUTHORIZED))
(try! (remove-item-listing collection-id item-id (unwrap-panic (get seller item))))
(map-delete item-for-sale { collection-id: collection-id, item-id: item-id })
(if (> (get offer bid) u0)
(begin
(try! (as-contract (stx-transfer? (get offer bid) (as-contract tx-sender) (get buyer bid))))
(map-delete item-bids { collection-id: collection-id, item-id: item-id })
)
true
)
(print {
type: "marketplace",
action: "admin-unlist",
data: { collection-id: collection-id, item-id: item-id }
})
(as-contract (contract-call? collection transfer item-id (as-contract tx-sender) (unwrap-panic (get seller item))))
)
)
(define-public (set-sale-commission (collection-id uint) (commission uint))
(let (
(collection-entry (get-collection-by-id collection-id))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
(merge collection-entry {
commission: commission
})
(ok true)
)
)
(define-public (set-royalty (collection-id uint) (royalty uint) (royalty-address principal))
(let (
(collection-entry (get-collection-by-id collection-id))
)
(asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
(merge collection-entry {
royalty: royalty,
royalty-address: (some royalty-address)
})
(ok true)
)
)
;; Read only functions
(define-read-only (get-collection-by-id (collection-id uint))
(default-to
{ name: "null", address: none, commission: u0, royalty: u0, royalty-address: none }
(map-get? collections { id: collection-id })
)
)
(define-read-only (get-listed-item-ids (collection-id uint))
(default-to
(list )
(get ids (map-get? listed-item-ids { collection-id: collection-id }))
)
)
(define-read-only (get-item-for-sale (collection-id uint) (item-id uint))
(default-to
{ seller: none, price: u99000000000000 }
(map-get? item-for-sale { collection-id: collection-id, item-id: item-id })
)
)
(define-read-only (get-item-ids-by-seller (collection-id uint) (seller principal))
(ok (get ids (get-item-entry-by-seller collection-id seller)))
)
(define-read-only (get-item-bid (collection-id uint) (item-id uint))
(default-to
{ buyer: CONTRACT-OWNER, offer: u0 }
(map-get? item-bids { collection-id: collection-id, item-id: item-id })
)
)
(define-read-only (get-item-entry-by-seller (collection-id uint) (seller principal))
(default-to
{ ids: (list ) }
(map-get? items-by-seller { collection-id: collection-id, seller: seller })
)
)