-
Notifications
You must be signed in to change notification settings - Fork 13
/
frodo-shire.clar
555 lines (487 loc) · 17.6 KB
/
frodo-shire.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait) ;; for mainnet
;; (impl-trait .nft-trait.nft-trait)
(use-trait commission-trait 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.commission-trait.commission) ;; for mainnet
;; (use-trait commission-trait .commission-trait.commission)
(define-non-fungible-token blue-hobbit-cloud-3 uint)
;; Storage
(define-map token-count principal uint)
(define-map market uint {price: uint, commission: principal})
(define-map private-presale-count principal uint)
(define-map public-sale-count principal uint)
;; Define Constants
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-SOLD-OUT (err u300))
(define-constant ERR-WRONG-COMMISSION (err u301))
(define-constant ERR-INSUFFICIENT-FUNDS (err u400))
(define-constant ERR-NOT-AUTHORIZED (err u401))
(define-constant ERR-NOT-FOUND (err u404))
(define-constant ERR-SALE-NOT-ACTIVE (err u500))
(define-constant ERR-NO-MINTPASS-REMAINING (err u501))
(define-constant ERR-METADATA-FROZEN (err u505))
(define-constant ERR-MINT-ALREADY-SET (err u506))
(define-constant ERR-LISTING (err u507))
(define-constant ERR-ONE-MINT-PER-WALLET (err u508))
(define-constant ERR-BEFORE-MINT-TIME (err u509))
(define-constant ERR-AFTER-MINT-TIME (err u510))
(define-constant MINT-LIMIT u1200) ;; u3000
;; Define Variables
(define-data-var last-id uint u0)
(define-data-var metadata-frozen bool false)
(define-data-var base-uri (string-ascii 100) "ipfs://TEST_META_HASH/")
(define-data-var contract-uri (string-ascii 100) "ipfs://placeholder")
(define-data-var commission-percentage uint u1000)
(define-data-var commission-address principal 'SP7NW9XVEG45SW1367SAHHZG67BFA4GMA9DVGZ0F)
(define-data-var royalty (list 1000 uint) (list u9000 )) ;; u4500 u4500
(define-data-var artist-address (list 1000 principal) (list 'SP2XQ2Y69BS0C6TYEVEEVY734QWCHFZ1Q4SM0WD47 ))
(define-data-var private-sale-active bool true)
(define-data-var sale-active bool true)
(define-data-var private-target-block-height uint u0)
(define-data-var public-target-block-height uint u0)
(define-data-var admin principal tx-sender)
(define-data-var stx-cost-per-mint uint u60000000) ;; u100000000
;; Get mint limit
(define-read-only (get-mint-limit)
MINT-LIMIT
)
;; Token count for account
(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? blue-hobbit-cloud-3 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-NOT-AUTHORIZED)
(asserts! (is-none (map-get? market id)) ERR-LISTING)
(trnsfr id sender recipient)))
;; SIP009: Get the owner of the specified token ID
(define-read-only (get-owner (id uint))
;; Make sure to replace blue-hobbit-cloud-3
(ok (nft-get-owner? blue-hobbit-cloud-3 id)))
;; SIP009: Get the last token ID
(define-read-only (get-last-token-id)
(ok (var-get last-id))
)
;; SIP009: Get the token URI. You can set it to any other URI
(define-read-only (get-token-uri (token-id uint))
(if (< token-id u5001)
(ok (some (concat (concat (var-get base-uri) (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.conversion lookup token-id))) ".json")))
(ok (some (concat (concat (var-get base-uri) (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.conversion-v2 lookup (- token-id u5001)))) ".json")))
)
)
(define-read-only (get-contract-uri)
(ok (var-get contract-uri)))
;; Mint new NFT
;; can only be called from the Mint
(define-private (mint (new-owner principal))
(let (
(next-id (+ u1 (var-get last-id)))
)
(asserts! (< (var-get last-id) MINT-LIMIT) ERR-SOLD-OUT)
(match (nft-mint? blue-hobbit-cloud-3 next-id new-owner)
success
(let
((current-balance (get-balance new-owner)))
(begin
;; (print payout)
(try! (payout-all))
(var-set last-id next-id)
(map-set token-count
new-owner
(+ current-balance u1)
)
(ok true)))
error (err (* error u10000)))))
(define-private (payout-all)
(let (
(price (var-get stx-cost-per-mint))
(commission-amount (/ (* price (var-get commission-percentage)) u10000))
(price-list (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.uint-lists lookup price (len (var-get royalty))))
(divider-list (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.uint-lists lookup u10000 (len (var-get royalty))))
(royalty-amounts (map * (var-get royalty) price-list))
(payout-amounts (map / royalty-amounts divider-list))
)
(asserts! (>= (stx-get-balance tx-sender) price) ERR-INSUFFICIENT-FUNDS)
(try! (stx-transfer? commission-amount tx-sender (var-get commission-address)))
(map payout (var-get artist-address) payout-amounts)
(ok payout-amounts)
)
)
(define-private (payout (receiver principal) (amount uint))
(stx-transfer? amount tx-sender receiver)
)
(define-public (burn (id uint) (owner principal))
(let (
(token-owner (unwrap-panic (unwrap-panic (get-owner id))))
)
(asserts! (is-none (map-get? market id)) ERR-LISTING)
(asserts! (is-eq tx-sender owner) ERR-NOT-AUTHORIZED)
(asserts! (is-eq tx-sender token-owner) ERR-NOT-AUTHORIZED)
(match (nft-burn? blue-hobbit-cloud-3 id owner)
success
(let
((current-balance (get-balance owner)))
(begin
(map-set token-count
owner
(- current-balance u1)
)
(ok true)))
error (err (* error u10000)))
)
)
(define-private (is-sender-owner (id uint))
(let ((owner (unwrap! (nft-get-owner? blue-hobbit-cloud-3 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 <commission-trait>))
(let ((listing {price: price, commission: (contract-of comm)}))
(asserts! (is-sender-owner id) 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-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? blue-hobbit-cloud-3 id) ERR-NOT-FOUND))
(listing (unwrap! (map-get? market id) ERR-LISTING))
(price (get price listing)))
(asserts! (is-eq (contract-of comm) (get commission listing)) 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)))
;; Set base uri
(define-public (set-base-uri (new-base-uri (string-ascii 100)))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(asserts! (not (var-get metadata-frozen)) ERR-METADATA-FROZEN)
(var-set base-uri new-base-uri)
(ok true)))
;; Set contract uri
(define-public (set-contract-uri (new-contract-uri (string-ascii 100)))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(asserts! (not (var-get metadata-frozen)) ERR-METADATA-FROZEN)
(var-set contract-uri new-contract-uri)
(ok true))
)
;; Freeze metadata
(define-public (freeze-metadata)
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set metadata-frozen true)
(ok true)))
(define-public (set-stx-cost (amount uint))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set stx-cost-per-mint amount)
(ok true)))
(define-public (set-artist-addresses (addresses (list 1000 principal)))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set artist-address addresses)
(ok true)))
(define-public (set-royalties (royalties (list 1000 uint)))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set royalty royalties)
(ok true)))
(define-public (set-admin (new-admin principal))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set admin new-admin)
(ok true)
)
)
(define-private (not-live)
(begin
(asserts! (var-get sale-active) ERR-SALE-NOT-ACTIVE)
(asserts! (var-get private-sale-active) ERR-SALE-NOT-ACTIVE)
(ok false)
)
)
;; Claim a new NFT
(define-public (claim)
(if (var-get sale-active)
(public-mint tx-sender)
(if (var-get private-sale-active)
(private-mintpass-mint tx-sender)
(not-live)
)
)
)
;; begin mint 2
(define-public (claim-two)
(begin
(try! (claim))
(try! (claim))
(ok true)))
;; end mint 2
;; begin mint 3
(define-public (claim-three)
(begin
(try! (claim))
(try! (claim))
(try! (claim))
(ok true)))
;; end mint 3
;; begin mint 4
(define-public (claim-four)
(begin
(try! (claim))
(try! (claim))
(try! (claim))
(try! (claim))
(ok true)))
;; end mint 4
;; begin mint 5
(define-public (claim-five)
(begin
(try! (claim))
(try! (claim))
(try! (claim))
(try! (claim))
(try! (claim))
(ok true)))
;; end mint 5
;; Set public sale flag
(define-public (flip-private-sale)
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
;; Disable the Public sale
(var-set sale-active false)
(var-set private-sale-active (not (var-get private-sale-active)))
(ok (var-get private-sale-active))))
;; Set public sale flag
(define-public (flip-sale)
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR-NOT-AUTHORIZED)
(var-set private-sale-active false)
(var-set sale-active (not (var-get sale-active)))
(ok (var-get sale-active))))
(define-read-only (get-sale-status)
(list (var-get private-sale-active) (var-get sale-active))
)
(define-read-only (get-private-target-block-height)
(var-get private-target-block-height)
)
(define-read-only (get-public-target-block-height)
(var-get public-target-block-height)
)
(define-read-only (get-private-presale-balance (account principal))
(default-to u0
(map-get? private-presale-count account)))
(define-read-only (get-public-balance (account principal))
(default-to u0
(map-get? public-sale-count account)))
;; Internal - Mint NFT using Mintpass mechanism
(define-private (private-mintpass-mint (new-owner principal))
(let ((mint-count (get-private-presale-balance new-owner)))
(asserts! (var-get private-sale-active) ERR-SALE-NOT-ACTIVE)
(asserts! (>= (var-get private-target-block-height) block-height) ERR-BEFORE-MINT-TIME)
(asserts! (> mint-count u0) ERR-NO-MINTPASS-REMAINING)
(map-set private-presale-count
new-owner
(- mint-count u1))
(mint new-owner)
)
)
;; Internal - Mint public sale NFT
(define-private (public-mint (new-owner principal))
(let ((mint-count (get-public-balance new-owner)))
(asserts! (var-get sale-active) ERR-SALE-NOT-ACTIVE)
(asserts! (>= (var-get public-target-block-height) block-height) ERR-BEFORE-MINT-TIME)
(mint new-owner)
)
)
;; update pre-sale counts
(define-public (set-private-wl (address principal) (limit uint))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(map-set private-presale-count address limit)
(ok true)
)
)
(define-public (bulk-set-private-wl (addresses (list 1000 principal)) (limits (list 1000 uint)))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(print (map set-private-wl addresses limits))
(ok true)
)
)
(define-public (set-public-target-block-height (target uint))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(var-set public-target-block-height target)
(ok true)
)
)
(define-public (set-private-target-block-height (target uint))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(var-set private-target-block-height target)
(ok true)
)
)
;; ###### BEGIN: commissions template code ##################################################
(define-data-var secondary-commission uint u250)
(define-public (pay (id uint) (price uint))
(if (> (var-get secondary-commission) u0)
(begin
(try! (stx-transfer? (/ (* price (var-get secondary-commission)) u10000) tx-sender (var-get commission-address)))
;; total of 5% = u500
(try! (stx-transfer? (/ (* price u500) u10000) tx-sender 'SP2XQ2Y69BS0C6TYEVEEVY734QWCHFZ1Q4SM0WD47))
(ok true)
)
(ok true)
)
)
(define-public (set-commission (amount uint))
(begin
(asserts! (is-eq tx-sender (var-get commission-address)) ERR-NOT-AUTHORIZED)
(var-set secondary-commission amount)
(ok true)
)
)
(define-public (set-commission-address (address principal))
(begin
(asserts! (is-eq tx-sender (var-get commission-address)) ERR-NOT-AUTHORIZED)
(var-set commission-address address)
(ok true)
)
)
(define-public (get-commission)
(ok (var-get secondary-commission))
)
(define-public (get-commission-address)
(ok (var-get commission-address))
)
;; ###### END: commission template code ##################################################
;; ###### BEGIN: fungible token mint template code ##################################################
(define-data-var banana-cost-per-mint uint u85000000)
(define-private (banana-mint (new-owner principal))
(let (
(next-id (+ u1 (var-get last-id)))
)
(asserts! (< (var-get last-id) MINT-LIMIT) ERR-SOLD-OUT)
(match (nft-mint? blue-hobbit-cloud-3 next-id new-owner)
success
(let
((current-balance (get-balance new-owner)))
(begin
;; (print payout)
(try! (banana-payout))
(var-set last-id next-id)
(map-set token-count
new-owner
(+ current-balance u1)
)
(ok true)))
error (err (* error u10000)))))
(define-private (banana-payout)
(let (
(price (var-get banana-cost-per-mint))
(commission-amount (/ (* price (var-get commission-percentage)) u10000))
(price-list (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.uint-lists lookup price (len (var-get royalty))))
(divider-list (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.uint-lists lookup u10000 (len (var-get royalty))))
(royalty-amounts (map * (var-get royalty) price-list))
(payout-amounts (map / royalty-amounts divider-list))
)
(asserts! (>= (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.btc-monkeys-bananas get-balance tx-sender)) price) ERR-INSUFFICIENT-FUNDS)
(map banana-pay (var-get artist-address) payout-amounts)
(try! (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.btc-monkeys-bananas transfer commission-amount tx-sender (var-get commission-address) (some 0x00)))
(ok payout-amounts)
)
)
(define-private (banana-pay (receiver principal) (amount uint))
(contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.btc-monkeys-bananas transfer amount tx-sender receiver (some 0x00))
)
(define-public (set-banana-cost (amount uint))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(var-set banana-cost-per-mint amount)
(ok true)))
;; Claim a new NFT
(define-public (claim-banana)
(if (var-get sale-active)
(banana-public-mint tx-sender)
(if (var-get private-sale-active)
(banana-private-mintpass-mint tx-sender)
(not-live)
)
)
)
;; begin mint 2
(define-public (claim-banana-two)
(begin
(try! (claim-banana))
(try! (claim-banana))
(ok true)))
;; end mint 2
;; begin mint 3
(define-public (claim-banana-three)
(begin
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(ok true)))
;; end mint 3
;; begin mint 4
(define-public (claim-banana-four)
(begin
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(ok true)))
;; end mint 4
;; begin mint 5
(define-public (claim-banana-five)
(begin
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(try! (claim-banana))
(ok true)))
;; end mint 5
;; Internal - Mint NFT using Mintpass mechanism
(define-private (banana-private-mintpass-mint (new-owner principal))
(let ((mint-count (get-private-presale-balance new-owner)))
(asserts! (var-get private-sale-active) ERR-SALE-NOT-ACTIVE)
(asserts! (> mint-count u0) ERR-NO-MINTPASS-REMAINING)
(map-set private-presale-count
new-owner
(- mint-count u1))
(banana-mint new-owner)
)
)
;; Internal - Mint public sale NFT
(define-private (banana-public-mint (new-owner principal))
(let ((mint-count (get-public-balance new-owner)))
(asserts! (var-get sale-active) ERR-SALE-NOT-ACTIVE)
(banana-mint new-owner)
)
)
;; ###### END: fungible token mint template code ##################################################