{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Plutus.PAB.Effects.Contract(
PABContract(..)
, requests
, ContractEffect(..)
, initialState
, updateContract
, ContractStore(..)
, putState
, getState
, getDefinition
, getActiveContracts
, getContracts
, putStartInstance
, putStopInstance
, deleteState
, ContractDefinition(..)
, addDefinition
, getDefinitions
) where
import Control.Monad.Freer (Eff, Member, send)
import Data.Aeson (Value)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Proxy (Proxy (..))
import Plutus.Contract.Effects (PABReq, PABResp)
import Plutus.Contract.Resumable (Request, Response)
import Plutus.Contract.State (ContractResponse)
import Plutus.Contract.State qualified as C
import Plutus.PAB.Webserver.Types (ContractActivationArgs)
import Wallet.Types (ContractActivityStatus (..), ContractInstanceId)
class PABContract contract where
type ContractDef contract
type State contract
serialisableState :: Proxy contract -> State contract -> ContractResponse Value Value PABResp PABReq
requests :: forall contract. PABContract contract => State contract -> [Request PABReq]
requests :: State contract -> [Request PABReq]
requests = ContractResponse Value Value PABResp PABReq -> [Request PABReq]
forall w e s h. ContractResponse w e s h -> [Request h]
C.hooks (ContractResponse Value Value PABResp PABReq -> [Request PABReq])
-> (State contract -> ContractResponse Value Value PABResp PABReq)
-> State contract
-> [Request PABReq]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy contract
-> State contract -> ContractResponse Value Value PABResp PABReq
forall contract.
PABContract contract =>
Proxy contract
-> State contract -> ContractResponse Value Value PABResp PABReq
serialisableState (Proxy contract
forall k (t :: k). Proxy t
Proxy @contract)
data ContractEffect t r where
InitialState :: PABContract t => ContractInstanceId -> ContractDef t -> ContractEffect t (State t)
UpdateContract :: PABContract t => ContractInstanceId -> ContractDef t -> State t -> Response PABResp -> ContractEffect t (State t)
initialState ::
forall t effs.
( Member (ContractEffect t) effs
, PABContract t
)
=> ContractInstanceId
-> ContractDef t
-> Eff effs (State t)
initialState :: ContractInstanceId -> ContractDef t -> Eff effs (State t)
initialState ContractInstanceId
i ContractDef t
def =
let ContractEffect t (State t)
command :: ContractEffect t (State t) = ContractInstanceId -> ContractDef t -> ContractEffect t (State t)
forall t.
PABContract t =>
ContractInstanceId -> ContractDef t -> ContractEffect t (State t)
InitialState ContractInstanceId
i ContractDef t
def
in ContractEffect t (State t) -> Eff effs (State t)
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractEffect t (State t)
command
updateContract ::
forall t effs.
( Member (ContractEffect t) effs
, PABContract t
)
=> ContractInstanceId
-> ContractDef t
-> State t
-> Response PABResp
-> Eff effs (State t)
updateContract :: ContractInstanceId
-> ContractDef t
-> State t
-> Response PABResp
-> Eff effs (State t)
updateContract ContractInstanceId
i ContractDef t
def State t
state Response PABResp
request =
let ContractEffect t (State t)
command :: ContractEffect t (State t) = ContractInstanceId
-> ContractDef t
-> State t
-> Response PABResp
-> ContractEffect t (State t)
forall t.
PABContract t =>
ContractInstanceId
-> ContractDef t
-> State t
-> Response PABResp
-> ContractEffect t (State t)
UpdateContract ContractInstanceId
i ContractDef t
def State t
state Response PABResp
request
in ContractEffect t (State t) -> Eff effs (State t)
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractEffect t (State t)
command
data ContractStore t r where
PutStartInstance :: ContractActivationArgs (ContractDef t) -> ContractInstanceId -> ContractStore t ()
PutState :: ContractActivationArgs (ContractDef t) -> ContractInstanceId -> State t -> ContractStore t ()
GetState :: ContractInstanceId -> ContractStore t (State t)
PutStopInstance :: ContractInstanceId -> ContractStore t ()
GetContracts :: Maybe ContractActivityStatus -> ContractStore t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
DeleteState :: ContractInstanceId -> ContractStore t ()
putStartInstance ::
forall t effs.
( Member (ContractStore t) effs
)
=> ContractActivationArgs (ContractDef t)
-> ContractInstanceId
-> Eff effs ()
putStartInstance :: ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> Eff effs ()
putStartInstance ContractActivationArgs (ContractDef t)
def ContractInstanceId
i =
let ContractStore t ()
command :: ContractStore t () = ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> ContractStore t ()
forall t.
ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> ContractStore t ()
PutStartInstance ContractActivationArgs (ContractDef t)
def ContractInstanceId
i
in ContractStore t () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore t ()
command
putStopInstance ::
forall t effs.
( Member (ContractStore t) effs
)
=> ContractInstanceId
-> Eff effs ()
putStopInstance :: ContractInstanceId -> Eff effs ()
putStopInstance ContractInstanceId
i =
let ContractStore t ()
command :: ContractStore t () = ContractInstanceId -> ContractStore t ()
forall t. ContractInstanceId -> ContractStore t ()
PutStopInstance ContractInstanceId
i
in ContractStore t () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore t ()
command
putState ::
forall t effs.
( Member (ContractStore t) effs
)
=> ContractActivationArgs (ContractDef t)
-> ContractInstanceId
-> State t
-> Eff effs ()
putState :: ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> State t -> Eff effs ()
putState ContractActivationArgs (ContractDef t)
def ContractInstanceId
i State t
state =
let ContractStore t ()
command :: ContractStore t () = ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> State t -> ContractStore t ()
forall t.
ContractActivationArgs (ContractDef t)
-> ContractInstanceId -> State t -> ContractStore t ()
PutState ContractActivationArgs (ContractDef t)
def ContractInstanceId
i State t
state
in ContractStore t () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore t ()
command
getState ::
forall t effs.
( Member (ContractStore t) effs
)
=> ContractInstanceId
-> Eff effs (State t)
getState :: ContractInstanceId -> Eff effs (State t)
getState ContractInstanceId
i =
let ContractStore t (State t)
command :: ContractStore t (State t) = ContractInstanceId -> ContractStore t (State t)
forall t. ContractInstanceId -> ContractStore t (State t)
GetState ContractInstanceId
i
in ContractStore t (State t) -> Eff effs (State t)
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore t (State t)
command
getActiveContracts ::
forall t effs.
( Member (ContractStore t) effs
)
=> Eff effs (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getActiveContracts :: Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getActiveContracts = Maybe ContractActivityStatus
-> Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
forall t (effs :: [* -> *]).
Member (ContractStore t) effs =>
Maybe ContractActivityStatus
-> Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getContracts @t (ContractActivityStatus -> Maybe ContractActivityStatus
forall a. a -> Maybe a
Just ContractActivityStatus
Active)
getContracts ::
forall t effs.
( Member (ContractStore t) effs
)
=> Maybe ContractActivityStatus
-> Eff effs (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getContracts :: Maybe ContractActivityStatus
-> Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getContracts Maybe ContractActivityStatus
mStatus =
let ContractStore
t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
command :: ContractStore t (Map ContractInstanceId (ContractActivationArgs (ContractDef t))) = Maybe ContractActivityStatus
-> ContractStore
t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
forall t.
Maybe ContractActivityStatus
-> ContractStore
t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
GetContracts Maybe ContractActivityStatus
mStatus
in ContractStore
t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
-> Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore
t (Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
command
deleteState ::
forall t effs.
( Member (ContractStore t) effs
)
=> ContractInstanceId
-> Eff effs ()
deleteState :: ContractInstanceId -> Eff effs ()
deleteState ContractInstanceId
i =
let ContractStore t ()
command :: ContractStore t () = ContractInstanceId -> ContractStore t ()
forall t. ContractInstanceId -> ContractStore t ()
DeleteState ContractInstanceId
i
in ContractStore t () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractStore t ()
command
getDefinition ::
forall t effs.
( Member (ContractStore t) effs)
=> ContractInstanceId
-> Eff effs (Maybe (ContractActivationArgs (ContractDef t)))
getDefinition :: ContractInstanceId
-> Eff effs (Maybe (ContractActivationArgs (ContractDef t)))
getDefinition ContractInstanceId
i = ContractInstanceId
-> Map ContractInstanceId (ContractActivationArgs (ContractDef t))
-> Maybe (ContractActivationArgs (ContractDef t))
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ContractInstanceId
i (Map ContractInstanceId (ContractActivationArgs (ContractDef t))
-> Maybe (ContractActivationArgs (ContractDef t)))
-> Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
-> Eff effs (Maybe (ContractActivationArgs (ContractDef t)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (effs :: [* -> *]).
Member (ContractStore t) effs =>
Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
forall t (effs :: [* -> *]).
Member (ContractStore t) effs =>
Eff
effs
(Map ContractInstanceId (ContractActivationArgs (ContractDef t)))
getActiveContracts @t)
data ContractDefinition t r where
AddDefinition :: ContractDef t -> ContractDefinition t ()
GetDefinitions :: ContractDefinition t [ContractDef t]
addDefinition ::
forall t effs.
( Member (ContractDefinition t) effs
)
=> ContractDef t
-> Eff effs ()
addDefinition :: ContractDef t -> Eff effs ()
addDefinition ContractDef t
def =
let command :: ContractDefinition t ()
command :: ContractDefinition t ()
command = ContractDef t -> ContractDefinition t ()
forall t. ContractDef t -> ContractDefinition t ()
AddDefinition ContractDef t
def
in ContractDefinition t () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send ContractDefinition t ()
command
getDefinitions ::
forall t effs.
( Member (ContractDefinition t) effs
)
=> Eff effs [ContractDef t]
getDefinitions :: Eff effs [ContractDef t]
getDefinitions = ContractDefinition t [ContractDef t] -> Eff effs [ContractDef t]
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send @(ContractDefinition t) ContractDefinition t [ContractDef t]
forall t. ContractDefinition t [ContractDef t]
GetDefinitions