View on GitHub
File Changes
import qualified Ledger.Tx                   as Tx
import           Ledger.Value                (Value)
import qualified Ledger.Value                as Value
-
import           Wallet.API                  (MonadWallet, PubKey, WalletAPIError)
+
import           Wallet.API                  (MonadWallet, PubKey, WalletAPIError, WalletAPI)
import qualified Wallet.API                  as WAPI
import           Wallet.Emulator             (Wallet)
import qualified Wallet.Emulator             as E

                      
-- | Balance an unbalanced transaction in a 'MonadWallet' context. See note
--   [Unbalanced transactions].
balanceWallet
-
    :: (WAPI.MonadWallet m)
+
    :: MonadWallet m
    => UnbalancedTx
    -> m Tx
balanceWallet utx = do
--
--   Fails if the unbalanced transaction contains an input that spends an output
--   unknown to the wallet.
-
computeBalance :: WAPI.MonadWallet m => Tx -> m Value
+
computeBalance :: MonadWallet m => Tx -> m Value
computeBalance tx = (P.-) <$> left <*> pure right  where
    right = L.txFee tx P.+ foldMap (view Tx.outValue) (tx ^. Tx.outputs)
    left = do
-- | Balance an unbalanced transaction by adding public key inputs
--   and outputs.
balanceTx
-
    :: ( WAPI.MonadWallet m )
+
    :: MonadWallet m
    => Map TxOutRef TxOut
    -- ^ Unspent transaction outputs that may be used to balance the
    --   left hand side (inputs) of the transaction.

                      
-- | Balance an unabalanced transaction, sign it, and submit
--   it to the chain in the context of a wallet.
-
handleTx :: MonadWallet m => SigningProcess -> UnbalancedTx -> m Tx
+
handleTx :: (MonadWallet m, WAPI.NodeAPI m) => SigningProcess -> UnbalancedTx -> m Tx
handleTx p utx =
    balanceWallet utx >>= addSignatures p (T.requiredSignatories utx) >>= WAPI.signTxAndSubmit

                      
-- | The signing process gets a finished transaction and a list of public keys,
--   and signs the transaction with the corresponding private keys.
-
newtype SigningProcess = SigningProcess { addSignatures :: forall m. MonadWallet m => [L.PubKeyHash] -> Tx -> m Tx }
+
newtype SigningProcess = SigningProcess { addSignatures :: forall m. (WalletAPI m, MonadError WalletAPIError m) => [L.PubKeyHash] -> Tx -> m Tx }

                      
-- | The default signing process is 'signWallet'
defaultSigningProcess :: SigningProcess
instance FromJSON WalletAPIError
instance ToJSON WalletAPIError

                      
-
type MonadWallet m = (WalletAPI m, NodeAPI m, WalletDiagnostics m)
+
type MonadWallet m = (WalletAPI m, WalletDiagnostics m, MonadError WalletAPIError m)

                      
-- | The ability to log messages and throw errors.
-
class MonadError WalletAPIError m => WalletDiagnostics m where
+
class Monad m => WalletDiagnostics m where
    -- | Write some information to the log.
    logMsg :: Text -> m ()

                      
-- | Used by Plutus client to interact with wallet
-
class WalletAPI m where
+
class Monad m => WalletAPI m where

                      
    -- | Access the wallet's 'PublicKey'.
    ownPubKey :: m PubKey

                      
import           Control.Lens
import           Control.Monad
+
import           Control.Monad.Except
import           Data.Either                    (rights)
import qualified Data.Map                       as Map
import           Data.Maybe                     (fromMaybe)

                      
mkInitialise
    :: forall s i m
-
    . (WAPI.WalletAPI m, WAPI.WalletDiagnostics m, PlutusTx.IsData s)
+
    . (WAPI.WalletAPI m, MonadError WAPI.WalletAPIError m, WAPI.WalletDiagnostics m, PlutusTx.IsData s)
    => SM.StateMachineInstance s i
    -- ^ Signatories and required signatures
    -> s
--
mkStep
    :: forall s i m
-
    . (WAPI.WalletAPI m, WAPI.WalletDiagnostics m, PlutusTx.IsData s, PlutusTx.IsData i)
+
    . (WAPI.WalletAPI m, MonadError WAPI.WalletAPIError m, WAPI.WalletDiagnostics m, PlutusTx.IsData s, PlutusTx.IsData i)
    => SM.StateMachineInstance s i
    -- ^ The parameters of the contract instance
    -> s
--
mkHalt
    :: forall s i m
-
    . (Show s, WAPI.WalletAPI m, WAPI.WalletDiagnostics m, PlutusTx.IsData s, PlutusTx.IsData i)
+
    . (Show s, MonadError WAPI.WalletAPIError m, WAPI.WalletAPI m, WAPI.WalletDiagnostics m, PlutusTx.IsData s, PlutusTx.IsData i)
    => SM.StateMachineInstance s i
    -- ^ The parameters of the contract instance
    -> s