-
Notifications
You must be signed in to change notification settings - Fork 50
/
User.hs
394 lines (339 loc) · 17.6 KB
/
User.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Plutus.Contracts.NftMarketplace.OffChain.User where
import Control.Lens (_2,
_Left,
_Right,
(^.),
(^?))
import qualified Control.Lens as Lens
import Control.Monad hiding
(fmap)
import qualified Data.Aeson as J
import Data.Proxy (Proxy (..))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Ext.Plutus.Contracts.Auction as Auction
import Ext.Plutus.Ledger.Value (utxoValue)
import qualified GHC.Generics as Haskell
import Ledger
import qualified Ledger.Typed.Scripts as Scripts
import Ledger.Typed.Tx
import qualified Ledger.Value as V
import Plutus.Abstract.ContractResponse
import Plutus.Contract
import Plutus.Contract.StateMachine
import Plutus.Contracts.Currency as Currency
import Plutus.Contracts.NftMarketplace.OffChain.ID
import Plutus.Contracts.NftMarketplace.OffChain.Info
import Plutus.Contracts.NftMarketplace.OffChain.Serialization (deserializeByteString)
import qualified Plutus.Contracts.NftMarketplace.OnChain.Core as Core
import qualified Plutus.Contracts.NftMarketplace.OnChain.Core.Marketplace as Marketplace
import qualified Plutus.Contracts.Services.Sale as Sale
import qualified PlutusTx
import qualified PlutusTx.AssocMap as AssocMap
import PlutusTx.Prelude hiding
(Semigroup (..))
import Prelude (Semigroup (..))
import qualified Prelude as Haskell
import qualified Schema
import Text.Printf (printf)
getOwnPubKey :: Contract w s Text PubKeyHash
getOwnPubKey = pubKeyHash <$> ownPubKey
data CreateNftParams =
CreateNftParams {
cnpIpfsCid :: Text,
cnpNftName :: Text,
cnpNftDescription :: Text,
cnpNftCategory :: [Text],
cnpRevealIssuer :: Bool
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''CreateNftParams
-- | The user specifies which NFT to mint and add to marketplace store,
-- he gets it into his wallet and the corresponding store entry is created
createNft :: Core.Marketplace -> CreateNftParams -> Contract w s Text ()
createNft marketplace CreateNftParams {..} = do
let ipfsCid = deserializeByteString cnpIpfsCid
let ipfsCidHash = sha2_256 ipfsCid
nftStore <- Core.mdSingletons <$> marketplaceStore marketplace
when (isJust $ AssocMap.lookup ipfsCidHash nftStore) $ throwError "Nft entry already exists"
pkh <- getOwnPubKey
let tokenName = V.TokenName ipfsCid
nft <-
mapError (T.pack . Haskell.show @Currency.CurrencyError) $
Currency.mintContract pkh [(tokenName, 1)]
-- TODO: get operatiorFee by minting (const fee?? We haven't a price on this step)
let client = Core.marketplaceClient marketplace
let nftEntry = Core.NftInfo
{ niCurrency = Currency.currencySymbol nft
, niName = deserializeByteString cnpNftName
, niDescription = deserializeByteString cnpNftDescription
, niCategory = deserializeByteString <$> cnpNftCategory
, niIssuer = if cnpRevealIssuer then Just pkh else Nothing
}
void $ mapError' $ runStep client $ Core.CreateNftRedeemer ipfsCidHash nftEntry
logInfo @Haskell.String $ printf "Created NFT %s with store entry %s" (Haskell.show nft) (Haskell.show nftEntry)
pure ()
data OpenSaleParams =
OpenSaleParams {
ospItemId :: UserItemId,
ospSalePrice :: Sale.LovelacePrice
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''OpenSaleParams
-- | The user opens sale for his NFT
openSale :: Core.Marketplace -> OpenSaleParams -> Contract w s Text ()
openSale marketplace OpenSaleParams {..} = do
let internalId = toInternalId ospItemId
nftStore <- marketplaceStore marketplace
saleValue <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) ->
Core.nftValue ipfsCid <$> getNftEntry nftStore nftId
Right bundleId@(Core.InternalBundleId bundleHash cids) ->
Core.bundleValue cids <$> getBundleEntry nftStore bundleId
let openSaleParams = Sale.OpenSaleParams {
ospSalePrice = ospSalePrice,
ospSaleValue = saleValue
}
sale <- Sale.openSale openSaleParams marketplace
let client = Core.marketplaceClient marketplace
let lot = Left sale
void $ mapError' $ runStep client $ Core.PutLotRedeemer internalId lot
logInfo @Haskell.String $ printf "Created NFT sale %s" (Haskell.show sale)
pure ()
data CloseLotParams =
CloseLotParams {
clpItemId :: UserItemId
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''CloseLotParams
-- | The user buys specified NFT lot
buyItem :: Core.Marketplace -> CloseLotParams -> Contract w s Text ()
buyItem marketplace CloseLotParams {..} = do
let internalId = toInternalId clpItemId
nftStore <- marketplaceStore marketplace
sale <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) -> do
nftEntry <- getNftEntry nftStore nftId
maybe (throwError "NFT has not been put on sale") pure $
nftEntry ^. Core._nftLot ^? traverse . _2 . _Left
Right bundleId@(Core.InternalBundleId bundleHash cids) -> do
bundleEntry <- getBundleEntry nftStore bundleId
maybe (throwError "Bundle has not been put on sale") pure $
bundleEntry ^. Core._nbTokens ^? Core._HasLot . _2 . _Left
_ <- Sale.buyLot sale
let client = Core.marketplaceClient marketplace
void $ mapError' $ runStep client $ Core.RemoveLotRedeemer $
Lens.bimap Core.iniIpfsCidHash Core.ibiBundleId internalId
logInfo @Haskell.String $ printf "Bought lot from sale %s" (Haskell.show sale)
pure ()
-- | The user closes NFT sale and receives his token back
closeSale :: Core.Marketplace -> CloseLotParams -> Contract w s Text ()
closeSale marketplace CloseLotParams {..} = do
let internalId = toInternalId clpItemId
nftStore <- marketplaceStore marketplace
sale <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) -> do
nftEntry <- getNftEntry nftStore nftId
maybe (throwError "NFT has not been put on sale") pure $
nftEntry ^. Core._nftLot ^? traverse . _2 . _Left
Right bundleId@(Core.InternalBundleId bundleHash cids) -> do
bundleEntry <- getBundleEntry nftStore bundleId
maybe (throwError "Bundle has not been put on sale") pure $
bundleEntry ^. Core._nbTokens ^? Core._HasLot . _2 . _Left
_ <- Sale.redeemLot sale
let client = Core.marketplaceClient marketplace
void $ mapError' $ runStep client $ Core.RemoveLotRedeemer $
Lens.bimap Core.iniIpfsCidHash Core.ibiBundleId internalId
logInfo @Haskell.String $ printf "Closed lot sale %s" (Haskell.show sale)
pure ()
deriving newtype instance Schema.ToSchema DiffMilliSeconds
data StartAnAuctionParams =
StartAnAuctionParams {
saapItemId :: UserItemId,
saapDuration :: DiffMilliSeconds,
saapInitialPrice :: Value
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''StartAnAuctionParams
-- | The user starts an auction for specified NFT
startAnAuction :: Core.Marketplace -> StartAnAuctionParams -> Contract w s Text ()
startAnAuction marketplace@Core.Marketplace{..} StartAnAuctionParams {..} = do
let internalId = toInternalId saapItemId
nftStore <- marketplaceStore marketplace
auctionValue <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) ->
Core.nftValue ipfsCid <$> getNftEntry nftStore nftId
Right bundleId@(Core.InternalBundleId bundleHash cids) ->
Core.bundleValue cids <$> getBundleEntry nftStore bundleId
currTime <- currentTime
let endTime = currTime + fromMilliSeconds saapDuration
self <- Ledger.pubKeyHash <$> ownPubKey
let auctionParams = Auction.AuctionParams {
apOwner = self,
apAsset = auctionValue,
apEndTime = endTime,
apInitialPrice = saapInitialPrice,
apMarketplaceOperator = marketplaceOperator,
apMarketplaceSaleFee = marketplaceSaleFee
}
auctionToken <- mapError (T.pack . Haskell.show) $ Auction.startAuction auctionParams
let client = Core.marketplaceClient marketplace
let lot = Right $ Auction.toAuction auctionToken auctionParams
void $ mapError' $ runStep client $ Core.PutLotRedeemer internalId lot
logInfo @Haskell.String $ printf "Started an auction %s" (Haskell.show auctionParams)
pure ()
-- | The user completes the auction for specified NFT
completeAnAuction :: Core.Marketplace -> CloseLotParams -> Contract w s Text ()
completeAnAuction marketplace CloseLotParams {..} = do
let internalId = toInternalId clpItemId
nftStore <- marketplaceStore marketplace
auction <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) -> do
nftEntry <- getNftEntry nftStore nftId
maybe (throwError "NFT has not been put on auction") pure $
nftEntry ^. Core._nftLot ^? traverse . _2 . _Right
Right bundleId@(Core.InternalBundleId bundleHash cids) -> do
bundleEntry <- getBundleEntry nftStore bundleId
maybe (throwError "Bundle has not been put on auction") pure $
bundleEntry ^. Core._nbTokens ^? Core._HasLot . _2 . _Right
let auctionToken = Auction.getStateToken auction
let auctionParams = Auction.fromAuction auction
_ <- mapError (T.pack . Haskell.show) $ Auction.payoutAuction auctionToken auctionParams
let client = Core.marketplaceClient marketplace
void $ mapError' $ runStep client $ Core.RemoveLotRedeemer $
Lens.bimap Core.iniIpfsCidHash Core.ibiBundleId internalId
logInfo @Haskell.String $ printf "Completed an auction %s" (Haskell.show auction)
pure ()
data BidOnAuctionParams =
BidOnAuctionParams {
boapItemId :: UserItemId,
boapBid :: Ada
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''BidOnAuctionParams
-- | The user submits a bid on the auction for specified NFT
bidOnAuction :: Core.Marketplace -> BidOnAuctionParams -> Contract w s Text ()
bidOnAuction marketplace BidOnAuctionParams {..} = do
let internalId = toInternalId boapItemId
nftStore <- marketplaceStore marketplace
auction <- case internalId of
Left nftId@(Core.InternalNftId ipfsCidHash ipfsCid) -> do
nftEntry <- getNftEntry nftStore nftId
maybe (throwError "NFT has not been put on auction") pure $
nftEntry ^. Core._nftLot ^? traverse . _2 . _Right
Right bundleId@(Core.InternalBundleId bundleHash cids) -> do
bundleEntry <- getBundleEntry nftStore bundleId
maybe (throwError "Bundle has not been put on auction") pure $
bundleEntry ^. Core._nbTokens ^? Core._HasLot . _2 . _Right
let auctionToken = Auction.getStateToken auction
let auctionParams = Auction.fromAuction auction
_ <- mapError (T.pack . Haskell.show) $ Auction.submitBid auctionToken auctionParams boapBid
logInfo @Haskell.String $ printf "Submitted bid for auction %s" (Haskell.show auction)
pure ()
data BundleUpParams =
BundleUpParams {
bupIpfsCids :: [Text],
bupName :: Text,
bupDescription :: Text,
bupCategory :: [Text]
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''BundleUpParams
-- | The user creates a bundle from specified NFTs
bundleUp :: forall w s. Core.Marketplace -> BundleUpParams -> Contract w s Text ()
bundleUp marketplace BundleUpParams {..} = do
let ipfsCids = deserializeByteString <$> bupIpfsCids
let bundleId = Core.calcBundleIdHash ipfsCids
bundles <- Core.mdBundles <$> marketplaceStore marketplace
when (isJust $ AssocMap.lookup bundleId bundles) $ throwError "Bundle entry already exists"
let nftIds = sha2_256 <$> ipfsCids
let bundleInfo = Core.BundleInfo
{ biName = deserializeByteString bupName
, biDescription = deserializeByteString bupDescription
, biCategory = deserializeByteString <$> bupCategory
}
let client = Core.marketplaceClient marketplace
void $ mapError' $ runStep client $ Core.BundleUpRedeemer nftIds bundleId bundleInfo
logInfo @Haskell.String $ printf "Created a bundle %s" (Haskell.show bundleInfo)
pure ()
data UnbundleParams =
UnbundleParams {
upIpfsCids :: [Text]
}
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON, Schema.ToSchema)
Lens.makeClassy_ ''UnbundleParams
-- | The user unbundles specified NFTs
unbundle :: Core.Marketplace -> UnbundleParams -> Contract w s Text ()
unbundle marketplace UnbundleParams {..} = do
let bundleId = Core.calcBundleIdHash $ fmap deserializeByteString upIpfsCids
bundles <- Core.mdBundles <$> marketplaceStore marketplace
when (isNothing $ AssocMap.lookup bundleId bundles) $ throwError "Bundle entry does not exist"
let client = Core.marketplaceClient marketplace
void $ mapError' $ runStep client $ Core.UnbundleRedeemer bundleId
logInfo @Haskell.String $ printf "Removed bundle by id %s" (Haskell.show bundleId)
pure ()
balanceAt :: PubKeyHash -> AssetClass -> Contract w s Text Integer
balanceAt pkh asset = flip V.assetClassValueOf asset <$> fundsAt pkh
ownPubKeyBalance :: Contract w s Text Value
ownPubKeyBalance = getOwnPubKey >>= fundsAt
type MarketplaceUserSchema =
Endpoint "createNft" CreateNftParams
.\/ Endpoint "openSale" OpenSaleParams
.\/ Endpoint "buyItem" CloseLotParams
.\/ Endpoint "closeSale" CloseLotParams
.\/ Endpoint "startAnAuction" StartAnAuctionParams
.\/ Endpoint "completeAnAuction" CloseLotParams
.\/ Endpoint "bidOnAuction" BidOnAuctionParams
.\/ Endpoint "bundleUp" BundleUpParams
.\/ Endpoint "unbundle" UnbundleParams
.\/ Endpoint "ownPubKey" ()
.\/ Endpoint "ownPubKeyBalance" ()
data UserContractState =
NftCreated
| OpenedSale
| NftBought
| ClosedSale
| AuctionStarted
| AuctionComplete
| BidSubmitted
| Bundled
| Unbundled
| GetPubKey PubKeyHash
| GetPubKeyBalance Value
deriving stock (Haskell.Eq, Haskell.Show, Haskell.Generic)
deriving anyclass (J.ToJSON, J.FromJSON)
Lens.makeClassyPrisms ''UserContractState
userEndpoints :: Core.Marketplace -> Promise (ContractResponse Text UserContractState) MarketplaceUserSchema Void ()
userEndpoints marketplace =
(withContractResponse (Proxy @"createNft") (const NftCreated) (createNft marketplace)
`select` withContractResponse (Proxy @"openSale") (const OpenedSale) (openSale marketplace)
`select` withContractResponse (Proxy @"buyItem") (const NftBought) (buyItem marketplace)
`select` withContractResponse (Proxy @"closeSale") (const ClosedSale) (closeSale marketplace)
`select` withContractResponse (Proxy @"startAnAuction") (const AuctionStarted) (startAnAuction marketplace)
`select` withContractResponse (Proxy @"completeAnAuction") (const AuctionComplete) (completeAnAuction marketplace)
`select` withContractResponse (Proxy @"bidOnAuction") (const BidSubmitted) (bidOnAuction marketplace)
`select` withContractResponse (Proxy @"bundleUp") (const Bundled) (bundleUp marketplace)
`select` withContractResponse (Proxy @"unbundle") (const Unbundled) (unbundle marketplace)
`select` withContractResponse (Proxy @"ownPubKey") GetPubKey (const getOwnPubKey)
`select` withContractResponse (Proxy @"ownPubKeyBalance") GetPubKeyBalance (const ownPubKeyBalance)) <> userEndpoints marketplace