{-# LANGUAGE DataKinds            #-}
{-# LANGUAGE DerivingStrategies   #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE NumericUnderscores   #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns         #-}
{-| Building transactions
-}
module Convex.BuildTx(
  -- * Effect
  MonadBuildTx(..),
  BuildTxT(..),
  runBuildTxT,
  runBuildTx,
  execBuildTxT,
  execBuildTx,
  execBuildTx',
  evalBuildTxT,

  TxBuild,
  -- * Building transactions
  spendPublicKeyOutput,
  payToAddress,
  payToAddressTxOut,
  payToPublicKey,
  payToScriptHash,
  payToPlutusV1,
  payToPlutusV2,
  payToPlutusV2InlineDatum,
  spendPlutusV1,
  spendPlutusV2,
  spendPlutusV2Ref,
  spendPlutusV2RefWithInlineDatum,
  spendPlutusV2RefWithoutInRef,
  spendPlutusV2RefWithoutInRefInlineDatum,
  spendPlutusV2InlineDatum,
  mintPlutusV1,
  mintPlutusV2,
  mintPlutusV2Ref,
  payToPlutusV2Inline,
  payToPlutusV2InlineWithInlineDatum,
  payToPlutusV2InlineWithDatum,
  addReference,
  addCollateral,
  addAuxScript,
  assetValue,
  setScriptsValid,
  addRequiredSignature,
  prependTxOut,
  -- * Minimum Ada deposit
  minAdaDeposit,
  setMinAdaDeposit,
  setMinAdaDepositAll
  ) where

import           Cardano.Api.Shelley        (Hash, HashableScriptData,
                                             NetworkId, PaymentKey,
                                             PlutusScript, PlutusScriptV1,
                                             PlutusScriptV2, ScriptHash)
import qualified Cardano.Api.Shelley        as C
import           Control.Lens               (_1, _2, at, mapped, over, set, (&))
import           Control.Monad.Except       (MonadError (..))
import qualified Control.Monad.State        as LazyState
import           Control.Monad.State.Class  (MonadState (..))
import qualified Control.Monad.State.Strict as StrictState
import           Control.Monad.Trans.Class  (MonadTrans (..))
import           Control.Monad.Trans.Except (ExceptT)
import           Control.Monad.Writer       (WriterT, execWriterT, runWriterT)
import           Control.Monad.Writer.Class (MonadWriter (..))
import           Convex.Class               (MonadBlockchain (..),
                                             MonadBlockchainCardanoNodeT,
                                             MonadMockchain (..))
import qualified Convex.Lenses              as L
import           Convex.MonadLog            (MonadLog (..), MonadLogIgnoreT,
                                             MonadLogKatipT)
import           Convex.Scripts             (toHashableScriptData)
import           Data.Functor.Identity      (Identity (..))
import           Data.List                  (nub)
import qualified Data.Map                   as Map
import           Data.Maybe                 (fromMaybe)
import qualified PlutusLedgerApi.V1         as Plutus

newtype BTX = BTX{ BTX
-> TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
unBtx :: C.TxBodyContent C.BuildTx C.BabbageEra -> C.TxBodyContent C.BuildTx C.BabbageEra }

instance Semigroup BTX where
    -- note that the order here is reversed, compared to @Data.Monoid.Endo@.
    -- This is so that @addBtx a >> addBtx b@ will result in a transaction
    -- where @a@ has been applied before @b@.
  (BTX TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
l) <> :: BTX -> BTX -> BTX
<> (BTX TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
r) = (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> BTX
BTX (TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
l)

instance Monoid BTX where
  mempty :: BTX
mempty = (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> BTX
BTX forall a. a -> a
id

class Monad m => MonadBuildTx m where
  addBtx :: TxBuild -> m ()

instance MonadBuildTx m => MonadBuildTx (ExceptT e m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> ExceptT e m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance MonadBuildTx m => MonadBuildTx (StrictState.StateT e m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> StateT e m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance MonadBuildTx m => MonadBuildTx (LazyState.StateT e m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> StateT e m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance (Monoid w, MonadBuildTx m) => MonadBuildTx (WriterT w m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> WriterT w m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance MonadBuildTx m => MonadBuildTx (MonadBlockchainCardanoNodeT e m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> MonadBlockchainCardanoNodeT e m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance MonadBuildTx m => MonadBuildTx (MonadLogIgnoreT m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> MonadLogIgnoreT m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

instance MonadBuildTx m => MonadBuildTx (MonadLogKatipT m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> MonadLogKatipT m ()
addBtx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx

{-| Monad transformer for the @MonadBuildTx@ effect
-}
newtype BuildTxT m a = BuildTxT{forall (m :: * -> *) a. BuildTxT m a -> WriterT BTX m a
unBuildTxT :: WriterT BTX m a }
  deriving newtype (forall a b. a -> BuildTxT m b -> BuildTxT m a
forall a b. (a -> b) -> BuildTxT m a -> BuildTxT m b
forall (m :: * -> *) a b.
Functor m =>
a -> BuildTxT m b -> BuildTxT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> BuildTxT m a -> BuildTxT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> BuildTxT m b -> BuildTxT m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> BuildTxT m b -> BuildTxT m a
fmap :: forall a b. (a -> b) -> BuildTxT m a -> BuildTxT m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> BuildTxT m a -> BuildTxT m b
Functor, forall a. a -> BuildTxT m a
forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m a
forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m b
forall a b. BuildTxT m (a -> b) -> BuildTxT m a -> BuildTxT m b
forall a b c.
(a -> b -> c) -> BuildTxT m a -> BuildTxT m b -> BuildTxT m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall {m :: * -> *}. Applicative m => Functor (BuildTxT m)
forall (m :: * -> *) a. Applicative m => a -> BuildTxT m a
forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m a
forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m b
forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m (a -> b) -> BuildTxT m a -> BuildTxT m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> BuildTxT m a -> BuildTxT m b -> BuildTxT m c
<* :: forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m a
*> :: forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m b
liftA2 :: forall a b c.
(a -> b -> c) -> BuildTxT m a -> BuildTxT m b -> BuildTxT m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> BuildTxT m a -> BuildTxT m b -> BuildTxT m c
<*> :: forall a b. BuildTxT m (a -> b) -> BuildTxT m a -> BuildTxT m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
BuildTxT m (a -> b) -> BuildTxT m a -> BuildTxT m b
pure :: forall a. a -> BuildTxT m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> BuildTxT m a
Applicative, forall a. a -> BuildTxT m a
forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m b
forall a b. BuildTxT m a -> (a -> BuildTxT m b) -> BuildTxT m b
forall {m :: * -> *}. Monad m => Applicative (BuildTxT m)
forall (m :: * -> *) a. Monad m => a -> BuildTxT m a
forall (m :: * -> *) a b.
Monad m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m b
forall (m :: * -> *) a b.
Monad m =>
BuildTxT m a -> (a -> BuildTxT m b) -> BuildTxT m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> BuildTxT m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> BuildTxT m a
>> :: forall a b. BuildTxT m a -> BuildTxT m b -> BuildTxT m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
BuildTxT m a -> BuildTxT m b -> BuildTxT m b
>>= :: forall a b. BuildTxT m a -> (a -> BuildTxT m b) -> BuildTxT m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
BuildTxT m a -> (a -> BuildTxT m b) -> BuildTxT m b
Monad)

instance MonadTrans BuildTxT where
  lift :: forall (m :: * -> *) a. Monad m => m a -> BuildTxT m a
lift = forall (m :: * -> *) a. WriterT BTX m a -> BuildTxT m a
BuildTxT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance Monad m => MonadBuildTx (BuildTxT m) where
  addBtx :: (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> BuildTxT m ()
addBtx = forall (m :: * -> *) a. WriterT BTX m a -> BuildTxT m a
BuildTxT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> BTX
BTX

instance MonadError e m => MonadError e (BuildTxT m) where
  throwError :: forall a. e -> BuildTxT m a
throwError = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
  catchError :: forall a. BuildTxT m a -> (e -> BuildTxT m a) -> BuildTxT m a
catchError BuildTxT m a
m e -> BuildTxT m a
action = forall (m :: * -> *) a. WriterT BTX m a -> BuildTxT m a
BuildTxT (forall (m :: * -> *) a. BuildTxT m a -> WriterT BTX m a
unBuildTxT forall a b. (a -> b) -> a -> b
$ forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError BuildTxT m a
m e -> BuildTxT m a
action)

instance MonadBlockchain m => MonadBlockchain (BuildTxT m) where
  sendTx :: Tx BabbageEra -> BuildTxT m TxId
sendTx = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadBlockchain m => Tx BabbageEra -> m TxId
sendTx
  utxoByTxIn :: Set TxIn -> BuildTxT m (UTxO BabbageEra)
utxoByTxIn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockchain m =>
Set TxIn -> m (UTxO BabbageEra)
utxoByTxIn
  queryProtocolParameters :: BuildTxT m (BundledProtocolParameters BabbageEra)
queryProtocolParameters = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *).
MonadBlockchain m =>
m (BundledProtocolParameters BabbageEra)
queryProtocolParameters
  queryStakePools :: BuildTxT m (Set PoolId)
queryStakePools = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadBlockchain m => m (Set PoolId)
queryStakePools
  querySystemStart :: BuildTxT m SystemStart
querySystemStart = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadBlockchain m => m SystemStart
querySystemStart
  queryEraHistory :: BuildTxT m (EraHistory CardanoMode)
queryEraHistory = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *).
MonadBlockchain m =>
m (EraHistory CardanoMode)
queryEraHistory
  querySlotNo :: BuildTxT m (SlotNo, SlotLength, UTCTime)
querySlotNo = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *).
MonadBlockchain m =>
m (SlotNo, SlotLength, UTCTime)
querySlotNo
  networkId :: BuildTxT m NetworkId
networkId = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadBlockchain m => m NetworkId
networkId

instance MonadMockchain m => MonadMockchain (BuildTxT m) where
  modifySlot :: forall a. (SlotNo -> (SlotNo, a)) -> BuildTxT m a
modifySlot = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadMockchain m =>
(SlotNo -> (SlotNo, a)) -> m a
modifySlot
  modifyUtxo :: forall a. (UTxO ERA -> (UTxO ERA, a)) -> BuildTxT m a
modifyUtxo = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadMockchain m =>
(UTxO ERA -> (UTxO ERA, a)) -> m a
modifyUtxo
  resolveDatumHash :: Hash ScriptData -> BuildTxT m (Maybe ScriptData)
resolveDatumHash = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadMockchain m =>
Hash ScriptData -> m (Maybe ScriptData)
resolveDatumHash

instance MonadState s m => MonadState s (BuildTxT m) where
  state :: forall a. (s -> (a, s)) -> BuildTxT m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state

instance MonadLog m => MonadLog (BuildTxT m) where
  logInfo' :: Doc Void -> BuildTxT m ()
logInfo' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadLog m => Doc Void -> m ()
logInfo'
  logWarn' :: Doc Void -> BuildTxT m ()
logWarn' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadLog m => Doc Void -> m ()
logWarn'
  logDebug' :: Doc Void -> BuildTxT m ()
logDebug' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadLog m => Doc Void -> m ()
logDebug'

{-| Run the @BuildTxT@ monad transformer
-}
runBuildTxT :: Functor m => BuildTxT m a -> m (a, TxBuild)
runBuildTxT :: forall (m :: * -> *) a.
Functor m =>
BuildTxT m a
-> m (a,
      TxBodyContent BuildTx BabbageEra
      -> TxBodyContent BuildTx BabbageEra)
runBuildTxT = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BTX
-> TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
unBtx) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. BuildTxT m a -> WriterT BTX m a
unBuildTxT

{-| Run the @BuildTxT@ monad transformer, returning the @TxBuild@ part only
-}
execBuildTxT :: Monad m => BuildTxT m a -> m TxBuild
execBuildTxT :: forall (m :: * -> *) a.
Monad m =>
BuildTxT m a
-> m (TxBodyContent BuildTx BabbageEra
      -> TxBodyContent BuildTx BabbageEra)
execBuildTxT = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BTX
-> TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
unBtx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. BuildTxT m a -> WriterT BTX m a
unBuildTxT

{-| Run the @BuildTxT@ monad transformer, returnin only the result
-}
evalBuildTxT :: Monad m => BuildTxT m a -> m a
evalBuildTxT :: forall (m :: * -> *) a. Monad m => BuildTxT m a -> m a
evalBuildTxT = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. BuildTxT m a -> WriterT BTX m a
unBuildTxT

runBuildTx :: BuildTxT Identity a -> (a, TxBuild)
runBuildTx :: forall a.
BuildTxT Identity a
-> (a,
    TxBodyContent BuildTx BabbageEra
    -> TxBodyContent BuildTx BabbageEra)
runBuildTx = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Functor m =>
BuildTxT m a
-> m (a,
      TxBodyContent BuildTx BabbageEra
      -> TxBodyContent BuildTx BabbageEra)
runBuildTxT

execBuildTx :: BuildTxT Identity a -> TxBuild
execBuildTx :: forall a.
BuildTxT Identity a
-> TxBodyContent BuildTx BabbageEra
-> TxBodyContent BuildTx BabbageEra
execBuildTx = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Monad m =>
BuildTxT m a
-> m (TxBodyContent BuildTx BabbageEra
      -> TxBodyContent BuildTx BabbageEra)
execBuildTxT

{-| Run the @BuildTx@ action and produce a transaction body
-}
execBuildTx' :: BuildTxT Identity a -> C.TxBodyContent C.BuildTx C.BabbageEra
execBuildTx' :: forall a. BuildTxT Identity a -> TxBodyContent BuildTx BabbageEra
execBuildTx' = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
($) TxBodyContent BuildTx BabbageEra
L.emptyTx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Monad m =>
BuildTxT m a
-> m (TxBodyContent BuildTx BabbageEra
      -> TxBodyContent BuildTx BabbageEra)
execBuildTxT

type TxBuild = C.TxBodyContent C.BuildTx C.BabbageEra -> C.TxBodyContent C.BuildTx C.BabbageEra

{-| Spend an output locked by a public key
-}
spendPublicKeyOutput :: MonadBuildTx m => C.TxIn -> m ()
spendPublicKeyOutput :: forall (m :: * -> *). MonadBuildTx m => TxIn -> m ()
spendPublicKeyOutput TxIn
txIn = do
  let wit :: BuildTxWith BuildTx (Witness WitCtxTxIn era)
wit = forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (forall witctx era. KeyWitnessInCtx witctx -> Witness witctx era
C.KeyWitness (KeyWitnessInCtx WitCtxTxIn
C.KeyWitnessForSpending))
  forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v. Lens' (TxBodyContent v BabbageEra) [TxIn v]
L.txIns ((TxIn
txIn, forall {era}. BuildTxWith BuildTx (Witness WitCtxTxIn era)
wit) forall a. a -> [a] -> [a]
:))

spendPlutusV1 :: forall datum redeemer m. (MonadBuildTx m, Plutus.ToData datum, Plutus.ToData redeemer) => C.TxIn -> PlutusScript PlutusScriptV1 -> datum -> redeemer -> m ()
spendPlutusV1 :: forall datum redeemer (m :: * -> *).
(MonadBuildTx m, ToData datum, ToData redeemer) =>
TxIn -> PlutusScript PlutusScriptV1 -> datum -> redeemer -> m ()
spendPlutusV1 TxIn
txIn PlutusScript PlutusScriptV1
s (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
dat) (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) =
  let wit :: ScriptWitness WitCtxTxIn BabbageEra
wit = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV1 BabbageEra
C.PlutusScriptV1InBabbage PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 (forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript PlutusScript PlutusScriptV1
s) (HashableScriptData -> ScriptDatum WitCtxTxIn
C.ScriptDatumForTxIn HashableScriptData
dat) HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
      wit' :: BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit' = forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (forall witctx era.
ScriptWitnessInCtx witctx
-> ScriptWitness witctx era -> Witness witctx era
C.ScriptWitness ScriptWitnessInCtx WitCtxTxIn
C.ScriptWitnessForSpending ScriptWitness WitCtxTxIn BabbageEra
wit)
  in forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v. Lens' (TxBodyContent v BabbageEra) [TxIn v]
L.txIns ((TxIn
txIn, BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit') forall a. a -> [a] -> [a]
:))

spendPlutusV2 :: forall datum redeemer m. (MonadBuildTx m, Plutus.ToData datum, Plutus.ToData redeemer) => C.TxIn -> PlutusScript PlutusScriptV2 -> datum -> redeemer -> m ()
spendPlutusV2 :: forall datum redeemer (m :: * -> *).
(MonadBuildTx m, ToData datum, ToData redeemer) =>
TxIn -> PlutusScript PlutusScriptV2 -> datum -> redeemer -> m ()
spendPlutusV2 TxIn
txIn PlutusScript PlutusScriptV2
s (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
dat) (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) =
  let wit :: ScriptWitness WitCtxTxIn BabbageEra
wit = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript PlutusScript PlutusScriptV2
s) (HashableScriptData -> ScriptDatum WitCtxTxIn
C.ScriptDatumForTxIn HashableScriptData
dat) HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
      wit' :: BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit' = forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (forall witctx era.
ScriptWitnessInCtx witctx
-> ScriptWitness witctx era -> Witness witctx era
C.ScriptWitness ScriptWitnessInCtx WitCtxTxIn
C.ScriptWitnessForSpending ScriptWitness WitCtxTxIn BabbageEra
wit)
  in forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v. Lens' (TxBodyContent v BabbageEra) [TxIn v]
L.txIns ((TxIn
txIn, BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit') forall a. a -> [a] -> [a]
:))

{-| Spend an output locked by a Plutus V2 validator with an inline datum
-}
spendPlutusV2InlineDatum :: forall redeemer m. (MonadBuildTx m, Plutus.ToData redeemer) => C.TxIn -> PlutusScript PlutusScriptV2 -> redeemer -> m ()
spendPlutusV2InlineDatum :: forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn -> PlutusScript PlutusScriptV2 -> redeemer -> m ()
spendPlutusV2InlineDatum TxIn
txIn PlutusScript PlutusScriptV2
s (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) =
  let wit :: ScriptWitness WitCtxTxIn BabbageEra
wit = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript PlutusScript PlutusScriptV2
s) ScriptDatum WitCtxTxIn
C.InlineScriptDatum HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
      wit' :: BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit' = forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (forall witctx era.
ScriptWitnessInCtx witctx
-> ScriptWitness witctx era -> Witness witctx era
C.ScriptWitness ScriptWitnessInCtx WitCtxTxIn
C.ScriptWitnessForSpending ScriptWitness WitCtxTxIn BabbageEra
wit)
  in forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v. Lens' (TxBodyContent v BabbageEra) [TxIn v]
L.txIns ((TxIn
txIn, BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit') forall a. a -> [a] -> [a]
:))

spendPlutusV2RefBase :: forall redeemer m. (MonadBuildTx m, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> C.ScriptDatum C.WitCtxTxIn -> redeemer -> m ()
spendPlutusV2RefBase :: forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBase TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh ScriptDatum WitCtxTxIn
dat (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) =
  let wit :: ScriptWitness WitCtxTxIn BabbageEra
wit = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript TxIn
refTxIn Maybe ScriptHash
sh) ScriptDatum WitCtxTxIn
dat HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
      wit' :: BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit' = forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (forall witctx era.
ScriptWitnessInCtx witctx
-> ScriptWitness witctx era -> Witness witctx era
C.ScriptWitness ScriptWitnessInCtx WitCtxTxIn
C.ScriptWitnessForSpending ScriptWitness WitCtxTxIn BabbageEra
wit)
  in forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v. Lens' (TxBodyContent v BabbageEra) [TxIn v]
L.txIns ((TxIn
txIn, BuildTxWith BuildTx (Witness WitCtxTxIn BabbageEra)
wit') forall a. a -> [a] -> [a]
:))

{-| same as spendPlutusV2RefBase but adds the reference script in the reference input list
-}
spendPlutusV2RefBaseWithInRef :: forall redeemer m. (MonadBuildTx m, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> C.ScriptDatum C.WitCtxTxIn -> redeemer -> m ()
spendPlutusV2RefBaseWithInRef :: forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBaseWithInRef TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh ScriptDatum WitCtxTxIn
dat redeemer
red = forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBase TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh ScriptDatum WitCtxTxIn
dat redeemer
red forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). MonadBuildTx m => TxIn -> m ()
addReference TxIn
refTxIn

spendPlutusV2Ref :: forall datum redeemer m. (MonadBuildTx m, Plutus.ToData datum, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> datum -> redeemer -> m ()
spendPlutusV2Ref :: forall datum redeemer (m :: * -> *).
(MonadBuildTx m, ToData datum, ToData redeemer) =>
TxIn -> TxIn -> Maybe ScriptHash -> datum -> redeemer -> m ()
spendPlutusV2Ref TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
dat) redeemer
red = forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBaseWithInRef TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh (HashableScriptData -> ScriptDatum WitCtxTxIn
C.ScriptDatumForTxIn HashableScriptData
dat) redeemer
red

{-| same as spendPlutusV2Ref but considers inline datum at the spent utxo
-}
spendPlutusV2RefWithInlineDatum :: forall redeemer m. (MonadBuildTx m, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> redeemer -> m ()
spendPlutusV2RefWithInlineDatum :: forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn -> TxIn -> Maybe ScriptHash -> redeemer -> m ()
spendPlutusV2RefWithInlineDatum TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh redeemer
red = forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBaseWithInRef TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh ScriptDatum WitCtxTxIn
C.InlineScriptDatum redeemer
red

{-| same as spendPlutusV2Ref but does not add the reference script in the reference input list
This is to cover the case whereby the reference script utxo is expected to be consumed in the same tx.
-}
spendPlutusV2RefWithoutInRef :: forall datum redeemer m. (MonadBuildTx m, Plutus.ToData datum, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> datum -> redeemer -> m ()
spendPlutusV2RefWithoutInRef :: forall datum redeemer (m :: * -> *).
(MonadBuildTx m, ToData datum, ToData redeemer) =>
TxIn -> TxIn -> Maybe ScriptHash -> datum -> redeemer -> m ()
spendPlutusV2RefWithoutInRef TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
dat) redeemer
red = forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBase TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh (HashableScriptData -> ScriptDatum WitCtxTxIn
C.ScriptDatumForTxIn HashableScriptData
dat) redeemer
red

{-| same as spendPlutusV2RefWithoutInRef but considers inline datum at the spent utxo
-}
spendPlutusV2RefWithoutInRefInlineDatum :: forall redeemer m. (MonadBuildTx m, Plutus.ToData redeemer) => C.TxIn -> C.TxIn -> Maybe C.ScriptHash -> redeemer -> m ()
spendPlutusV2RefWithoutInRefInlineDatum :: forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn -> TxIn -> Maybe ScriptHash -> redeemer -> m ()
spendPlutusV2RefWithoutInRefInlineDatum TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh redeemer
red = forall redeemer (m :: * -> *).
(MonadBuildTx m, ToData redeemer) =>
TxIn
-> TxIn
-> Maybe ScriptHash
-> ScriptDatum WitCtxTxIn
-> redeemer
-> m ()
spendPlutusV2RefBase TxIn
txIn TxIn
refTxIn Maybe ScriptHash
sh ScriptDatum WitCtxTxIn
C.InlineScriptDatum redeemer
red


mintPlutusV1 :: forall redeemer m. (Plutus.ToData redeemer, MonadBuildTx m) => PlutusScript PlutusScriptV1 -> redeemer -> C.AssetName -> C.Quantity -> m ()
mintPlutusV1 :: forall redeemer (m :: * -> *).
(ToData redeemer, MonadBuildTx m) =>
PlutusScript PlutusScriptV1
-> redeemer -> AssetName -> Quantity -> m ()
mintPlutusV1 PlutusScript PlutusScriptV1
script (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) AssetName
assetName Quantity
quantity =
  let sh :: ScriptHash
sh = forall lang. Script lang -> ScriptHash
C.hashScript (forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 PlutusScript PlutusScriptV1
script)
      v :: Value
v = ScriptHash -> AssetName -> Quantity -> Value
assetValue ScriptHash
sh AssetName
assetName Quantity
quantity
      policyId :: PolicyId
policyId = ScriptHash -> PolicyId
C.PolicyId ScriptHash
sh
      wit :: ScriptWitness WitCtxMint BabbageEra
wit      = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV1 BabbageEra
C.PlutusScriptV1InBabbage PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 (forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript PlutusScript PlutusScriptV1
script) (ScriptDatum WitCtxMint
C.NoScriptDatumForMint) HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
  in  forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxMintValue v BabbageEra)
L.txMintValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso'
  (TxMintValue BuildTx BabbageEra)
  (Value, Map PolicyId (ScriptWitness WitCtxMint BabbageEra))
L._TxMintValue) (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field1 s t a b => Lens s t a b
_1 (forall a. Semigroup a => a -> a -> a
<> Value
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field2 s t a b => Lens s t a b
_2 (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert PolicyId
policyId ScriptWitness WitCtxMint BabbageEra
wit)))

{-| A value containing the given amount of the native asset
-}
assetValue :: ScriptHash -> C.AssetName -> C.Quantity -> C.Value
assetValue :: ScriptHash -> AssetName -> Quantity -> Value
assetValue ScriptHash
hsh AssetName
assetName Quantity
quantity =
  [(AssetId, Quantity)] -> Value
C.valueFromList [(PolicyId -> AssetName -> AssetId
C.AssetId (ScriptHash -> PolicyId
C.PolicyId ScriptHash
hsh) AssetName
assetName, Quantity
quantity)]

mintPlutusV2 :: forall redeemer m. (Plutus.ToData redeemer, MonadBuildTx m) => PlutusScript PlutusScriptV2 -> redeemer -> C.AssetName -> C.Quantity -> m ()
mintPlutusV2 :: forall redeemer (m :: * -> *).
(ToData redeemer, MonadBuildTx m) =>
PlutusScript PlutusScriptV2
-> redeemer -> AssetName -> Quantity -> m ()
mintPlutusV2 PlutusScript PlutusScriptV2
script (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) AssetName
assetName Quantity
quantity =
  let sh :: ScriptHash
sh = forall lang. Script lang -> ScriptHash
C.hashScript (forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 PlutusScript PlutusScriptV2
script)
      v :: Value
v = ScriptHash -> AssetName -> Quantity -> Value
assetValue ScriptHash
sh AssetName
assetName Quantity
quantity
      policyId :: PolicyId
policyId = ScriptHash -> PolicyId
C.PolicyId ScriptHash
sh
      wit :: ScriptWitness WitCtxMint BabbageEra
wit      = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript PlutusScript PlutusScriptV2
script) (ScriptDatum WitCtxMint
C.NoScriptDatumForMint) HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
  in  forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxMintValue v BabbageEra)
L.txMintValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso'
  (TxMintValue BuildTx BabbageEra)
  (Value, Map PolicyId (ScriptWitness WitCtxMint BabbageEra))
L._TxMintValue) (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field1 s t a b => Lens s t a b
_1 (forall a. Semigroup a => a -> a -> a
<> Value
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field2 s t a b => Lens s t a b
_2 (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert PolicyId
policyId ScriptWitness WitCtxMint BabbageEra
wit)))

mintPlutusV2Ref :: forall redeemer m. (Plutus.ToData redeemer, MonadBuildTx m) => C.TxIn -> C.ScriptHash -> redeemer -> C.AssetName -> C.Quantity -> m ()
mintPlutusV2Ref :: forall redeemer (m :: * -> *).
(ToData redeemer, MonadBuildTx m) =>
TxIn -> ScriptHash -> redeemer -> AssetName -> Quantity -> m ()
mintPlutusV2Ref TxIn
refTxIn ScriptHash
sh (forall a. ToData a => a -> HashableScriptData
toHashableScriptData -> HashableScriptData
red) AssetName
assetName Quantity
quantity =
  let v :: Value
v = ScriptHash -> AssetName -> Quantity -> Value
assetValue ScriptHash
sh AssetName
assetName Quantity
quantity
      wit :: ScriptWitness WitCtxMint BabbageEra
wit = forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> HashableScriptData
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript TxIn
refTxIn (forall a. a -> Maybe a
Just ScriptHash
sh)) (ScriptDatum WitCtxMint
C.NoScriptDatumForMint) HashableScriptData
red (Natural -> Natural -> ExecutionUnits
C.ExecutionUnits Natural
0 Natural
0)
      policyId :: PolicyId
policyId = ScriptHash -> PolicyId
C.PolicyId ScriptHash
sh
  in  forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid
      forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxMintValue v BabbageEra)
L.txMintValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso'
  (TxMintValue BuildTx BabbageEra)
  (Value, Map PolicyId (ScriptWitness WitCtxMint BabbageEra))
L._TxMintValue) (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field1 s t a b => Lens s t a b
_1 (forall a. Semigroup a => a -> a -> a
<> Value
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field2 s t a b => Lens s t a b
_2 (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert PolicyId
policyId ScriptWitness WitCtxMint BabbageEra
wit)))
      forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). MonadBuildTx m => TxIn -> m ()
addReference TxIn
refTxIn

addCollateral :: MonadBuildTx m => C.TxIn -> m ()
addCollateral :: forall (m :: * -> *). MonadBuildTx m => TxIn -> m ()
addCollateral TxIn
i = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxInsCollateral BabbageEra)
L.txInsCollateral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' (TxInsCollateral BabbageEra) [TxIn]
L._TxInsCollateral) ((:) TxIn
i)

addReference :: MonadBuildTx m => C.TxIn -> m ()
addReference :: forall (m :: * -> *). MonadBuildTx m => TxIn -> m ()
addReference TxIn
i = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxInsReference v BabbageEra)
L.txInsReference forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall build. Iso' (TxInsReference build BabbageEra) [TxIn]
L._TxInsReference) ((:) TxIn
i)

addAuxScript :: MonadBuildTx m => C.ScriptInEra C.BabbageEra -> m ()
addAuxScript :: forall (m :: * -> *).
MonadBuildTx m =>
ScriptInEra BabbageEra -> m ()
addAuxScript ScriptInEra BabbageEra
s = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxAuxScripts BabbageEra)
L.txAuxScripts forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' (TxAuxScripts BabbageEra) [ScriptInEra BabbageEra]
L._TxAuxScripts) ((:) ScriptInEra BabbageEra
s))

payToAddressTxOut :: C.AddressInEra C.BabbageEra -> C.Value -> C.TxOut C.CtxTx C.BabbageEra
payToAddressTxOut :: AddressInEra BabbageEra -> Value -> TxOut CtxTx BabbageEra
payToAddressTxOut AddressInEra BabbageEra
addr Value
vl = forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
addr (forall era. MultiAssetSupportedInEra era -> Value -> TxOutValue era
C.TxOutValue MultiAssetSupportedInEra BabbageEra
C.MultiAssetInBabbageEra Value
vl) forall ctx era. TxOutDatum ctx era
C.TxOutDatumNone forall era. ReferenceScript era
C.ReferenceScriptNone

payToAddress :: MonadBuildTx m => C.AddressInEra C.BabbageEra -> C.Value -> m ()
payToAddress :: forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra -> Value -> m ()
payToAddress AddressInEra BabbageEra
addr Value
vl = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v.
Lens' (TxBodyContent v BabbageEra) [TxOut CtxTx BabbageEra]
L.txOuts ((:) (AddressInEra BabbageEra -> Value -> TxOut CtxTx BabbageEra
payToAddressTxOut AddressInEra BabbageEra
addr Value
vl))

payToPublicKey :: MonadBuildTx m => NetworkId -> Hash PaymentKey -> C.Value -> m ()
payToPublicKey :: forall (m :: * -> *).
MonadBuildTx m =>
NetworkId -> Hash PaymentKey -> Value -> m ()
payToPublicKey NetworkId
network Hash PaymentKey
pk Value
vl =
  let val :: TxOutValue BabbageEra
val = forall era. MultiAssetSupportedInEra era -> Value -> TxOutValue era
C.TxOutValue MultiAssetSupportedInEra BabbageEra
C.MultiAssetInBabbageEra Value
vl
      addr :: AddressInEra BabbageEra
addr = forall era.
IsShelleyBasedEra era =>
NetworkId
-> PaymentCredential -> StakeAddressReference -> AddressInEra era
C.makeShelleyAddressInEra NetworkId
network (Hash PaymentKey -> PaymentCredential
C.PaymentCredentialByKey Hash PaymentKey
pk) StakeAddressReference
C.NoStakeAddress
      txo :: TxOut ctx BabbageEra
txo = forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
addr TxOutValue BabbageEra
val forall ctx era. TxOutDatum ctx era
C.TxOutDatumNone forall era. ReferenceScript era
C.ReferenceScriptNone
  in forall (m :: * -> *).
MonadBuildTx m =>
TxOut CtxTx BabbageEra -> m ()
prependTxOut forall {ctx}. TxOut ctx BabbageEra
txo

payToScriptHash :: MonadBuildTx m => NetworkId -> ScriptHash -> HashableScriptData -> C.StakeAddressReference -> C.Value -> m ()
payToScriptHash :: forall (m :: * -> *).
MonadBuildTx m =>
NetworkId
-> ScriptHash
-> HashableScriptData
-> StakeAddressReference
-> Value
-> m ()
payToScriptHash NetworkId
network ScriptHash
script HashableScriptData
datum StakeAddressReference
stakeAddress Value
vl =
  let val :: TxOutValue BabbageEra
val = forall era. MultiAssetSupportedInEra era -> Value -> TxOutValue era
C.TxOutValue MultiAssetSupportedInEra BabbageEra
C.MultiAssetInBabbageEra Value
vl
      addr :: AddressInEra BabbageEra
addr = forall era.
IsShelleyBasedEra era =>
NetworkId
-> PaymentCredential -> StakeAddressReference -> AddressInEra era
C.makeShelleyAddressInEra NetworkId
network (ScriptHash -> PaymentCredential
C.PaymentCredentialByScript ScriptHash
script) StakeAddressReference
stakeAddress
      dat :: TxOutDatum CtxTx BabbageEra
dat = forall era.
ScriptDataSupportedInEra era
-> HashableScriptData -> TxOutDatum CtxTx era
C.TxOutDatumInTx ScriptDataSupportedInEra BabbageEra
C.ScriptDataInBabbageEra HashableScriptData
datum
      txo :: TxOut CtxTx BabbageEra
txo = forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
addr TxOutValue BabbageEra
val TxOutDatum CtxTx BabbageEra
dat forall era. ReferenceScript era
C.ReferenceScriptNone
  in forall (m :: * -> *).
MonadBuildTx m =>
TxOut CtxTx BabbageEra -> m ()
prependTxOut TxOut CtxTx BabbageEra
txo

payToPlutusV1 :: forall a m. (MonadBuildTx m, Plutus.ToData a) => NetworkId -> PlutusScript PlutusScriptV1 -> a -> C.StakeAddressReference -> C.Value -> m ()
payToPlutusV1 :: forall a (m :: * -> *).
(MonadBuildTx m, ToData a) =>
NetworkId
-> PlutusScript PlutusScriptV1
-> a
-> StakeAddressReference
-> Value
-> m ()
payToPlutusV1 NetworkId
network PlutusScript PlutusScriptV1
s a
datum StakeAddressReference
stakeRef Value
vl =
  let sh :: ScriptHash
sh = forall lang. Script lang -> ScriptHash
C.hashScript (forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 PlutusScript PlutusScriptV1
s)
      dt :: HashableScriptData
dt = forall a. ToData a => a -> HashableScriptData
toHashableScriptData a
datum
  in forall (m :: * -> *).
MonadBuildTx m =>
NetworkId
-> ScriptHash
-> HashableScriptData
-> StakeAddressReference
-> Value
-> m ()
payToScriptHash NetworkId
network ScriptHash
sh HashableScriptData
dt StakeAddressReference
stakeRef Value
vl

payToPlutusV2 :: forall a m. (MonadBuildTx m, Plutus.ToData a) => NetworkId -> PlutusScript PlutusScriptV2 -> a -> C.StakeAddressReference -> C.Value -> m ()
payToPlutusV2 :: forall a (m :: * -> *).
(MonadBuildTx m, ToData a) =>
NetworkId
-> PlutusScript PlutusScriptV2
-> a
-> StakeAddressReference
-> Value
-> m ()
payToPlutusV2 NetworkId
network PlutusScript PlutusScriptV2
s a
datum StakeAddressReference
stakeRef Value
vl =
  let sh :: ScriptHash
sh = forall lang. Script lang -> ScriptHash
C.hashScript (forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 PlutusScript PlutusScriptV2
s)
      dt :: HashableScriptData
dt = forall a. ToData a => a -> HashableScriptData
toHashableScriptData a
datum
  in forall (m :: * -> *).
MonadBuildTx m =>
NetworkId
-> ScriptHash
-> HashableScriptData
-> StakeAddressReference
-> Value
-> m ()
payToScriptHash NetworkId
network ScriptHash
sh HashableScriptData
dt StakeAddressReference
stakeRef Value
vl

payToPlutusV2InlineBase :: MonadBuildTx m => C.AddressInEra C.BabbageEra -> C.PlutusScript C.PlutusScriptV2 -> C.TxOutDatum C.CtxTx C.BabbageEra -> C.Value -> m ()
payToPlutusV2InlineBase :: forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2
-> TxOutDatum CtxTx BabbageEra
-> Value
-> m ()
payToPlutusV2InlineBase AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script TxOutDatum CtxTx BabbageEra
dat Value
vl =
  let refScript :: ReferenceScript BabbageEra
refScript = forall era.
ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> ScriptInAnyLang -> ReferenceScript era
C.ReferenceScript ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
C.ReferenceTxInsScriptsInlineDatumsInBabbageEra (forall lang. Script lang -> ScriptInAnyLang
C.toScriptInAnyLang forall a b. (a -> b) -> a -> b
$ forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 PlutusScript PlutusScriptV2
script)
      txo :: TxOut CtxTx BabbageEra
txo = forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
addr (forall era. MultiAssetSupportedInEra era -> Value -> TxOutValue era
C.TxOutValue MultiAssetSupportedInEra BabbageEra
C.MultiAssetInBabbageEra Value
vl) TxOutDatum CtxTx BabbageEra
dat ReferenceScript BabbageEra
refScript
  in forall (m :: * -> *).
MonadBuildTx m =>
TxOut CtxTx BabbageEra -> m ()
prependTxOut TxOut CtxTx BabbageEra
txo

payToPlutusV2Inline :: MonadBuildTx m => C.AddressInEra C.BabbageEra -> PlutusScript PlutusScriptV2 -> C.Value -> m ()
payToPlutusV2Inline :: forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2 -> Value -> m ()
payToPlutusV2Inline AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script Value
vl = forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2
-> TxOutDatum CtxTx BabbageEra
-> Value
-> m ()
payToPlutusV2InlineBase AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script forall ctx era. TxOutDatum ctx era
C.TxOutDatumNone Value
vl

{-| same as payToPlutusV2Inline but also specify an inline datum -}
payToPlutusV2InlineWithInlineDatum :: forall a m. (MonadBuildTx m, Plutus.ToData a) => C.AddressInEra C.BabbageEra -> C.PlutusScript C.PlutusScriptV2 -> a -> C.Value -> m ()
payToPlutusV2InlineWithInlineDatum :: forall a (m :: * -> *).
(MonadBuildTx m, ToData a) =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2 -> a -> Value -> m ()
payToPlutusV2InlineWithInlineDatum AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script a
datum Value
vl =
  let dat :: TxOutDatum ctx BabbageEra
dat = forall era ctx.
ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> HashableScriptData -> TxOutDatum ctx era
C.TxOutDatumInline ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
C.ReferenceTxInsScriptsInlineDatumsInBabbageEra (forall a. ToData a => a -> HashableScriptData
toHashableScriptData a
datum)
  in forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2
-> TxOutDatum CtxTx BabbageEra
-> Value
-> m ()
payToPlutusV2InlineBase AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script forall {ctx}. TxOutDatum ctx BabbageEra
dat Value
vl

{-| same as payToPlutusV2Inline but also specify a datum -}
payToPlutusV2InlineWithDatum :: forall a m. (MonadBuildTx m, Plutus.ToData a) => C.AddressInEra C.BabbageEra -> C.PlutusScript C.PlutusScriptV2 -> a -> C.Value -> m ()
payToPlutusV2InlineWithDatum :: forall a (m :: * -> *).
(MonadBuildTx m, ToData a) =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2 -> a -> Value -> m ()
payToPlutusV2InlineWithDatum AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script a
datum Value
vl =
  let dat :: TxOutDatum CtxTx BabbageEra
dat = forall era.
ScriptDataSupportedInEra era
-> HashableScriptData -> TxOutDatum CtxTx era
C.TxOutDatumInTx ScriptDataSupportedInEra BabbageEra
C.ScriptDataInBabbageEra (forall a. ToData a => a -> HashableScriptData
toHashableScriptData a
datum)
  in forall (m :: * -> *).
MonadBuildTx m =>
AddressInEra BabbageEra
-> PlutusScript PlutusScriptV2
-> TxOutDatum CtxTx BabbageEra
-> Value
-> m ()
payToPlutusV2InlineBase AddressInEra BabbageEra
addr PlutusScript PlutusScriptV2
script TxOutDatum CtxTx BabbageEra
dat Value
vl

payToPlutusV2InlineDatum :: forall a m. (MonadBuildTx m, Plutus.ToData a) => NetworkId -> PlutusScript PlutusScriptV2 -> a -> C.StakeAddressReference -> C.Value -> m ()
payToPlutusV2InlineDatum :: forall a (m :: * -> *).
(MonadBuildTx m, ToData a) =>
NetworkId
-> PlutusScript PlutusScriptV2
-> a
-> StakeAddressReference
-> Value
-> m ()
payToPlutusV2InlineDatum NetworkId
network PlutusScript PlutusScriptV2
script a
datum StakeAddressReference
stakeRef Value
vl =
  let val :: TxOutValue BabbageEra
val = forall era. MultiAssetSupportedInEra era -> Value -> TxOutValue era
C.TxOutValue MultiAssetSupportedInEra BabbageEra
C.MultiAssetInBabbageEra Value
vl
      sh :: ScriptHash
sh  = forall lang. Script lang -> ScriptHash
C.hashScript (forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 PlutusScript PlutusScriptV2
script)
      addr :: AddressInEra BabbageEra
addr = forall era.
IsShelleyBasedEra era =>
NetworkId
-> PaymentCredential -> StakeAddressReference -> AddressInEra era
C.makeShelleyAddressInEra NetworkId
network (ScriptHash -> PaymentCredential
C.PaymentCredentialByScript ScriptHash
sh) StakeAddressReference
stakeRef
      dat :: TxOutDatum ctx BabbageEra
dat = forall era ctx.
ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> HashableScriptData -> TxOutDatum ctx era
C.TxOutDatumInline ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
C.ReferenceTxInsScriptsInlineDatumsInBabbageEra (forall a. ToData a => a -> HashableScriptData
toHashableScriptData a
datum)
      txo :: TxOut ctx BabbageEra
txo = forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
addr TxOutValue BabbageEra
val forall {ctx}. TxOutDatum ctx BabbageEra
dat forall era. ReferenceScript era
C.ReferenceScriptNone
  in forall (m :: * -> *).
MonadBuildTx m =>
TxOut CtxTx BabbageEra -> m ()
prependTxOut forall {ctx}. TxOut ctx BabbageEra
txo
-- TODO: Functions for building outputs (Output -> Output)

setScriptsValid :: MonadBuildTx m => m ()
setScriptsValid :: forall (m :: * -> *). MonadBuildTx m => m ()
setScriptsValid = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> b -> s -> t
set forall v e. Lens' (TxBodyContent v e) (TxScriptValidity e)
L.txScriptValidity (forall era.
TxScriptValiditySupportedInEra era
-> ScriptValidity -> TxScriptValidity era
C.TxScriptValidity TxScriptValiditySupportedInEra BabbageEra
C.TxScriptValiditySupportedInBabbageEra ScriptValidity
C.ScriptValid)

{-| Set the Ada component in an output's value to at least the amount needed to cover the
minimum UTxO deposit for this output
-}
setMinAdaDeposit :: C.BundledProtocolParameters C.BabbageEra -> C.TxOut C.CtxTx C.BabbageEra -> C.TxOut C.CtxTx C.BabbageEra
setMinAdaDeposit :: BundledProtocolParameters BabbageEra
-> TxOut CtxTx BabbageEra -> TxOut CtxTx BabbageEra
setMinAdaDeposit BundledProtocolParameters BabbageEra
params TxOut CtxTx BabbageEra
txOut =
  let minUtxo :: Quantity
minUtxo = BundledProtocolParameters BabbageEra
-> TxOut CtxTx BabbageEra -> Quantity
minAdaDeposit BundledProtocolParameters BabbageEra
params TxOut CtxTx BabbageEra
txOut
  in TxOut CtxTx BabbageEra
txOut forall a b. a -> (a -> b) -> b
& forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall ctx era.
Iso'
  (TxOut ctx era)
  (AddressInEra era, TxOutValue era, TxOutDatum ctx era,
   ReferenceScript era)
L._TxOut forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. Field2 s t a b => Lens s t a b
_2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' (TxOutValue BabbageEra) Value
L._TxOutValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' Value (Map AssetId Quantity)
L._Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at AssetId
C.AdaAssetId) (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. a -> Maybe a
Just Quantity
minUtxo) (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => a -> a -> a
max Quantity
minUtxo))

minAdaDeposit :: C.BundledProtocolParameters C.BabbageEra -> C.TxOut C.CtxTx C.BabbageEra -> C.Quantity
minAdaDeposit :: BundledProtocolParameters BabbageEra
-> TxOut CtxTx BabbageEra -> Quantity
minAdaDeposit BundledProtocolParameters BabbageEra
params TxOut CtxTx BabbageEra
txOut =
  let txo :: TxOut CtxTx BabbageEra
txo = TxOut CtxTx BabbageEra
txOut
              -- set the Ada value to a dummy amount to ensure that it is not 0 (if it was 0, the size of the output
              -- would be smaller, causing 'calculateMinimumUTxO' to compute an amount that is a little too small)
              forall a b. a -> (a -> b) -> b
& forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall ctx era.
Iso'
  (TxOut ctx era)
  (AddressInEra era, TxOutValue era, TxOutDatum ctx era,
   ReferenceScript era)
L._TxOut forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. Field2 s t a b => Lens s t a b
_2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' (TxOutValue BabbageEra) Value
L._TxOutValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' Value (Map AssetId Quantity)
L._Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at AssetId
C.AdaAssetId) (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer -> Quantity
C.Quantity Integer
3_000_000) forall a. a -> Maybe a
Just)
  in forall a. a -> Maybe a -> a
fromMaybe (Integer -> Quantity
C.Quantity Integer
0) forall a b. (a -> b) -> a -> b
$ do
        let C.Lovelace Integer
l = forall era.
ShelleyBasedEra era
-> TxOut CtxTx era -> BundledProtocolParameters era -> Lovelace
C.calculateMinimumUTxO ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage TxOut CtxTx BabbageEra
txo BundledProtocolParameters BabbageEra
params
        forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Quantity
C.Quantity Integer
l)

{-| Apply 'setMinAdaDeposit' to all outputs
-}
setMinAdaDepositAll :: MonadBuildTx m => C.BundledProtocolParameters C.BabbageEra -> m ()
setMinAdaDepositAll :: forall (m :: * -> *).
MonadBuildTx m =>
BundledProtocolParameters BabbageEra -> m ()
setMinAdaDepositAll BundledProtocolParameters BabbageEra
params = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) [TxOut CtxTx BabbageEra]
L.txOuts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => Setter (f a) (f b) a b
mapped) (BundledProtocolParameters BabbageEra
-> TxOut CtxTx BabbageEra -> TxOut CtxTx BabbageEra
setMinAdaDeposit BundledProtocolParameters BabbageEra
params)

{-| Add a public key hash to the list of required signatures.
-}
addRequiredSignature :: MonadBuildTx m => Hash PaymentKey -> m ()
addRequiredSignature :: forall (m :: * -> *). MonadBuildTx m => Hash PaymentKey -> m ()
addRequiredSignature Hash PaymentKey
sig =
  forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall v.
Lens' (TxBodyContent v BabbageEra) (TxExtraKeyWitnesses BabbageEra)
L.txExtraKeyWits forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' (TxExtraKeyWitnesses BabbageEra) [Hash PaymentKey]
L._TxExtraKeyWitnesses) (forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:) Hash PaymentKey
sig)

{-| Add a transaction output to the start of the list of transaction outputs.
-}
prependTxOut :: MonadBuildTx m => C.TxOut C.CtxTx C.BabbageEra -> m ()
prependTxOut :: forall (m :: * -> *).
MonadBuildTx m =>
TxOut CtxTx BabbageEra -> m ()
prependTxOut TxOut CtxTx BabbageEra
txOut = forall (m :: * -> *).
MonadBuildTx m =>
(TxBodyContent BuildTx BabbageEra
 -> TxBodyContent BuildTx BabbageEra)
-> m ()
addBtx (forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall v.
Lens' (TxBodyContent v BabbageEra) [TxOut CtxTx BabbageEra]
L.txOuts ((:) TxOut CtxTx BabbageEra
txOut))