Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data StateMachineClient s i = StateMachineClient {
- scInstance :: StateMachineInstance s i
- scChooser :: [OnChainState s i] -> Either SMContractError (OnChainState s i)
- data TxConstraints i o
- data SMContractError
- class AsSMContractError r where
- _SMContractError :: Prism' r SMContractError
- _ChooserError :: Prism' r Text
- _UnableToExtractTransition :: Prism' r ()
- _SMCContractError :: Prism' r ContractError
- data StateMachine s i = StateMachine {
- smTransition :: State s -> i -> Maybe (TxConstraints Void Void, State s)
- smFinal :: s -> Bool
- smCheck :: s -> i -> ScriptContext -> Bool
- smThreadToken :: Maybe ThreadToken
- data StateMachineInstance s i = StateMachineInstance {
- stateMachine :: StateMachine s i
- typedValidator :: TypedValidator (StateMachine s i)
- data State s = State {
- stateData :: s
- stateValue :: Value
- newtype OnChainState s i = OnChainState {
- ocsTxOutRef :: TypedScriptTxOutRef (StateMachine s i)
- data WaitingResult t i s
- = Timeout t
- | ContractEnded i
- | Transition i s
- | InitialState s
- data InvalidTransition s i = InvalidTransition {}
- data TransitionResult s i
- = TransitionFailure (InvalidTransition s i)
- | TransitionSuccess s
- data ThreadToken = ThreadToken TxOutRef CurrencySymbol
- mkValidator :: forall s i. ToData s => StateMachine s i -> ValidatorType (StateMachine s i)
- mkStateMachine :: Maybe ThreadToken -> (State s -> i -> Maybe (TxConstraints Void Void, State s)) -> (s -> Bool) -> StateMachine s i
- mkStateMachineClient :: forall state input. StateMachineInstance state input -> StateMachineClient state input
- defaultChooser :: forall state input. [OnChainState state input] -> Either SMContractError (OnChainState state input)
- getStates :: forall s i. (FromData s, ToData s) => StateMachineInstance s i -> Map TxOutRef DecoratedTxOut -> [OnChainState s i]
- runGuardedStep :: forall w a e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) => StateMachineClient state input -> input -> (UnbalancedTx -> state -> state -> Maybe a) -> Contract w schema e (Either a (TransitionResult state input))
- runStep :: forall w e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) => StateMachineClient state input -> input -> Contract w schema e (TransitionResult state input)
- runInitialise :: forall w e state schema input. (FromData state, ToData state, ToData input, AsSMContractError e) => StateMachineClient state input -> state -> Value -> Contract w schema e state
- runGuardedStepWith :: forall w a e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) => ScriptLookups (StateMachine state input) -> TxConstraints input state -> StateMachineClient state input -> input -> (UnbalancedTx -> state -> state -> Maybe a) -> Contract w schema e (Either a (TransitionResult state input))
- runStepWith :: forall w e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) => ScriptLookups (StateMachine state input) -> TxConstraints input state -> StateMachineClient state input -> input -> Contract w schema e (TransitionResult state input)
- runInitialiseWith :: forall w e state schema input. (FromData state, ToData state, ToData input, AsSMContractError e) => ScriptLookups (StateMachine state input) -> TxConstraints input state -> StateMachineClient state input -> state -> Value -> Contract w schema e state
- getThreadToken :: AsSMContractError e => Contract w schema e ThreadToken
- getOnChainState :: (AsSMContractError e, FromData state, ToData state) => StateMachineClient state i -> Contract w schema e (Maybe (OnChainState state i, Map TxOutRef DecoratedTxOut))
- getStateData :: OnChainState s i -> s
- waitForUpdate :: forall state i w schema e. (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> Contract w schema e (Maybe (OnChainState state i))
- waitForUpdateUntilSlot :: (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> Slot -> Contract w schema e (WaitingResult Slot i state)
- waitForUpdateUntilTime :: (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> POSIXTime -> Contract w schema e (WaitingResult POSIXTime i state)
- waitForUpdateTimeout :: forall state i t w schema e. (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> Promise w schema e t -> Contract w schema e (Promise w schema e (WaitingResult t i (OnChainState state i)))
- data StateMachineTransition state input = StateMachineTransition {
- smtConstraints :: TxConstraints input state
- smtOldState :: State state
- smtNewState :: State state
- smtLookups :: ScriptLookups (StateMachine state input)
- mkStep :: forall w e state schema input. (AsSMContractError e, FromData state, ToData state) => StateMachineClient state input -> input -> Contract w schema e (Either (InvalidTransition state input) (StateMachineTransition state input))
- data Void
Documentation
To write your contract as a state machine you need
* Two types state
and input
for the state and inputs of the machine
* A 'SM.StateMachineInstance state input' describing the transitions and
checks of the state machine (this is the on-chain code)
* A 'StateMachineClient state input' with the state machine instance and
an allocation function
In many cases it is enough to define the transition function
t :: (state, Value) -> input -> Maybe (TxConstraints state)
and use
mkStateMachine
and mkStateMachineClient
to get the client.
You can then use runInitialise
and runStep
to initialise and transition
the state machine. runStep
gets the current state from the utxo set and
makes the transition to the next state using the given input and taking care
of all payments.
data StateMachineClient s i Source #
Client-side definition of a state machine.
StateMachineClient | |
|
data TxConstraints i o #
Instances
data SMContractError Source #
Instances
class AsSMContractError r where Source #
_SMContractError :: Prism' r SMContractError Source #
_ChooserError :: Prism' r Text Source #
_UnableToExtractTransition :: Prism' r () Source #
_SMCContractError :: Prism' r ContractError Source #
Instances
AsSMContractError SMContractError Source # | |
Defined in Plutus.Contract.StateMachine _SMContractError :: Prism' SMContractError SMContractError Source # _ChooserError :: Prism' SMContractError Text Source # _UnableToExtractTransition :: Prism' SMContractError () Source # _SMCContractError :: Prism' SMContractError ContractError Source # |
data StateMachine s i Source #
Specification of a state machine, consisting of a transition function that determines the next state from the current state and an input, and a checking function that checks the validity of the transition in the context of the current transaction.
StateMachine | |
|
Instances
ValidatorTypes (StateMachine s i) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain type RedeemerType (StateMachine s i) type DatumType (StateMachine s i) | |
type DatumType (StateMachine s i) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain type DatumType (StateMachine s i) = s | |
type RedeemerType (StateMachine s i) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain type RedeemerType (StateMachine s i) = i |
data StateMachineInstance s i Source #
StateMachineInstance | |
|
State | |
|
Instances
Eq s => Eq (State s) Source # | |
Show s => Show (State s) Source # | |
Generic (State s) Source # | |
FromJSON s => FromJSON (State s) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain parseJSON :: Value -> Parser (State s) parseJSONList :: Value -> Parser [State s] | |
ToJSON s => ToJSON (State s) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain toEncoding :: State s -> Encoding toJSONList :: [State s] -> Value toEncodingList :: [State s] -> Encoding | |
type Rep (State s) Source # | |
Defined in Plutus.Contract.StateMachine.OnChain type Rep (State s) = D1 ('MetaData "State" "Plutus.Contract.StateMachine.OnChain" "plutus-contract-1.2.0.0-FH8LC9wh7UV4Nmv68NHXrC" 'False) (C1 ('MetaCons "State" 'PrefixI 'True) (S1 ('MetaSel ('Just "stateData") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 s) :*: S1 ('MetaSel ('Just "stateValue") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Value))) |
newtype OnChainState s i Source #
Typed representation of the on-chain state of a state machine instance
OnChainState | |
|
data WaitingResult t i s Source #
The outcome of waitForUpdateTimeout
Timeout t | The timeout happened before any change of the on-chain state was detected |
ContractEnded i | The state machine instance ended |
Transition i s | The state machine instance transitioned to a new state |
InitialState s | The state machine instance was initialised |
Instances
data InvalidTransition s i Source #
An invalid transition
Instances
data TransitionResult s i Source #
Result of an attempted transition
TransitionFailure (InvalidTransition s i) | The transition is not allowed |
TransitionSuccess s | The transition is allowed and results in a new state |
data ThreadToken Source #
ThreadToken TxOutRef CurrencySymbol |
Instances
Constructing the machine instance
mkValidator :: forall s i. ToData s => StateMachine s i -> ValidatorType (StateMachine s i) Source #
Turn a state machine into a validator script.
mkStateMachine :: Maybe ThreadToken -> (State s -> i -> Maybe (TxConstraints Void Void, State s)) -> (s -> Bool) -> StateMachine s i Source #
A state machine that does not perform any additional checks on the
ScriptContext
(beyond enforcing the constraints)
Constructing the state machine client
mkStateMachineClient :: forall state input. StateMachineInstance state input -> StateMachineClient state input Source #
A state machine client with the defaultChooser
function
defaultChooser :: forall state input. [OnChainState state input] -> Either SMContractError (OnChainState state input) Source #
A state chooser function that fails if confronted with anything other than exactly one output
getStates :: forall s i. (FromData s, ToData s) => StateMachineInstance s i -> Map TxOutRef DecoratedTxOut -> [OnChainState s i] Source #
Running the state machine
:: forall w a e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) | |
=> StateMachineClient state input | The state machine |
-> input | The input to apply to the state machine |
-> (UnbalancedTx -> state -> state -> Maybe a) | The guard to check before running the step |
-> Contract w schema e (Either a (TransitionResult state input)) |
:: forall w e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) | |
=> StateMachineClient state input | The state machine |
-> input | The input to apply to the state machine |
-> Contract w schema e (TransitionResult state input) |
Run one step of a state machine, returning the new state.
:: forall w e state schema input. (FromData state, ToData state, ToData input, AsSMContractError e) | |
=> StateMachineClient state input | The state machine |
-> state | The initial state |
-> Value | The value locked by the contract at the beginning |
-> Contract w schema e state |
Initialise a state machine
:: forall w a e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) | |
=> ScriptLookups (StateMachine state input) | Additional lookups |
-> TxConstraints input state | Additional constraints |
-> StateMachineClient state input | The state machine |
-> input | The input to apply to the state machine |
-> (UnbalancedTx -> state -> state -> Maybe a) | The guard to check before running the step |
-> Contract w schema e (Either a (TransitionResult state input)) |
The same as runGuardedStep
but we can supply additional constraints and lookups for transaction.
:: forall w e state schema input. (AsSMContractError e, FromData state, ToData state, ToData input) | |
=> ScriptLookups (StateMachine state input) | Additional lookups |
-> TxConstraints input state | Additional constraints |
-> StateMachineClient state input | The state machine |
-> input | The input to apply to the state machine |
-> Contract w schema e (TransitionResult state input) |
Run one step of a state machine, returning the new state. We can supply additional constraints and lookups for transaction.
:: forall w e state schema input. (FromData state, ToData state, ToData input, AsSMContractError e) | |
=> ScriptLookups (StateMachine state input) | Additional lookups |
-> TxConstraints input state | Additional constraints |
-> StateMachineClient state input | The state machine |
-> state | The initial state |
-> Value | The value locked by the contract at the beginning |
-> Contract w schema e state |
Initialise a state machine and supply additional constraints and lookups for transaction.
getThreadToken :: AsSMContractError e => Contract w schema e ThreadToken Source #
Create a thread token. The thread token contains a reference to an unspent output of the wallet,
so it needs to used with mkStateMachine
immediately, and the machine must be initialised,
to prevent the output from getting spent in the mean time.
getOnChainState :: (AsSMContractError e, FromData state, ToData state) => StateMachineClient state i -> Contract w schema e (Maybe (OnChainState state i, Map TxOutRef DecoratedTxOut)) Source #
Get the current on-chain state of the state machine instance.
Return Nothing if there is no state on chain.
Throws an SMContractError
if the number of outputs at the machine address is greater than one.
getStateData :: OnChainState s i -> s Source #
waitForUpdate :: forall state i w schema e. (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> Contract w schema e (Maybe (OnChainState state i)) Source #
Wait until the on-chain state of the state machine instance has changed,
and return the new state, or return Nothing
if the instance has been
terminated. If waitForUpdate
is called before the instance has even
started then it returns the first state of the instance as soon as it
has started.
waitForUpdateUntilSlot :: (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> Slot -> Contract w schema e (WaitingResult Slot i state) Source #
Wait for the on-chain state of the state machine instance to change until timeoutSlot,
and return the new state, or return ContractEnded
if the instance has been
terminated. If waitForUpdate
is called before the instance has even
started then it returns the first state of the instance as soon as it
has started.
waitForUpdateUntilTime :: (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) => StateMachineClient state i -> POSIXTime -> Contract w schema e (WaitingResult POSIXTime i state) Source #
Same as waitForUpdateUntilSlot
, but works with POSIXTime
instead.
:: forall state i t w schema e. (AsSMContractError e, AsContractError e, FromData state, ToData state, FromData i) | |
=> StateMachineClient state i | The state machine client |
-> Promise w schema e t | The timeout |
-> Contract w schema e (Promise w schema e (WaitingResult t i (OnChainState state i))) |
Construct a Promise
that waits for an update to the state machine's
on-chain state, or a user-defined timeout (whichever happens first).
Lower-level API
data StateMachineTransition state input Source #
Constraints & lookups needed to transition a state machine instance
StateMachineTransition | |
|
mkStep :: forall w e state schema input. (AsSMContractError e, FromData state, ToData state) => StateMachineClient state input -> input -> Contract w schema e (Either (InvalidTransition state input) (StateMachineTransition state input)) Source #
Given a state machine client and an input to apply to
the client's state machine instance, compute the StateMachineTransition
that can produce an actual transaction performing the transition
Re-exports
Uninhabited data type
Since: base-4.8.0.0
Instances
Eq Void | Since: base-4.8.0.0 |
Data Void | Since: base-4.8.0.0 |
Defined in Data.Void gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Void -> c Void Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Void Source # toConstr :: Void -> Constr Source # dataTypeOf :: Void -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Void) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Void) Source # gmapT :: (forall b. Data b => b -> b) -> Void -> Void Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Void -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Void -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Void -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Void -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Void -> m Void Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Void -> m Void Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Void -> m Void Source # | |
Ord Void | Since: base-4.8.0.0 |
Read Void | Reading a Since: base-4.8.0.0 |
Show Void | Since: base-4.8.0.0 |
Ix Void | Since: base-4.8.0.0 |
Generic Void | Since: base-4.8.0.0 |
Semigroup Void | Since: base-4.9.0.0 |
Exception Void | Since: base-4.8.0.0 |
Defined in Data.Void toException :: Void -> SomeException Source # fromException :: SomeException -> Maybe Void Source # displayException :: Void -> String Source # | |
FromJSON Void | |
Defined in Data.Aeson.Types.FromJSON parseJSON :: Value -> Parser Void parseJSONList :: Value -> Parser [Void] | |
ToJSON Void | |
Defined in Data.Aeson.Types.ToJSON | |
Hashable Void | |
Defined in Data.Hashable.Class | |
Pretty Void | |
Defined in Prettyprinter.Internal prettyList :: [Void] -> Doc ann | |
Serialise Void | |
Defined in Codec.Serialise.Class encodeList :: [Void] -> Encoding decodeList :: Decoder s [Void] | |
FromData Void | |
Defined in PlutusTx.IsData.Class fromBuiltinData :: BuiltinData -> Maybe Void | |
ToData Void | |
Defined in PlutusTx.IsData.Class toBuiltinData :: Void -> BuiltinData | |
UnsafeFromData Void | |
Defined in PlutusTx.IsData.Class unsafeFromBuiltinData :: BuiltinData -> Void | |
FromHttpApiData Void | |
Defined in Web.Internal.HttpApiData parseUrlPiece :: Text -> Either Text Void parseHeader :: ByteString -> Either Text Void parseQueryParam :: Text -> Either Text Void | |
ToHttpApiData Void | |
Defined in Web.Internal.HttpApiData toUrlPiece :: Void -> Text toEncodedUrlPiece :: Void -> Builder toHeader :: Void -> ByteString toQueryParam :: Void -> Text | |
NoThunks Void | |
ShowErrorComponent Void | |
Defined in Text.Megaparsec.Error showErrorComponent :: Void -> String errorComponentLen :: Void -> Int | |
ValidatorTypes Void | |
Defined in Plutus.Script.Utils.Typed | |
FromFormKey Void | |
Defined in Web.Internal.FormUrlEncoded parseFormKey :: Text -> Either Text Void | |
ToFormKey Void | |
Defined in Web.Internal.FormUrlEncoded | |
Lift Void | Since: template-haskell-2.15.0.0 |
PrettyDefaultBy config Void => PrettyBy config Void | |
Defined in Text.PrettyBy.Internal prettyBy :: config -> Void -> Doc ann prettyListBy :: config -> [Void] -> Doc ann | |
DefaultPrettyBy config Void | |
Defined in Text.PrettyBy.Internal defaultPrettyBy :: config -> Void -> Doc ann defaultPrettyListBy :: config -> [Void] -> Doc ann | |
FoldableWithIndex Void (V1 :: Type -> Type) | |
FoldableWithIndex Void (U1 :: Type -> Type) | |
FoldableWithIndex Void (Proxy :: Type -> Type) | |
FunctorWithIndex Void (V1 :: Type -> Type) | |
FunctorWithIndex Void (U1 :: Type -> Type) | |
FunctorWithIndex Void (Proxy :: Type -> Type) | |
TraversableWithIndex Void (V1 :: Type -> Type) | |
TraversableWithIndex Void (U1 :: Type -> Type) | |
TraversableWithIndex Void (Proxy :: Type -> Type) | |
FoldableWithIndex Void (Const e :: Type -> Type) | |
Defined in WithIndex | |
FoldableWithIndex Void (Constant e :: Type -> Type) | |
Defined in WithIndex | |
FunctorWithIndex Void (Const e :: Type -> Type) | |
FunctorWithIndex Void (Constant e :: Type -> Type) | |
TraversableWithIndex Void (Const e :: Type -> Type) | |
TraversableWithIndex Void (Constant e :: Type -> Type) | |
Defined in WithIndex itraverse :: Applicative f => (Void -> a -> f b) -> Constant e a -> f (Constant e b) | |
FoldableWithIndex Void (K1 i c :: Type -> Type) | |
FunctorWithIndex Void (K1 i c :: Type -> Type) | |
TraversableWithIndex Void (K1 i c :: Type -> Type) | |
type Rep Void | |
type DatumType Void | |
Defined in Plutus.Script.Utils.Typed | |
type RedeemerType Void | |
Defined in Plutus.Script.Utils.Typed | |
type Code Void | |
Defined in Generics.SOP.Instances | |
type DatatypeInfoOf Void | |
Defined in Generics.SOP.Instances type DatatypeInfoOf Void = 'ADT "Data.Void" "Void" ('[] :: [ConstructorInfo]) ('[] :: [[StrictnessInfo]]) |