/
marketplace-bid-v5.clar
248 lines (220 loc) · 8.78 KB
/
marketplace-bid-v5.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
(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
;; bids map
;; if nft-id is `none`, the bid is a collection bid
;; and can be accepted by anyone holding a token from
;; that collection.
(define-map bids
uint
{
collection: principal,
nft-id: (optional uint),
bid-amount: uint,
buyer: principal,
seller: (optional principal),
expiration-block: uint,
action-event-index: uint
}
)
(define-constant contract-address (as-contract tx-sender))
(define-constant contract-owner tx-sender)
(define-constant err-contract-not-authorized u101)
(define-constant err-placing-bids-disabled u102)
(define-constant err-accepting-bids-disabled u103)
(define-constant err-withdrawing-bids-disabled u104)
(define-constant err-user-not-authorized u105)
(define-constant err-no-bid-found u106)
(define-constant err-bid-expired u107)
(define-constant err-wrong-collection u108)
(define-constant err-wrong-nft-id u109)
(define-data-var placing-bids-enabled bool true)
(define-data-var accepting-bids-enabled bool true)
(define-data-var withdrawing-bids-enabled bool true)
(define-data-var commission uint u200)
(define-data-var id uint u0)
(define-public (place-bid (collection <nft-trait>) (nft-id (optional uint)) (amount uint) (expiration uint) (memo (optional (string-ascii 256))))
(let ((block block-height)
(next-bid-id (var-get id))
(nft-owner
(if (is-some nft-id)
(get-owner collection (unwrap-panic nft-id))
none)
)
(nft {collection: (contract-of collection), nft-id: nft-id, bid-amount: amount, buyer: tx-sender, seller: nft-owner, expiration-block: (+ expiration block), action-event-index: u0}))
(asserts! (var-get placing-bids-enabled)
(err err-placing-bids-disabled))
(asserts! (contract-call? .nft-oracle-v2 is-trusted (contract-of collection))
(err err-contract-not-authorized))
(try! (stx-transfer? amount tx-sender contract-address))
(map-set bids next-bid-id nft)
(var-set id (+ next-bid-id u1))
(print {
action: "place-bid",
payload: {
bid_id: next-bid-id,
action_event_index: (get action-event-index nft),
collection_id: collection,
;; asset_id: asset-id',
token_id: nft-id,
bidder_address: tx-sender,
seller_address: nft-owner,
bid_amount: amount,
expiration_block: (get expiration-block nft),
memo: memo
}
})
(ok next-bid-id)
)
)
(define-public (withdraw-bid (bid-id uint))
(let ((previous-bid (get-bid bid-id))
(previous-bidder (get buyer previous-bid))
(previous-bid-action-event-index (get action-event-index previous-bid))
(previous-bid-amount (get bid-amount previous-bid)))
(asserts! (var-get withdrawing-bids-enabled)
(err err-withdrawing-bids-disabled))
(asserts! (> previous-bid-amount u0) (err err-no-bid-found))
(asserts! (or (is-eq previous-bidder tx-sender) (is-eq contract-owner tx-sender))
(err err-user-not-authorized))
(map-delete bids bid-id)
(print {
action: "withdraw-bid",
payload: {
bid_id: bid-id,
action_event_index: (+ u1 previous-bid-action-event-index),
collection_id: (get collection previous-bid),
token_id: (get nft-id previous-bid),
bidder_address: previous-bidder,
seller_address: (get seller previous-bid),
bid_amount: previous-bid-amount,
expiration_block: (get expiration-block previous-bid)
}
})
(as-contract (stx-transfer? previous-bid-amount contract-address previous-bidder))
)
)
(define-public (accept-bid (bid-id uint) (collection <nft-trait>) (nft-id uint))
(let ((bid (get-bid bid-id))
(bid-nft-id (get nft-id bid))
(bid-collection (get collection bid))
(bidder (get buyer bid))
(bid-amount (get bid-amount bid))
(bid-action-event-index (get action-event-index bid))
(expiration-block (get expiration-block bid))
(nft-owner (unwrap! (get-owner collection nft-id) (err err-user-not-authorized)))
(royalty (get-royalty (contract-of collection)))
(royalty-address (get address royalty))
(commission-amount (/ (* bid-amount (var-get commission)) u10000))
(royalty-amount (/ (* bid-amount (get percent royalty)) u10000))
(to-owner-amount (- (- bid-amount commission-amount) royalty-amount))
(block block-height))
(asserts! (var-get accepting-bids-enabled)
(err err-accepting-bids-disabled))
(asserts! (contract-call? .nft-oracle-v2 is-trusted (contract-of collection))
(err err-contract-not-authorized))
(asserts! (> bid-amount u0) (err err-no-bid-found))
(asserts! (is-eq (contract-of collection) bid-collection) (err err-wrong-collection))
(asserts! (or
(is-none bid-nft-id)
(and (is-some bid-nft-id) (is-eq (unwrap-panic bid-nft-id) nft-id)))
(err err-wrong-nft-id))
(asserts! (is-eq tx-sender nft-owner) (err err-user-not-authorized))
(asserts! (> expiration-block block) (err err-bid-expired))
(map-delete bids bid-id)
(try! (contract-call? collection transfer nft-id tx-sender bidder))
(and (> to-owner-amount u0)
(try! (as-contract (stx-transfer? to-owner-amount contract-address nft-owner))))
(and (> commission-amount u0)
(try! (as-contract (stx-transfer? commission-amount contract-address contract-owner))))
(and (> royalty-amount u0)
(try! (as-contract (stx-transfer? royalty-amount contract-address royalty-address))))
(print {
action: "accept-bid",
payload: {
bid_id: bid-id,
action_event_index: (+ u1 bid-action-event-index),
collection_id: collection,
token_id: nft-id,
bidder_address: bidder,
seller_address: nft-owner,
bid_amount: bid-amount,
expiration_block: expiration-block,
royalty: {
recipient_address: royalty-address,
percent: (get percent royalty),
}
}
})
(ok true)
)
)
(define-public (change-bid-amount-and-expiration (bid-id uint) (new-amount uint) (new-expiration uint))
(let ((bid (get-bid bid-id))
(bidder (get buyer bid))
(bid-amount (get bid-amount bid))
(bid-collection (get collection bid))
(bid-nft-id (get nft-id bid))
(bid-action-event-index (get action-event-index bid))
(seller (get seller bid))
(block block-height)
(new-bid (merge bid {bid-amount: new-amount, expiration-block: (+ new-expiration block), action-event-index: (+ u1 bid-action-event-index)})))
(asserts! (var-get accepting-bids-enabled)
(err err-accepting-bids-disabled))
(asserts! (> bid-amount u0) (err err-no-bid-found))
(asserts! (is-eq tx-sender bidder) (err err-user-not-authorized))
(if (is-eq bid-amount new-amount)
true
(if (< new-amount bid-amount)
(try! (as-contract (stx-transfer? (- bid-amount new-amount) contract-address bidder)))
(try! (stx-transfer? (- new-amount bid-amount) tx-sender contract-address))
)
)
(map-set bids bid-id new-bid)
(print {
action: "change-bid-amount-and-expiration",
payload: {
bid_id: bid-id,
action_event_index: (get action-event-index new-bid),
collection_id: bid-collection,
;; asset_id: asset-id',
token_id: bid-nft-id,
bidder_address: bidder,
seller_address: seller,
bid_amount: (get bid-amount new-bid),
expiration_block: (get expiration-block new-bid),
}
})
(ok true)
)
)
(define-public (set-placing-bids-enabled (enabled bool))
(begin
(asserts! (is-eq tx-sender contract-owner) (err err-user-not-authorized))
(ok (var-set placing-bids-enabled enabled))
)
)
(define-public (set-accepting-bids-enabled (enabled bool))
(begin
(asserts! (is-eq tx-sender contract-owner) (err err-user-not-authorized))
(ok (var-set accepting-bids-enabled enabled))
)
)
(define-public (set-withdrawing-bids-enabled (enabled bool))
(begin
(asserts! (is-eq tx-sender contract-owner) (err err-user-not-authorized))
(ok (var-set withdrawing-bids-enabled enabled))
)
)
(define-read-only (get-bid (bid-id uint))
(default-to
{collection: tx-sender, nft-id: none, buyer: contract-owner, seller: none, bid-amount: u0, expiration-block: block-height, action-event-index: u0}
(map-get? bids bid-id)
)
)
(define-private (get-owner (nft <nft-trait>) (nft-id uint))
(unwrap-panic (contract-call? nft get-owner nft-id))
)
(define-private (get-royalty (collection principal))
(default-to
{ address: contract-owner, percent: u0 }
(contract-call? .nft-oracle-v2 get-royalty-amount collection))
)