{-# 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(appEnv)
    -- * Contracts and instances
    , reportContractState
    , activateContract
    , activateContract'
    , callEndpointOnInstance
    , callEndpointOnInstance'
    , payToPaymentPublicKey
    -- * Agent threads
    , ContractInstanceEffects
    , handleAgentThread
    , stopInstance
    , instanceActivity
    -- * Querying the state
    , instanceState
    , observableState
    , waitForState
    , waitForTxStatusChange
    , waitForTxOutStatusChange
    , activeEndpoints
    , waitForEndpoint
    , yieldedExportTxs
    , currentSlot
    , waitUntilSlot
    , waitNSlots
    , activeContracts
    , finalResult
    , waitUntilFinished
    , blockchainEnv
    , valueAt
    , askUserEnv
    , askBlockchainEnv
    , askInstancesState
    , instancesWithStatuses
    -- * Run PAB effects in separate threads
    , PABRunner(..)
    , pabRunner
    -- * Effect handlers
    , handleMappedReader
    , handleUserEnvReader
    , handleBlockchainEnvReader
    , handleInstancesStateReader
    , timed
    ) where

import Control.Applicative (Alternative ((<|>)))
import Control.Concurrent.STM (STM)
import Control.Concurrent.STM qualified as STM
import Control.Lens (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.Default (Default (def))
import Data.Foldable (traverse_)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (catMaybes)
import Data.Proxy (Proxy (Proxy))
import Data.Set (Set)
import Data.Text (Text)
import Ledger (Address (addressCredential), TxOutRef)
import Ledger.Address (PaymentPubKeyHash)
import Ledger.Tx (CardanoTx, ciTxOutValue)
import Ledger.TxId (TxId)
import Ledger.Value (Value)
import Plutus.ChainIndex (ChainIndexQueryEffect, RollbackState (Unknown), TxOutStatus, TxStatus)
import Plutus.ChainIndex qualified as ChainIndex
import Plutus.ChainIndex.Api (UtxosResponse (page))
import Plutus.Contract.Effects (ActiveEndpoint (ActiveEndpoint, aeDescription), PABReq)
import Plutus.Contract.Wallet (ExportTx)
import Plutus.PAB.Core.ContractInstance (ContractInstanceMsg, ContractInstanceState)
import Plutus.PAB.Core.ContractInstance qualified as ContractInstance
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active), 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, WalletError))
import Plutus.PAB.Webserver.Types (ContractActivationArgs (ContractActivationArgs, caID, caWallet))
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)

-- | 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
        , 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
    -> EffectHandlers t env
    -> PABAction t env a
    -> IO (Either PABError a)
runPAB :: Timeout
-> EffectHandlers t env
-> PABAction t env a
-> IO (Either PABError a)
runPAB Timeout
endpointTimeout 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
-> 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}

    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
>>= IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (TMVar ()) -> Eff (PABEffects t env) (TMVar ()))
-> (Timeout -> IO (TMVar ()))
-> Timeout
-> Eff (PABEffects t env) (TMVar ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Timeout -> IO (TMVar ())
Timeout.startTimeout
    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
$ STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a. STM a -> IO a
STM.atomically
        (STM (Maybe NotificationError) -> IO (Maybe NotificationError))
-> STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a b. (a -> b) -> a -> b
$ TMVar ()
-> InstancesState
-> EndpointDescription
-> Value
-> ContractInstanceId
-> 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

-- | 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
    InstancesState
instancesState <- (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
    Either InstanceState PABError
r <- IO (Either InstanceState PABError)
-> Eff (PABEffects t env) (Either InstanceState PABError)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either InstanceState PABError)
 -> Eff (PABEffects t env) (Either InstanceState PABError))
-> IO (Either InstanceState PABError)
-> Eff (PABEffects t env) (Either InstanceState PABError)
forall a b. (a -> b) -> a -> b
$ STM (Either InstanceState PABError)
-> IO (Either InstanceState PABError)
forall a. STM a -> IO a
STM.atomically (STM (Either InstanceState PABError)
 -> IO (Either InstanceState PABError))
-> STM (Either InstanceState PABError)
-> IO (Either InstanceState PABError)
forall a b. (a -> b) -> a -> b
$ (InstanceState -> Either InstanceState PABError
forall a b. a -> Either a b
Left (InstanceState -> Either InstanceState PABError)
-> STM InstanceState -> STM (Either InstanceState PABError)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ContractInstanceId -> InstancesState -> STM InstanceState
Instances.instanceState ContractInstanceId
instanceId InstancesState
instancesState)
                               STM (Either InstanceState PABError)
-> STM (Either InstanceState PABError)
-> STM (Either InstanceState PABError)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Either InstanceState PABError
-> STM (Either InstanceState PABError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either InstanceState PABError
 -> STM (Either InstanceState PABError))
-> Either InstanceState PABError
-> STM (Either InstanceState PABError)
forall a b. (a -> b) -> a -> b
$ PABError -> Either InstanceState PABError
forall a b. b -> Either a b
Right (PABError -> Either InstanceState PABError)
-> PABError -> Either InstanceState PABError
forall a b. (a -> b) -> a -> b
$ ContractInstanceId -> PABError
ContractInstanceNotFound ContractInstanceId
instanceId)
    case Either InstanceState PABError
r of
        Right PABError
err -> PABError -> PABAction t env InstanceState
forall e (effs :: [* -> *]) a.
Member (Error e) effs =>
e -> Eff effs a
throwError PABError
err
        Left InstanceState
s    -> InstanceState -> PABAction t env InstanceState
forall (f :: * -> *) a. Applicative f => a -> f a
pure InstanceState
s

-- | 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
        (IO (Maybe NotificationError)
 -> PABAction t env (Maybe NotificationError))
-> IO (Maybe NotificationError)
-> PABAction t env (Maybe NotificationError)
forall a b. (a -> b) -> a -> b
$ STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a. STM a -> IO a
STM.atomically
        (STM (Maybe NotificationError) -> IO (Maybe NotificationError))
-> STM (Maybe NotificationError) -> IO (Maybe NotificationError)
forall a b. (a -> b) -> a -> b
$ InstancesState
-> EndpointDescription
-> Value
-> ContractInstanceId
-> 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

-- | Make a payment to a payment public key.
payToPaymentPublicKey :: ContractInstanceId -> Wallet -> PaymentPubKeyHash -> Value -> PABAction t env CardanoTx
payToPaymentPublicKey :: ContractInstanceId
-> Wallet
-> PaymentPubKeyHash
-> Value
-> PABAction t env CardanoTx
payToPaymentPublicKey ContractInstanceId
cid Wallet
source PaymentPubKeyHash
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
$ SlotRange
-> Value
-> PaymentPubKeyHash
-> Eff
     (Error WalletAPIError : ContractInstanceEffects t env '[IO])
     CardanoTx
forall (effs :: [* -> *]).
(Member WalletEffect effs, Member (Error WalletAPIError) effs,
 Member (LogMsg Text) effs) =>
SlotRange -> Value -> PaymentPubKeyHash -> Eff effs CardanoTx
WAPI.payToPaymentPublicKeyHash SlotRange
WAPI.defaultSlotRange Value
amount PaymentPubKeyHash
target

-- | 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 = do
    InstancesState
instancesState <- (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
    STM Value -> PABAction t env (STM Value)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STM Value -> PABAction t env (STM Value))
-> STM Value -> PABAction t env (STM Value)
forall a b. (a -> b) -> a -> b
$ ContractInstanceId -> InstancesState -> STM Value
Instances.observableContractState ContractInstanceId
instanceId InstancesState
instancesState

-- | 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 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
    InstancesState
instancesState <- (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
    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
        InstanceState
is <- ContractInstanceId -> InstancesState -> STM InstanceState
Instances.instanceState ContractInstanceId
instanceId InstancesState
instancesState
        (((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
is

-- | 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 = do
    InstancesState
instancesState <- (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 [ExportTx] -> PABAction t env [ExportTx]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [ExportTx] -> PABAction t env [ExportTx])
-> IO [ExportTx] -> PABAction t env [ExportTx]
forall a b. (a -> b) -> a -> b
$ STM [ExportTx] -> IO [ExportTx]
forall a. STM a -> IO a
STM.atomically (STM [ExportTx] -> IO [ExportTx])
-> STM [ExportTx] -> IO [ExportTx]
forall a b. (a -> b) -> a -> b
$ do
        InstanceState
is <- ContractInstanceId -> InstancesState -> STM InstanceState
Instances.instanceState ContractInstanceId
instanceId InstancesState
instancesState
        InstanceState -> STM [ExportTx]
Instances.yieldedExportTxs InstanceState
is

currentSlot :: forall t env. PABAction t env (STM Slot)
currentSlot :: PABAction t env (STM Slot)
currentSlot = do
    Instances.BlockchainEnv{TVar Slot
beCurrentSlot :: BlockchainEnv -> TVar Slot
beCurrentSlot :: TVar Slot
Instances.beCurrentSlot} <- (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
$ TVar Slot -> STM Slot
forall a. TVar a -> STM a
STM.readTVar TVar Slot
beCurrentSlot

-- | Wait until the target slot number has been reached
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)

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
    InstancesState
instancesState <- (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 (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))
-> IO (Set ContractInstanceId)
-> PABAction t env (Set ContractInstanceId)
forall a b. (a -> b) -> a -> b
$ STM (Set ContractInstanceId) -> IO (Set ContractInstanceId)
forall a. STM a -> IO a
STM.atomically (STM (Set ContractInstanceId) -> IO (Set ContractInstanceId))
-> STM (Set ContractInstanceId) -> IO (Set ContractInstanceId)
forall a b. (a -> b) -> a -> b
$ InstancesState -> STM (Set ContractInstanceId)
Instances.instanceIDs InstancesState
instancesState

-- | 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 = do
    InstancesState
instancesState <- (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
    STM (Maybe Value) -> PABAction t env (STM (Maybe Value))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STM (Maybe Value) -> PABAction t env (STM (Maybe Value)))
-> STM (Maybe Value) -> PABAction t env (STM (Maybe Value))
forall a b. (a -> b) -> a -> b
$ ContractInstanceId -> InstancesState -> STM (Maybe Value)
Instances.finalResult ContractInstanceId
instanceId InstancesState
instancesState

-- | 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]
utxoRefs <- PageQuery TxOutRef
-> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
getAllUtxoRefs PageQuery TxOutRef
forall a. Default a => a
def
    [Maybe ChainIndexTxOut]
txOutsM <- (TxOutRef
 -> Eff
      (ContractInstanceEffects t env '[IO]) (Maybe ChainIndexTxOut))
-> [TxOutRef]
-> Eff
     (ContractInstanceEffects t env '[IO]) [Maybe ChainIndexTxOut]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse TxOutRef
-> Eff
     (ContractInstanceEffects t env '[IO]) (Maybe ChainIndexTxOut)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
TxOutRef -> Eff effs (Maybe ChainIndexTxOut)
ChainIndex.unspentTxOutFromRef [TxOutRef]
utxoRefs
    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
$ (ChainIndexTxOut -> Value) -> [ChainIndexTxOut] -> Value
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Getting Value ChainIndexTxOut Value -> ChainIndexTxOut -> Value
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Value ChainIndexTxOut Value
Lens' ChainIndexTxOut Value
ciTxOutValue) ([ChainIndexTxOut] -> Value) -> [ChainIndexTxOut] -> Value
forall a b. (a -> b) -> a -> b
$ [Maybe ChainIndexTxOut] -> [ChainIndexTxOut]
forall a. [Maybe a] -> [a]
catMaybes [Maybe ChainIndexTxOut]
txOutsM
  where
    cred :: Credential
cred = Address -> Credential
addressCredential (Address -> Credential) -> Address -> Credential
forall a b. (a -> b) -> a -> b
$ Wallet -> Address
mockWalletAddress Wallet
wallet
    getAllUtxoRefs :: PageQuery TxOutRef
-> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
getAllUtxoRefs PageQuery TxOutRef
pq = do
      Page TxOutRef
utxoRefsPage <- UtxosResponse -> Page TxOutRef
page (UtxosResponse -> Page TxOutRef)
-> Eff (ContractInstanceEffects t env '[IO]) UtxosResponse
-> Eff (ContractInstanceEffects t env '[IO]) (Page TxOutRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PageQuery TxOutRef
-> Credential
-> Eff (ContractInstanceEffects t env '[IO]) UtxosResponse
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef -> Credential -> Eff effs UtxosResponse
ChainIndex.utxoSetAtAddress PageQuery TxOutRef
pq Credential
cred
      case Page TxOutRef -> Maybe (PageQuery TxOutRef)
forall a. Page a -> Maybe (PageQuery a)
ChainIndex.nextPageQuery Page TxOutRef
utxoRefsPage of
        Maybe (PageQuery TxOutRef)
Nothing -> [TxOutRef] -> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TxOutRef]
 -> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef])
-> [TxOutRef]
-> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
forall a b. (a -> b) -> a -> b
$ Page TxOutRef -> [TxOutRef]
forall a. Page a -> [a]
ChainIndex.pageItems Page TxOutRef
utxoRefsPage
        Just PageQuery TxOutRef
newPageQuery -> do
          [TxOutRef]
restOfUtxoRefs <- PageQuery TxOutRef
-> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
getAllUtxoRefs PageQuery TxOutRef
newPageQuery
          [TxOutRef] -> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TxOutRef]
 -> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef])
-> [TxOutRef]
-> Eff (ContractInstanceEffects t env '[IO]) [TxOutRef]
forall a b. (a -> b) -> a -> b
$ Page TxOutRef -> [TxOutRef]
forall a. Page a -> [a]
ChainIndex.pageItems Page TxOutRef
utxoRefsPage [TxOutRef] -> [TxOutRef] -> [TxOutRef]
forall a. Semigroup a => a -> a -> a
<> [TxOutRef]
restOfUtxoRefs

-- | 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 = 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 Eff (PABEffects t env) InstancesState
-> (InstancesState
    -> PABAction t env (Map ContractInstanceId ContractActivityStatus))
-> PABAction t env (Map ContractInstanceId ContractActivityStatus)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO (Map ContractInstanceId ContractActivityStatus)
-> PABAction t env (Map ContractInstanceId ContractActivityStatus)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Map ContractInstanceId ContractActivityStatus)
 -> PABAction t env (Map ContractInstanceId ContractActivityStatus))
-> (InstancesState
    -> IO (Map ContractInstanceId ContractActivityStatus))
-> InstancesState
-> PABAction t env (Map ContractInstanceId ContractActivityStatus)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM (Map ContractInstanceId ContractActivityStatus)
-> IO (Map ContractInstanceId ContractActivityStatus)
forall a. STM a -> IO a
STM.atomically (STM (Map ContractInstanceId ContractActivityStatus)
 -> IO (Map ContractInstanceId ContractActivityStatus))
-> (InstancesState
    -> STM (Map ContractInstanceId ContractActivityStatus))
-> InstancesState
-> IO (Map ContractInstanceId ContractActivityStatus)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InstancesState
-> STM (Map ContractInstanceId ContractActivityStatus)
Instances.instancesWithStatuses

-- | 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
        Instances.BlockchainEnv{TVar Slot
beCurrentSlot :: TVar Slot
beCurrentSlot :: BlockchainEnv -> TVar Slot
Instances.beCurrentSlot} <- (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
$ TVar Slot -> IO Slot
forall a. TVar a -> IO a
STM.readTVarIO TVar Slot
beCurrentSlot