-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmutant-monkeys.clar
More file actions
314 lines (281 loc) · 11.6 KB
/
Copy pathmutant-monkeys.clar
File metadata and controls
314 lines (281 loc) · 11.6 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
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(use-trait commission-trait 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.commission-trait.commission)
(use-trait staking-trait 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.staking-trait.staking)
(use-trait lookup-trait 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.lookup-trait.lookup-trait)
(define-non-fungible-token mutant-monkeys uint)
;; Storage
(define-map token-count principal uint)
(define-map minted principal bool)
(define-map market uint {price: uint, commission: principal})
;; 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-NOT-AUTHORIZED (err u401))
(define-constant ERR-NOT-FOUND (err u404))
(define-constant ERR-METADATA-FROZEN (err u505))
(define-constant ERR-LISTING (err u507))
(define-constant ERR-MONKEY-STAKED (err u508))
(define-constant ERR-NOT-ENOUGH-STX (err u509))
(define-constant MINT-LIMIT u5000)
;; 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://placeholder/")
(define-data-var contract-uri (string-ascii 100) "ipfs://placeholder")
(define-data-var artist-address principal 'SP1RS2VFY0K4A2Z006QBJDYHE2RJ6F63KV0JSSXFE)
(define-data-var admins (list 1000 principal) (list 'SP125J1ADVYWGWB9NQRCVGKYAG73R17ZNMV17XEJ7 'SPM8EYQKX80FFNVRV7F877TEAYZVYJX0WQ2Z4PJN 'SPGCBABRBWZ3EQNXP94VBNQMJYW1QN0RN0054VKF 'SP1PTRJMNEBVJDJ29W3DKWDEMQ29D3AFAKGSYBXFZ 'SP7VK7V27R0H2C7WRR378457WX8VX1Q32RCZRV6H 'SP4RPAM72DWTCXFQXTQ35PTSEC0CDAW4QB5Z5QZR))
(define-data-var wallets (list 1000 principal) (list 'SPM8EYQKX80FFNVRV7F877TEAYZVYJX0WQ2Z4PJN 'SP3ZJP253DENMN3CQFEQSPZWY7DK35EH3SEH0J8PK 'SP3GDV2YWE3E4CGZK4NYM2YASZ82G8E4AC7C9CFQT))
(define-data-var royalties (list 1000 uint) (list u500 u100 u0))
(define-map mint-address bool principal)
;; Token count for account
(define-read-only (get-balance (account principal))
(default-to u0
(map-get? token-count account)))
;; Get minted
(define-read-only (get-minted (account principal))
(default-to false
(map-get? minted account)))
(define-private (trnsfr (id uint) (sender principal) (recipient principal))
(match (nft-transfer? mutant-monkeys 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-none (index-of (contract-call? .mutant-staking get-staked-nfts tx-sender .mutant-monkeys) id)) ERR-MONKEY-STAKED)
(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 mutant-monkeys
(ok (nft-get-owner? mutant-monkeys 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")))
)
)
;; SIP009: Get the token URI. You can set it to any other URI
(define-read-only (get-total-royalty)
(fold + (var-get royalties) u0)
)
(define-read-only (get-contract-uri)
(ok (var-get contract-uri)))
(define-public (mint (id uint))
(let (
(new-owner tx-sender)
)
(asserts! (is-eq (unwrap-panic (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs get-owner id))) tx-sender) ERR-NOT-AUTHORIZED)
(asserts! (< (var-get last-id) MINT-LIMIT) ERR-SOLD-OUT)
(match (nft-mint? mutant-monkeys id new-owner)
success
(let
((current-balance (get-balance new-owner)))
(begin
(try! (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs burn id new-owner))
(var-set last-id (+ u1 (var-get last-id)))
(map-set token-count
new-owner
(+ current-balance u1)
)
(map-set minted new-owner true)
(ok true)))
error (err (* error u10000)))))
(define-public (admin-mint (id uint))
(let (
(new-owner tx-sender)
)
(asserts! (is-eq (unwrap-panic (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs get-owner id))) tx-sender) ERR-NOT-AUTHORIZED)
(asserts! (< (var-get last-id) MINT-LIMIT) ERR-SOLD-OUT)
(asserts! (is-some (index-of (var-get admins) tx-sender)) ERR-NOT-AUTHORIZED)
(match (nft-mint? mutant-monkeys id new-owner)
success
(let
((current-balance (get-balance new-owner)))
(begin
(var-set last-id (+ u1 (var-get last-id)))
(map-set token-count
new-owner
(+ current-balance u1)
)
(map-set minted new-owner true)
(ok true)))
error (err (* error u10000)))))
(define-private (mint-many-iter (id uint) (new-owner principal))
(begin
(asserts! (is-eq (unwrap-panic (unwrap-panic (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs get-owner id))) tx-sender) ERR-NOT-AUTHORIZED)
(asserts! (< (var-get last-id) MINT-LIMIT) ERR-SOLD-OUT)
(match (nft-mint? mutant-monkeys id new-owner)
success
(let
((current-balance (get-balance new-owner)))
(begin
(try! (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.bitcoin-monkeys-labs burn id new-owner))
(var-set last-id (+ u1 (var-get last-id)))
(map-set token-count
new-owner
(+ current-balance u1)
)
(map-set minted new-owner true)
(ok true)))
error (err (* error u10000)))
)
)
(define-public (mint-many (ids (list 50 uint)))
(let (
(indexer (- (len ids) u1))
(addresses (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.principal-lists lookup tx-sender indexer))
)
(print (map mint-many-iter ids addresses))
(ok true)
)
)
(define-public (burn (id uint))
(let (
(token-owner (unwrap-panic (unwrap-panic (get-owner id))))
)
(asserts! (is-eq tx-sender token-owner) ERR-NOT-AUTHORIZED)
(asserts! (is-none (map-get? market id)) ERR-LISTING)
(asserts! (not (is-some (index-of (contract-call? .mutant-staking get-staked-nfts tx-sender .mutant-monkeys) id))) ERR-MONKEY-STAKED)
(match (nft-burn? mutant-monkeys id token-owner)
success
(let
((current-balance (get-balance token-owner)))
(begin
(map-set token-count
token-owner
(- current-balance u1)
)
(ok true)))
error (err (* error u10000)))
)
)
(define-private (is-sender-owner (id uint))
(let ((owner (unwrap! (nft-get-owner? mutant-monkeys 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! (not (is-some (index-of (contract-call? .mutant-staking get-staked-nfts tx-sender .mutant-monkeys) id))) ERR-MONKEY-STAKED)
(asserts! (is-sender-owner id) ERR-NOT-AUTHORIZED)
(try! (contract-call? .mutant-staking set-listed .mutant-monkeys id))
(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)
(try! (contract-call? .mutant-staking set-unlisted .mutant-monkeys id))
(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? mutant-monkeys id) ERR-NOT-FOUND))
(listing (unwrap! (map-get? market id) ERR-LISTING))
(price (get price listing))
(paid (pay-royalties (var-get wallets) price))
)
(asserts! (is-eq (contract-of comm) (get commission listing)) ERR-WRONG-COMMISSION)
(try! (contract-call? .mutant-staking set-unlisted .mutant-monkeys id))
(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)
)
)
(define-read-only (calculate-royalties (amount uint))
(let (
(indexer (- (len (var-get royalties)) u1))
(transformer (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.lists lookup amount indexer))
(dividers (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.lists lookup u10000 indexer))
(amounts (map * transformer (var-get royalties)))
(royalty-amounts (map / amounts dividers))
)
royalty-amounts
)
)
(define-public (pay-royalties (addresses (list 1000 principal)) (amount uint))
(let (
(amounts (calculate-royalties amount))
(total-royalties (fold + amounts u0))
(total (+ total-royalties amount))
)
(asserts! (>= (stx-get-balance tx-sender) total) (err ERR-NOT-ENOUGH-STX))
(print (map pay addresses amounts))
(ok true)
)
)
(define-public (pay (address principal) (amount uint))
(begin
(if (> amount u0)
(begin
(try! (stx-transfer? amount tx-sender address))
(ok true)
)
(begin
(ok true)
)
)
)
)
;; Set base uri
(define-public (set-base-uri (new-base-uri (string-ascii 100)))
(begin
(asserts! (is-some (index-of (var-get admins) tx-sender)) 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-some (index-of (var-get admins) tx-sender)) 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-some (index-of (var-get admins) tx-sender)) ERR-NOT-AUTHORIZED)
(var-set metadata-frozen true)
(ok true)))
(define-public (royalty-change (amounts (list 1000 uint)))
(if (is-some (index-of (var-get admins) tx-sender))
(ok (var-set royalties amounts))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (admin-change (addresses (list 1000 principal)))
(if (is-some (index-of (var-get admins) tx-sender))
(ok (var-set admins addresses))
(err ERR-NOT-AUTHORIZED)
)
)
(define-public (payout-addresses-change (addresses (list 1000 principal)))
(if (is-some (index-of (var-get admins) tx-sender))
(ok (var-set wallets addresses))
(err ERR-NOT-AUTHORIZED)
)
)