{-# LANGUAGE AllowAmbiguousTypes   #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DerivingStrategies    #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeApplications      #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}
{-

The core of the PAB. Runs contract instances, handling their requests by
talking to external services (wallet backend, chain index) and forwarding
notifications from node and user endpoints to them.

The main entry point is 'runPAB'. It runs a 'PABAction t env' using the
@EffectHandlers@ provided. The @EffectHandlers@ makes it easy to customise
the handlers for external effects and for actually submitting requests
to compiled contracts. By choosing different @EffectHandlers@ we can use the
same @runPAB@ function for test and live environments.

A number @PABAction@s are defined in this module. The most important one is
@activateContract@, which starts a new contract instance.

Another important @PABAction@ is 'Plutus.PAB.Core.Server.startServer', which
starts a webserver that implements the API defined in
'Plutus.PAB.Webserver.API'.

-}
module Plutus.PAB.Core
    ( PABEffects
    , PABAction
    , EffectHandlers(..)
    , runPAB
    , runPAB'
    , PABEnvironment(..)
    -- * Contracts and instances
    , reportContractState
    , activateContract
    , activateContract'
    , callEndpointOnInstance
    , callEndpointOnInstance'
    , payToAddress
    , payToPaymentPublicKey
    -- * Agent threads
    , ContractInstanceEffects
    , handleAgentThread
    , stopInstance
    , removeInstance
    , instanceActivity
    -- * Querying the state
    , instanceState
    , instanceStateInternal
    , observableState
    , waitForState
    , waitForInstanceState
    , waitForInstanceStateWithResult
    , waitForTxStatusChange
    , waitForTxOutStatusChange
    , activeEndpoints
    , waitForEndpoint
    , yieldedExportTxs
    , currentSlot
    , waitUntilSlot
    , waitNSlots
    , activeContracts
    , finalResult
    , waitUntilFinished
    , valueAt
    , askUserEnv
    , askBlockchainEnv
    , askInstancesState
    , instancesWithStatuses
    -- * Run PAB effects in separate threads
    , PABRunner(..)
    , pabRunner
    -- * Effect handlers
    , handleMappedReader
    , handleUserEnvReader
    , handleBlockchainEnvReader
    , handleInstancesStateReader
    , timed
    ) where

import Cardano.Node.Emulator.Internal.Node (Params)
import Cardano.Wallet.LocalClient.ExportTx (ExportTx)
import Control.Applicative (empty, (<|>))
import Control.Concurrent.STM (STM)
import Control.Concurrent.STM qualified as STM
import Control.Lens (_2, view)
import Control.Monad (forM, guard, void)
import Control.Monad.Freer (Eff, LastMember, Member, interpret, reinterpret, runM, send, subsume, type (~>))
import Control.Monad.Freer.Error (Error, runError, throwError)
import Control.Monad.Freer.Extras.Log (LogMessage, LogMsg (LMessage), LogObserve, handleObserveLog, mapLog)
import Control.Monad.Freer.Extras.Modify qualified as Modify
import Control.Monad.Freer.Reader (Reader (Ask), ask, asks, runReader)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson qualified as JSON
import Data.Foldable (traverse_)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (isJust)
import Data.Proxy (Proxy (Proxy))
import Data.Set (Set)
import Data.Text (Text)
import Ledger (TxOutRef)
import Ledger.Address (Address, PaymentPubKeyHash, pubKeyHashAddress)
import Ledger.Tx (CardanoTx, decoratedTxOutValue)
import Ledger.Value.CardanoAPI (fromCardanoValue)
import Plutus.ChainIndex (ChainIndexQueryEffect, RollbackState (Unknown), TxOutStatus, TxStatus)
import Plutus.ChainIndex qualified as ChainIndex
import Plutus.ChainIndex.Api qualified as ChainIndex
import Plutus.Contract.Effects (ActiveEndpoint (ActiveEndpoint, aeDescription), PABReq)
import Plutus.PAB.Core.ContractInstance (ContractInstanceMsg, ContractInstanceState)
import Plutus.PAB.Core.ContractInstance qualified as ContractInstance
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active, Done, Stopped), BlockchainEnv, InstancesState,
                                             OpenEndpoint)
import Plutus.PAB.Core.ContractInstance.STM qualified as Instances
import Plutus.PAB.Effects.Contract (ContractDefinition, ContractEffect, ContractStore, PABContract (ContractDef),
                                    getState)
import Plutus.PAB.Effects.Contract qualified as Contract
import Plutus.PAB.Effects.TimeEffect (TimeEffect (SystemTime), systemTime)
import Plutus.PAB.Effects.UUID (UUIDEffect, handleUUIDEffect)
import Plutus.PAB.Events.ContractInstanceState (PartiallyDecodedResponse, fromResp)
import Plutus.PAB.Monitoring.PABLogMsg (PABMultiAgentMsg (ContractInstanceLog, EmulatorMsg))
import Plutus.PAB.Timeout (Timeout (..))
import Plutus.PAB.Timeout qualified as Timeout
import Plutus.PAB.Types (PABError (ContractInstanceNotFound, InstanceAlreadyStopped, OtherError, WalletError))
import Plutus.PAB.Webserver.Types (ContractActivationArgs (ContractActivationArgs, caID, caWallet))
import Plutus.Script.Utils.Value (Value)
import Plutus.V1.Ledger.Api (TxId)
import Wallet.API (Slot)
import Wallet.API qualified as WAPI
import Wallet.Effects (NodeClientEffect, WalletEffect)
import Wallet.Emulator.LogMessages (RequestHandlerLogMsg, TxBalanceMsg)
import Wallet.Emulator.MultiAgent (EmulatorEvent' (WalletEvent), EmulatorTimeEvent (EmulatorTimeEvent))
import Wallet.Emulator.Wallet (Wallet, WalletEvent (GenericLog, RequestHandlerLog, TxBalanceLog), mockWalletAddress)
import Wallet.Types (ContractActivityStatus, ContractInstanceId, EndpointDescription (EndpointDescription),
                     NotificationError)
import Wallet.Types qualified as Wallet

-- | Effects that are available in 'PABAction's.
type PABEffects t env =
    '[ ContractStore t
     , ContractEffect t
     , ContractDefinition t
     , LogMsg (PABMultiAgentMsg t)
     , TimeEffect
     , Reader (PABEnvironment t env)
     , Error PABError
     , IO
     ]

-- | Actions that are run by the PAB.
type PABAction t env a = Eff (PABEffects t env) a

-- | A handler for 'PABAction' types.
newtype PABRunner t env = PABRunner { PABRunner t env
-> forall a. PABAction t env a -> IO (Either PABError a)
runPABAction :: forall a. PABAction t env a -> IO (Either PABError a) }

-- | Get a 'PABRunner' that uses the current environment.
pabRunner :: forall t env. PABAction t env (PABRunner t env)
pabRunner :: PABAction t env (PABRunner t env)
pabRunner = do
    h :: PABEnvironment t env
h@PABEnvironment{effectHandlers :: forall t env. PABEnvironment t env -> EffectHandlers t env
effectHandlers=EffectHandlers{forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member TimeEffect effs, Member (Error PABError) effs,
    LastMember IO effs) =>
   Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages, forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect, forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect, forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect}} <- forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff effs (PABEnvironment t env)
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask @(PABEnvironment t env)
    PABRunner t env -> PABAction t env (PABRunner t env)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PABRunner t env -> PABAction t env (PABRunner t env))
-> PABRunner t env -> PABAction t env (PABRunner t env)
forall a b. (a -> b) -> a -> b
$ (forall a. PABAction t env a -> IO (Either PABError a))
-> PABRunner t env
forall t env.
(forall a. PABAction t env a -> IO (Either PABError a))
-> PABRunner t env
PABRunner ((forall a. PABAction t env a -> IO (Either PABError a))
 -> PABRunner t env)
-> (forall a. PABAction t env a -> IO (Either PABError a))
-> PABRunner t env
forall a b. (a -> b) -> a -> b
$ \PABAction t env a
action -> do
        Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall (m :: * -> *) a. Monad m => Eff '[m] a -> m a
runM
            (Eff '[IO] (Either PABError a) -> IO (Either PABError a))
-> Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall a b. (a -> b) -> a -> b
$ Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall e (effs :: [* -> *]) a.
Eff (Error e : effs) a -> Eff effs (Either e a)
runError
            (Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a))
-> Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall a b. (a -> b) -> a -> b
$ PABEnvironment t env
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader PABEnvironment t env
h
            (Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Error PABError, IO] a)
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ (TimeEffect
 ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO])
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO]
   ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO]
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall t env (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
forall (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
handleTimeEffect @t @env)
            (Eff
   '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages
            (Eff
   '[LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect
            (Eff
   '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractEffect t, ContractDefinition t,
    LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect
            (Eff
   '[ContractEffect t, ContractDefinition t,
     LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ PABAction t env a
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect PABAction t env a
action

-- | Shared data that is needed by all PAB threads.
data PABEnvironment t env =
    PABEnvironment
        { PABEnvironment t env -> InstancesState
instancesState    :: InstancesState
        -- | How long to wait for an endpoint to become active before throwing the
        --   'EndpointNotAvailable' error.
        , PABEnvironment t env -> Timeout
endpointTimeout   :: Timeout
        -- | How long to wait when querying the status for an invoked endpoint before throwing the
        --   a timeout error.
        , PABEnvironment t env -> Timeout
waitStatusTimeout :: Timeout
        , PABEnvironment t env -> BlockchainEnv
blockchainEnv     :: BlockchainEnv
        , PABEnvironment t env -> env
appEnv            :: env
        , PABEnvironment t env -> EffectHandlers t env
effectHandlers    :: EffectHandlers t env
        }

-- | Top-level entry point. Run a 'PABAction', using the 'EffectHandlers' to
--   deal with logs, startup and shutdown, contract requests and communication
--   with external services.
runPAB ::
    forall t env a.
    Timeout
    -> Timeout
    -> EffectHandlers t env
    -> PABAction t env a
    -> IO (Either PABError a)
runPAB :: Timeout
-> Timeout
-> EffectHandlers t env
-> PABAction t env a
-> IO (Either PABError a)
runPAB Timeout
endpointTimeout Timeout
waitStatusTimeout EffectHandlers t env
effectHandlers PABAction t env a
action = Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall (m :: * -> *) a. Monad m => Eff '[m] a -> m a
runM (Eff '[IO] (Either PABError a) -> IO (Either PABError a))
-> Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall a b. (a -> b) -> a -> b
$ Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall e (effs :: [* -> *]) a.
Eff (Error e : effs) a -> Eff effs (Either e a)
runError (Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a))
-> Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall a b. (a -> b) -> a -> b
$ do
    let EffectHandlers { forall (effs :: [* -> *]).
(Member (Error PABError) effs, LastMember IO effs) =>
Eff effs (InstancesState, BlockchainEnv, env)
initialiseEnvironment :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Error PABError) effs, LastMember IO effs) =>
   Eff effs (InstancesState, BlockchainEnv, env)
initialiseEnvironment :: forall (effs :: [* -> *]).
(Member (Error PABError) effs, LastMember IO effs) =>
Eff effs (InstancesState, BlockchainEnv, env)
initialiseEnvironment
                       , PABAction t env ()
onStartup :: forall t env. EffectHandlers t env -> PABAction t env ()
onStartup :: PABAction t env ()
onStartup
                       , PABAction t env ()
onShutdown :: forall t env. EffectHandlers t env -> PABAction t env ()
onShutdown :: PABAction t env ()
onShutdown
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member TimeEffect effs, Member (Error PABError) effs,
    LastMember IO effs) =>
   Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect
                       } = EffectHandlers t env
effectHandlers
    (InstancesState
instancesState, BlockchainEnv
blockchainEnv, env
appEnv) <- Eff '[Error PABError, IO] (InstancesState, BlockchainEnv, env)
forall (effs :: [* -> *]).
(Member (Error PABError) effs, LastMember IO effs) =>
Eff effs (InstancesState, BlockchainEnv, env)
initialiseEnvironment
    let env :: PABEnvironment t env
env = PABEnvironment :: forall t env.
InstancesState
-> Timeout
-> Timeout
-> BlockchainEnv
-> env
-> EffectHandlers t env
-> PABEnvironment t env
PABEnvironment{InstancesState
instancesState :: InstancesState
instancesState :: InstancesState
instancesState, BlockchainEnv
blockchainEnv :: BlockchainEnv
blockchainEnv :: BlockchainEnv
blockchainEnv, env
appEnv :: env
appEnv :: env
appEnv, EffectHandlers t env
effectHandlers :: EffectHandlers t env
effectHandlers :: EffectHandlers t env
effectHandlers, Timeout
endpointTimeout :: Timeout
endpointTimeout :: Timeout
endpointTimeout, Timeout
waitStatusTimeout :: Timeout
waitStatusTimeout :: Timeout
waitStatusTimeout}

    PABEnvironment t env
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader PABEnvironment t env
env (Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Error PABError, IO] a)
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ (TimeEffect
 ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO])
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO]
   ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO]
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall t env (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
forall (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
handleTimeEffect @t @env)
                  (Eff
   '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages
                  (Eff
   '[LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect
                  (Eff
   '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractEffect t, ContractDefinition t,
    LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect
                  (Eff
   '[ContractEffect t, ContractDefinition t,
     LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ PABAction t env a
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect
                  (PABAction t env a
 -> Eff
      '[ContractEffect t, ContractDefinition t,
        LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> PABAction t env a
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ do PABAction t env ()
onStartup
                       a
result <- PABAction t env a
action
                       PABAction t env ()
onShutdown
                       a -> PABAction t env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result

-- | Run a PABAction in the context of the given environment.
-- TODO: Clean it up so there is less duplication of the above.
runPAB' ::
    forall t env a.
    PABEnvironment t env
    -> PABAction t env a
    -> IO (Either PABError a)
runPAB' :: PABEnvironment t env -> PABAction t env a -> IO (Either PABError a)
runPAB' env :: PABEnvironment t env
env@PABEnvironment{EffectHandlers t env
effectHandlers :: EffectHandlers t env
effectHandlers :: forall t env. PABEnvironment t env -> EffectHandlers t env
effectHandlers} PABAction t env a
action = Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall (m :: * -> *) a. Monad m => Eff '[m] a -> m a
runM (Eff '[IO] (Either PABError a) -> IO (Either PABError a))
-> Eff '[IO] (Either PABError a) -> IO (Either PABError a)
forall a b. (a -> b) -> a -> b
$ Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall e (effs :: [* -> *]) a.
Eff (Error e : effs) a -> Eff effs (Either e a)
runError (Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a))
-> Eff '[Error PABError, IO] a -> Eff '[IO] (Either PABError a)
forall a b. (a -> b) -> a -> b
$ do
    let EffectHandlers { PABAction t env ()
onStartup :: PABAction t env ()
onStartup :: forall t env. EffectHandlers t env -> PABAction t env ()
onStartup
                       , PABAction t env ()
onShutdown :: PABAction t env ()
onShutdown :: forall t env. EffectHandlers t env -> PABAction t env ()
onShutdown
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member TimeEffect effs, Member (Error PABError) effs,
    LastMember IO effs) =>
   Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect
                       , forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect
                       } = EffectHandlers t env
effectHandlers

    PABEnvironment t env
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader PABEnvironment t env
env (Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Error PABError, IO] a)
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ (TimeEffect
 ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO])
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO]
   ~> Eff '[Reader (PABEnvironment t env), Error PABError, IO]
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall t env (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
forall (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
handleTimeEffect @t @env)
                  (Eff
   '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
 -> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
-> Eff '[Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member TimeEffect effs, Member (Error PABError) effs,
 LastMember IO effs) =>
Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages
                  (Eff
   '[LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a)
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[TimeEffect, Reader (PABEnvironment t env), Error PABError, IO] a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect
                  (Eff
   '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ Eff
  '[ContractEffect t, ContractDefinition t,
    LogMsg (PABMultiAgentMsg t), TimeEffect,
    Reader (PABEnvironment t env), Error PABError, IO]
  a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect
                  (Eff
   '[ContractEffect t, ContractDefinition t,
     LogMsg (PABMultiAgentMsg t), TimeEffect,
     Reader (PABEnvironment t env), Error PABError, IO]
   a
 -> Eff
      '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
-> Eff
     '[ContractDefinition t, LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ PABAction t env a
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect
                  (PABAction t env a
 -> Eff
      '[ContractEffect t, ContractDefinition t,
        LogMsg (PABMultiAgentMsg t), TimeEffect,
        Reader (PABEnvironment t env), Error PABError, IO]
      a)
-> PABAction t env a
-> Eff
     '[ContractEffect t, ContractDefinition t,
       LogMsg (PABMultiAgentMsg t), TimeEffect,
       Reader (PABEnvironment t env), Error PABError, IO]
     a
forall a b. (a -> b) -> a -> b
$ do
                    PABAction t env ()
onStartup
                    a
result <- PABAction t env a
action
                    PABAction t env ()
onShutdown
                    a -> PABAction t env a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result

-- | Start a new instance of a contract, with a given state. Note that we skip
-- running the effects that push the state into the contract store, because we
-- assume that if you're providing the state, it's already present in the
-- store.
activateContract' ::
    forall t env.
    ( PABContract t
    )
    => ContractInstanceState t
    -> ContractInstanceId
    -> Wallet
    -> ContractDef t
    -> PABAction t env ContractInstanceId
activateContract' :: ContractInstanceState t
-> ContractInstanceId
-> Wallet
-> ContractDef t
-> PABAction t env ContractInstanceId
activateContract' ContractInstanceState t
state ContractInstanceId
cid Wallet
w ContractDef t
contractDef = do
    PABRunner{forall a. PABAction t env a -> IO (Either PABError a)
runPABAction :: forall a. PABAction t env a -> IO (Either PABError a)
runPABAction :: forall t env.
PABRunner t env
-> forall a. PABAction t env a -> IO (Either PABError a)
runPABAction} <- PABAction t env (PABRunner t env)
forall t env. PABAction t env (PABRunner t env)
pabRunner

    let handler :: forall a. ContractInstanceId -> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
        handler :: ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
handler ContractInstanceId
_ Eff (ContractInstanceEffects t env '[IO]) a
x = (Either PABError a -> a) -> IO (Either PABError a) -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((PABError -> a) -> (a -> a) -> Either PABError a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> (PABError -> [Char]) -> PABError -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PABError -> [Char]
forall a. Show a => a -> [Char]
show) a -> a
forall a. a -> a
id) (PABAction t env a -> IO (Either PABError a)
forall a. PABAction t env a -> IO (Either PABError a)
runPABAction (PABAction t env a -> IO (Either PABError a))
-> PABAction t env a -> IO (Either PABError a)
forall a b. (a -> b) -> a -> b
$ Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
w (ContractInstanceId -> Maybe ContractInstanceId
forall a. a -> Maybe a
Just ContractInstanceId
cid) Eff (ContractInstanceEffects t env '[IO]) a
x)
        args :: ContractActivationArgs (ContractDef t)
        args :: ContractActivationArgs (ContractDef t)
args = ContractActivationArgs :: forall t. t -> Maybe Wallet -> ContractActivationArgs t
ContractActivationArgs{caWallet :: Maybe Wallet
caWallet = Wallet -> Maybe Wallet
forall a. a -> Maybe a
Just Wallet
w, caID :: ContractDef t
caID = ContractDef t
contractDef}
    Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
-> PABAction t env ContractInstanceId
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
w (ContractInstanceId -> Maybe ContractInstanceId
forall a. a -> Maybe a
Just ContractInstanceId
cid)
        (Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
 -> PABAction t env ContractInstanceId)
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
-> PABAction t env ContractInstanceId
forall a b. (a -> b) -> a -> b
$ ContractInstanceState t
-> ContractInstanceId
-> (ContractInstanceId
    -> Eff (ContractInstanceEffects t env '[IO]) ~> IO)
-> ContractActivationArgs (ContractDef t)
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
forall t (m :: * -> *) (appBackend :: [* -> *]) (effs :: [* -> *]).
(Member (Reader InstancesState) effs, PABContract t,
 AppBackendConstraints t m appBackend,
 LastMember m (Reader ContractInstanceId : appBackend),
 LastMember m effs) =>
ContractInstanceState t
-> ContractInstanceId
-> (ContractInstanceId -> Eff appBackend ~> IO)
-> ContractActivationArgs (ContractDef t)
-> Eff effs ContractInstanceId
ContractInstance.startContractInstanceThread' @t @IO @(ContractInstanceEffects t env '[IO]) ContractInstanceState t
state ContractInstanceId
cid ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) ~> IO
forall a.
ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
handler ContractActivationArgs (ContractDef t)
args

-- | Start a new instance of a contract
activateContract :: forall t env. PABContract t => Wallet -> ContractDef t -> PABAction t env ContractInstanceId
activateContract :: Wallet -> ContractDef t -> PABAction t env ContractInstanceId
activateContract Wallet
w ContractDef t
contractDef = do
    PABRunner{forall a. PABAction t env a -> IO (Either PABError a)
runPABAction :: forall a. PABAction t env a -> IO (Either PABError a)
runPABAction :: forall t env.
PABRunner t env
-> forall a. PABAction t env a -> IO (Either PABError a)
runPABAction} <- PABAction t env (PABRunner t env)
forall t env. PABAction t env (PABRunner t env)
pabRunner

    let handler :: forall a. ContractInstanceId -> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
        handler :: ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
handler ContractInstanceId
cid Eff (ContractInstanceEffects t env '[IO]) a
x = (Either PABError a -> a) -> IO (Either PABError a) -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((PABError -> a) -> (a -> a) -> Either PABError a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> (PABError -> [Char]) -> PABError -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PABError -> [Char]
forall a. Show a => a -> [Char]
show) a -> a
forall a. a -> a
id) (PABAction t env a -> IO (Either PABError a)
forall a. PABAction t env a -> IO (Either PABError a)
runPABAction (PABAction t env a -> IO (Either PABError a))
-> PABAction t env a -> IO (Either PABError a)
forall a b. (a -> b) -> a -> b
$ Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
w (ContractInstanceId -> Maybe ContractInstanceId
forall a. a -> Maybe a
Just ContractInstanceId
cid) Eff (ContractInstanceEffects t env '[IO]) a
x)
        args :: ContractActivationArgs (ContractDef t)
        args :: ContractActivationArgs (ContractDef t)
args = ContractActivationArgs :: forall t. t -> Maybe Wallet -> ContractActivationArgs t
ContractActivationArgs{caWallet :: Maybe Wallet
caWallet = Wallet -> Maybe Wallet
forall a. a -> Maybe a
Just Wallet
w, caID :: ContractDef t
caID = ContractDef t
contractDef}
    Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
-> PABAction t env ContractInstanceId
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
w Maybe ContractInstanceId
forall a. Maybe a
Nothing
        (Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
 -> PABAction t env ContractInstanceId)
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
-> PABAction t env ContractInstanceId
forall a b. (a -> b) -> a -> b
$ (ContractInstanceId
 -> Eff (ContractInstanceEffects t env '[IO]) ~> IO)
-> ContractActivationArgs (ContractDef t)
-> Eff (ContractInstanceEffects t env '[IO]) ContractInstanceId
forall t (m :: * -> *) (appBackend :: [* -> *]) (effs :: [* -> *]).
(Member (LogMsg (ContractInstanceMsg t)) effs,
 Member UUIDEffect effs, Member (ContractEffect t) effs,
 Member (ContractStore t) effs, Member (Reader InstancesState) effs,
 PABContract t, AppBackendConstraints t m appBackend,
 LastMember m (Reader ContractInstanceId : appBackend),
 LastMember m effs) =>
(ContractInstanceId -> Eff appBackend ~> IO)
-> ContractActivationArgs (ContractDef t)
-> Eff effs ContractInstanceId
ContractInstance.activateContractSTM @t @IO @(ContractInstanceEffects t env '[IO]) ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) ~> IO
forall a.
ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a -> IO a
handler ContractActivationArgs (ContractDef t)
args

-- | Call a named endpoint on a contract instance. Waits if the endpoint is not
--   available.
callEndpointOnInstance ::
    forall t env a.
    ( JSON.ToJSON a
    )
    => ContractInstanceId
    -> String
    -> a
    -> PABAction t env (Maybe NotificationError)
callEndpointOnInstance :: ContractInstanceId
-> [Char] -> a -> PABAction t env (Maybe NotificationError)
callEndpointOnInstance ContractInstanceId
instanceID [Char]
ep a
value = do
    InstancesState
state <- (PABEnvironment t env -> InstancesState)
-> Eff (PABEffects t env) InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState
    TMVar ()
timeoutVar <-
        (PABEnvironment t env -> Timeout) -> Eff (PABEffects t env) Timeout
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> Timeout
forall t env. PABEnvironment t env -> Timeout
endpointTimeout Eff (PABEffects t env) Timeout
-> (Timeout -> Eff (PABEffects t env) (TMVar ()))
-> Eff (PABEffects t env) (TMVar ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
        (\Timeout
t -> IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ()))
-> IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ())
forall a b. (a -> b) -> a -> b
$ if Maybe Second -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Second -> Bool) -> Maybe Second -> Bool
forall a b. (a -> b) -> a -> b
$ Timeout -> Maybe Second
unTimeout Timeout
t then Timeout -> IO (TMVar ())
Timeout.startTimeout Timeout
t else () -> IO (TMVar ())
forall a. a -> IO (TMVar a)
STM.newTMVarIO ())
    IO (Maybe NotificationError)
-> PABAction t env (Maybe NotificationError)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe NotificationError)
 -> PABAction t env (Maybe NotificationError))
-> IO (Maybe NotificationError)
-> PABAction t env (Maybe NotificationError)
forall a b. (a -> b) -> a -> b
$
        TMVar ()
-> InstancesState
-> EndpointDescription
-> Value
-> ContractInstanceId
-> IO (STM (Maybe NotificationError))
Instances.callEndpointOnInstanceTimeout
            TMVar ()
timeoutVar
            InstancesState
state
            ([Char] -> EndpointDescription
EndpointDescription [Char]
ep)
            (a -> Value
forall a. ToJSON a => a -> Value
JSON.toJSON a
value)
            ContractInstanceId
instanceID
        IO (STM (Maybe NotificationError))
-> (STM (Maybe NotificationError) -> IO (Maybe NotificationError))
-> IO (Maybe NotificationError)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a. STM a -> IO a
STM.atomically

-- | The 'InstanceState' for the instance. Throws a 'ContractInstanceNotFound' error if the instance does not exist.
instanceStateInternal :: forall t env. ContractInstanceId -> PABAction t env Instances.InstanceState
instanceStateInternal :: ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId = do
    (PABEnvironment t env -> InstancesState)
-> Eff (PABEffects t env) InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState
        Eff (PABEffects t env) InstancesState
-> (InstancesState -> Eff (PABEffects t env) (Maybe InstanceState))
-> Eff (PABEffects t env) (Maybe InstanceState)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO (Maybe InstanceState)
-> Eff (PABEffects t env) (Maybe InstanceState)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe InstanceState)
 -> Eff (PABEffects t env) (Maybe InstanceState))
-> (InstancesState -> IO (Maybe InstanceState))
-> InstancesState
-> Eff (PABEffects t env) (Maybe InstanceState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractInstanceId -> InstancesState -> IO (Maybe InstanceState)
Instances.instanceState ContractInstanceId
instanceId
        Eff (PABEffects t env) (Maybe InstanceState)
-> (Maybe InstanceState -> PABAction t env InstanceState)
-> PABAction t env InstanceState
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= PABAction t env InstanceState
-> (InstanceState -> PABAction t env InstanceState)
-> Maybe InstanceState
-> PABAction t env InstanceState
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (PABError -> PABAction t env InstanceState
forall e (effs :: [* -> *]) a.
Member (Error e) effs =>
e -> Eff effs a
throwError (PABError -> PABAction t env InstanceState)
-> PABError -> PABAction t env InstanceState
forall a b. (a -> b) -> a -> b
$ ContractInstanceId -> PABError
ContractInstanceNotFound ContractInstanceId
instanceId) InstanceState -> PABAction t env InstanceState
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Delete the instance from the 'InstancesState' of the @PABEnvironment@ and from `InMemInstances` / Beam db.
removeInstance :: forall t env. ContractInstanceId -> PABAction t env ()
removeInstance :: ContractInstanceId -> PABAction t env ()
removeInstance ContractInstanceId
instanceId = do
    (PABEnvironment t env -> InstancesState)
-> Eff (PABEffects t env) InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState Eff (PABEffects t env) InstancesState
-> (InstancesState -> PABAction t env ()) -> PABAction t env ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> PABAction t env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> PABAction t env ())
-> (InstancesState -> IO ())
-> InstancesState
-> PABAction t env ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractInstanceId -> InstancesState -> IO ()
Instances.removeInstance ContractInstanceId
instanceId
    ContractInstanceId -> PABAction t env ()
forall t (effs :: [* -> *]).
Member (ContractStore t) effs =>
ContractInstanceId -> Eff effs ()
Contract.deleteState @t ContractInstanceId
instanceId

-- | Stop the instance.
stopInstance :: forall t env. ContractInstanceId -> PABAction t env ()
stopInstance :: ContractInstanceId -> PABAction t env ()
stopInstance ContractInstanceId
instanceId = do
    Instances.InstanceState{TVar Activity
issStatus :: InstanceState -> TVar Activity
issStatus :: TVar Activity
Instances.issStatus, TMVar ()
issStop :: InstanceState -> TMVar ()
issStop :: TMVar ()
Instances.issStop} <- ContractInstanceId -> PABAction t env InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId
    Maybe PABError
r' <- IO (Maybe PABError) -> Eff (PABEffects t env) (Maybe PABError)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe PABError) -> Eff (PABEffects t env) (Maybe PABError))
-> IO (Maybe PABError) -> Eff (PABEffects t env) (Maybe PABError)
forall a b. (a -> b) -> a -> b
$ STM (Maybe PABError) -> IO (Maybe PABError)
forall a. STM a -> IO a
STM.atomically (STM (Maybe PABError) -> IO (Maybe PABError))
-> STM (Maybe PABError) -> IO (Maybe PABError)
forall a b. (a -> b) -> a -> b
$ do
            Activity
status <- TVar Activity -> STM Activity
forall a. TVar a -> STM a
STM.readTVar TVar Activity
issStatus
            case Activity
status of
                Activity
Active -> TMVar () -> () -> STM ()
forall a. TMVar a -> a -> STM ()
STM.putTMVar TMVar ()
issStop () STM () -> STM (Maybe PABError) -> STM (Maybe PABError)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe PABError -> STM (Maybe PABError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe PABError
forall a. Maybe a
Nothing
                Activity
_      -> Maybe PABError -> STM (Maybe PABError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PABError -> Maybe PABError
forall a. a -> Maybe a
Just (PABError -> Maybe PABError) -> PABError -> Maybe PABError
forall a b. (a -> b) -> a -> b
$ ContractInstanceId -> PABError
InstanceAlreadyStopped ContractInstanceId
instanceId)
    (PABError -> Eff (PABEffects t env) Any)
-> Maybe PABError -> PABAction t env ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ PABError -> Eff (PABEffects t env) Any
forall e (effs :: [* -> *]) a.
Member (Error e) effs =>
e -> Eff effs a
throwError Maybe PABError
r'

-- | The 'Activity' of the instance.
instanceActivity :: forall t env. ContractInstanceId -> PABAction t env Activity
instanceActivity :: ContractInstanceId -> PABAction t env Activity
instanceActivity ContractInstanceId
instanceId = do
    Instances.InstanceState{TVar Activity
issStatus :: TVar Activity
issStatus :: InstanceState -> TVar Activity
Instances.issStatus} <- ContractInstanceId -> PABAction t env InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId
    IO Activity -> PABAction t env Activity
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Activity -> PABAction t env Activity)
-> IO Activity -> PABAction t env Activity
forall a b. (a -> b) -> a -> b
$ TVar Activity -> IO Activity
forall a. TVar a -> IO a
STM.readTVarIO TVar Activity
issStatus

-- | Call a named endpoint on a contract instance. Fails immediately if the
--   endpoint is not available.
callEndpointOnInstance' ::
    forall t env a.
    ( JSON.ToJSON a
    )
    => ContractInstanceId
    -> String
    -> a
    -> PABAction t env (Maybe NotificationError)
callEndpointOnInstance' :: ContractInstanceId
-> [Char] -> a -> PABAction t env (Maybe NotificationError)
callEndpointOnInstance' ContractInstanceId
instanceID [Char]
ep a
value = do
    InstancesState
state <- (PABEnvironment t env -> InstancesState)
-> Eff (PABEffects t env) InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState
    IO (Maybe NotificationError)
-> PABAction t env (Maybe NotificationError)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
        (InstancesState
-> EndpointDescription
-> Value
-> ContractInstanceId
-> IO (STM (Maybe NotificationError))
Instances.callEndpointOnInstance InstancesState
state ([Char] -> EndpointDescription
EndpointDescription [Char]
ep) (a -> Value
forall a. ToJSON a => a -> Value
JSON.toJSON a
value) ContractInstanceId
instanceID IO (STM (Maybe NotificationError))
-> (STM (Maybe NotificationError) -> IO (Maybe NotificationError))
-> IO (Maybe NotificationError)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a. STM a -> IO a
STM.atomically)

-- | Make a payment.
payToAddress :: Params -> ContractInstanceId -> Wallet -> Address -> Value -> PABAction t env CardanoTx
payToAddress :: Params
-> ContractInstanceId
-> Wallet
-> Address
-> Value
-> PABAction t env CardanoTx
payToAddress Params
params ContractInstanceId
cid Wallet
source Address
target Value
amount =
    Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) CardanoTx
-> PABAction t env CardanoTx
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
source (ContractInstanceId -> Maybe ContractInstanceId
forall a. a -> Maybe a
Just ContractInstanceId
cid)
        (Eff (ContractInstanceEffects t env '[IO]) CardanoTx
 -> PABAction t env CardanoTx)
-> Eff (ContractInstanceEffects t env '[IO]) CardanoTx
-> PABAction t env CardanoTx
forall a b. (a -> b) -> a -> b
$ (WalletAPIError -> PABError)
-> Eff (Error WalletAPIError : ContractInstanceEffects t env '[IO])
   ~> Eff (ContractInstanceEffects t env '[IO])
forall e f (effs :: [* -> *]).
Member (Error f) effs =>
(e -> f) -> Eff (Error e : effs) ~> Eff effs
Modify.wrapError WalletAPIError -> PABError
WalletError
        (Eff
   (Error WalletAPIError : ContractInstanceEffects t env '[IO])
   CardanoTx
 -> Eff (ContractInstanceEffects t env '[IO]) CardanoTx)
-> Eff
     (Error WalletAPIError : ContractInstanceEffects t env '[IO])
     CardanoTx
-> Eff (ContractInstanceEffects t env '[IO]) CardanoTx
forall a b. (a -> b) -> a -> b
$ Params
-> SlotRange
-> Value
-> Address
-> Eff
     (Error WalletAPIError : ContractInstanceEffects t env '[IO])
     CardanoTx
forall (effs :: [* -> *]).
(Member WalletEffect effs, Member (Error WalletAPIError) effs,
 Member (LogMsg Text) effs,
 Member (LogMsg RequestHandlerLogMsg) effs) =>
Params -> SlotRange -> Value -> Address -> Eff effs CardanoTx
WAPI.payToAddress Params
params SlotRange
WAPI.defaultSlotRange Value
amount Address
target

-- | Make a payment to a payment public key.
payToPaymentPublicKey :: Params -> ContractInstanceId -> Wallet -> PaymentPubKeyHash -> Value -> PABAction t env CardanoTx
payToPaymentPublicKey :: Params
-> ContractInstanceId
-> Wallet
-> PaymentPubKeyHash
-> Value
-> PABAction t env CardanoTx
payToPaymentPublicKey Params
params ContractInstanceId
cid Wallet
source PaymentPubKeyHash
target =
    Params
-> ContractInstanceId
-> Wallet
-> Address
-> Value
-> PABAction t env CardanoTx
forall t env.
Params
-> ContractInstanceId
-> Wallet
-> Address
-> Value
-> PABAction t env CardanoTx
payToAddress Params
params ContractInstanceId
cid Wallet
source (PaymentPubKeyHash -> Maybe StakingCredential -> Address
pubKeyHashAddress PaymentPubKeyHash
target Maybe StakingCredential
forall a. Maybe a
Nothing)

-- | Effects available to contract instances with access to external services.
type ContractInstanceEffects t env effs =
    ContractEffect t
    ': ContractStore t
    ': WalletEffect
    ': ChainIndexQueryEffect
    ': NodeClientEffect
    ': UUIDEffect
    ': LogMsg TxBalanceMsg
    ': LogMsg RequestHandlerLogMsg
    ': LogMsg (ContractInstanceMsg t)
    ': LogObserve (LogMessage Text)
    ': LogMsg Text
    ': Error PABError
    ': TimeEffect
    ': Reader BlockchainEnv
    ': Reader InstancesState
    ': Reader (PABEnvironment t env)
    ': Reader Wallet
    ': effs

-- | Handle an action with 'ContractInstanceEffects' in the context of a wallet.
handleAgentThread ::
    forall t env a.
    Wallet
    -> Maybe ContractInstanceId
    -> Eff (ContractInstanceEffects t env '[IO]) a
    -> PABAction t env a
handleAgentThread :: Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
wallet Maybe ContractInstanceId
cidM Eff (ContractInstanceEffects t env '[IO]) a
action = do
    PABEnvironment{EffectHandlers t env
effectHandlers :: EffectHandlers t env
effectHandlers :: forall t env. PABEnvironment t env -> EffectHandlers t env
effectHandlers, BlockchainEnv
blockchainEnv :: BlockchainEnv
blockchainEnv :: forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv, InstancesState
instancesState :: InstancesState
instancesState :: forall t env. PABEnvironment t env -> InstancesState
instancesState} <- forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff effs (PABEnvironment t env)
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask @(PABEnvironment t env)
    let EffectHandlers{forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect, forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect, forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Wallet
-> Maybe ContractInstanceId
-> Eff
     (WalletEffect : ChainIndexQueryEffect : NodeClientEffect : effs)
   ~> Eff effs
handleServicesEffects :: forall t env.
EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Wallet
   -> Maybe ContractInstanceId
   -> Eff
        (WalletEffect : ChainIndexQueryEffect : NodeClientEffect : effs)
      ~> Eff effs
handleServicesEffects :: forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Wallet
-> Maybe ContractInstanceId
-> Eff
     (WalletEffect : ChainIndexQueryEffect : NodeClientEffect : effs)
   ~> Eff effs
handleServicesEffects} = EffectHandlers t env
effectHandlers
    let Eff (ContractInstanceEffects t env (IO : PABEffects t env)) a
action' :: Eff (ContractInstanceEffects t env (IO ': PABEffects t env)) a = Eff (ContractInstanceEffects t env '[IO]) a
-> Eff (ContractInstanceEffects t env (IO : PABEffects t env)) a
forall (effs :: [* -> *]) (as :: [* -> *]).
CanWeakenEnd as effs =>
Eff as ~> Eff effs
Modify.raiseEnd Eff (ContractInstanceEffects t env '[IO]) a
action

    forall (effs :: [* -> *]).
Member IO effs =>
Eff (IO : effs) ~> Eff effs
forall (eff :: * -> *) (effs :: [* -> *]).
Member eff effs =>
Eff (eff : effs) ~> Eff effs
subsume @IO
        (Eff (IO : PABEffects t env) a -> PABAction t env a)
-> Eff (IO : PABEffects t env) a -> PABAction t env a
forall a b. (a -> b) -> a -> b
$ Wallet
-> Eff (Reader Wallet : IO : PABEffects t env) a
-> Eff (IO : PABEffects t env) a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader Wallet
wallet
        (Eff (Reader Wallet : IO : PABEffects t env) a
 -> Eff (IO : PABEffects t env) a)
-> Eff (Reader Wallet : IO : PABEffects t env) a
-> Eff (IO : PABEffects t env) a
forall a b. (a -> b) -> a -> b
$ forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff (Reader (PABEnvironment t env) : effs) ~> Eff effs
forall (eff :: * -> *) (effs :: [* -> *]).
Member eff effs =>
Eff (eff : effs) ~> Eff effs
subsume @(Reader (PABEnvironment t env))
        (Eff
   (Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff (Reader Wallet : IO : PABEffects t env) a)
-> Eff
     (Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff (Reader Wallet : IO : PABEffects t env) a
forall a b. (a -> b) -> a -> b
$ InstancesState
-> Eff
     (Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader InstancesState
instancesState
        (Eff
   (Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ BlockchainEnv
-> Eff
     (Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall r (effs :: [* -> *]) a.
r -> Eff (Reader r : effs) a -> Eff effs a
runReader BlockchainEnv
blockchainEnv
        (Eff
   (Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> Eff
     (Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ (TimeEffect
 ~> Eff
      (Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (TimeEffect
        : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember IO effs,
 MonadIO IO) =>
TimeEffect ~> Eff effs
forall t env (m :: * -> *) (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs, LastMember m effs,
 MonadIO m) =>
TimeEffect ~> Eff effs
handleTimeEffect @t @env @IO)
        (Eff
   (TimeEffect
      : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (TimeEffect
        : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ forall (effs :: [* -> *]).
Member (Error PABError) effs =>
Eff (Error PABError : effs) ~> Eff effs
forall (eff :: * -> *) (effs :: [* -> *]).
Member eff effs =>
Eff (eff : effs) ~> Eff effs
subsume @(Error PABError)
        (Eff
   (Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (TimeEffect
         : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> Eff
     (Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (TimeEffect
        : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ ((LogMsg EmulatorEvent
 ~> Eff
      (Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret ((EmulatorEvent -> PABMultiAgentMsg t)
-> LogMsg EmulatorEvent
   ~> Eff
        (Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog @_ @(PABMultiAgentMsg t) EmulatorEvent -> PABMultiAgentMsg t
forall t. EmulatorEvent -> PABMultiAgentMsg t
EmulatorMsg) (Eff
   (LogMsg EmulatorEvent
      : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> (Eff
      (LogMsg Text
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent
            : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg EmulatorEvent'
 ~> Eff
      (LogMsg EmulatorEvent
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent'
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent
           : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret (forall (effs :: [* -> *]).
(Member (LogMsg EmulatorEvent) effs, Member TimeEffect effs) =>
LogMsg EmulatorEvent' ~> Eff effs
forall e (effs :: [* -> *]).
(Member (LogMsg (EmulatorTimeEvent e)) effs,
 Member TimeEffect effs) =>
LogMsg e ~> Eff effs
timed @EmulatorEvent') (Eff
   (LogMsg EmulatorEvent'
      : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> (Eff
      (LogMsg Text
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent'
            : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg WalletEvent
 ~> Eff
      (LogMsg EmulatorEvent'
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg WalletEvent
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent'
           : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((WalletEvent -> EmulatorEvent')
-> LogMsg WalletEvent
   ~> Eff
        (LogMsg EmulatorEvent'
           : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog (Wallet -> WalletEvent -> EmulatorEvent'
WalletEvent Wallet
wallet)) (Eff
   (LogMsg WalletEvent
      : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent'
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> (Eff
      (LogMsg Text
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg WalletEvent
            : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent'
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg Text
 ~> Eff
      (LogMsg WalletEvent
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg WalletEvent
           : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((Text -> WalletEvent)
-> LogMsg Text
   ~> Eff
        (LogMsg WalletEvent
           : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog Text -> WalletEvent
GenericLog))
        (Eff
   (LogMsg Text
      : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ Eff
  (LogObserve (LogMessage Text)
     : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
     : Reader InstancesState : Reader (PABEnvironment t env)
     : Reader Wallet : IO : PABEffects t env)
  a
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall (effs :: [* -> *]).
Member (LogMsg Text) effs =>
Eff (LogObserve (LogMessage Text) : effs) ~> Eff effs
handleObserveLog
        (Eff
   (LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg Text
         : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg Text
        : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ (LogMsg (ContractInstanceMsg t)
 ~> Eff
      (LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret ((ContractInstanceMsg t -> PABMultiAgentMsg t)
-> LogMsg (ContractInstanceMsg t)
   ~> Eff
        (LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog ContractInstanceMsg t -> PABMultiAgentMsg t
forall t. ContractInstanceMsg t -> PABMultiAgentMsg t
ContractInstanceLog)
        (Eff
   (LogMsg (ContractInstanceMsg t)
      : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ ((LogMsg EmulatorEvent
 ~> Eff
      (LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret ((EmulatorEvent -> PABMultiAgentMsg t)
-> LogMsg EmulatorEvent
   ~> Eff
        (LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog @_ @(PABMultiAgentMsg t) EmulatorEvent -> PABMultiAgentMsg t
forall t. EmulatorEvent -> PABMultiAgentMsg t
EmulatorMsg) (Eff
   (LogMsg EmulatorEvent
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> (Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent
            : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
            : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg EmulatorEvent'
 ~> Eff
      (LogMsg EmulatorEvent
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent'
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret (forall (effs :: [* -> *]).
(Member (LogMsg EmulatorEvent) effs, Member TimeEffect effs) =>
LogMsg EmulatorEvent' ~> Eff effs
forall e (effs :: [* -> *]).
(Member (LogMsg (EmulatorTimeEvent e)) effs,
 Member TimeEffect effs) =>
LogMsg e ~> Eff effs
timed @EmulatorEvent') (Eff
   (LogMsg EmulatorEvent'
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> (Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent'
            : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
            : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg WalletEvent
 ~> Eff
      (LogMsg EmulatorEvent'
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg WalletEvent
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent'
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((WalletEvent -> EmulatorEvent')
-> LogMsg WalletEvent
   ~> Eff
        (LogMsg EmulatorEvent'
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog (Wallet -> WalletEvent -> EmulatorEvent'
WalletEvent Wallet
wallet)) (Eff
   (LogMsg WalletEvent
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent'
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> (Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a
    -> Eff
         (LogMsg WalletEvent
            : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
            : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
            : Reader InstancesState : Reader (PABEnvironment t env)
            : Reader Wallet : IO : PABEffects t env)
         a)
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent'
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg RequestHandlerLogMsg
 ~> Eff
      (LogMsg WalletEvent
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (LogMsg WalletEvent
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((RequestHandlerLogMsg -> WalletEvent)
-> LogMsg RequestHandlerLogMsg
   ~> Eff
        (LogMsg WalletEvent
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog RequestHandlerLogMsg -> WalletEvent
RequestHandlerLog))
        (Eff
   (LogMsg RequestHandlerLogMsg
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ ((LogMsg EmulatorEvent
 ~> Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (LogMsg RequestHandlerLogMsg
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret ((EmulatorEvent -> PABMultiAgentMsg t)
-> LogMsg EmulatorEvent
   ~> Eff
        (LogMsg RequestHandlerLogMsg
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog @_ @(PABMultiAgentMsg t) EmulatorEvent -> PABMultiAgentMsg t
forall t. EmulatorEvent -> PABMultiAgentMsg t
EmulatorMsg) (Eff
   (LogMsg EmulatorEvent
      : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
      : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> (Eff
      (LogMsg TxBalanceMsg
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent
            : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
            : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
            : TimeEffect : Reader BlockchainEnv : Reader InstancesState
            : Reader (PABEnvironment t env) : Reader Wallet : IO
            : PABEffects t env)
         a)
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg EmulatorEvent'
 ~> Eff
      (LogMsg EmulatorEvent
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env))
-> Eff
     (LogMsg EmulatorEvent'
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent
           : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret (forall (effs :: [* -> *]).
(Member (LogMsg EmulatorEvent) effs, Member TimeEffect effs) =>
LogMsg EmulatorEvent' ~> Eff effs
forall e (effs :: [* -> *]).
(Member (LogMsg (EmulatorTimeEvent e)) effs,
 Member TimeEffect effs) =>
LogMsg e ~> Eff effs
timed @EmulatorEvent') (Eff
   (LogMsg EmulatorEvent'
      : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
      : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> (Eff
      (LogMsg TxBalanceMsg
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a
    -> Eff
         (LogMsg EmulatorEvent'
            : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
            : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
            : TimeEffect : Reader BlockchainEnv : Reader InstancesState
            : Reader (PABEnvironment t env) : Reader Wallet : IO
            : PABEffects t env)
         a)
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg WalletEvent
 ~> Eff
      (LogMsg EmulatorEvent'
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env))
-> Eff
     (LogMsg WalletEvent
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (LogMsg EmulatorEvent'
           : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((WalletEvent -> EmulatorEvent')
-> LogMsg WalletEvent
   ~> Eff
        (LogMsg EmulatorEvent'
           : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog (Wallet -> WalletEvent -> EmulatorEvent'
WalletEvent Wallet
wallet)) (Eff
   (LogMsg WalletEvent
      : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
      : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (LogMsg EmulatorEvent'
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> (Eff
      (LogMsg TxBalanceMsg
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a
    -> Eff
         (LogMsg WalletEvent
            : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
            : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
            : TimeEffect : Reader BlockchainEnv : Reader InstancesState
            : Reader (PABEnvironment t env) : Reader Wallet : IO
            : PABEffects t env)
         a)
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (LogMsg EmulatorEvent'
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LogMsg TxBalanceMsg
 ~> Eff
      (LogMsg WalletEvent
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env))
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
   ~> Eff
        (LogMsg WalletEvent
           : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall (f :: * -> *) (g :: * -> *) (effs :: [* -> *]).
(f ~> Eff (g : effs)) -> Eff (f : effs) ~> Eff (g : effs)
reinterpret ((TxBalanceMsg -> WalletEvent)
-> LogMsg TxBalanceMsg
   ~> Eff
        (LogMsg WalletEvent
           : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
           : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
           : TimeEffect : Reader BlockchainEnv : Reader InstancesState
           : Reader (PABEnvironment t env) : Reader Wallet : IO
           : PABEffects t env)
forall a b (effs :: [* -> *]).
Member (LogMsg b) effs =>
(a -> b) -> LogMsg a ~> Eff effs
mapLog TxBalanceMsg -> WalletEvent
TxBalanceLog))
        (Eff
   (LogMsg TxBalanceMsg
      : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
      : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
      : TimeEffect : Reader BlockchainEnv : Reader InstancesState
      : Reader (PABEnvironment t env) : Reader Wallet : IO
      : PABEffects t env)
   a
 -> Eff
      (LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
-> Eff
     (LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ Eff
  (UUIDEffect
     : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
     : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
     : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
     : Reader InstancesState : Reader (PABEnvironment t env)
     : Reader Wallet : IO : PABEffects t env)
  a
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall (m :: * -> *) (effs :: [* -> *]).
(LastMember m effs, MonadIO m) =>
Eff (UUIDEffect : effs) ~> Eff effs
handleUUIDEffect
        (Eff
   (UUIDEffect
      : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (LogMsg TxBalanceMsg
         : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
         : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
         : TimeEffect : Reader BlockchainEnv : Reader InstancesState
         : Reader (PABEnvironment t env) : Reader Wallet : IO
         : PABEffects t env)
      a)
-> Eff
     (UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (LogMsg TxBalanceMsg
        : LogMsg RequestHandlerLogMsg : LogMsg (ContractInstanceMsg t)
        : LogObserve (LogMessage Text) : LogMsg Text : Error PABError
        : TimeEffect : Reader BlockchainEnv : Reader InstancesState
        : Reader (PABEnvironment t env) : Reader Wallet : IO
        : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ Wallet
-> Maybe ContractInstanceId
-> Eff
     (WalletEffect
        : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
   ~> Eff
        (UUIDEffect
           : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
           : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
           : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
           : Reader InstancesState : Reader (PABEnvironment t env)
           : Reader Wallet : IO : PABEffects t env)
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Wallet
-> Maybe ContractInstanceId
-> Eff
     (WalletEffect : ChainIndexQueryEffect : NodeClientEffect : effs)
   ~> Eff effs
handleServicesEffects Wallet
wallet Maybe ContractInstanceId
cidM
        (Eff
   (WalletEffect
      : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
      : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (UUIDEffect
         : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (WalletEffect
        : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ Eff
  (ContractStore t
     : WalletEffect : ChainIndexQueryEffect : NodeClientEffect
     : UUIDEffect : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
     : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
     : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
     : Reader InstancesState : Reader (PABEnvironment t env)
     : Reader Wallet : IO : PABEffects t env)
  a
-> Eff
     (WalletEffect
        : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect
        (Eff
   (ContractStore t
      : WalletEffect : ChainIndexQueryEffect : NodeClientEffect
      : UUIDEffect : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
      : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
      : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
      : Reader InstancesState : Reader (PABEnvironment t env)
      : Reader Wallet : IO : PABEffects t env)
   a
 -> Eff
      (WalletEffect
         : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
         : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
         : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
         : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
         : Reader InstancesState : Reader (PABEnvironment t env)
         : Reader Wallet : IO : PABEffects t env)
      a)
-> Eff
     (ContractStore t
        : WalletEffect : ChainIndexQueryEffect : NodeClientEffect
        : UUIDEffect : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
-> Eff
     (WalletEffect
        : ChainIndexQueryEffect : NodeClientEffect : UUIDEffect
        : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall a b. (a -> b) -> a -> b
$ Eff (ContractInstanceEffects t env (IO : PABEffects t env)) a
-> Eff
     (ContractStore t
        : WalletEffect : ChainIndexQueryEffect : NodeClientEffect
        : UUIDEffect : LogMsg TxBalanceMsg : LogMsg RequestHandlerLogMsg
        : LogMsg (ContractInstanceMsg t) : LogObserve (LogMessage Text)
        : LogMsg Text : Error PABError : TimeEffect : Reader BlockchainEnv
        : Reader InstancesState : Reader (PABEnvironment t env)
        : Reader Wallet : IO : PABEffects t env)
     a
forall (effs :: [* -> *]).
(Member (Reader (PABEnvironment t env)) effs,
 Member (Error PABError) effs, Member TimeEffect effs,
 Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect Eff (ContractInstanceEffects t env (IO : PABEffects t env)) a
action'

-- | Effect handlers for running the PAB.
data EffectHandlers t env =
    EffectHandlers
        { -- | Create the initial environment. This value is shared between all threads
          --   started by the PAB.
          EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Error PABError) effs, LastMember IO effs) =>
   Eff effs (InstancesState, BlockchainEnv, env)
initialiseEnvironment :: forall effs.
            ( Member (Error PABError) effs
            , LastMember IO effs
            )
            => Eff effs (InstancesState, BlockchainEnv, env)

        -- | Handle log messages
        , EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member TimeEffect effs, Member (Error PABError) effs,
    LastMember IO effs) =>
   Eff (LogMsg (PABMultiAgentMsg t) : effs) ~> Eff effs
handleLogMessages :: forall effs.
            ( Member (Reader (PABEnvironment t env)) effs
            , Member TimeEffect effs
            , Member (Error PABError) effs
            , LastMember IO effs
            )
            => Eff (LogMsg (PABMultiAgentMsg t) ': effs)
            ~> Eff effs

        -- | Handle the 'ContractStore' effect
        , EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractStore t : effs) ~> Eff effs
handleContractStoreEffect :: forall effs.
            ( Member (Reader (PABEnvironment t env)) effs
            , Member (Error PABError) effs
            , Member TimeEffect effs
            , Member (LogMsg (PABMultiAgentMsg t)) effs
            , LastMember IO effs
            )
            => Eff (ContractStore t ': effs)
            ~> Eff effs

        -- | Handle the 'ContractEffect'
        , EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractEffect t : effs) ~> Eff effs
handleContractEffect :: forall effs.
            ( Member (Reader (PABEnvironment t env)) effs
            , Member (Error PABError) effs
            , Member TimeEffect effs
            , Member (LogMsg (PABMultiAgentMsg t)) effs
            , LastMember IO effs
            )
            => Eff (ContractEffect t ': effs)
            ~> Eff effs

        -- | Handle the 'ContractDefinition' effect
        , EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Eff (ContractDefinition t : effs) ~> Eff effs
handleContractDefinitionEffect :: forall effs.
            ( Member (Reader (PABEnvironment t env)) effs
            , Member (Error PABError) effs
            , Member TimeEffect effs
            , Member (LogMsg (PABMultiAgentMsg t)) effs
            , LastMember IO effs
            )
            => Eff (ContractDefinition t ': effs)
            ~> Eff effs

        -- | Handle effects that serve requests to external services managed by the PAB
        --   Runs in the context of a particular wallet.
        , EffectHandlers t env
-> forall (effs :: [* -> *]).
   (Member (Reader (PABEnvironment t env)) effs,
    Member (Error PABError) effs, Member TimeEffect effs,
    Member (LogMsg (PABMultiAgentMsg t)) effs, LastMember IO effs) =>
   Wallet
   -> Maybe ContractInstanceId
   -> Eff
        (WalletEffect : ChainIndexQueryEffect : NodeClientEffect : effs)
      ~> Eff effs
handleServicesEffects :: forall effs.
            ( Member (Reader (PABEnvironment t env)) effs
            , Member (Error PABError) effs
            , Member TimeEffect effs
            , Member (LogMsg (PABMultiAgentMsg t)) effs
            , LastMember IO effs
            )
            => Wallet
            -> Maybe ContractInstanceId
            -> Eff (WalletEffect ': ChainIndexQueryEffect ': NodeClientEffect ': effs)
            ~> Eff effs

        -- | Action to run on startup.
        , EffectHandlers t env -> PABAction t env ()
onStartup :: PABAction t env ()

        -- | Action to run on shutdown
        , EffectHandlers t env -> PABAction t env ()
onShutdown :: PABAction t env ()
        }

-- | Report the state of a running contract.
reportContractState ::
    forall t effs.
    ( Member (ContractStore t) effs
    , PABContract t
    )
    => ContractInstanceId
    -> Eff effs (PartiallyDecodedResponse PABReq)
reportContractState :: ContractInstanceId -> Eff effs (PartiallyDecodedResponse PABReq)
reportContractState ContractInstanceId
cid = ContractResponse Value Value PABResp PABReq
-> PartiallyDecodedResponse PABReq
forall s v.
ContractResponse Value Value s v -> PartiallyDecodedResponse v
fromResp (ContractResponse Value Value PABResp PABReq
 -> PartiallyDecodedResponse PABReq)
-> (State t -> ContractResponse Value Value PABResp PABReq)
-> State t
-> PartiallyDecodedResponse PABReq
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> State t -> ContractResponse Value Value PABResp PABReq
forall contract.
PABContract contract =>
Proxy contract
-> State contract -> ContractResponse Value Value PABResp PABReq
Contract.serialisableState (Proxy t
forall k (t :: k). Proxy t
Proxy @t) (State t -> PartiallyDecodedResponse PABReq)
-> Eff effs (State t) -> Eff effs (PartiallyDecodedResponse PABReq)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ContractInstanceId -> Eff effs (State t)
forall t (effs :: [* -> *]).
Member (ContractStore t) effs =>
ContractInstanceId -> Eff effs (State t)
getState @t ContractInstanceId
cid

-- | Annotate log messages with the current slot number.
timed ::
    forall e effs.
    ( Member (LogMsg (EmulatorTimeEvent e)) effs
    , Member TimeEffect effs
    )
    => LogMsg e
    ~> Eff effs
timed :: LogMsg e ~> Eff effs
timed = \case
    LMessage LogMessage e
m -> do
        LogMessage (EmulatorTimeEvent e)
m' <- LogMessage e
-> (e -> Eff effs (EmulatorTimeEvent e))
-> Eff effs (LogMessage (EmulatorTimeEvent e))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM LogMessage e
m ((e -> Eff effs (EmulatorTimeEvent e))
 -> Eff effs (LogMessage (EmulatorTimeEvent e)))
-> (e -> Eff effs (EmulatorTimeEvent e))
-> Eff effs (LogMessage (EmulatorTimeEvent e))
forall a b. (a -> b) -> a -> b
$ \e
msg -> do
            Slot
sl <- Eff effs Slot
forall (effs :: [* -> *]). Member TimeEffect effs => Eff effs Slot
systemTime
            EmulatorTimeEvent e -> Eff effs (EmulatorTimeEvent e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Slot -> e -> EmulatorTimeEvent e
forall e. Slot -> e -> EmulatorTimeEvent e
EmulatorTimeEvent Slot
sl e
msg)
        LogMsg (EmulatorTimeEvent e) () -> Eff effs ()
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send (LogMessage (EmulatorTimeEvent e) -> LogMsg (EmulatorTimeEvent e) ()
forall a. LogMessage a -> LogMsg a ()
LMessage LogMessage (EmulatorTimeEvent e)
m')

-- | Get the current state of the contract instance.
instanceState :: forall t env. Wallet -> ContractInstanceId -> PABAction t env (Contract.State t)
instanceState :: Wallet -> ContractInstanceId -> PABAction t env (State t)
instanceState Wallet
wallet ContractInstanceId
instanceId = Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) (State t)
-> PABAction t env (State t)
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
wallet (ContractInstanceId -> Maybe ContractInstanceId
forall a. a -> Maybe a
Just ContractInstanceId
instanceId) (ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) (State t)
forall t (effs :: [* -> *]).
Member (ContractStore t) effs =>
ContractInstanceId -> Eff effs (State t)
Contract.getState @t ContractInstanceId
instanceId)

-- | An STM transaction that returns the observable state of the contract instance.
observableState :: forall t env. ContractInstanceId -> PABAction t env (STM JSON.Value)
observableState :: ContractInstanceId -> PABAction t env (STM Value)
observableState ContractInstanceId
instanceId =
    InstanceState -> STM Value
Instances.observableContractState (InstanceState -> STM Value)
-> Eff (PABEffects t env) InstanceState
-> PABAction t env (STM Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ContractInstanceId -> Eff (PABEffects t env) InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal @t @env ContractInstanceId
instanceId

-- | Wait until the observable state of the instance matches a predicate.
waitForState :: forall t env a. (JSON.Value -> Maybe a) -> ContractInstanceId -> PABAction t env a
waitForState :: (Value -> Maybe a) -> ContractInstanceId -> PABAction t env a
waitForState Value -> Maybe a
extract ContractInstanceId
instanceId = do
    STM Value
stm <- ContractInstanceId -> PABAction t env (STM Value)
forall t env. ContractInstanceId -> PABAction t env (STM Value)
observableState ContractInstanceId
instanceId
    IO a -> PABAction t env a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> PABAction t env a) -> IO a -> PABAction t env a
forall a b. (a -> b) -> a -> b
$ STM a -> IO a
forall a. STM a -> IO a
STM.atomically (STM a -> IO a) -> STM a -> IO a
forall a b. (a -> b) -> a -> b
$ do
        Value
state <- STM Value
stm
        STM a -> (a -> STM a) -> Maybe a -> STM a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe STM a
forall a. STM a
STM.retry a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Maybe a
extract Value
state)


-- | Wait until the instance state of the instance satisfies a predicate and returns the activity status
waitForInstanceState ::
  forall t env.
  (Instances.InstanceState -> STM (Maybe ContractActivityStatus)) ->
  ContractInstanceId ->
  PABAction t env ContractActivityStatus
waitForInstanceState :: (InstanceState -> STM (Maybe ContractActivityStatus))
-> ContractInstanceId -> PABAction t env ContractActivityStatus
waitForInstanceState InstanceState -> STM (Maybe ContractActivityStatus)
extract ContractInstanceId
instanceId = do
  InstanceState
is <- ContractInstanceId -> PABAction t env InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId
  PABEnvironment{Timeout
waitStatusTimeout :: Timeout
waitStatusTimeout :: forall t env. PABEnvironment t env -> Timeout
waitStatusTimeout} <- forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff effs (PABEnvironment t env)
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask @(PABEnvironment t env)
  TMVar ()
timeout <- IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Timeout -> IO (TMVar ())
Timeout.startTimeout Timeout
waitStatusTimeout)
  let waitAction :: STM ContractActivityStatus
waitAction = InstanceState -> STM (Maybe ContractActivityStatus)
extract InstanceState
is STM (Maybe ContractActivityStatus)
-> (Maybe ContractActivityStatus -> STM ContractActivityStatus)
-> STM ContractActivityStatus
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STM ContractActivityStatus
-> (ContractActivityStatus -> STM ContractActivityStatus)
-> Maybe ContractActivityStatus
-> STM ContractActivityStatus
forall b a. b -> (a -> b) -> Maybe a -> b
maybe STM ContractActivityStatus
forall (f :: * -> *) a. Alternative f => f a
empty ContractActivityStatus -> STM ContractActivityStatus
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Either () ContractActivityStatus
result <- IO (Either () ContractActivityStatus)
-> Eff (PABEffects t env) (Either () ContractActivityStatus)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (STM (Either () ContractActivityStatus)
-> IO (Either () ContractActivityStatus)
forall a. STM a -> IO a
STM.atomically ((() -> Either () ContractActivityStatus
forall a b. a -> Either a b
Left (() -> Either () ContractActivityStatus)
-> STM () -> STM (Either () ContractActivityStatus)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TMVar () -> STM ()
forall a. TMVar a -> STM a
STM.takeTMVar TMVar ()
timeout) STM (Either () ContractActivityStatus)
-> STM (Either () ContractActivityStatus)
-> STM (Either () ContractActivityStatus)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ContractActivityStatus -> Either () ContractActivityStatus
forall a b. b -> Either a b
Right (ContractActivityStatus -> Either () ContractActivityStatus)
-> STM ContractActivityStatus
-> STM (Either () ContractActivityStatus)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM ContractActivityStatus
waitAction)))
  case Either () ContractActivityStatus
result of
    Left () -> PABError -> PABAction t env ContractActivityStatus
forall e (effs :: [* -> *]) a.
Member (Error e) effs =>
e -> Eff effs a
throwError (Text -> PABError
OtherError Text
"waitForInstanceState: Timeout")
    Right ContractActivityStatus
r -> ContractActivityStatus -> PABAction t env ContractActivityStatus
forall (f :: * -> *) a. Applicative f => a -> f a
pure ContractActivityStatus
r

-- | Wait until the instance state is updated with a response form an invoked endpoint.
-- Note that the waiting is performed only when a contract is expected to end with a Done status, i.e.,
-- no open endpoints available after invocation.
waitForInstanceStateWithResult :: forall t env. ContractInstanceId -> PABAction t env ContractActivityStatus
waitForInstanceStateWithResult :: ContractInstanceId -> PABAction t env ContractActivityStatus
waitForInstanceStateWithResult ContractInstanceId
instanceId = do
  -- retry query when waiting for a response from an invoked endpoint s.t.
  -- contract is supposed to end with status Done
  let parseStatus :: Activity -> ContractActivityStatus
      parseStatus :: Activity -> ContractActivityStatus
parseStatus = \case
        Activity
Active  -> ContractActivityStatus
Wallet.Active
        Activity
Stopped -> ContractActivityStatus
Wallet.Stopped
        Done Maybe Value
_  -> ContractActivityStatus
Wallet.Done
  let r_check :: Instances.InstanceState -> STM (Maybe ContractActivityStatus)
      r_check :: InstanceState -> STM (Maybe ContractActivityStatus)
r_check Instances.InstanceState{TVar (Map (RequestID, IterationID) OpenEndpoint)
issEndpoints :: InstanceState -> TVar (Map (RequestID, IterationID) OpenEndpoint)
issEndpoints :: TVar (Map (RequestID, IterationID) OpenEndpoint)
Instances.issEndpoints, TVar Activity
issStatus :: TVar Activity
issStatus :: InstanceState -> TVar Activity
Instances.issStatus, TVar (Maybe Value)
issObservableState :: InstanceState -> TVar (Maybe Value)
issObservableState :: TVar (Maybe Value)
Instances.issObservableState} = do
        Activity
status <- TVar Activity -> STM Activity
forall a. TVar a -> STM a
STM.readTVar TVar Activity
issStatus
        Maybe Value
ostate <- TVar (Maybe Value) -> STM (Maybe Value)
forall a. TVar a -> STM a
STM.readTVar TVar (Maybe Value)
issObservableState
        Map (RequestID, IterationID) OpenEndpoint
currentEndpoints <- TVar (Map (RequestID, IterationID) OpenEndpoint)
-> STM (Map (RequestID, IterationID) OpenEndpoint)
forall a. TVar a -> STM a
STM.readTVar TVar (Map (RequestID, IterationID) OpenEndpoint)
issEndpoints
        case (Activity
status, Maybe Value
ostate) of
          (Activity
Active, Just Value
JSON.Null) | Map (RequestID, IterationID) OpenEndpoint -> Bool
forall k a. Map k a -> Bool
Map.null Map (RequestID, IterationID) OpenEndpoint
currentEndpoints ->
                                     Maybe ContractActivityStatus -> STM (Maybe ContractActivityStatus)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ContractActivityStatus
forall a. Maybe a
Nothing
          (Activity, Maybe Value)
_ -> Maybe ContractActivityStatus -> STM (Maybe ContractActivityStatus)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe ContractActivityStatus
 -> STM (Maybe ContractActivityStatus))
-> Maybe ContractActivityStatus
-> STM (Maybe ContractActivityStatus)
forall a b. (a -> b) -> a -> b
$ ContractActivityStatus -> Maybe ContractActivityStatus
forall a. a -> Maybe a
Just (ContractActivityStatus -> Maybe ContractActivityStatus)
-> ContractActivityStatus -> Maybe ContractActivityStatus
forall a b. (a -> b) -> a -> b
$ Activity -> ContractActivityStatus
parseStatus Activity
status
  (InstanceState -> STM (Maybe ContractActivityStatus))
-> ContractInstanceId -> PABAction t env ContractActivityStatus
forall t env.
(InstanceState -> STM (Maybe ContractActivityStatus))
-> ContractInstanceId -> PABAction t env ContractActivityStatus
waitForInstanceState InstanceState -> STM (Maybe ContractActivityStatus)
r_check ContractInstanceId
instanceId

-- | Wait for the transaction to be confirmed on the blockchain.
waitForTxStatusChange :: forall t env. TxId -> PABAction t env TxStatus
waitForTxStatusChange :: TxId -> PABAction t env TxStatus
waitForTxStatusChange TxId
t = do
    BlockchainEnv
env <- (PABEnvironment t env -> BlockchainEnv)
-> Eff (PABEffects t env) BlockchainEnv
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> BlockchainEnv
forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv
    IO TxStatus -> PABAction t env TxStatus
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO TxStatus -> PABAction t env TxStatus)
-> IO TxStatus -> PABAction t env TxStatus
forall a b. (a -> b) -> a -> b
$ STM TxStatus -> IO TxStatus
forall a. STM a -> IO a
STM.atomically (STM TxStatus -> IO TxStatus) -> STM TxStatus -> IO TxStatus
forall a b. (a -> b) -> a -> b
$ TxStatus -> TxId -> BlockchainEnv -> STM TxStatus
Instances.waitForTxStatusChange TxStatus
forall a. RollbackState a
Unknown TxId
t BlockchainEnv
env

-- | Wait for the transaction output to be confirmed on the blockchain.
waitForTxOutStatusChange :: forall t env. TxOutRef -> PABAction t env TxOutStatus
waitForTxOutStatusChange :: TxOutRef -> PABAction t env TxOutStatus
waitForTxOutStatusChange TxOutRef
t = do
    BlockchainEnv
env <- (PABEnvironment t env -> BlockchainEnv)
-> Eff (PABEffects t env) BlockchainEnv
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> BlockchainEnv
forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv
    IO TxOutStatus -> PABAction t env TxOutStatus
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO TxOutStatus -> PABAction t env TxOutStatus)
-> IO TxOutStatus -> PABAction t env TxOutStatus
forall a b. (a -> b) -> a -> b
$ STM TxOutStatus -> IO TxOutStatus
forall a. STM a -> IO a
STM.atomically (STM TxOutStatus -> IO TxOutStatus)
-> STM TxOutStatus -> IO TxOutStatus
forall a b. (a -> b) -> a -> b
$ TxOutStatus -> TxOutRef -> BlockchainEnv -> STM TxOutStatus
Instances.waitForTxOutStatusChange TxOutStatus
forall a. RollbackState a
Unknown TxOutRef
t BlockchainEnv
env

-- | The list of endpoints that are currently open
activeEndpoints :: forall t env. ContractInstanceId -> PABAction t env (STM [OpenEndpoint])
activeEndpoints :: ContractInstanceId -> PABAction t env (STM [OpenEndpoint])
activeEndpoints ContractInstanceId
instanceId = do
    InstanceState
state <- ContractInstanceId -> PABAction t env InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId
    STM [OpenEndpoint] -> PABAction t env (STM [OpenEndpoint])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STM [OpenEndpoint] -> PABAction t env (STM [OpenEndpoint]))
-> STM [OpenEndpoint] -> PABAction t env (STM [OpenEndpoint])
forall a b. (a -> b) -> a -> b
$ do
        (((RequestID, IterationID), OpenEndpoint) -> OpenEndpoint)
-> [((RequestID, IterationID), OpenEndpoint)] -> [OpenEndpoint]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((RequestID, IterationID), OpenEndpoint) -> OpenEndpoint
forall a b. (a, b) -> b
snd ([((RequestID, IterationID), OpenEndpoint)] -> [OpenEndpoint])
-> (Map (RequestID, IterationID) OpenEndpoint
    -> [((RequestID, IterationID), OpenEndpoint)])
-> Map (RequestID, IterationID) OpenEndpoint
-> [OpenEndpoint]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map (RequestID, IterationID) OpenEndpoint
-> [((RequestID, IterationID), OpenEndpoint)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map (RequestID, IterationID) OpenEndpoint -> [OpenEndpoint])
-> STM (Map (RequestID, IterationID) OpenEndpoint)
-> STM [OpenEndpoint]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InstanceState -> STM (Map (RequestID, IterationID) OpenEndpoint)
Instances.openEndpoints InstanceState
state

-- | Wait until the endpoint becomes active.
waitForEndpoint :: forall t env. ContractInstanceId -> String -> PABAction t env ()
waitForEndpoint :: ContractInstanceId -> [Char] -> PABAction t env ()
waitForEndpoint ContractInstanceId
instanceId [Char]
endpointName = do
    STM [OpenEndpoint]
tx <- ContractInstanceId -> PABAction t env (STM [OpenEndpoint])
forall t env.
ContractInstanceId -> PABAction t env (STM [OpenEndpoint])
activeEndpoints ContractInstanceId
instanceId
    IO () -> PABAction t env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> PABAction t env ()) -> IO () -> PABAction t env ()
forall a b. (a -> b) -> a -> b
$ STM () -> IO ()
forall a. STM a -> IO a
STM.atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        [OpenEndpoint]
eps <- STM [OpenEndpoint]
tx
        Bool -> STM ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> STM ()) -> Bool -> STM ()
forall a b. (a -> b) -> a -> b
$ (OpenEndpoint -> Bool) -> [OpenEndpoint] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Instances.OpenEndpoint{oepName :: OpenEndpoint -> ActiveEndpoint
Instances.oepName=ActiveEndpoint{aeDescription :: ActiveEndpoint -> EndpointDescription
aeDescription=EndpointDescription [Char]
nm}} -> [Char]
nm [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
endpointName) [OpenEndpoint]
eps

-- | Get exported transactions waiting to be balanced, signed and submitted by
-- an external client.
yieldedExportTxs :: forall t env. ContractInstanceId -> PABAction t env [ExportTx]
yieldedExportTxs :: ContractInstanceId -> PABAction t env [ExportTx]
yieldedExportTxs ContractInstanceId
instanceId =
    ContractInstanceId -> PABAction t env InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId PABAction t env InstanceState
-> (InstanceState -> PABAction t env [ExportTx])
-> PABAction t env [ExportTx]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO [ExportTx] -> PABAction t env [ExportTx]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [ExportTx] -> PABAction t env [ExportTx])
-> (InstanceState -> IO [ExportTx])
-> InstanceState
-> PABAction t env [ExportTx]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM [ExportTx] -> IO [ExportTx]
forall a. STM a -> IO a
STM.atomically (STM [ExportTx] -> IO [ExportTx])
-> (InstanceState -> STM [ExportTx])
-> InstanceState
-> IO [ExportTx]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InstanceState -> STM [ExportTx]
Instances.yieldedExportTxs

currentSlot :: forall t env. PABAction t env (STM Slot)
currentSlot :: PABAction t env (STM Slot)
currentSlot = do
    BlockchainEnv
be <- (PABEnvironment t env -> BlockchainEnv)
-> Eff (PABEffects t env) BlockchainEnv
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> BlockchainEnv
forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv
    STM Slot -> PABAction t env (STM Slot)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STM Slot -> PABAction t env (STM Slot))
-> STM Slot -> PABAction t env (STM Slot)
forall a b. (a -> b) -> a -> b
$ BlockchainEnv -> STM Slot
Instances.currentSlot BlockchainEnv
be

-- | Wait until the target slot number has been reached relative to the current
-- slot.
waitUntilSlot :: forall t env. Slot -> PABAction t env ()
waitUntilSlot :: Slot -> PABAction t env ()
waitUntilSlot Slot
targetSlot = do
    STM Slot
tx <- PABAction t env (STM Slot)
forall t env. PABAction t env (STM Slot)
currentSlot
    PABAction t env () -> PABAction t env ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (PABAction t env () -> PABAction t env ())
-> PABAction t env () -> PABAction t env ()
forall a b. (a -> b) -> a -> b
$ IO () -> PABAction t env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> PABAction t env ()) -> IO () -> PABAction t env ()
forall a b. (a -> b) -> a -> b
$ STM () -> IO ()
forall a. STM a -> IO a
STM.atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        Slot
s <- STM Slot
tx
        Bool -> STM ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Slot
s Slot -> Slot -> Bool
forall a. Ord a => a -> a -> Bool
>= Slot
targetSlot)

-- | Wait for a certain number of slots relative to the current slot.
waitNSlots :: forall t env. Int -> PABAction t env ()
waitNSlots :: Int -> PABAction t env ()
waitNSlots Int
i = do
    Slot
current <- PABAction t env (STM Slot)
forall t env. PABAction t env (STM Slot)
currentSlot PABAction t env (STM Slot)
-> (STM Slot -> Eff (PABEffects t env) Slot)
-> Eff (PABEffects t env) Slot
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO Slot -> Eff (PABEffects t env) Slot
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Slot -> Eff (PABEffects t env) Slot)
-> (STM Slot -> IO Slot) -> STM Slot -> Eff (PABEffects t env) Slot
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM Slot -> IO Slot
forall a. STM a -> IO a
STM.atomically
    Slot -> PABAction t env ()
forall t env. Slot -> PABAction t env ()
waitUntilSlot (Slot
current Slot -> Slot -> Slot
forall a. Num a => a -> a -> a
+ Int -> Slot
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)

-- | The set of all active contracts.
activeContracts :: forall t env. PABAction t env (Set ContractInstanceId)
activeContracts :: PABAction t env (Set ContractInstanceId)
activeContracts = do
    (PABEnvironment t env -> InstancesState)
-> Eff (PABEffects t env) InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState Eff (PABEffects t env) InstancesState
-> (InstancesState -> PABAction t env (Set ContractInstanceId))
-> PABAction t env (Set ContractInstanceId)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO (Set ContractInstanceId)
-> PABAction t env (Set ContractInstanceId)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Set ContractInstanceId)
 -> PABAction t env (Set ContractInstanceId))
-> (InstancesState -> IO (Set ContractInstanceId))
-> InstancesState
-> PABAction t env (Set ContractInstanceId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InstancesState -> IO (Set ContractInstanceId)
Instances.instanceIDs

-- | The final result of the instance (waits until it is available)
finalResult :: forall t env. ContractInstanceId -> PABAction t env (STM (Maybe JSON.Value))
finalResult :: ContractInstanceId -> PABAction t env (STM (Maybe Value))
finalResult ContractInstanceId
instanceId = InstanceState -> STM (Maybe Value)
Instances.finalResult (InstanceState -> STM (Maybe Value))
-> Eff (PABEffects t env) InstanceState
-> PABAction t env (STM (Maybe Value))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ContractInstanceId -> Eff (PABEffects t env) InstanceState
forall t env. ContractInstanceId -> PABAction t env InstanceState
instanceStateInternal ContractInstanceId
instanceId

-- | The value in a wallet.
--
-- TODO: Change from 'Wallet' to 'Address' (see SCP-2208).
valueAt :: Wallet -> PABAction t env Value
valueAt :: Wallet -> PABAction t env Value
valueAt Wallet
wallet = do
  Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) Value
-> PABAction t env Value
forall t env a.
Wallet
-> Maybe ContractInstanceId
-> Eff (ContractInstanceEffects t env '[IO]) a
-> PABAction t env a
handleAgentThread Wallet
wallet Maybe ContractInstanceId
forall a. Maybe a
Nothing (Eff (ContractInstanceEffects t env '[IO]) Value
 -> PABAction t env Value)
-> Eff (ContractInstanceEffects t env '[IO]) Value
-> PABAction t env Value
forall a b. (a -> b) -> a -> b
$ do
    [[(TxOutRef, DecoratedTxOut)]]
txOutsM <- (PageQuery TxOutRef
 -> Eff
      (ContractInstanceEffects t env '[IO])
      (QueryResponse [(TxOutRef, DecoratedTxOut)]))
-> Eff
     (ContractInstanceEffects t env '[IO])
     [[(TxOutRef, DecoratedTxOut)]]
forall (m :: * -> *) a.
Monad m =>
(PageQuery TxOutRef -> m (QueryResponse a)) -> m [a]
ChainIndex.collectQueryResponse (\PageQuery TxOutRef
pq -> PageQuery TxOutRef
-> CardanoAddress
-> Eff
     (ContractInstanceEffects t env '[IO])
     (QueryResponse [(TxOutRef, DecoratedTxOut)])
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef
-> CardanoAddress
-> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)])
ChainIndex.unspentTxOutSetAtAddress PageQuery TxOutRef
pq CardanoAddress
addr)
    Value -> Eff (ContractInstanceEffects t env '[IO]) Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Eff (ContractInstanceEffects t env '[IO]) Value)
-> Value -> Eff (ContractInstanceEffects t env '[IO]) Value
forall a b. (a -> b) -> a -> b
$ Value -> Value
fromCardanoValue (Value -> Value) -> Value -> Value
forall a b. (a -> b) -> a -> b
$ ((TxOutRef, DecoratedTxOut) -> Value)
-> [(TxOutRef, DecoratedTxOut)] -> Value
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Getting Value (TxOutRef, DecoratedTxOut) Value
-> (TxOutRef, DecoratedTxOut) -> Value
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting Value (TxOutRef, DecoratedTxOut) Value
 -> (TxOutRef, DecoratedTxOut) -> Value)
-> Getting Value (TxOutRef, DecoratedTxOut) Value
-> (TxOutRef, DecoratedTxOut)
-> Value
forall a b. (a -> b) -> a -> b
$ (DecoratedTxOut -> Const Value DecoratedTxOut)
-> (TxOutRef, DecoratedTxOut)
-> Const Value (TxOutRef, DecoratedTxOut)
forall s t a b. Field2 s t a b => Lens s t a b
_2 ((DecoratedTxOut -> Const Value DecoratedTxOut)
 -> (TxOutRef, DecoratedTxOut)
 -> Const Value (TxOutRef, DecoratedTxOut))
-> ((Value -> Const Value Value)
    -> DecoratedTxOut -> Const Value DecoratedTxOut)
-> Getting Value (TxOutRef, DecoratedTxOut) Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Const Value Value)
-> DecoratedTxOut -> Const Value DecoratedTxOut
Lens' DecoratedTxOut Value
decoratedTxOutValue) ([(TxOutRef, DecoratedTxOut)] -> Value)
-> [(TxOutRef, DecoratedTxOut)] -> Value
forall a b. (a -> b) -> a -> b
$ [[(TxOutRef, DecoratedTxOut)]] -> [(TxOutRef, DecoratedTxOut)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(TxOutRef, DecoratedTxOut)]]
txOutsM
  where
    addr :: CardanoAddress
addr = Wallet -> CardanoAddress
mockWalletAddress Wallet
wallet

-- | Wait until the contract is done, then return
--   the error (if any)
waitUntilFinished :: forall t env. ContractInstanceId -> PABAction t env (Maybe JSON.Value)
waitUntilFinished :: ContractInstanceId -> PABAction t env (Maybe Value)
waitUntilFinished ContractInstanceId
i = ContractInstanceId -> PABAction t env (STM (Maybe Value))
forall t env.
ContractInstanceId -> PABAction t env (STM (Maybe Value))
finalResult ContractInstanceId
i PABAction t env (STM (Maybe Value))
-> (STM (Maybe Value) -> PABAction t env (Maybe Value))
-> PABAction t env (Maybe Value)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO (Maybe Value) -> PABAction t env (Maybe Value)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe Value) -> PABAction t env (Maybe Value))
-> (STM (Maybe Value) -> IO (Maybe Value))
-> STM (Maybe Value)
-> PABAction t env (Maybe Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM (Maybe Value) -> IO (Maybe Value)
forall a. STM a -> IO a
STM.atomically

instancesWithStatuses :: forall t env. PABAction t env (Map ContractInstanceId ContractActivityStatus)
instancesWithStatuses :: PABAction t env (Map ContractInstanceId ContractActivityStatus)
instancesWithStatuses = do
    InstancesState
s <- forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff effs InstancesState
forall t env (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Eff effs InstancesState
askInstancesState @t @env
    IO (Map ContractInstanceId ContractActivityStatus)
-> PABAction t env (Map ContractInstanceId ContractActivityStatus)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (InstancesState
-> IO (STM (Map ContractInstanceId ContractActivityStatus))
Instances.instancesWithStatuses InstancesState
s IO (STM (Map ContractInstanceId ContractActivityStatus))
-> (STM (Map ContractInstanceId ContractActivityStatus)
    -> IO (Map ContractInstanceId ContractActivityStatus))
-> IO (Map ContractInstanceId ContractActivityStatus)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STM (Map ContractInstanceId ContractActivityStatus)
-> IO (Map ContractInstanceId ContractActivityStatus)
forall a. STM a -> IO a
STM.atomically)

-- | Read the 'env' from the environment
askUserEnv :: forall t env effs. Member (Reader (PABEnvironment t env)) effs => Eff effs env
askUserEnv :: Eff effs env
askUserEnv = (Reader env ~> Eff effs)
-> Eff (Reader env : effs) env -> Eff effs env
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader env ~> Eff effs
forall t env (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader env ~> Eff effs
handleUserEnvReader @t @env) Eff (Reader env : effs) env
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask

-- | Read the 'BlockchainEnv' from the environment
askBlockchainEnv :: forall t env effs. Member (Reader (PABEnvironment t env)) effs => Eff effs BlockchainEnv
askBlockchainEnv :: Eff effs BlockchainEnv
askBlockchainEnv = (Reader BlockchainEnv ~> Eff effs)
-> Eff (Reader BlockchainEnv : effs) BlockchainEnv
-> Eff effs BlockchainEnv
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader BlockchainEnv ~> Eff effs
forall t env (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader BlockchainEnv ~> Eff effs
handleBlockchainEnvReader @t @env) Eff (Reader BlockchainEnv : effs) BlockchainEnv
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask

-- | Read the 'InstancesState' from the environment
askInstancesState :: forall t env effs. Member (Reader (PABEnvironment t env)) effs => Eff effs InstancesState
askInstancesState :: Eff effs InstancesState
askInstancesState = (Reader InstancesState ~> Eff effs)
-> Eff (Reader InstancesState : effs) InstancesState
-> Eff effs InstancesState
forall (eff :: * -> *) (effs :: [* -> *]).
(eff ~> Eff effs) -> Eff (eff : effs) ~> Eff effs
interpret (forall (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader InstancesState ~> Eff effs
forall t env (effs :: [* -> *]).
Member (Reader (PABEnvironment t env)) effs =>
Reader InstancesState ~> Eff effs
handleInstancesStateReader @t @env) Eff (Reader InstancesState : effs) InstancesState
forall r (effs :: [* -> *]). Member (Reader r) effs => Eff effs r
ask

handleMappedReader :: forall f g effs.
    Member (Reader f) effs
    => (f -> g)
    -> Reader g
    ~> Eff effs
handleMappedReader :: (f -> g) -> Reader g ~> Eff effs
handleMappedReader f -> g
f = \case
    Reader g x
Ask -> (f -> g) -> Eff effs g
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @f f -> g
f

handleUserEnvReader :: forall t env effs.
    Member (Reader (PABEnvironment t env)) effs
    => Reader env
    ~> Eff effs
handleUserEnvReader :: Reader env ~> Eff effs
handleUserEnvReader = \case
    Reader env x
Ask -> (PABEnvironment t env -> env) -> Eff effs env
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> env
forall t env. PABEnvironment t env -> env
appEnv

handleBlockchainEnvReader :: forall t env effs.
    Member (Reader (PABEnvironment t env)) effs
    => Reader BlockchainEnv
    ~> Eff effs
handleBlockchainEnvReader :: Reader BlockchainEnv ~> Eff effs
handleBlockchainEnvReader = \case
    Reader BlockchainEnv x
Ask -> (PABEnvironment t env -> BlockchainEnv) -> Eff effs BlockchainEnv
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> BlockchainEnv
forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv

handleInstancesStateReader :: forall t env effs.
    Member (Reader (PABEnvironment t env)) effs
    => Reader InstancesState
    ~> Eff effs
handleInstancesStateReader :: Reader InstancesState ~> Eff effs
handleInstancesStateReader = \case
    Reader InstancesState x
Ask -> (PABEnvironment t env -> InstancesState) -> Eff effs InstancesState
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> InstancesState
forall t env. PABEnvironment t env -> InstancesState
instancesState

-- | Handle the 'TimeEffect' by reading the current slot number from
-- the blockchain env.
handleTimeEffect ::
    forall t env m effs.
    ( Member (Reader (PABEnvironment t env)) effs
    , LastMember m effs
    , MonadIO m
    )
    => TimeEffect
    ~> Eff effs
handleTimeEffect :: TimeEffect ~> Eff effs
handleTimeEffect = \case
    TimeEffect x
SystemTime -> do
        BlockchainEnv
be <- (PABEnvironment t env -> BlockchainEnv) -> Eff effs BlockchainEnv
forall r (effs :: [* -> *]) a.
Member (Reader r) effs =>
(r -> a) -> Eff effs a
asks @(PABEnvironment t env) PABEnvironment t env -> BlockchainEnv
forall t env. PABEnvironment t env -> BlockchainEnv
blockchainEnv
        IO Slot -> Eff effs Slot
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Slot -> Eff effs Slot) -> IO Slot -> Eff effs Slot
forall a b. (a -> b) -> a -> b
$ STM Slot -> IO Slot
forall a. STM a -> IO a
STM.atomically (STM Slot -> IO Slot) -> STM Slot -> IO Slot
forall a b. (a -> b) -> a -> b
$ BlockchainEnv -> STM Slot
Instances.currentSlot BlockchainEnv
be