-- | Transaction metadata

module Blockfrost.Types.Cardano.Metadata
  ( TxMeta (..)
  , TxMetaJSON (..)
  , TxMetaCBOR (..)
  ) where

import Data.Aeson (Value, object, (.=))
import Data.Text (Text)
import Deriving.Aeson
import Servant.Docs (ToSample (..), samples)

import Blockfrost.Types.Shared

-- | Transaction metadata label in use
data TxMeta = TxMeta
  { TxMeta -> Text
_txMetaLabel :: Text -- ^ Metadata label
  , TxMeta -> Maybe Text
_txMetaCip10 :: Maybe Text -- ^ CIP10 defined description
  , TxMeta -> Quantity
_txMetaCount :: Quantity -- ^ The count of metadata entries with a specific label
  }
  deriving stock (Int -> TxMeta -> ShowS
[TxMeta] -> ShowS
TxMeta -> String
(Int -> TxMeta -> ShowS)
-> (TxMeta -> String) -> ([TxMeta] -> ShowS) -> Show TxMeta
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxMeta] -> ShowS
$cshowList :: [TxMeta] -> ShowS
show :: TxMeta -> String
$cshow :: TxMeta -> String
showsPrec :: Int -> TxMeta -> ShowS
$cshowsPrec :: Int -> TxMeta -> ShowS
Show, TxMeta -> TxMeta -> Bool
(TxMeta -> TxMeta -> Bool)
-> (TxMeta -> TxMeta -> Bool) -> Eq TxMeta
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxMeta -> TxMeta -> Bool
$c/= :: TxMeta -> TxMeta -> Bool
== :: TxMeta -> TxMeta -> Bool
$c== :: TxMeta -> TxMeta -> Bool
Eq, (forall x. TxMeta -> Rep TxMeta x)
-> (forall x. Rep TxMeta x -> TxMeta) -> Generic TxMeta
forall x. Rep TxMeta x -> TxMeta
forall x. TxMeta -> Rep TxMeta x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxMeta x -> TxMeta
$cfrom :: forall x. TxMeta -> Rep TxMeta x
Generic)
  deriving (Value -> Parser [TxMeta]
Value -> Parser TxMeta
(Value -> Parser TxMeta)
-> (Value -> Parser [TxMeta]) -> FromJSON TxMeta
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxMeta]
$cparseJSONList :: Value -> Parser [TxMeta]
parseJSON :: Value -> Parser TxMeta
$cparseJSON :: Value -> Parser TxMeta
FromJSON, [TxMeta] -> Encoding
[TxMeta] -> Value
TxMeta -> Encoding
TxMeta -> Value
(TxMeta -> Value)
-> (TxMeta -> Encoding)
-> ([TxMeta] -> Value)
-> ([TxMeta] -> Encoding)
-> ToJSON TxMeta
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxMeta] -> Encoding
$ctoEncodingList :: [TxMeta] -> Encoding
toJSONList :: [TxMeta] -> Value
$ctoJSONList :: [TxMeta] -> Value
toEncoding :: TxMeta -> Encoding
$ctoEncoding :: TxMeta -> Encoding
toJSON :: TxMeta -> Value
$ctoJSON :: TxMeta -> Value
ToJSON)
  via CustomJSON '[FieldLabelModifier '[StripPrefix "_txMeta", CamelToSnake]] TxMeta

instance ToSample TxMeta where
  toSamples :: Proxy TxMeta -> [(Text, TxMeta)]
toSamples = [(Text, TxMeta)] -> Proxy TxMeta -> [(Text, TxMeta)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, TxMeta)] -> Proxy TxMeta -> [(Text, TxMeta)])
-> [(Text, TxMeta)] -> Proxy TxMeta -> [(Text, TxMeta)]
forall a b. (a -> b) -> a -> b
$ [TxMeta] -> [(Text, TxMeta)]
forall a. [a] -> [(Text, a)]
samples
    [ Text -> Maybe Text -> Quantity -> TxMeta
TxMeta Text
"1990" Maybe Text
forall a. Maybe a
Nothing Quantity
1
    , Text -> Maybe Text -> Quantity -> TxMeta
TxMeta Text
"1967" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"nut.link metadata oracles registry") Quantity
3
    , Text -> Maybe Text -> Quantity -> TxMeta
TxMeta Text
"1968" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"nut.link metadata oracles data points") Quantity
16321
    ]

-- | Transaction metadata content in JSON
data TxMetaJSON = TxMetaJSON
  { TxMetaJSON -> Text
_txMetaJSONTxHash       :: Text -- ^ Transaction hash that contains the specific metadata
  , TxMetaJSON -> Maybe Value
_txMetaJSONJSONMetadata :: Maybe Value -- ^ Content of the JSON metadata
  }
  deriving stock (Int -> TxMetaJSON -> ShowS
[TxMetaJSON] -> ShowS
TxMetaJSON -> String
(Int -> TxMetaJSON -> ShowS)
-> (TxMetaJSON -> String)
-> ([TxMetaJSON] -> ShowS)
-> Show TxMetaJSON
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxMetaJSON] -> ShowS
$cshowList :: [TxMetaJSON] -> ShowS
show :: TxMetaJSON -> String
$cshow :: TxMetaJSON -> String
showsPrec :: Int -> TxMetaJSON -> ShowS
$cshowsPrec :: Int -> TxMetaJSON -> ShowS
Show, TxMetaJSON -> TxMetaJSON -> Bool
(TxMetaJSON -> TxMetaJSON -> Bool)
-> (TxMetaJSON -> TxMetaJSON -> Bool) -> Eq TxMetaJSON
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxMetaJSON -> TxMetaJSON -> Bool
$c/= :: TxMetaJSON -> TxMetaJSON -> Bool
== :: TxMetaJSON -> TxMetaJSON -> Bool
$c== :: TxMetaJSON -> TxMetaJSON -> Bool
Eq, (forall x. TxMetaJSON -> Rep TxMetaJSON x)
-> (forall x. Rep TxMetaJSON x -> TxMetaJSON) -> Generic TxMetaJSON
forall x. Rep TxMetaJSON x -> TxMetaJSON
forall x. TxMetaJSON -> Rep TxMetaJSON x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxMetaJSON x -> TxMetaJSON
$cfrom :: forall x. TxMetaJSON -> Rep TxMetaJSON x
Generic)
  deriving (Value -> Parser [TxMetaJSON]
Value -> Parser TxMetaJSON
(Value -> Parser TxMetaJSON)
-> (Value -> Parser [TxMetaJSON]) -> FromJSON TxMetaJSON
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxMetaJSON]
$cparseJSONList :: Value -> Parser [TxMetaJSON]
parseJSON :: Value -> Parser TxMetaJSON
$cparseJSON :: Value -> Parser TxMetaJSON
FromJSON, [TxMetaJSON] -> Encoding
[TxMetaJSON] -> Value
TxMetaJSON -> Encoding
TxMetaJSON -> Value
(TxMetaJSON -> Value)
-> (TxMetaJSON -> Encoding)
-> ([TxMetaJSON] -> Value)
-> ([TxMetaJSON] -> Encoding)
-> ToJSON TxMetaJSON
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxMetaJSON] -> Encoding
$ctoEncodingList :: [TxMetaJSON] -> Encoding
toJSONList :: [TxMetaJSON] -> Value
$ctoJSONList :: [TxMetaJSON] -> Value
toEncoding :: TxMetaJSON -> Encoding
$ctoEncoding :: TxMetaJSON -> Encoding
toJSON :: TxMetaJSON -> Value
$ctoJSON :: TxMetaJSON -> Value
ToJSON)
  via CustomJSON '[FieldLabelModifier '[StripPrefix "_txMetaJSON", CamelToSnake]] TxMetaJSON

instance ToSample TxMetaJSON where
  toSamples :: Proxy TxMetaJSON -> [(Text, TxMetaJSON)]
toSamples =
    let oracleMeta :: Text -> Value
oracleMeta Text
val =
          [Pair] -> Value
object [
            Text
"ADAUSD" Text -> [Value] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.=
              [ [Pair] -> Value
object [ Text
"value" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
val :: Text)
                       , Text
"source" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"ergoOracles" :: Text) ]
              ]
          ]
    in [(Text, TxMetaJSON)] -> Proxy TxMetaJSON -> [(Text, TxMetaJSON)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, TxMetaJSON)] -> Proxy TxMetaJSON -> [(Text, TxMetaJSON)])
-> [(Text, TxMetaJSON)] -> Proxy TxMetaJSON -> [(Text, TxMetaJSON)]
forall a b. (a -> b) -> a -> b
$ [TxMetaJSON] -> [(Text, TxMetaJSON)]
forall a. [a] -> [(Text, a)]
samples
    [ Text -> Maybe Value -> TxMetaJSON
TxMetaJSON
        Text
"257d75c8ddb0434e9b63e29ebb6241add2b835a307aa33aedba2effe09ed4ec8"
        (Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value) -> Value -> Maybe Value
forall a b. (a -> b) -> a -> b
$ Text -> Value
oracleMeta Text
"0.10409800535729975")
    , Text -> Maybe Value -> TxMetaJSON
TxMetaJSON
        Text
"e865f2cc01ca7381cf98dcdc4de07a5e8674b8ea16e6a18e3ed60c186fde2b9c"
        (Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value) -> Value -> Maybe Value
forall a b. (a -> b) -> a -> b
$ Text -> Value
oracleMeta Text
"0.15409850555139935")
    , Text -> Maybe Value -> TxMetaJSON
TxMetaJSON
        Text
"4237501da3cfdd53ade91e8911e764bd0699d88fd43b12f44a1f459b89bc91be"
        Maybe Value
forall a. Maybe a
Nothing
    ]

-- | Transaction metadata content in CBOR
data TxMetaCBOR = TxMetaCBOR
  { TxMetaCBOR -> Text
_txMetaCBORTxHash       :: Text -- ^ Transaction hash that contains the specific metadata
  , TxMetaCBOR -> Maybe Text
_txMetaCBORCBORMetadata :: Maybe Text -- ^ Content of the CBOR metadata
  }
  deriving stock (Int -> TxMetaCBOR -> ShowS
[TxMetaCBOR] -> ShowS
TxMetaCBOR -> String
(Int -> TxMetaCBOR -> ShowS)
-> (TxMetaCBOR -> String)
-> ([TxMetaCBOR] -> ShowS)
-> Show TxMetaCBOR
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxMetaCBOR] -> ShowS
$cshowList :: [TxMetaCBOR] -> ShowS
show :: TxMetaCBOR -> String
$cshow :: TxMetaCBOR -> String
showsPrec :: Int -> TxMetaCBOR -> ShowS
$cshowsPrec :: Int -> TxMetaCBOR -> ShowS
Show, TxMetaCBOR -> TxMetaCBOR -> Bool
(TxMetaCBOR -> TxMetaCBOR -> Bool)
-> (TxMetaCBOR -> TxMetaCBOR -> Bool) -> Eq TxMetaCBOR
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxMetaCBOR -> TxMetaCBOR -> Bool
$c/= :: TxMetaCBOR -> TxMetaCBOR -> Bool
== :: TxMetaCBOR -> TxMetaCBOR -> Bool
$c== :: TxMetaCBOR -> TxMetaCBOR -> Bool
Eq, (forall x. TxMetaCBOR -> Rep TxMetaCBOR x)
-> (forall x. Rep TxMetaCBOR x -> TxMetaCBOR) -> Generic TxMetaCBOR
forall x. Rep TxMetaCBOR x -> TxMetaCBOR
forall x. TxMetaCBOR -> Rep TxMetaCBOR x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxMetaCBOR x -> TxMetaCBOR
$cfrom :: forall x. TxMetaCBOR -> Rep TxMetaCBOR x
Generic)
  deriving (Value -> Parser [TxMetaCBOR]
Value -> Parser TxMetaCBOR
(Value -> Parser TxMetaCBOR)
-> (Value -> Parser [TxMetaCBOR]) -> FromJSON TxMetaCBOR
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxMetaCBOR]
$cparseJSONList :: Value -> Parser [TxMetaCBOR]
parseJSON :: Value -> Parser TxMetaCBOR
$cparseJSON :: Value -> Parser TxMetaCBOR
FromJSON, [TxMetaCBOR] -> Encoding
[TxMetaCBOR] -> Value
TxMetaCBOR -> Encoding
TxMetaCBOR -> Value
(TxMetaCBOR -> Value)
-> (TxMetaCBOR -> Encoding)
-> ([TxMetaCBOR] -> Value)
-> ([TxMetaCBOR] -> Encoding)
-> ToJSON TxMetaCBOR
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxMetaCBOR] -> Encoding
$ctoEncodingList :: [TxMetaCBOR] -> Encoding
toJSONList :: [TxMetaCBOR] -> Value
$ctoJSONList :: [TxMetaCBOR] -> Value
toEncoding :: TxMetaCBOR -> Encoding
$ctoEncoding :: TxMetaCBOR -> Encoding
toJSON :: TxMetaCBOR -> Value
$ctoJSON :: TxMetaCBOR -> Value
ToJSON)
  via CustomJSON '[FieldLabelModifier '[StripPrefix "_txMetaCBOR", CamelToSnake]] TxMetaCBOR

instance ToSample TxMetaCBOR where
  toSamples :: Proxy TxMetaCBOR -> [(Text, TxMetaCBOR)]
toSamples = [(Text, TxMetaCBOR)] -> Proxy TxMetaCBOR -> [(Text, TxMetaCBOR)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, TxMetaCBOR)] -> Proxy TxMetaCBOR -> [(Text, TxMetaCBOR)])
-> [(Text, TxMetaCBOR)] -> Proxy TxMetaCBOR -> [(Text, TxMetaCBOR)]
forall a b. (a -> b) -> a -> b
$ [TxMetaCBOR] -> [(Text, TxMetaCBOR)]
forall a. [a] -> [(Text, a)]
samples
    [ Text -> Maybe Text -> TxMetaCBOR
TxMetaCBOR
        Text
"257d75c8ddb0434e9b63e29ebb6241add2b835a307aa33aedba2effe09ed4ec8"
        Maybe Text
forall a. Maybe a
Nothing
    , Text -> Maybe Text -> TxMetaCBOR
TxMetaCBOR
        Text
"e865f2cc01ca7381cf98dcdc4de07a5e8674b8ea16e6a18e3ed60c186fde2b9c"
        Maybe Text
forall a. Maybe a
Nothing
    , Text -> Maybe Text -> TxMetaCBOR
TxMetaCBOR
        Text
"4237501da3cfdd53ade91e8911e764bd0699d88fd43b12f44a1f459b89bc91be"
        (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"\\xa100a16b436f6d62696e6174696f6e8601010101010c")
    ]