plutus-contract-1.2.0.0
Safe HaskellNone
LanguageHaskell2010

Plutus.Contract.State

Synopsis

Contract state

Types for initialising and running instances of Contracts. The types and functions in this module are convenient wrappers around types and functions from Types, exposing an interface that is suitable for consumption by the PAB. In particular this means that insertAndUpdateContract has a single argument, and its argument & return types can be serialised to JSON easily.

To actually run a contract, follow this workflow:

  1. Call initialiseContract to get the initial ContractResponse.
  2. Look at the hooks of this value and generate an answer to one of them. This answer is a Response s value.
  3. Call insertAndUpdateContract with a ContractRequest whose oldState field has the value of newState of the previous response, and whose event is the next answer (step 2).
  4. Take the new ContractResponse and go back to step 2, until you get a response with no requests, or an error.

data Contract w (s :: Row *) e a Source #

Contract w s e a is a contract with schema s, producing a value of type a or an error e. See note [Contract Schema].

Instances

Instances details
IsContract Contract Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

toContract :: forall w (s :: Row Type) e a. Contract w s e a -> Contract w s e a Source #

MonadError e (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

throwError :: e -> Contract w s e a #

catchError :: Contract w s e a -> (e -> Contract w s e a) -> Contract w s e a

Bifunctor (Contract w s) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

bimap :: (a -> b) -> (c -> d) -> Contract w s a c -> Contract w s b d Source #

first :: (a -> b) -> Contract w s a c -> Contract w s b c Source #

second :: (b -> c) -> Contract w s a b -> Contract w s a c Source #

Monad (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

(>>=) :: Contract w s e a -> (a -> Contract w s e b) -> Contract w s e b Source #

(>>) :: Contract w s e a -> Contract w s e b -> Contract w s e b Source #

return :: a -> Contract w s e a Source #

Functor (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

fmap :: (a -> b) -> Contract w s e a -> Contract w s e b Source #

(<$) :: a -> Contract w s e b -> Contract w s e a Source #

Applicative (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

pure :: a -> Contract w s e a Source #

(<*>) :: Contract w s e (a -> b) -> Contract w s e a -> Contract w s e b Source #

liftA2 :: (a -> b -> c) -> Contract w s e a -> Contract w s e b -> Contract w s e c Source #

(*>) :: Contract w s e a -> Contract w s e b -> Contract w s e b Source #

(<*) :: Contract w s e a -> Contract w s e b -> Contract w s e a Source #

Applicative (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

pure :: a -> Contract w s e a

(<*>) :: Contract w s e (a -> b) -> Contract w s e a -> Contract w s e b

Functor (Contract w s e) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

fmap :: (a -> b) -> Contract w s e a -> Contract w s e b

Semigroup a => Semigroup (Contract w s e a) Source # 
Instance details

Defined in Plutus.Contract.Types

Methods

(<>) :: Contract w s e a -> Contract w s e a -> Contract w s e a Source #

sconcat :: NonEmpty (Contract w s e a) -> Contract w s e a Source #

stimes :: Integral b => b -> Contract w s e a -> Contract w s e a Source #

data State w e Source #

The state of a Contract, containing all responses that have been fed to it, and checkpoints that it produced.

Constructors

State 

Instances

Instances details
Bifunctor State Source # 
Instance details

Defined in Plutus.Contract.State

Methods

bimap :: (a -> b) -> (c -> d) -> State a c -> State b d Source #

first :: (a -> b) -> State a c -> State b c Source #

second :: (b -> c) -> State a b -> State a c Source #

Functor (State w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

fmap :: (a -> b) -> State w a -> State w b Source #

(<$) :: a -> State w b -> State w a Source #

Foldable (State w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

fold :: Monoid m => State w m -> m Source #

foldMap :: Monoid m => (a -> m) -> State w a -> m Source #

foldMap' :: Monoid m => (a -> m) -> State w a -> m Source #

foldr :: (a -> b -> b) -> b -> State w a -> b Source #

foldr' :: (a -> b -> b) -> b -> State w a -> b Source #

foldl :: (b -> a -> b) -> b -> State w a -> b Source #

foldl' :: (b -> a -> b) -> b -> State w a -> b Source #

foldr1 :: (a -> a -> a) -> State w a -> a Source #

foldl1 :: (a -> a -> a) -> State w a -> a Source #

toList :: State w a -> [a] Source #

null :: State w a -> Bool Source #

length :: State w a -> Int Source #

elem :: Eq a => a -> State w a -> Bool Source #

maximum :: Ord a => State w a -> a Source #

minimum :: Ord a => State w a -> a Source #

sum :: Num a => State w a -> a Source #

product :: Num a => State w a -> a Source #

Traversable (State w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

traverse :: Applicative f => (a -> f b) -> State w a -> f (State w b) Source #

sequenceA :: Applicative f => State w (f a) -> f (State w a) Source #

mapM :: Monad m => (a -> m b) -> State w a -> m (State w b) Source #

sequence :: Monad m => State w (m a) -> m (State w a) Source #

(Eq e, Eq w) => Eq (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

(==) :: State w e -> State w e -> Bool Source #

(/=) :: State w e -> State w e -> Bool Source #

(Show e, Show w) => Show (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

showsPrec :: Int -> State w e -> ShowS Source #

show :: State w e -> String Source #

showList :: [State w e] -> ShowS Source #

Generic (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

Associated Types

type Rep (State w e) :: Type -> Type Source #

Methods

from :: State w e -> Rep (State w e) x Source #

to :: Rep (State w e) x -> State w e Source #

(FromJSON e, FromJSON w) => FromJSON (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

parseJSON :: Value -> Parser (State w e)

parseJSONList :: Value -> Parser [State w e]

(ToJSON w, ToJSON e) => ToJSON (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

toJSON :: State w e -> Value

toEncoding :: State w e -> Encoding

toJSONList :: [State w e] -> Value

toEncodingList :: [State w e] -> Encoding

type Rep (State w e) Source # 
Instance details

Defined in Plutus.Contract.State

type Rep (State w e) = D1 ('MetaData "State" "Plutus.Contract.State" "plutus-contract-1.2.0.0-FH8LC9wh7UV4Nmv68NHXrC" 'False) (C1 ('MetaCons "State" 'PrefixI 'True) (S1 ('MetaSel ('Just "record") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Responses e)) :*: (S1 ('MetaSel ('Just "checkpoints") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CheckpointStore) :*: S1 ('MetaSel ('Just "observableState") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 w))))

data ContractRequest w s Source #

A request sent to a contract instance. It contains the previous State of the instance, and a Response to one of the requests of the instance.

Constructors

ContractRequest 

Fields

Instances

Instances details
Functor (ContractRequest w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

fmap :: (a -> b) -> ContractRequest w a -> ContractRequest w b Source #

(<$) :: a -> ContractRequest w b -> ContractRequest w a Source #

Foldable (ContractRequest w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

fold :: Monoid m => ContractRequest w m -> m Source #

foldMap :: Monoid m => (a -> m) -> ContractRequest w a -> m Source #

foldMap' :: Monoid m => (a -> m) -> ContractRequest w a -> m Source #

foldr :: (a -> b -> b) -> b -> ContractRequest w a -> b Source #

foldr' :: (a -> b -> b) -> b -> ContractRequest w a -> b Source #

foldl :: (b -> a -> b) -> b -> ContractRequest w a -> b Source #

foldl' :: (b -> a -> b) -> b -> ContractRequest w a -> b Source #

foldr1 :: (a -> a -> a) -> ContractRequest w a -> a Source #

foldl1 :: (a -> a -> a) -> ContractRequest w a -> a Source #

toList :: ContractRequest w a -> [a] Source #

null :: ContractRequest w a -> Bool Source #

length :: ContractRequest w a -> Int Source #

elem :: Eq a => a -> ContractRequest w a -> Bool Source #

maximum :: Ord a => ContractRequest w a -> a Source #

minimum :: Ord a => ContractRequest w a -> a Source #

sum :: Num a => ContractRequest w a -> a Source #

product :: Num a => ContractRequest w a -> a Source #

Traversable (ContractRequest w) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

traverse :: Applicative f => (a -> f b) -> ContractRequest w a -> f (ContractRequest w b) Source #

sequenceA :: Applicative f => ContractRequest w (f a) -> f (ContractRequest w a) Source #

mapM :: Monad m => (a -> m b) -> ContractRequest w a -> m (ContractRequest w b) Source #

sequence :: Monad m => ContractRequest w (m a) -> m (ContractRequest w a) Source #

(Eq s, Eq w) => Eq (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

(Show s, Show w) => Show (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

Generic (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

Associated Types

type Rep (ContractRequest w s) :: Type -> Type Source #

(FromJSON s, FromJSON w) => FromJSON (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

parseJSON :: Value -> Parser (ContractRequest w s)

parseJSONList :: Value -> Parser [ContractRequest w s]

(ToJSON s, ToJSON w) => ToJSON (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

toJSON :: ContractRequest w s -> Value

toEncoding :: ContractRequest w s -> Encoding

toJSONList :: [ContractRequest w s] -> Value

toEncodingList :: [ContractRequest w s] -> Encoding

(Show s, Show w) => Pretty (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

pretty :: ContractRequest w s -> Doc ann

prettyList :: [ContractRequest w s] -> Doc ann

type Rep (ContractRequest w s) Source # 
Instance details

Defined in Plutus.Contract.State

type Rep (ContractRequest w s) = D1 ('MetaData "ContractRequest" "Plutus.Contract.State" "plutus-contract-1.2.0.0-FH8LC9wh7UV4Nmv68NHXrC" 'False) (C1 ('MetaCons "ContractRequest" 'PrefixI 'True) (S1 ('MetaSel ('Just "oldState") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (State w (CheckpointKey, s))) :*: S1 ('MetaSel ('Just "event") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Response s))))

data ContractResponse w e s h Source #

A response produced by a contract instance. It contains the new State, the list of endpoints that can be called, logs produced by the contract, possibly an error message, and the accumulated observable state.

Constructors

ContractResponse 

Fields

  • newState :: State w (CheckpointKey, s)

    Serialised state of the contract (internal)

  • hooks :: [Request h]

    Open requests that can be handled

  • logs :: [LogMessage Value]

    Logs produced by the contract

  • lastLogs :: [LogMessage Value]

    Logs produced in the last step

  • err :: Maybe e

    Error that happened during contract execution

  • lastState :: w

    Observable state produced in the last step

Instances

Instances details
Bifunctor (ContractResponse w e) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

bimap :: (a -> b) -> (c -> d) -> ContractResponse w e a c -> ContractResponse w e b d Source #

first :: (a -> b) -> ContractResponse w e a c -> ContractResponse w e b c Source #

second :: (b -> c) -> ContractResponse w e a b -> ContractResponse w e a c Source #

Functor (ContractResponse w e s) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

fmap :: (a -> b) -> ContractResponse w e s a -> ContractResponse w e s b Source #

(<$) :: a -> ContractResponse w e s b -> ContractResponse w e s a Source #

(Eq s, Eq w, Eq h, Eq e) => Eq (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

(==) :: ContractResponse w e s h -> ContractResponse w e s h -> Bool Source #

(/=) :: ContractResponse w e s h -> ContractResponse w e s h -> Bool Source #

(Show s, Show w, Show h, Show e) => Show (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

Generic (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

Associated Types

type Rep (ContractResponse w e s h) :: Type -> Type Source #

Methods

from :: ContractResponse w e s h -> Rep (ContractResponse w e s h) x Source #

to :: Rep (ContractResponse w e s h) x -> ContractResponse w e s h Source #

(FromJSON s, FromJSON w, FromJSON h, FromJSON e) => FromJSON (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

parseJSON :: Value -> Parser (ContractResponse w e s h)

parseJSONList :: Value -> Parser [ContractResponse w e s h]

(ToJSON e, ToJSON h, ToJSON s, ToJSON w) => ToJSON (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

Methods

toJSON :: ContractResponse w e s h -> Value

toEncoding :: ContractResponse w e s h -> Encoding

toJSONList :: [ContractResponse w e s h] -> Value

toEncodingList :: [ContractResponse w e s h] -> Encoding

type Rep (ContractResponse w e s h) Source # 
Instance details

Defined in Plutus.Contract.State

type Rep (ContractResponse w e s h) = D1 ('MetaData "ContractResponse" "Plutus.Contract.State" "plutus-contract-1.2.0.0-FH8LC9wh7UV4Nmv68NHXrC" 'False) (C1 ('MetaCons "ContractResponse" 'PrefixI 'True) ((S1 ('MetaSel ('Just "newState") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (State w (CheckpointKey, s))) :*: (S1 ('MetaSel ('Just "hooks") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Request h]) :*: S1 ('MetaSel ('Just "logs") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [LogMessage Value]))) :*: (S1 ('MetaSel ('Just "lastLogs") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [LogMessage Value]) :*: (S1 ('MetaSel ('Just "err") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe e)) :*: S1 ('MetaSel ('Just "lastState") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 w)))))

mapE :: forall e f w s h. (e -> f) -> ContractResponse w e s h -> ContractResponse w f s h Source #

mapW :: forall w q e s h. (w -> q) -> ContractResponse w e s h -> ContractResponse q e s h Source #

insertAndUpdateContract Source #

Arguments

:: forall w s e a. Monoid w 
=> Contract w s e a

The Contract with schema s error type e.

-> ContractRequest w PABResp

The ContractRequest value with the previous state and the new event.

-> ContractResponse w e PABResp PABReq 

Run one step of the contract by restoring it to its previous state and feeding it a single new Response event.

initialiseContract :: forall w s e a. Monoid w => Contract w s e a -> ContractResponse w e PABResp PABReq Source #

The ContractResponse with the initial state of the contract.

mkResponse :: forall w e s h a. Monoid w => w -> ResumableResult w e s h a -> ContractResponse w e s h Source #