-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest-byzantion-auctions.clar
More file actions
386 lines (345 loc) · 13.5 KB
/
Copy pathtest-byzantion-auctions.clar
File metadata and controls
386 lines (345 loc) · 13.5 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
;; (impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(impl-trait .nft-trait.nft-trait)
;; (use-trait commission-trait 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.commission-trait.commission)
(use-trait commission-trait .commission-trait.commission)
;; Non Fungible Token, using sip-009
(define-non-fungible-token test-byzantion-auctions uint)
(define-constant ERR-NOT-AUTHORIZED u401)
(define-constant ERR-TOKEN-NOT-MINTED u404)
(define-constant ERR-AUCTION-NOT-OVER (err u1001))
(define-constant ERR-AUCTION-OVER (err u1002))
(define-constant ERR-RESERVE-NOT-MET (err u1003))
(define-constant ERR-BID-TOO-LOW (err u1004))
(define-constant ERR-AUCTION-NOT-LIVE (err u1005))
(define-constant ERR-NOT-FOUND u1006)
(define-constant ERR-LISTING u106)
(define-constant ERR-WRONG-COMMISSION u107)
(define-constant CONTRACT-OWNER tx-sender)
(define-data-var last-id uint u0)
(define-data-var commission-percentage uint u1000)
(define-data-var active bool false)
(define-data-var commission-address principal 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C)
(define-data-var admin (list 1000 principal) (list 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C))
(define-map metadata uint { uri: (string-ascii 100) })
(define-map bids uint { buyer: principal, offer: uint })
(define-map token-count principal uint)
(define-map market uint {price: uint, commission: principal})
(define-map items uint {artist: principal, royalty: uint, name: (string-ascii 256), reserve: uint, start-block: uint, end-block: uint})
(define-public (bid (item-id uint) (amount uint))
(let (
(bid-object (get-bid item-id))
(item-object (unwrap! (map-get? items item-id) (err u2)))
(offer (get offer bid-object))
(buyer (get buyer bid-object))
(start (get start-block item-object))
(target (get end-block item-object))
(reserve-price (get reserve item-object))
)
(asserts! (>= block-height start) ERR-AUCTION-NOT-LIVE)
(asserts! (<= block-height target) ERR-AUCTION-OVER)
(asserts! (> amount offer) ERR-BID-TOO-LOW)
(asserts! (>= amount reserve-price) ERR-BID-TOO-LOW)
(match (stx-transfer? amount tx-sender (as-contract tx-sender))
success (begin
(if (> offer u0)
(begin
(try! (as-contract (stx-transfer? offer (as-contract tx-sender) buyer)))
(map-set bids item-id { buyer: tx-sender, offer: amount })
)
(map-set bids item-id { buyer: tx-sender, offer: amount })
)
(ok item-id)
)
error (err error)
)
)
)
(define-read-only (get-bid (item-id uint))
(default-to
{ buyer: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM, offer: u0 }
(map-get? bids item-id )
)
)
(define-public (auction-ended (item-id uint))
(let (
(bid-object (map-get? bids item-id))
(item-object (map-get? items item-id))
(offer (unwrap-panic (get offer bid-object)))
(buyer (unwrap-panic (get buyer bid-object)))
(commiss (/ (* offer (var-get commission-percentage)) u10000))
(commission-addr (var-get commission-address))
(target (unwrap-panic (get end-block item-object)))
(reserve-price (unwrap-panic (get reserve item-object)))
(artist (unwrap-panic (get artist (map-get? items item-id))))
(current-balance (get-balance tx-sender))
(allowed (is-some (index-of (var-get admin) tx-sender)))
)
(asserts! (> block-height target) ERR-AUCTION-NOT-OVER)
(asserts! (>= offer reserve-price) ERR-RESERVE-NOT-MET)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(begin
(try! (as-contract (stx-transfer? commiss (as-contract tx-sender) commission-addr)))
(try! (as-contract (stx-transfer? (- offer commiss) (as-contract tx-sender) artist)))
(try! (nft-mint? test-byzantion-auctions item-id buyer))
(map-set token-count buyer (+ current-balance u1))
(map-delete bids item-id )
(var-set last-id item-id)
)
(ok item-id)
)
)
(define-public (admin-unbid (item-id uint))
(let (
(bid-object (map-get? bids item-id))
(offer (unwrap-panic (get offer bid-object)))
(buyer (unwrap-panic (get buyer bid-object)))
(allowed (is-some (index-of (var-get admin) tx-sender)))
)
(begin
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(try! (as-contract (stx-transfer? offer (as-contract tx-sender) buyer)))
(map-delete bids item-id )
(ok true)
)
)
)
(define-public (burn (item-id uint))
(let (
(allowed (is-some (index-of (var-get admin) tx-sender)))
)
(asserts! (not (is-eq (nft-get-owner? test-byzantion-auctions item-id) none)) (err ERR-TOKEN-NOT-MINTED))
(if (is-eq tx-sender (unwrap-panic (nft-get-owner? test-byzantion-auctions item-id)))
(begin
(try! (nft-burn? test-byzantion-auctions item-id tx-sender))
(ok true)
)
(err ERR-NOT-AUTHORIZED)
)
))
(define-public (set-commission (value uint))
(if (is-some (index-of (var-get admin) tx-sender))
(ok (var-set commission-percentage value))
(err ERR-NOT-AUTHORIZED)
)
)
(define-read-only (get-item (id uint))
(default-to
{ artist: 'SP3ZJP253DENMN3CQFEQSPZWY7DK35EH3SEH0J8PK, royalty: u0, name: "None", reserve: u0, start-block: u0, end-block: u0 }
(map-get? items id)
)
)
(define-public (set-item (id uint) (address principal) (royalty uint) (name (string-ascii 256)) (reserve uint) (start uint) (end uint) (meta (string-ascii 100)))
(begin
(try! (set-royalty-address id address))
(try! (set-royalty-amount id royalty))
(try! (set-name id name))
(try! (set-reserve id reserve))
(try! (set-start-block id start))
(try! (set-end-block id end))
(try! (set-metadata id meta))
(var-set last-id id)
(ok true)
)
)
(define-public (set-royalty-address (id uint) (item-address principal))
(let (
(previous-address (get artist (get-item id)))
(item-name (get name (get-item id)))
(item-royalty (get royalty (get-item id)))
(item-reserve (get reserve (get-item id)))
(item-start (get start-block (get-item id)))
(item-end (get end-block (get-item id)))
(object { artist: item-address, royalty: item-royalty, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender previous-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-royalty-amount (id uint) (item-amount uint))
(let (
(item-address (get artist (get-item id)))
(item-name (get name (get-item id)))
(item-reserve (get reserve (get-item id)))
(item-start (get start-block (get-item id)))
(item-end (get end-block (get-item id)))
(object { artist: item-address, royalty: item-amount, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender item-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-name (id uint) (item-name (string-ascii 256)))
(let (
(item-address (get artist (get-item id)))
(item-royalty (get royalty (get-item id)))
(item-reserve (get reserve (get-item id)))
(item-start (get start-block (get-item id)))
(item-end (get end-block (get-item id)))
(object { artist: item-address, royalty: item-royalty, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender item-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-reserve (id uint) (item-reserve uint))
(let (
(item-address (get artist (get-item id)))
(item-royalty (get royalty (get-item id)))
(item-start (get start-block (get-item id)))
(item-name (get name (get-item id)))
(item-end (get end-block (get-item id)))
(object { artist: item-address, royalty: item-royalty, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender item-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-start-block (id uint) (item-start uint))
(let (
(item-address (get artist (get-item id)))
(item-royalty (get royalty (get-item id)))
(item-reserve (get reserve (get-item id)))
(item-name (get name (get-item id)))
(item-end (get end-block (get-item id)))
(object { artist: item-address, royalty: item-royalty, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender item-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-end-block (id uint) (item-end uint))
(let (
(item-address (get artist (get-item id)))
(item-royalty (get royalty (get-item id)))
(item-reserve (get reserve (get-item id)))
(item-name (get name (get-item id)))
(item-start (get start-block (get-item id)))
(object { artist: item-address, royalty: item-royalty, name: item-name, reserve: item-reserve, start-block: item-start, end-block: item-end })
(allowed (or (is-some (index-of (var-get admin) tx-sender)) (is-eq tx-sender item-address)))
)
(asserts! allowed (err ERR-NOT-AUTHORIZED))
(map-set items id object)
(ok true)
)
)
(define-public (set-commission-address (value principal))
(if (is-some (index-of (var-get admin) tx-sender))
(ok (var-set commission-address value))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (set-admin (values (list 100 principal)))
(if (is-some (index-of (var-get admin) tx-sender))
(ok (var-set admin values))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (set-metadata (item-id uint) (meta (string-ascii 100)))
(if (is-some (index-of (var-get admin) tx-sender))
(ok (map-set metadata item-id {
uri: meta
}))
(err ERR-NOT-AUTHORIZED)
)
)
;; Transfers stx from contract to contract owner
(define-public (transfer-stx (address principal) (amount uint))
(if (is-some (index-of (var-get admin) tx-sender))
(as-contract (stx-transfer? amount (as-contract tx-sender) address))
(err ERR-NOT-AUTHORIZED)
)
)
;; Gets the owner of the specified token ID.
(define-read-only (get-owner (token-id uint))
(ok (nft-get-owner? test-byzantion-auctions token-id)))
;; Gets commission
(define-read-only (get-commission)
(ok (var-get commission-percentage))
)
;; Gets artist address
(define-read-only (get-artist (id uint))
(ok (get artist (get-item id)))
)
(define-read-only (get-auction-start (id uint))
(ok (get start-block (get-item id)))
)
(define-read-only (get-auction-end (id uint))
(ok (get end-block (get-item id)))
)
(define-read-only (get-royalty (id uint))
(ok (get royalty (get-item id)))
)
(define-read-only (get-name (id uint))
(ok (get name (get-item id)))
)
(define-read-only (get-reserve (id uint))
(ok (get reserve (get-item id)))
)
;; Gets the last created 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 (get uri (unwrap-panic (map-get? metadata token-id)))))
)
(define-read-only (get-balance (account principal))
(default-to u0
(map-get? token-count account)))
;; Non-custodial SIP-009 transfer function
(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-private (trnsfr (id uint) (sender principal) (recipient principal))
(match (nft-transfer? test-byzantion-auctions 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? test-byzantion-auctions 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? test-byzantion-auctions 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! (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)))