{-# LANGUAGE DeriveAnyClass     #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts   #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE TemplateHaskell    #-}
{-

Events that we store in the database.

-}
module Plutus.PAB.Events
    ( PABEvent(..)
    , _UpdateContractInstanceState
    , _SubmitTx
    , _ActivateContract
    , _StopContract
    ) where

import Control.Lens.TH (makePrisms)
import Data.Aeson (FromJSON, ToJSON, Value)
import GHC.Generics (Generic)
import Ledger.Tx (CardanoTx, getCardanoTxId)
import Plutus.Contract.Effects (PABReq, PABResp)
import Plutus.Contract.State (ContractResponse)
import Plutus.PAB.Webserver.Types (ContractActivationArgs)
import Prettyprinter (Pretty, pretty, (<+>))
import Wallet.Types (ContractInstanceId)

-- | A structure which ties together all possible event types into one parent.
data PABEvent t =
    UpdateContractInstanceState !(ContractActivationArgs t) !ContractInstanceId !(ContractResponse Value Value PABResp PABReq) -- ^ Update the state of a contract instance
    | SubmitTx !CardanoTx -- ^ Send a transaction to the node
    | ActivateContract !(ContractActivationArgs t) !ContractInstanceId
    | StopContract !ContractInstanceId
    deriving stock (PABEvent t -> PABEvent t -> Bool
(PABEvent t -> PABEvent t -> Bool)
-> (PABEvent t -> PABEvent t -> Bool) -> Eq (PABEvent t)
forall t. Eq t => PABEvent t -> PABEvent t -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PABEvent t -> PABEvent t -> Bool
$c/= :: forall t. Eq t => PABEvent t -> PABEvent t -> Bool
== :: PABEvent t -> PABEvent t -> Bool
$c== :: forall t. Eq t => PABEvent t -> PABEvent t -> Bool
Eq, Int -> PABEvent t -> ShowS
[PABEvent t] -> ShowS
PABEvent t -> String
(Int -> PABEvent t -> ShowS)
-> (PABEvent t -> String)
-> ([PABEvent t] -> ShowS)
-> Show (PABEvent t)
forall t. Show t => Int -> PABEvent t -> ShowS
forall t. Show t => [PABEvent t] -> ShowS
forall t. Show t => PABEvent t -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PABEvent t] -> ShowS
$cshowList :: forall t. Show t => [PABEvent t] -> ShowS
show :: PABEvent t -> String
$cshow :: forall t. Show t => PABEvent t -> String
showsPrec :: Int -> PABEvent t -> ShowS
$cshowsPrec :: forall t. Show t => Int -> PABEvent t -> ShowS
Show, (forall x. PABEvent t -> Rep (PABEvent t) x)
-> (forall x. Rep (PABEvent t) x -> PABEvent t)
-> Generic (PABEvent t)
forall x. Rep (PABEvent t) x -> PABEvent t
forall x. PABEvent t -> Rep (PABEvent t) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall t x. Rep (PABEvent t) x -> PABEvent t
forall t x. PABEvent t -> Rep (PABEvent t) x
$cto :: forall t x. Rep (PABEvent t) x -> PABEvent t
$cfrom :: forall t x. PABEvent t -> Rep (PABEvent t) x
Generic)
    deriving anyclass ([PABEvent t] -> Value
[PABEvent t] -> Encoding
PABEvent t -> Value
PABEvent t -> Encoding
(PABEvent t -> Value)
-> (PABEvent t -> Encoding)
-> ([PABEvent t] -> Value)
-> ([PABEvent t] -> Encoding)
-> ToJSON (PABEvent t)
forall t. ToJSON t => [PABEvent t] -> Value
forall t. ToJSON t => [PABEvent t] -> Encoding
forall t. ToJSON t => PABEvent t -> Value
forall t. ToJSON t => PABEvent t -> Encoding
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [PABEvent t] -> Encoding
$ctoEncodingList :: forall t. ToJSON t => [PABEvent t] -> Encoding
toJSONList :: [PABEvent t] -> Value
$ctoJSONList :: forall t. ToJSON t => [PABEvent t] -> Value
toEncoding :: PABEvent t -> Encoding
$ctoEncoding :: forall t. ToJSON t => PABEvent t -> Encoding
toJSON :: PABEvent t -> Value
$ctoJSON :: forall t. ToJSON t => PABEvent t -> Value
ToJSON, Value -> Parser [PABEvent t]
Value -> Parser (PABEvent t)
(Value -> Parser (PABEvent t))
-> (Value -> Parser [PABEvent t]) -> FromJSON (PABEvent t)
forall t. FromJSON t => Value -> Parser [PABEvent t]
forall t. FromJSON t => Value -> Parser (PABEvent t)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [PABEvent t]
$cparseJSONList :: forall t. FromJSON t => Value -> Parser [PABEvent t]
parseJSON :: Value -> Parser (PABEvent t)
$cparseJSON :: forall t. FromJSON t => Value -> Parser (PABEvent t)
FromJSON)

makePrisms ''PABEvent

instance Pretty t => Pretty (PABEvent t) where
    pretty :: PABEvent t -> Doc ann
pretty = \case
        UpdateContractInstanceState ContractActivationArgs t
t ContractInstanceId
i ContractResponse Value Value PABResp PABReq
_ -> Doc ann
"Update state:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ContractActivationArgs t -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty ContractActivationArgs t
t 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
        SubmitTx CardanoTx
t                        -> Doc ann
"SubmitTx:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TxId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (CardanoTx -> TxId
getCardanoTxId CardanoTx
t)
        ActivateContract ContractActivationArgs t
_ ContractInstanceId
i              -> Doc ann
"Start contract 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
i
        StopContract ContractInstanceId
i                    -> Doc ann
"Stop contract 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
i