{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveAnyClass    #-}
{-# LANGUAGE DerivingVia       #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell   #-}
-- | Defines a number of types that are used in Wallet.XXX modules
module Wallet.Types(
    ContractInstanceId(..)
    , contractInstanceIDs
    , randomID
    , ContractActivityStatus(..)
    , parseContractActivityStatus
    , Notification(..)
    , NotificationError(..)
    , EndpointDescription(..)
    , EndpointValue(..)
    ) where

import Control.Lens.TH (makeClassyPrisms)
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import Data.Aeson qualified as Aeson
import Data.Aeson.Encode.Pretty qualified as JSON
import Data.ByteString.Lazy.Char8 qualified as BSL8
import Data.String (IsString (..))
import Data.Text (Text)
import Data.Text qualified as T
import Data.UUID (UUID)
import Data.UUID.Extras qualified as UUID
import Data.UUID.V4 qualified as UUID
import GHC.Generics (Generic)
import Language.Haskell.TH.Syntax qualified as TH
import Prettyprinter (Pretty (..), colon, hang, viaShow, vsep, (<+>))

import Prettyprinter.Extras (PrettyShow (..), Tagged (..))

import Data.OpenApi.Schema qualified as OpenApi

-- | Unique ID for contract instance
newtype ContractInstanceId = ContractInstanceId { ContractInstanceId -> UUID
unContractInstanceId :: UUID }
    deriving (ContractInstanceId -> ContractInstanceId -> Bool
(ContractInstanceId -> ContractInstanceId -> Bool)
-> (ContractInstanceId -> ContractInstanceId -> Bool)
-> Eq ContractInstanceId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ContractInstanceId -> ContractInstanceId -> Bool
$c/= :: ContractInstanceId -> ContractInstanceId -> Bool
== :: ContractInstanceId -> ContractInstanceId -> Bool
$c== :: ContractInstanceId -> ContractInstanceId -> Bool
Eq, Eq ContractInstanceId
Eq ContractInstanceId
-> (ContractInstanceId -> ContractInstanceId -> Ordering)
-> (ContractInstanceId -> ContractInstanceId -> Bool)
-> (ContractInstanceId -> ContractInstanceId -> Bool)
-> (ContractInstanceId -> ContractInstanceId -> Bool)
-> (ContractInstanceId -> ContractInstanceId -> Bool)
-> (ContractInstanceId -> ContractInstanceId -> ContractInstanceId)
-> (ContractInstanceId -> ContractInstanceId -> ContractInstanceId)
-> Ord ContractInstanceId
ContractInstanceId -> ContractInstanceId -> Bool
ContractInstanceId -> ContractInstanceId -> Ordering
ContractInstanceId -> ContractInstanceId -> ContractInstanceId
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ContractInstanceId -> ContractInstanceId -> ContractInstanceId
$cmin :: ContractInstanceId -> ContractInstanceId -> ContractInstanceId
max :: ContractInstanceId -> ContractInstanceId -> ContractInstanceId
$cmax :: ContractInstanceId -> ContractInstanceId -> ContractInstanceId
>= :: ContractInstanceId -> ContractInstanceId -> Bool
$c>= :: ContractInstanceId -> ContractInstanceId -> Bool
> :: ContractInstanceId -> ContractInstanceId -> Bool
$c> :: ContractInstanceId -> ContractInstanceId -> Bool
<= :: ContractInstanceId -> ContractInstanceId -> Bool
$c<= :: ContractInstanceId -> ContractInstanceId -> Bool
< :: ContractInstanceId -> ContractInstanceId -> Bool
$c< :: ContractInstanceId -> ContractInstanceId -> Bool
compare :: ContractInstanceId -> ContractInstanceId -> Ordering
$ccompare :: ContractInstanceId -> ContractInstanceId -> Ordering
$cp1Ord :: Eq ContractInstanceId
Ord, Int -> ContractInstanceId -> ShowS
[ContractInstanceId] -> ShowS
ContractInstanceId -> String
(Int -> ContractInstanceId -> ShowS)
-> (ContractInstanceId -> String)
-> ([ContractInstanceId] -> ShowS)
-> Show ContractInstanceId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ContractInstanceId] -> ShowS
$cshowList :: [ContractInstanceId] -> ShowS
show :: ContractInstanceId -> String
$cshow :: ContractInstanceId -> String
showsPrec :: Int -> ContractInstanceId -> ShowS
$cshowsPrec :: Int -> ContractInstanceId -> ShowS
Show, (forall x. ContractInstanceId -> Rep ContractInstanceId x)
-> (forall x. Rep ContractInstanceId x -> ContractInstanceId)
-> Generic ContractInstanceId
forall x. Rep ContractInstanceId x -> ContractInstanceId
forall x. ContractInstanceId -> Rep ContractInstanceId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ContractInstanceId x -> ContractInstanceId
$cfrom :: forall x. ContractInstanceId -> Rep ContractInstanceId x
Generic)
    deriving newtype (FromJSONKeyFunction [ContractInstanceId]
FromJSONKeyFunction ContractInstanceId
FromJSONKeyFunction ContractInstanceId
-> FromJSONKeyFunction [ContractInstanceId]
-> FromJSONKey ContractInstanceId
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [ContractInstanceId]
$cfromJSONKeyList :: FromJSONKeyFunction [ContractInstanceId]
fromJSONKey :: FromJSONKeyFunction ContractInstanceId
$cfromJSONKey :: FromJSONKeyFunction ContractInstanceId
FromJSONKey, ToJSONKeyFunction [ContractInstanceId]
ToJSONKeyFunction ContractInstanceId
ToJSONKeyFunction ContractInstanceId
-> ToJSONKeyFunction [ContractInstanceId]
-> ToJSONKey ContractInstanceId
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [ContractInstanceId]
$ctoJSONKeyList :: ToJSONKeyFunction [ContractInstanceId]
toJSONKey :: ToJSONKeyFunction ContractInstanceId
$ctoJSONKey :: ToJSONKeyFunction ContractInstanceId
ToJSONKey)
    deriving anyclass (Value -> Parser [ContractInstanceId]
Value -> Parser ContractInstanceId
(Value -> Parser ContractInstanceId)
-> (Value -> Parser [ContractInstanceId])
-> FromJSON ContractInstanceId
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ContractInstanceId]
$cparseJSONList :: Value -> Parser [ContractInstanceId]
parseJSON :: Value -> Parser ContractInstanceId
$cparseJSON :: Value -> Parser ContractInstanceId
FromJSON, [ContractInstanceId] -> Encoding
[ContractInstanceId] -> Value
ContractInstanceId -> Encoding
ContractInstanceId -> Value
(ContractInstanceId -> Value)
-> (ContractInstanceId -> Encoding)
-> ([ContractInstanceId] -> Value)
-> ([ContractInstanceId] -> Encoding)
-> ToJSON ContractInstanceId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ContractInstanceId] -> Encoding
$ctoEncodingList :: [ContractInstanceId] -> Encoding
toJSONList :: [ContractInstanceId] -> Value
$ctoJSONList :: [ContractInstanceId] -> Value
toEncoding :: ContractInstanceId -> Encoding
$ctoEncoding :: ContractInstanceId -> Encoding
toJSON :: ContractInstanceId -> Value
$ctoJSON :: ContractInstanceId -> Value
ToJSON, Typeable ContractInstanceId
Typeable ContractInstanceId
-> (Proxy ContractInstanceId
    -> Declare (Definitions Schema) NamedSchema)
-> ToSchema ContractInstanceId
Proxy ContractInstanceId
-> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a
-> (Proxy a -> Declare (Definitions Schema) NamedSchema)
-> ToSchema a
declareNamedSchema :: Proxy ContractInstanceId
-> Declare (Definitions Schema) NamedSchema
$cdeclareNamedSchema :: Proxy ContractInstanceId
-> Declare (Definitions Schema) NamedSchema
$cp1ToSchema :: Typeable ContractInstanceId
OpenApi.ToSchema)
    deriving [ContractInstanceId] -> Doc ann
ContractInstanceId -> Doc ann
(forall ann. ContractInstanceId -> Doc ann)
-> (forall ann. [ContractInstanceId] -> Doc ann)
-> Pretty ContractInstanceId
forall ann. [ContractInstanceId] -> Doc ann
forall ann. ContractInstanceId -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [ContractInstanceId] -> Doc ann
$cprettyList :: forall ann. [ContractInstanceId] -> Doc ann
pretty :: ContractInstanceId -> Doc ann
$cpretty :: forall ann. ContractInstanceId -> Doc ann
Pretty via (PrettyShow UUID)

-- | A pure list of all 'ContractInstanceId' values. To be used in testing.
contractInstanceIDs :: [ContractInstanceId]
contractInstanceIDs :: [ContractInstanceId]
contractInstanceIDs = UUID -> ContractInstanceId
ContractInstanceId (UUID -> ContractInstanceId) -> [UUID] -> [ContractInstanceId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [UUID]
UUID.mockUUIDs

randomID :: IO ContractInstanceId
randomID :: IO ContractInstanceId
randomID = UUID -> ContractInstanceId
ContractInstanceId (UUID -> ContractInstanceId) -> IO UUID -> IO ContractInstanceId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO UUID
UUID.nextRandom

data ContractActivityStatus = Active | Stopped | Done deriving (ContractActivityStatus -> ContractActivityStatus -> Bool
(ContractActivityStatus -> ContractActivityStatus -> Bool)
-> (ContractActivityStatus -> ContractActivityStatus -> Bool)
-> Eq ContractActivityStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ContractActivityStatus -> ContractActivityStatus -> Bool
$c/= :: ContractActivityStatus -> ContractActivityStatus -> Bool
== :: ContractActivityStatus -> ContractActivityStatus -> Bool
$c== :: ContractActivityStatus -> ContractActivityStatus -> Bool
Eq, Int -> ContractActivityStatus -> ShowS
[ContractActivityStatus] -> ShowS
ContractActivityStatus -> String
(Int -> ContractActivityStatus -> ShowS)
-> (ContractActivityStatus -> String)
-> ([ContractActivityStatus] -> ShowS)
-> Show ContractActivityStatus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ContractActivityStatus] -> ShowS
$cshowList :: [ContractActivityStatus] -> ShowS
show :: ContractActivityStatus -> String
$cshow :: ContractActivityStatus -> String
showsPrec :: Int -> ContractActivityStatus -> ShowS
$cshowsPrec :: Int -> ContractActivityStatus -> ShowS
Show, (forall x. ContractActivityStatus -> Rep ContractActivityStatus x)
-> (forall x.
    Rep ContractActivityStatus x -> ContractActivityStatus)
-> Generic ContractActivityStatus
forall x. Rep ContractActivityStatus x -> ContractActivityStatus
forall x. ContractActivityStatus -> Rep ContractActivityStatus x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ContractActivityStatus x -> ContractActivityStatus
$cfrom :: forall x. ContractActivityStatus -> Rep ContractActivityStatus x
Generic, [ContractActivityStatus] -> Encoding
[ContractActivityStatus] -> Value
ContractActivityStatus -> Encoding
ContractActivityStatus -> Value
(ContractActivityStatus -> Value)
-> (ContractActivityStatus -> Encoding)
-> ([ContractActivityStatus] -> Value)
-> ([ContractActivityStatus] -> Encoding)
-> ToJSON ContractActivityStatus
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ContractActivityStatus] -> Encoding
$ctoEncodingList :: [ContractActivityStatus] -> Encoding
toJSONList :: [ContractActivityStatus] -> Value
$ctoJSONList :: [ContractActivityStatus] -> Value
toEncoding :: ContractActivityStatus -> Encoding
$ctoEncoding :: ContractActivityStatus -> Encoding
toJSON :: ContractActivityStatus -> Value
$ctoJSON :: ContractActivityStatus -> Value
ToJSON, Value -> Parser [ContractActivityStatus]
Value -> Parser ContractActivityStatus
(Value -> Parser ContractActivityStatus)
-> (Value -> Parser [ContractActivityStatus])
-> FromJSON ContractActivityStatus
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ContractActivityStatus]
$cparseJSONList :: Value -> Parser [ContractActivityStatus]
parseJSON :: Value -> Parser ContractActivityStatus
$cparseJSON :: Value -> Parser ContractActivityStatus
FromJSON, Typeable ContractActivityStatus
Typeable ContractActivityStatus
-> (Proxy ContractActivityStatus
    -> Declare (Definitions Schema) NamedSchema)
-> ToSchema ContractActivityStatus
Proxy ContractActivityStatus
-> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a
-> (Proxy a -> Declare (Definitions Schema) NamedSchema)
-> ToSchema a
declareNamedSchema :: Proxy ContractActivityStatus
-> Declare (Definitions Schema) NamedSchema
$cdeclareNamedSchema :: Proxy ContractActivityStatus
-> Declare (Definitions Schema) NamedSchema
$cp1ToSchema :: Typeable ContractActivityStatus
OpenApi.ToSchema)

parseContractActivityStatus :: Text -> Maybe ContractActivityStatus
parseContractActivityStatus :: Text -> Maybe ContractActivityStatus
parseContractActivityStatus Text
t = case Text -> Text
T.toLower Text
t of
    Text
"active"  -> ContractActivityStatus -> Maybe ContractActivityStatus
forall a. a -> Maybe a
Just ContractActivityStatus
Active
    Text
"stopped" -> ContractActivityStatus -> Maybe ContractActivityStatus
forall a. a -> Maybe a
Just ContractActivityStatus
Stopped
    Text
"done"    -> ContractActivityStatus -> Maybe ContractActivityStatus
forall a. a -> Maybe a
Just ContractActivityStatus
Done
    Text
_         -> Maybe ContractActivityStatus
forall a. Maybe a
Nothing

newtype EndpointDescription = EndpointDescription { EndpointDescription -> String
getEndpointDescription :: String }
    deriving stock (EndpointDescription -> EndpointDescription -> Bool
(EndpointDescription -> EndpointDescription -> Bool)
-> (EndpointDescription -> EndpointDescription -> Bool)
-> Eq EndpointDescription
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EndpointDescription -> EndpointDescription -> Bool
$c/= :: EndpointDescription -> EndpointDescription -> Bool
== :: EndpointDescription -> EndpointDescription -> Bool
$c== :: EndpointDescription -> EndpointDescription -> Bool
Eq, Eq EndpointDescription
Eq EndpointDescription
-> (EndpointDescription -> EndpointDescription -> Ordering)
-> (EndpointDescription -> EndpointDescription -> Bool)
-> (EndpointDescription -> EndpointDescription -> Bool)
-> (EndpointDescription -> EndpointDescription -> Bool)
-> (EndpointDescription -> EndpointDescription -> Bool)
-> (EndpointDescription
    -> EndpointDescription -> EndpointDescription)
-> (EndpointDescription
    -> EndpointDescription -> EndpointDescription)
-> Ord EndpointDescription
EndpointDescription -> EndpointDescription -> Bool
EndpointDescription -> EndpointDescription -> Ordering
EndpointDescription -> EndpointDescription -> EndpointDescription
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EndpointDescription -> EndpointDescription -> EndpointDescription
$cmin :: EndpointDescription -> EndpointDescription -> EndpointDescription
max :: EndpointDescription -> EndpointDescription -> EndpointDescription
$cmax :: EndpointDescription -> EndpointDescription -> EndpointDescription
>= :: EndpointDescription -> EndpointDescription -> Bool
$c>= :: EndpointDescription -> EndpointDescription -> Bool
> :: EndpointDescription -> EndpointDescription -> Bool
$c> :: EndpointDescription -> EndpointDescription -> Bool
<= :: EndpointDescription -> EndpointDescription -> Bool
$c<= :: EndpointDescription -> EndpointDescription -> Bool
< :: EndpointDescription -> EndpointDescription -> Bool
$c< :: EndpointDescription -> EndpointDescription -> Bool
compare :: EndpointDescription -> EndpointDescription -> Ordering
$ccompare :: EndpointDescription -> EndpointDescription -> Ordering
$cp1Ord :: Eq EndpointDescription
Ord, (forall x. EndpointDescription -> Rep EndpointDescription x)
-> (forall x. Rep EndpointDescription x -> EndpointDescription)
-> Generic EndpointDescription
forall x. Rep EndpointDescription x -> EndpointDescription
forall x. EndpointDescription -> Rep EndpointDescription x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep EndpointDescription x -> EndpointDescription
$cfrom :: forall x. EndpointDescription -> Rep EndpointDescription x
Generic, Int -> EndpointDescription -> ShowS
[EndpointDescription] -> ShowS
EndpointDescription -> String
(Int -> EndpointDescription -> ShowS)
-> (EndpointDescription -> String)
-> ([EndpointDescription] -> ShowS)
-> Show EndpointDescription
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EndpointDescription] -> ShowS
$cshowList :: [EndpointDescription] -> ShowS
show :: EndpointDescription -> String
$cshow :: EndpointDescription -> String
showsPrec :: Int -> EndpointDescription -> ShowS
$cshowsPrec :: Int -> EndpointDescription -> ShowS
Show, EndpointDescription -> Q Exp
EndpointDescription -> Q (TExp EndpointDescription)
(EndpointDescription -> Q Exp)
-> (EndpointDescription -> Q (TExp EndpointDescription))
-> Lift EndpointDescription
forall t. (t -> Q Exp) -> (t -> Q (TExp t)) -> Lift t
liftTyped :: EndpointDescription -> Q (TExp EndpointDescription)
$cliftTyped :: EndpointDescription -> Q (TExp EndpointDescription)
lift :: EndpointDescription -> Q Exp
$clift :: EndpointDescription -> Q Exp
TH.Lift)
    deriving newtype (String -> EndpointDescription
(String -> EndpointDescription) -> IsString EndpointDescription
forall a. (String -> a) -> IsString a
fromString :: String -> EndpointDescription
$cfromString :: String -> EndpointDescription
IsString, [EndpointDescription] -> Doc ann
EndpointDescription -> Doc ann
(forall ann. EndpointDescription -> Doc ann)
-> (forall ann. [EndpointDescription] -> Doc ann)
-> Pretty EndpointDescription
forall ann. [EndpointDescription] -> Doc ann
forall ann. EndpointDescription -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [EndpointDescription] -> Doc ann
$cprettyList :: forall ann. [EndpointDescription] -> Doc ann
pretty :: EndpointDescription -> Doc ann
$cpretty :: forall ann. EndpointDescription -> Doc ann
Pretty)
    deriving anyclass ([EndpointDescription] -> Encoding
[EndpointDescription] -> Value
EndpointDescription -> Encoding
EndpointDescription -> Value
(EndpointDescription -> Value)
-> (EndpointDescription -> Encoding)
-> ([EndpointDescription] -> Value)
-> ([EndpointDescription] -> Encoding)
-> ToJSON EndpointDescription
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [EndpointDescription] -> Encoding
$ctoEncodingList :: [EndpointDescription] -> Encoding
toJSONList :: [EndpointDescription] -> Value
$ctoJSONList :: [EndpointDescription] -> Value
toEncoding :: EndpointDescription -> Encoding
$ctoEncoding :: EndpointDescription -> Encoding
toJSON :: EndpointDescription -> Value
$ctoJSON :: EndpointDescription -> Value
ToJSON, Value -> Parser [EndpointDescription]
Value -> Parser EndpointDescription
(Value -> Parser EndpointDescription)
-> (Value -> Parser [EndpointDescription])
-> FromJSON EndpointDescription
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [EndpointDescription]
$cparseJSONList :: Value -> Parser [EndpointDescription]
parseJSON :: Value -> Parser EndpointDescription
$cparseJSON :: Value -> Parser EndpointDescription
FromJSON, Typeable EndpointDescription
Typeable EndpointDescription
-> (Proxy EndpointDescription
    -> Declare (Definitions Schema) NamedSchema)
-> ToSchema EndpointDescription
Proxy EndpointDescription
-> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a
-> (Proxy a -> Declare (Definitions Schema) NamedSchema)
-> ToSchema a
declareNamedSchema :: Proxy EndpointDescription
-> Declare (Definitions Schema) NamedSchema
$cdeclareNamedSchema :: Proxy EndpointDescription
-> Declare (Definitions Schema) NamedSchema
$cp1ToSchema :: Typeable EndpointDescription
OpenApi.ToSchema)

newtype EndpointValue a = EndpointValue { EndpointValue a -> a
unEndpointValue :: a }
    deriving stock (EndpointValue a -> EndpointValue a -> Bool
(EndpointValue a -> EndpointValue a -> Bool)
-> (EndpointValue a -> EndpointValue a -> Bool)
-> Eq (EndpointValue a)
forall a. Eq a => EndpointValue a -> EndpointValue a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EndpointValue a -> EndpointValue a -> Bool
$c/= :: forall a. Eq a => EndpointValue a -> EndpointValue a -> Bool
== :: EndpointValue a -> EndpointValue a -> Bool
$c== :: forall a. Eq a => EndpointValue a -> EndpointValue a -> Bool
Eq, Eq (EndpointValue a)
Eq (EndpointValue a)
-> (EndpointValue a -> EndpointValue a -> Ordering)
-> (EndpointValue a -> EndpointValue a -> Bool)
-> (EndpointValue a -> EndpointValue a -> Bool)
-> (EndpointValue a -> EndpointValue a -> Bool)
-> (EndpointValue a -> EndpointValue a -> Bool)
-> (EndpointValue a -> EndpointValue a -> EndpointValue a)
-> (EndpointValue a -> EndpointValue a -> EndpointValue a)
-> Ord (EndpointValue a)
EndpointValue a -> EndpointValue a -> Bool
EndpointValue a -> EndpointValue a -> Ordering
EndpointValue a -> EndpointValue a -> EndpointValue a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (EndpointValue a)
forall a. Ord a => EndpointValue a -> EndpointValue a -> Bool
forall a. Ord a => EndpointValue a -> EndpointValue a -> Ordering
forall a.
Ord a =>
EndpointValue a -> EndpointValue a -> EndpointValue a
min :: EndpointValue a -> EndpointValue a -> EndpointValue a
$cmin :: forall a.
Ord a =>
EndpointValue a -> EndpointValue a -> EndpointValue a
max :: EndpointValue a -> EndpointValue a -> EndpointValue a
$cmax :: forall a.
Ord a =>
EndpointValue a -> EndpointValue a -> EndpointValue a
>= :: EndpointValue a -> EndpointValue a -> Bool
$c>= :: forall a. Ord a => EndpointValue a -> EndpointValue a -> Bool
> :: EndpointValue a -> EndpointValue a -> Bool
$c> :: forall a. Ord a => EndpointValue a -> EndpointValue a -> Bool
<= :: EndpointValue a -> EndpointValue a -> Bool
$c<= :: forall a. Ord a => EndpointValue a -> EndpointValue a -> Bool
< :: EndpointValue a -> EndpointValue a -> Bool
$c< :: forall a. Ord a => EndpointValue a -> EndpointValue a -> Bool
compare :: EndpointValue a -> EndpointValue a -> Ordering
$ccompare :: forall a. Ord a => EndpointValue a -> EndpointValue a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (EndpointValue a)
Ord, (forall x. EndpointValue a -> Rep (EndpointValue a) x)
-> (forall x. Rep (EndpointValue a) x -> EndpointValue a)
-> Generic (EndpointValue a)
forall x. Rep (EndpointValue a) x -> EndpointValue a
forall x. EndpointValue a -> Rep (EndpointValue a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (EndpointValue a) x -> EndpointValue a
forall a x. EndpointValue a -> Rep (EndpointValue a) x
$cto :: forall a x. Rep (EndpointValue a) x -> EndpointValue a
$cfrom :: forall a x. EndpointValue a -> Rep (EndpointValue a) x
Generic, Int -> EndpointValue a -> ShowS
[EndpointValue a] -> ShowS
EndpointValue a -> String
(Int -> EndpointValue a -> ShowS)
-> (EndpointValue a -> String)
-> ([EndpointValue a] -> ShowS)
-> Show (EndpointValue a)
forall a. Show a => Int -> EndpointValue a -> ShowS
forall a. Show a => [EndpointValue a] -> ShowS
forall a. Show a => EndpointValue a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EndpointValue a] -> ShowS
$cshowList :: forall a. Show a => [EndpointValue a] -> ShowS
show :: EndpointValue a -> String
$cshow :: forall a. Show a => EndpointValue a -> String
showsPrec :: Int -> EndpointValue a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> EndpointValue a -> ShowS
Show)
    deriving anyclass ([EndpointValue a] -> Encoding
[EndpointValue a] -> Value
EndpointValue a -> Encoding
EndpointValue a -> Value
(EndpointValue a -> Value)
-> (EndpointValue a -> Encoding)
-> ([EndpointValue a] -> Value)
-> ([EndpointValue a] -> Encoding)
-> ToJSON (EndpointValue a)
forall a. ToJSON a => [EndpointValue a] -> Encoding
forall a. ToJSON a => [EndpointValue a] -> Value
forall a. ToJSON a => EndpointValue a -> Encoding
forall a. ToJSON a => EndpointValue a -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [EndpointValue a] -> Encoding
$ctoEncodingList :: forall a. ToJSON a => [EndpointValue a] -> Encoding
toJSONList :: [EndpointValue a] -> Value
$ctoJSONList :: forall a. ToJSON a => [EndpointValue a] -> Value
toEncoding :: EndpointValue a -> Encoding
$ctoEncoding :: forall a. ToJSON a => EndpointValue a -> Encoding
toJSON :: EndpointValue a -> Value
$ctoJSON :: forall a. ToJSON a => EndpointValue a -> Value
ToJSON, Value -> Parser [EndpointValue a]
Value -> Parser (EndpointValue a)
(Value -> Parser (EndpointValue a))
-> (Value -> Parser [EndpointValue a])
-> FromJSON (EndpointValue a)
forall a. FromJSON a => Value -> Parser [EndpointValue a]
forall a. FromJSON a => Value -> Parser (EndpointValue a)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [EndpointValue a]
$cparseJSONList :: forall a. FromJSON a => Value -> Parser [EndpointValue a]
parseJSON :: Value -> Parser (EndpointValue a)
$cparseJSON :: forall a. FromJSON a => Value -> Parser (EndpointValue a)
FromJSON)

deriving via (Tagged "EndpointValue:" (PrettyShow a)) instance (Show a => Pretty (EndpointValue a))

data Notification =
    Notification
        { Notification -> ContractInstanceId
notificationContractID       :: ContractInstanceId
        , Notification -> EndpointDescription
notificationContractEndpoint :: EndpointDescription
        , Notification -> Value
notificationContractArg      :: Aeson.Value
        }
    deriving stock (Notification -> Notification -> Bool
(Notification -> Notification -> Bool)
-> (Notification -> Notification -> Bool) -> Eq Notification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Notification -> Notification -> Bool
$c/= :: Notification -> Notification -> Bool
== :: Notification -> Notification -> Bool
$c== :: Notification -> Notification -> Bool
Eq, Int -> Notification -> ShowS
[Notification] -> ShowS
Notification -> String
(Int -> Notification -> ShowS)
-> (Notification -> String)
-> ([Notification] -> ShowS)
-> Show Notification
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Notification] -> ShowS
$cshowList :: [Notification] -> ShowS
show :: Notification -> String
$cshow :: Notification -> String
showsPrec :: Int -> Notification -> ShowS
$cshowsPrec :: Int -> Notification -> ShowS
Show, (forall x. Notification -> Rep Notification x)
-> (forall x. Rep Notification x -> Notification)
-> Generic Notification
forall x. Rep Notification x -> Notification
forall x. Notification -> Rep Notification x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Notification x -> Notification
$cfrom :: forall x. Notification -> Rep Notification x
Generic)
    deriving anyclass ([Notification] -> Encoding
[Notification] -> Value
Notification -> Encoding
Notification -> Value
(Notification -> Value)
-> (Notification -> Encoding)
-> ([Notification] -> Value)
-> ([Notification] -> Encoding)
-> ToJSON Notification
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Notification] -> Encoding
$ctoEncodingList :: [Notification] -> Encoding
toJSONList :: [Notification] -> Value
$ctoJSONList :: [Notification] -> Value
toEncoding :: Notification -> Encoding
$ctoEncoding :: Notification -> Encoding
toJSON :: Notification -> Value
$ctoJSON :: Notification -> Value
ToJSON, Value -> Parser [Notification]
Value -> Parser Notification
(Value -> Parser Notification)
-> (Value -> Parser [Notification]) -> FromJSON Notification
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Notification]
$cparseJSONList :: Value -> Parser [Notification]
parseJSON :: Value -> Parser Notification
$cparseJSON :: Value -> Parser Notification
FromJSON)

instance Pretty Notification where
    pretty :: Notification -> Doc ann
pretty Notification{ContractInstanceId
notificationContractID :: ContractInstanceId
notificationContractID :: Notification -> ContractInstanceId
notificationContractID,EndpointDescription
notificationContractEndpoint :: EndpointDescription
notificationContractEndpoint :: Notification -> EndpointDescription
notificationContractEndpoint,Value
notificationContractArg :: Value
notificationContractArg :: Notification -> Value
notificationContractArg} =
        Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
hang Int
2 (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep
            [ Doc ann
"Instance:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ContractInstanceId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty ContractInstanceId
notificationContractID
            , Doc ann
"Endpoint:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> EndpointDescription -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty EndpointDescription
notificationContractEndpoint
            , Doc ann
"Argument:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Value -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow Value
notificationContractArg
            ]

data NotificationError =
    EndpointNotAvailable ContractInstanceId EndpointDescription
    | MoreThanOneEndpointAvailable ContractInstanceId EndpointDescription
    | InstanceDoesNotExist ContractInstanceId
    | NotificationJSONDecodeError EndpointDescription Aeson.Value String
    -- ^ Indicates that the target contract does not have the expected schema
    --
    -- TODO: SCP-2137
    -- Not currently used. As endpoint parameter decoding happends inside the Contract and
    -- a throwError is used is decoding failed.
    -- However, still valuable to be used by the PAB to throw an error is an endpoint
    -- could not be decoded.
    deriving stock (NotificationError -> NotificationError -> Bool
(NotificationError -> NotificationError -> Bool)
-> (NotificationError -> NotificationError -> Bool)
-> Eq NotificationError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NotificationError -> NotificationError -> Bool
$c/= :: NotificationError -> NotificationError -> Bool
== :: NotificationError -> NotificationError -> Bool
$c== :: NotificationError -> NotificationError -> Bool
Eq, Int -> NotificationError -> ShowS
[NotificationError] -> ShowS
NotificationError -> String
(Int -> NotificationError -> ShowS)
-> (NotificationError -> String)
-> ([NotificationError] -> ShowS)
-> Show NotificationError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NotificationError] -> ShowS
$cshowList :: [NotificationError] -> ShowS
show :: NotificationError -> String
$cshow :: NotificationError -> String
showsPrec :: Int -> NotificationError -> ShowS
$cshowsPrec :: Int -> NotificationError -> ShowS
Show, (forall x. NotificationError -> Rep NotificationError x)
-> (forall x. Rep NotificationError x -> NotificationError)
-> Generic NotificationError
forall x. Rep NotificationError x -> NotificationError
forall x. NotificationError -> Rep NotificationError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NotificationError x -> NotificationError
$cfrom :: forall x. NotificationError -> Rep NotificationError x
Generic)
    deriving anyclass ([NotificationError] -> Encoding
[NotificationError] -> Value
NotificationError -> Encoding
NotificationError -> Value
(NotificationError -> Value)
-> (NotificationError -> Encoding)
-> ([NotificationError] -> Value)
-> ([NotificationError] -> Encoding)
-> ToJSON NotificationError
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [NotificationError] -> Encoding
$ctoEncodingList :: [NotificationError] -> Encoding
toJSONList :: [NotificationError] -> Value
$ctoJSONList :: [NotificationError] -> Value
toEncoding :: NotificationError -> Encoding
$ctoEncoding :: NotificationError -> Encoding
toJSON :: NotificationError -> Value
$ctoJSON :: NotificationError -> Value
ToJSON, Value -> Parser [NotificationError]
Value -> Parser NotificationError
(Value -> Parser NotificationError)
-> (Value -> Parser [NotificationError])
-> FromJSON NotificationError
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [NotificationError]
$cparseJSONList :: Value -> Parser [NotificationError]
parseJSON :: Value -> Parser NotificationError
$cparseJSON :: Value -> Parser NotificationError
FromJSON)

instance Pretty NotificationError where
    pretty :: NotificationError -> Doc ann
pretty = \case
        EndpointNotAvailable ContractInstanceId
i EndpointDescription
ep -> Doc ann
"Endpoint" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> EndpointDescription -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty EndpointDescription
ep Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"not available on" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ContractInstanceId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty ContractInstanceId
i
        MoreThanOneEndpointAvailable ContractInstanceId
i EndpointDescription
ep -> Doc ann
"Endpoint" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> EndpointDescription -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty EndpointDescription
ep Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"is exposed more than once on" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ContractInstanceId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty ContractInstanceId
i
        InstanceDoesNotExist ContractInstanceId
i -> Doc ann
"Instance does not exist:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ContractInstanceId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty ContractInstanceId
i
        NotificationJSONDecodeError EndpointDescription
ep Value
vv String
e ->
                Doc ann
"Notification JSON decoding error:"
                    Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty String
e
                    Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
forall ann. Doc ann
colon
                    Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (ByteString -> String
BSL8.unpack (Value -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encodePretty Value
vv))
                    Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> EndpointDescription -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty EndpointDescription
ep

makeClassyPrisms ''NotificationError