Module

Contract.Monad

A module defining the Contract monad.

#liftContractAffM

liftContractAffM :: forall (a :: Type). String -> Aff (Maybe a) -> Contract a

Same as liftContractM but the Maybe value is in the Aff context.

#liftContractE

liftContractE :: forall (e :: Type) (a :: Type). Show e => Either e a -> Contract a

Similar to liftContractE except it directly throws the showable error via throwContractError instead of an arbitrary string.

#liftContractE'

liftContractE' :: forall (e :: Type) (a :: Type). String -> Either e a -> Contract a

Similar to liftContractM, throwing the string instead of the Left value. For throwing the Left value, see liftEither in Contract.Prelude.

#liftContractM

liftContractM :: forall (a :: Type). String -> Maybe a -> Contract a

Given a string error and Maybe value, if the latter is Nothing, throw the error with the given string, otherwise, return the value. If using using runExceptT, see liftM inside Contract.Prelude. This can be thought of as liftM restricted to JavaScript's Error and without the need to call error :: String -> Error each time.

#liftedE

liftedE :: forall (e :: Type) (a :: Type). Show e => Contract (Either e a) -> Contract a

Similar to liftedE except it directly throws the showable error via throwContractError instead of an arbitrary string.

#liftedE'

liftedE' :: forall (e :: Type) (a :: Type). String -> Contract (Either e a) -> Contract a

Same as liftContractE but the Either value is already in the Contract context.

#liftedM

liftedM :: forall (a :: Type). String -> Contract (Maybe a) -> Contract a

Same as liftContractM but the Maybe value is already in the Contract context.

#throwContractError

throwContractError :: forall (e :: Type) (a :: Type). Show e => e -> Contract a

Throws an Error for any showable error using Effect.Exception.throw and lifting into the Contract monad.

#mkContractEnv

mkContractEnv :: Warn (Text "Using `mkContractEnv` is not recommended: it does not ensure `ContractEnv` finalization. Consider using `withContractEnv`") => ContractParams -> Aff ContractEnv

Initializes a Contract environment. Does not ensure finalization. Consider using withContractEnv if possible - otherwise use stopContractEnv to properly finalize.

#stopContractEnv

stopContractEnv :: Warn (Text "Using `stopContractEnv` is not recommended: users should rely on `withContractEnv` to finalize the runtime environment instead") => ContractEnv -> Aff Unit

Finalizes a Contract environment. Closes the connections in ContractEnv, effectively making it unusable.

Re-exports from Ctl.Internal.Contract.Monad

#ContractParams

type ContractParams = { backendParams :: QueryBackendParams, customLogger :: Maybe (LogLevel -> Message -> Aff Unit), hooks :: Hooks, logLevel :: LogLevel, networkId :: NetworkId, suppressLogs :: Boolean, synchronizationParams :: ContractSynchronizationParams, timeParams :: ContractTimeParams, walletSpec :: Maybe WalletSpec }

Options to construct an environment for a Contract to run.

See Contract.Config for pre-defined values for testnet and mainnet.

Use runContract to run a Contract within an implicity constructed ContractEnv environment, or use withContractEnv if your application contains multiple contracts that can reuse the same environment.

#ContractEnv

type ContractEnv = { backend :: QueryBackend, customLogger :: Maybe (LogLevel -> Message -> Aff Unit), handle :: QueryHandle, hooks :: Hooks, knownTxs :: { backend :: Ref (Set TransactionHash) }, ledgerConstants :: LedgerConstants, logLevel :: LogLevel, networkId :: NetworkId, suppressLogs :: Boolean, synchronizationParams :: ContractSynchronizationParams, timeParams :: ContractTimeParams, usedTxOuts :: UsedTxOuts, wallet :: Maybe Wallet }

A record containing Contract environment - everything a Contract needs to run. It is recommended to use one environment per application to save on websocket connections and to keep track of UsedTxOuts.

Instances

#Contract

newtype Contract (a :: Type)

The Contract monad is a newtype wrapper over ReaderT on ContractEnv over asynchronous effects, Aff. Throwing and catching errors can therefore be implemented with native JavaScript Effect.Exception.Errors and Effect.Class.Console.log replaces the Writer monad. Aff enables the user to make effectful calls inside this Contract monad.

Constructors

Instances

#withContractEnv

withContractEnv :: forall (a :: Type). ContractParams -> (ContractEnv -> Aff a) -> Aff a

Constructs and finalizes a contract environment that is usable inside a bracket callback. One environment can be used by multiple Contracts in parallel (see runContractInEnv). Make sure that Aff action does not end before all contracts that use the runtime terminate. Otherwise WebSockets will be closed too early.

#runContractInEnv

runContractInEnv :: forall (a :: Type). ContractEnv -> Contract a -> Aff a

Runs a contract in existing environment. Does not destroy the environment when contract execution ends.

#runContract

runContract :: forall (a :: Type). ContractParams -> Contract a -> Aff a

Interprets a contract into an Aff context. Implicitly initializes and finalizes a new ContractEnv runtime.

Use withContractEnv if your application contains multiple contracts that can be run in parallel, reusing the same environment (see withContractEnv)

Re-exports from Effect.Aff

#Aff

data Aff t0

An Aff a is an asynchronous computation with effects. The computation may either error with an exception, or produce a result of type a. Aff effects are assembled from primitive Effect effects using makeAff or liftEffect.

Instances

#launchAff_

launchAff_ :: Aff Unit -> Effect Unit

Forks an Aff from an Effect context, discarding the Fiber.

Modules