-
Notifications
You must be signed in to change notification settings - Fork 50
/
Types.hs
122 lines (105 loc) · 4.52 KB
/
Types.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# options_ghc -fno-warn-orphans #-}
{-# options_ghc -Wno-redundant-constraints #-}
{-# options_ghc -fno-strictness #-}
{-# options_ghc -fno-specialise #-}
module Contracts.NFT.Types
where
import Ledger
import Ledger.Value (AssetClass (..), assetClass, assetClassValue, assetClassValueOf)
import Playground.Contract (Show, FromJSON, Generic, ToJSON, ToSchema)
import qualified PlutusTx
import PlutusTx.Prelude
import qualified Prelude
import Prelude (String)
import Text.Printf (PrintfArg)
-- | An nft market
data NFTMarket = NFTMarket
{ marketId :: AssetClass
, marketTokenSymbol :: CurrencySymbol
, marketTokenMetaSymbol :: CurrencySymbol
, marketTokenMetaNameSuffix:: BuiltinByteString
, marketFee :: Integer
, marketOwner :: PubKeyHash
} deriving (Show, Generic, ToJSON, FromJSON, ToSchema, Prelude.Eq, Prelude.Ord)
PlutusTx.makeLift ''NFTMarket
data NFTMetadata = NFTMetadata
{
nftTokenName:: TokenName
, nftMetaTokenName:: TokenName
, nftMetaDescription:: BuiltinByteString
, nftMetaAuthor:: BuiltinByteString
, nftMetaFile:: BuiltinByteString
, nftTokenSymbol :: CurrencySymbol
, nftMetaTokenSymbol :: CurrencySymbol
, nftSeller :: Maybe PubKeyHash
, nftSellPrice:: Integer
}
deriving (Show, Generic, ToJSON, FromJSON)
instance Eq NFTMetadata where
{-# INLINABLE (==) #-}
x == y = (nftTokenSymbol x PlutusTx.Prelude.== nftTokenSymbol y) &&
(nftMetaTokenSymbol x PlutusTx.Prelude.== nftMetaTokenSymbol y) &&
(nftTokenName x PlutusTx.Prelude.== nftTokenName y) &&
(nftMetaTokenName x PlutusTx.Prelude.== nftMetaTokenName y)
PlutusTx.makeIsDataIndexed ''NFTMetadata [('NFTMetadata, 0)]
PlutusTx.makeLift ''NFTMetadata
data NFTMarketAction = Create NFTMetadata | Sell | CancelSell | Buy PubKeyHash
deriving Show
PlutusTx.makeIsDataIndexed ''NFTMarketAction [ ('Create , 0)
, ('Sell, 1)
, ('CancelSell, 2)
, ('Buy, 3)
]
PlutusTx.makeLift ''NFTMarketAction
data NFTMarketDatum =
Factory [NFTMetadata]
| NFTMeta NFTMetadata
deriving stock (Show)
PlutusTx.unstableMakeIsData ''NFTMarketDatum
PlutusTx.makeLift ''NFTMarketDatum
-- | Data transfer object, used to send repsonse to the frontend,
-- | it allows to avoid using of cabal repl for create complex request objects
data NFTMetadataDto = NFTMetadataDto
{ nftDtoTokenName:: String
, nftDtoMetaDescription:: String
, nftDtoMetaAuthor:: String
, nftDtoMetaFile:: String
, nftDtoTokenSymbol :: String
, nftDtoSeller :: String
, nftDtoSellPrice:: Integer
} deriving (Show, Generic, ToJSON, FromJSON, ToSchema)
instance Eq NFTMetadataDto where
x == y = (nftDtoTokenName x Prelude.== nftDtoTokenName y)
&& (nftDtoMetaDescription x Prelude.== nftDtoMetaDescription y)
&& (nftDtoMetaAuthor x Prelude.== nftDtoMetaAuthor y)
&& (nftDtoMetaFile x Prelude.== nftDtoMetaFile y)
&& (nftDtoTokenSymbol x Prelude.== nftDtoTokenSymbol y)
&& (nftDtoSeller x Prelude.== nftDtoSeller y)
&& (nftDtoSellPrice x Prelude.== nftDtoSellPrice y)
{-# INLINABLE isNftToken #-}
isNftToken :: Value -> CurrencySymbol -> TokenName -> Bool
isNftToken v cur tokenName = assetClassValueOf v (assetClass cur tokenName) == 1
{-# INLINABLE getNftValue #-}
getNftValue :: CurrencySymbol -> TokenName -> Value
getNftValue cur tokenName = assetClassValue (assetClass cur tokenName) 1
{-# INLINABLE isMarketToken #-}
isMarketToken :: Value -> AssetClass -> Bool
isMarketToken v ac = assetClassValueOf v ac == 1
{-# INLINABLE valueWithin #-}
valueWithin :: TxInInfo -> Value
valueWithin = txOutValue . txInInfoResolved