/
ScriptHash.hs
47 lines (39 loc) · 1.73 KB
/
ScriptHash.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
-- | Script Hash newtype
module Blockfrost.Types.Shared.ScriptHash
( ScriptHash (..)
) where
import Data.Aeson (FromJSON (..), ToJSON (..), Value(..), (.=), (.:))
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text
import qualified Data.Vector
import GHC.Generics
import Servant.API (Capture, FromHttpApiData (..), ToHttpApiData (..))
import Servant.Docs (DocCapture (..), ToCapture (..), ToSample (..), samples)
-- | Id (hash) of the transaction
newtype ScriptHash = ScriptHash { unScriptHash :: Text }
deriving stock (Show, Eq, Generic)
deriving newtype (FromHttpApiData, ToHttpApiData)
instance IsString ScriptHash where
fromString = ScriptHash . Data.Text.pack
instance ToJSON ScriptHash where
toJSON = toJSON . unScriptHash
toEncoding = toEncoding . unScriptHash
instance FromJSON ScriptHash where
parseJSON = fmap ScriptHash <$> parseJSON
-- Custom instance for list used by script list endpoint
instance {-# OVERLAPS #-} ToJSON [ScriptHash] where
toJSON = Array . Data.Vector.fromList . map (\sh -> Object ("script_hash" .= (toJSON . unScriptHash $ sh)))
instance {-# OVERLAPS #-} FromJSON [ScriptHash] where
parseJSON (Array a) = mapM parseJSON' (Data.Vector.toList a)
where
parseJSON' (Object b) = b .: "script_hash"
parseJSON' _ = fail "Unexpected type for ScriptHash"
parseJSON _ = fail "Expected array for [ScriptHash]"
instance ToSample ScriptHash where
toSamples _ = samples $ map ScriptHash
[ "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656"
, "e1457a0c47dfb7a2f6b8fbb059bdceab163c05d34f195b87b9f2b30e"
]
instance ToCapture (Capture "script_hash" ScriptHash) where
toCapture _ = DocCapture "script_hash" "Hash of the script."