-
Notifications
You must be signed in to change notification settings - Fork 50
/
Auction.hs
380 lines (320 loc) · 14.1 KB
/
Auction.hs
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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Marketplace.Spec.Auction
( tests
) where
import Control.Lens (_2, _Right, (&),
(^.), (^?))
import Control.Monad (void)
import Data.Foldable (find)
import Data.Maybe (isNothing)
import Data.Text (Text)
import Data.Void (Void)
import qualified Ext.Plutus.Contracts.Auction as Auction
import Ledger (Value)
import Ledger.Ada (lovelaceValueOf)
import qualified Ledger.Value as V
import qualified Marketplace.Fixtures as Fixtures
import qualified Marketplace.Spec.Bundles as Bundles
import qualified Marketplace.Spec.CreateNft as CreateNft
import qualified Marketplace.Spec.Start as Start
import Plutus.Abstract.ContractResponse (ContractResponse)
import Plutus.Contract.Test
import qualified Plutus.Contracts.NftMarketplace.Endpoints as Marketplace
import qualified Plutus.Contracts.NftMarketplace.OnChain.Core as Marketplace
import qualified Plutus.Trace as Trace
import qualified PlutusTx.AssocMap as AssocMap
import Test.Tasty
import qualified Utils
import Wallet.Emulator.Wallet (walletAddress)
tests :: TestTree
tests =
testGroup
"Auction"
[testGroup
"NFT singletons"
[
checkPredicateOptions
Fixtures.options
"Should open an auction for one NFT locking it in auction script & saving link"
(startAnAuctionValueCheck .&&. startAnAuctionDatumsCheck)
(void startAnAuctionTrace),
checkPredicateOptions
Fixtures.options
"Should not put on auction if NFT does not exist"
errorCheckUser
startAnAuctionTrace',
checkPredicateOptions
Fixtures.options
"Should close auction and pay locked NFT back if there were no bids"
(completeAnAuctionValueCheck .&&. completeAuctionDatumsCheck)
completeAnAuctionTrace,
checkPredicateOptions
Fixtures.options
"Should not close auction if it was not started"
errorCheckUser
completeAnAuctionTrace',
checkPredicateOptions
Fixtures.options
"Should bid on NFT"
startAnAuctionDatumsCheck
(void bidOnAuctionTrace),
checkPredicateOptions
Fixtures.options
"Should not bid if NFT is not on auction"
errorCheckBuyer
bidOnAuctionTrace',
checkPredicateOptions
Fixtures.options
"Should close auction and pay locked NFT to the highest bidder"
(buyOnAuctionValueCheck .&&. completeAuctionDatumsCheck)
buyOnAuctionTrace,
checkPredicateOptions
Fixtures.options
"Should close auction and pay pay marketplace operator a saleFee"
marketplaceOperatorFeeCheck
buyOnAuctionTrace
],
testGroup
"NFT bundles"
[
checkPredicateOptions
Fixtures.options
"Should open an auction for NFT bundle locking its value in auction script & saving link"
(startAnAuctionValueCheckB .&&. startAnAuctionDatumsCheckB)
(void startAnAuctionTraceB),
checkPredicateOptions
Fixtures.options
"Should not put on auction if bundle does not exist"
errorCheckUser
startAnAuctionTraceB',
checkPredicateOptions
Fixtures.options
"Should close auction and pay locked bundle value back if there were no bids"
(completeAnAuctionValueCheckB .&&. completeAuctionDatumsCheckB)
completeAnAuctionTraceB,
checkPredicateOptions
Fixtures.options
"Should bid on bundle"
startAnAuctionDatumsCheckB
(void bidOnAuctionTraceB),
checkPredicateOptions
Fixtures.options
"Should close auction and pay locked bundle value to the highest bidder"
(buyOnAuctionValueCheckB .&&. completeAuctionDatumsCheckB)
buyOnAuctionTraceB,
checkPredicateOptions
Fixtures.options
"Should close bundle auction and pay marketplace operator a saleFee"
marketplaceOperatorFeeCheckB
buyOnAuctionTraceB
]]
auctionValue :: Marketplace.Auction -> Value
auctionValue = Auction.apAsset . Auction.fromAuction
-- \/\/\/ "NFT singletons"
startAnAuctionParams :: Marketplace.StartAnAuctionParams
startAnAuctionParams = Marketplace.StartAnAuctionParams
{
Marketplace.saapItemId = Marketplace.UserNftId Fixtures.catTokenIpfsCid,
Marketplace.saapDuration = 155 * 1000,
Marketplace.saapInitialPrice = lovelaceValueOf 100000000
}
closeLotParams :: Marketplace.CloseLotParams
closeLotParams = Marketplace.CloseLotParams {
Marketplace.clpItemId = Marketplace.UserNftId Fixtures.catTokenIpfsCid
}
bidOnAuctionParams :: Marketplace.BidOnAuctionParams
bidOnAuctionParams = Marketplace.BidOnAuctionParams {
Marketplace.boapItemId = Marketplace.UserNftId Fixtures.catTokenIpfsCid,
Marketplace.boapBid = fromInteger $ 25 * Fixtures.oneAdaInLovelace
}
startAnAuctionTrace :: Trace.EmulatorTrace (Trace.ContractHandle (ContractResponse Text Marketplace.UserContractState) Marketplace.MarketplaceUserSchema Void)
startAnAuctionTrace = do
h <- CreateNft.createNftTrace
_ <- Trace.callEndpoint @"startAnAuction" h startAnAuctionParams
_ <- Trace.waitNSlots 50
pure h
startAnAuctionTrace' :: Trace.EmulatorTrace ()
startAnAuctionTrace' = do
_ <- Start.startTrace
h <- Trace.activateContractWallet Fixtures.userWallet $ Marketplace.userEndpoints Fixtures.marketplace
_ <- Trace.callEndpoint @"startAnAuction" h startAnAuctionParams
_ <- Trace.waitNSlots 50
pure ()
completeAnAuctionTrace :: Trace.EmulatorTrace ()
completeAnAuctionTrace = do
h <- startAnAuctionTrace
_ <- Trace.callEndpoint @"completeAnAuction" h closeLotParams
_ <- Trace.waitNSlots 250
pure ()
completeAnAuctionTrace' :: Trace.EmulatorTrace ()
completeAnAuctionTrace' = do
h <- CreateNft.createNftTrace
_ <- Trace.callEndpoint @"completeAnAuction" h closeLotParams
_ <- Trace.waitNSlots 50
pure ()
bidOnAuctionTrace :: Trace.EmulatorTrace (Trace.ContractHandle (ContractResponse Text Marketplace.UserContractState) Marketplace.MarketplaceUserSchema Void)
bidOnAuctionTrace = do
_ <- startAnAuctionTrace
h <- Trace.activateContractWallet Fixtures.buyerWallet $ Marketplace.userEndpoints Fixtures.marketplace
_ <- Trace.callEndpoint @"bidOnAuction" h bidOnAuctionParams
_ <- Trace.waitNSlots 50
pure h
bidOnAuctionTrace' :: Trace.EmulatorTrace ()
bidOnAuctionTrace' = do
_ <- CreateNft.createNftTrace
h <- Trace.activateContractWallet Fixtures.buyerWallet $ Marketplace.userEndpoints Fixtures.marketplace
_ <- Trace.callEndpoint @"bidOnAuction" h bidOnAuctionParams
_ <- Trace.waitNSlots 50
pure ()
buyOnAuctionTrace :: Trace.EmulatorTrace ()
buyOnAuctionTrace = do
h <- bidOnAuctionTrace
_ <- Trace.callEndpoint @"completeAnAuction" h closeLotParams
_ <- Trace.waitNSlots 250
pure ()
startAnAuctionDatumsCheck :: TracePredicate
startAnAuctionDatumsCheck =
dataAtAddress
Fixtures.marketplaceAddress
(Utils.checkOneDatum (nftIsOnAuction . Marketplace.mdSingletons))
where
nftIsOnAuction = maybe False (\t -> t ^. Marketplace._nftLot ^? traverse . _2 . _Right & fmap auctionValue &
(== Just (Marketplace.nftValue Fixtures.catTokenIpfsCidBs t))) .
AssocMap.lookup Fixtures.catTokenIpfsCidHash
completeAuctionDatumsCheck :: TracePredicate
completeAuctionDatumsCheck =
dataAtAddress
Fixtures.marketplaceAddress
(Utils.checkOneDatum (nftNotOnAuction . Marketplace.mdSingletons))
where
nftNotOnAuction = maybe False (isNothing . Marketplace.nftLot) .
AssocMap.lookup Fixtures.catTokenIpfsCidHash
startAnAuctionValueCheck :: TracePredicate
startAnAuctionValueCheck =
valueAtAddress
(walletAddress Fixtures.userWallet)
(isNothing . find hasNft . V.flattenValue)
where
hasNft v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
completeAnAuctionValueCheck :: TracePredicate
completeAnAuctionValueCheck =
valueAtAddress
(walletAddress Fixtures.userWallet)
(Utils.one hasNft . V.flattenValue)
where
hasNft v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
buyOnAuctionValueCheck :: TracePredicate
buyOnAuctionValueCheck =
valueAtAddress
(walletAddress Fixtures.buyerWallet)
(Utils.one hasNft . V.flattenValue)
where
hasNft v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
errorCheckUser :: TracePredicate
errorCheckUser = Utils.assertCrError (Marketplace.userEndpoints Fixtures.marketplace) (Trace.walletInstanceTag Fixtures.userWallet)
errorCheckBuyer :: TracePredicate
errorCheckBuyer = Utils.assertCrError (Marketplace.userEndpoints Fixtures.marketplace) (Trace.walletInstanceTag Fixtures.buyerWallet)
-- \/\/\/ "NFT bundles"
startAnAuctionParamsB :: Marketplace.StartAnAuctionParams
startAnAuctionParamsB = Marketplace.StartAnAuctionParams
{
Marketplace.saapItemId = Marketplace.UserBundleId Fixtures.cids,
Marketplace.saapDuration = 142 * 1000,
Marketplace.saapInitialPrice = lovelaceValueOf 100000000
}
closeLotParamsB :: Marketplace.CloseLotParams
closeLotParamsB = Marketplace.CloseLotParams {
Marketplace.clpItemId = Marketplace.UserBundleId Fixtures.cids
}
bidOnAuctionParamsB :: Marketplace.BidOnAuctionParams
bidOnAuctionParamsB = Marketplace.BidOnAuctionParams {
Marketplace.boapItemId = Marketplace.UserBundleId Fixtures.cids,
Marketplace.boapBid = fromInteger $ 35 * Fixtures.oneAdaInLovelace
}
startAnAuctionTraceB :: Trace.EmulatorTrace (Trace.ContractHandle (ContractResponse Text Marketplace.UserContractState) Marketplace.MarketplaceUserSchema Void)
startAnAuctionTraceB = do
h <- Bundles.bundleTrace
_ <- Trace.callEndpoint @"startAnAuction" h startAnAuctionParamsB
_ <- Trace.waitNSlots 50
pure h
startAnAuctionTraceB' :: Trace.EmulatorTrace ()
startAnAuctionTraceB' = do
_ <- Start.startTrace
h <- Trace.activateContractWallet Fixtures.userWallet $ Marketplace.userEndpoints Fixtures.marketplace
_ <- Trace.callEndpoint @"startAnAuction" h startAnAuctionParamsB
_ <- Trace.waitNSlots 50
pure ()
completeAnAuctionTraceB :: Trace.EmulatorTrace ()
completeAnAuctionTraceB = do
h <- startAnAuctionTraceB
_ <- Trace.callEndpoint @"completeAnAuction" h closeLotParamsB
_ <- Trace.waitNSlots 250
pure ()
bidOnAuctionTraceB :: Trace.EmulatorTrace (Trace.ContractHandle (ContractResponse Text Marketplace.UserContractState) Marketplace.MarketplaceUserSchema Void)
bidOnAuctionTraceB = do
_ <- startAnAuctionTraceB
h <- Trace.activateContractWallet Fixtures.buyerWallet $ Marketplace.userEndpoints Fixtures.marketplace
_ <- Trace.callEndpoint @"bidOnAuction" h bidOnAuctionParamsB
_ <- Trace.waitNSlots 50
pure h
buyOnAuctionTraceB :: Trace.EmulatorTrace ()
buyOnAuctionTraceB = do
h <- bidOnAuctionTraceB
_ <- Trace.callEndpoint @"completeAnAuction" h closeLotParamsB
_ <- Trace.waitNSlots 250
pure ()
startAnAuctionDatumsCheckB :: TracePredicate
startAnAuctionDatumsCheckB =
dataAtAddress
Fixtures.marketplaceAddress
(Utils.checkOneDatum (bundleIsOnAuction . Marketplace.mdBundles))
where
bundleIsOnAuction = maybe False (\b -> b ^. Marketplace._nbTokens ^? Marketplace._HasLot . _2 . _Right & fmap auctionValue &
(== Just (Marketplace.bundleValue AssocMap.empty b))) .
AssocMap.lookup Fixtures.bundleId
completeAuctionDatumsCheckB :: TracePredicate
completeAuctionDatumsCheckB =
dataAtAddress
Fixtures.marketplaceAddress
(Utils.checkOneDatum (bundleNotOnAuction . Marketplace.mdBundles))
where
bundleNotOnAuction = maybe False (Prelude.not . Marketplace.hasLotBundle) .
AssocMap.lookup Fixtures.bundleId
startAnAuctionValueCheckB :: TracePredicate
startAnAuctionValueCheckB =
valueAtAddress
(walletAddress Fixtures.userWallet) $
\v -> (isNothing . find hasCatToken . V.flattenValue $ v) && (isNothing . find hasPhotoToken . V.flattenValue $ v)
where
hasCatToken v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
hasPhotoToken v = (v ^. _2 & V.unTokenName) == Fixtures.photoTokenIpfsCidBs
completeAnAuctionValueCheckB :: TracePredicate
completeAnAuctionValueCheckB =
valueAtAddress
(walletAddress Fixtures.userWallet) $
\v -> (Utils.one hasCatToken . V.flattenValue $ v) && (Utils.one hasPhotoToken . V.flattenValue $ v)
where
hasCatToken v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
hasPhotoToken v = (v ^. _2 & V.unTokenName) == Fixtures.photoTokenIpfsCidBs
buyOnAuctionValueCheckB :: TracePredicate
buyOnAuctionValueCheckB =
valueAtAddress
(walletAddress Fixtures.buyerWallet) $
\v -> (Utils.one hasCatToken . V.flattenValue $ v) && (Utils.one hasPhotoToken . V.flattenValue $ v)
where
hasCatToken v = (v ^. _2 & V.unTokenName) == Fixtures.catTokenIpfsCidBs
hasPhotoToken v = (v ^. _2 & V.unTokenName) == Fixtures.photoTokenIpfsCidBs
marketplaceOperatorFeeCheck :: TracePredicate
marketplaceOperatorFeeCheck =
walletFundsChange Fixtures.ownerWallet $ lovelaceValueOf 725000
-- 25000000 * 2.5 /100 = 625000 - fee by complete auction
-- 100000 - fee by minting token
marketplaceOperatorFeeCheckB :: TracePredicate
marketplaceOperatorFeeCheckB =
walletFundsChange Fixtures.ownerWallet $ lovelaceValueOf 1175000
-- 35000000 * 2.5 /100 = 875000 - fee by complete auction
-- 100000 * 2 = 200000 - fee by minting 2 tokens
-- 100000 - fee by bundling