/
dsa.clar
304 lines (259 loc) · 8.25 KB
/
dsa.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
;; use the SIP009 interface
(impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(use-trait commission-trait 'SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335.commission-trait.commission)
(define-non-fungible-token dsa uint)
;; Define errors
(define-constant ERR_NOT_ENOUGH_STX (err u0))
(define-constant ERR_TOKEN_ID (err u1))
(define-constant ERR_SOLD_OUT (err u2))
(define-constant ERR_TRANSFER_FAIL (err u3))
(define-constant ERR_OWNER_ONLY (err u4))
(define-constant ERR_NOT_AUTHORIZED (err u5))
(define-constant ERR_LISTING (err u6))
(define-constant ERR_NO_OWNER (err u7))
(define-constant ERR_NOT_OWNER (err u8))
(define-constant ERR_WRONG_COMM (err u9))
(define-constant ERR_MAX_MINTED (err u10))
(define-constant ERR_MINT_PAUSED (err u11))
(define-constant ERR_FROZEN (err u12))
;; Constants
(define-constant CONTRACT_OWNER tx-sender)
;; testnet
(define-constant LMNFT 'SPRTNMT0HRZC1JXK6YV9ZW2Q7X9B50G6QWNNG9E9)
;; Maps
(define-map market uint {price: uint, commission: principal})
(define-map token-count principal uint)
;; Define variables
(define-data-var last-id uint u0)
(define-data-var base-uri (string-ascii 80) "/")
(define-data-var mint-price uint u100000)
;; Percent u100 = 1%
(define-data-var royalty-percent uint u0)
(define-data-var mint-limit uint u5)
(define-data-var max-supply uint u1)
(define-data-var mint-paused bool true)
(define-data-var metadata-frozen bool false)
;; Mint a new NFT
(define-public (mint)
(let
(
(next-id (+ u1 (var-get last-id)))
(fee (/ (var-get mint-price) u40))
(mint-pay ( - (var-get mint-price) fee ))
(current-balance (get-balance tx-sender))
)
(asserts! (or (is-eq false (var-get mint-paused)) (is-eq tx-sender CONTRACT_OWNER)) ERR_MINT_PAUSED)
(asserts! (< (var-get last-id) (var-get max-supply)) ERR_SOLD_OUT)
(asserts! (< current-balance (var-get mint-limit)) ERR_MAX_MINTED)
(asserts! (>= (stx-get-balance tx-sender) (var-get mint-price) ) ERR_NOT_ENOUGH_STX)
(asserts! (var-set last-id next-id) ERR_TOKEN_ID)
(begin
(unwrap! (stx-transfer? mint-pay tx-sender CONTRACT_OWNER) ERR_TRANSFER_FAIL)
(unwrap! (stx-transfer? fee tx-sender LMNFT) ERR_TRANSFER_FAIL)
(try! (nft-mint? dsa next-id tx-sender))
(map-set token-count tx-sender (+ current-balance u1))
(ok next-id)
)
)
)
;; Airdrop mint
(define-public (airdrop (recipient principal))
(let
(
(next-id (+ u1 (var-get last-id)))
(fee (/ (var-get mint-price) u40))
(current-balance (get-balance tx-sender))
)
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(asserts! (< (var-get last-id) (var-get max-supply)) ERR_SOLD_OUT)
(asserts! (>= (stx-get-balance tx-sender) fee ) ERR_NOT_ENOUGH_STX)
(asserts! (var-set last-id next-id) ERR_TOKEN_ID)
(begin
(unwrap! (stx-transfer? fee tx-sender LMNFT) ERR_TRANSFER_FAIL)
(try! (nft-mint? dsa next-id recipient))
(map-set token-count recipient (+ current-balance u1))
(ok next-id)
)
)
)
;; 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)
)
)
(define-private (trnsfr (id uint) (sender principal) (recipient principal))
(match (nft-transfer? dsa 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)
)
)
;; Set functions
(define-public (set-price (price uint))
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(ok (var-set mint-price price))
)
)
(define-public (set-mint-limit (limit uint))
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(ok (var-set mint-limit limit))
)
)
(define-public (toggle-pause)
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(ok (var-set mint-paused (not (var-get mint-paused))))
)
)
(define-public (set-base-uri (new-base-uri (string-ascii 80)))
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(asserts! (not (var-get metadata-frozen)) ERR_FROZEN)
(ok (var-set base-uri new-base-uri))
)
)
(define-public (freeze-metadata)
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(ok (var-set metadata-frozen true))
)
)
(define-public (launch (new-base-uri (string-ascii 80)))
(begin
(asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_OWNER_ONLY)
(asserts! (not (var-get metadata-frozen)) ERR_FROZEN)
(var-set base-uri new-base-uri)
(var-set mint-paused false)
(ok true)
)
)
;; Read functions
;; SIP009: Get the owner of the specified token ID
(define-read-only (get-owner (id uint))
(ok (nft-get-owner? dsa 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 (id uint))
(ok (some (concat "ipfs://" (concat (concat (var-get base-uri) (uint-to-ascii id)) ".json"))))
)
(define-read-only (get-base-uri)
(ok (var-get base-uri))
)
(define-read-only (get-mint-price)
(ok (var-get mint-price))
)
(define-read-only (get-mint-limit)
(ok (var-get mint-limit))
)
(define-read-only (get-max-supply)
(ok (var-get max-supply))
)
(define-read-only (get-royalty-percent)
(ok (var-get royalty-percent))
)
(define-read-only (get-mint-paused)
(ok (var-get mint-paused))
)
(define-read-only (get-balance (account principal))
(default-to u0
(map-get? token-count account)
)
)
;; Marketplace functions
(define-private (is-sender-owner (id uint))
(let
(
(owner (unwrap! (nft-get-owner? dsa 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_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-trait <commission-trait>))
(let
(
(owner (unwrap! (nft-get-owner? dsa id) ERR_NO_OWNER))
(listing (unwrap! (map-get? market id) ERR_LISTING))
(price (get price listing))
(royalties (/ (* price (var-get royalty-percent)) u10000 ))
(pay-price (- price royalties))
)
(asserts! (is-eq (contract-of comm-trait) (get commission listing)) ERR_WRONG_COMM)
(try! (stx-transfer? pay-price tx-sender owner))
(try! (pay-royalty royalties))
(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)
)
)
(define-private (pay-royalty (royalties uint))
(let
(
(royalties-amount royalties)
)
(if (> (var-get royalty-percent) u0)
(try! (stx-transfer? royalties-amount tx-sender CONTRACT_OWNER))
(print false)
)
(ok true)
)
)
;; Helpers
(define-private (uint-to-ascii (value uint))
(if (<= value u9)
(unwrap-panic (element-at "0123456789" value))
(get r (fold uint-to-ascii-inner
0x000000000000000000000000000000000000000000000000000000000000000000000000000000
{v: value, r: ""}
))
)
)
(define-private (uint-to-ascii-inner (i (buff 1)) (d {v: uint, r: (string-ascii 39)}))
(if (> (get v d) u0)
{
v: (/ (get v d) u10),
r: (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at "0123456789" (mod (get v d) u10))) (get r d)) u39))
}
d
)
)