11-- | A module that defines the different transaction data types, balancing
22-- | functionality, transaction fees, signing and submission.
33module Contract.Transaction
4- ( TxBlueprint
4+ ( module BalanceTxError
5+ , module X
6+ , TxBlueprint
57 , TxReceipt
68 , balanceMultipleTxs
79 , balanceTx
810 , balanceTxE
911 , balanceTxs
12+ , buildTx
1013 , createAdditionalUtxos
1114 , getTxAuxiliaryData
12- , module BalanceTxError
13- , module X
15+ , hashTransaction
16+ , lookupTxHash
17+ , mkPoolPubKeyHash
1418 , submit
1519 , submitE
20+ , submitTxFromBlueprint
21+ , submitTxFromBuildPlan
1622 , submitTxFromConstraints
1723 , withBalancedTx
1824 , withBalancedTxs
19- , lookupTxHash
20- , mkPoolPubKeyHash
21- , hashTransaction
22- , buildTx
23- , submitTxFromBlueprint
24- , submitTxFromBuildPlan
2525 ) where
2626
2727import Prelude
@@ -106,6 +106,7 @@ import Ctl.Internal.BalanceTx
106106 , CtlBalancerContext
107107 , defaultBalancer
108108 , defaultBalancerErr
109+ , emptyBalancerCtx
109110 ) as X
110111import Ctl.Internal.BalanceTx (defaultBalancerErr )
111112import Ctl.Internal.Contract.AwaitTxConfirmed
@@ -137,8 +138,8 @@ import Data.Maybe (Maybe(Nothing))
137138import Data.Newtype (unwrap )
138139import Data.String.Utils (startsWith )
139140import Data.Traversable (class Traversable , for_ , traverse , traverse_ )
140- import Data.Tuple (fst , uncurry )
141- import Data.Tuple.Nested (type (/\), (/\))
141+ import Data.Tuple (fst )
142+ import Data.Tuple.Nested ((/\))
142143import Data.UInt (UInt )
143144import Effect.Aff (bracket , error )
144145import Effect.Aff.Class (liftAff )
@@ -232,49 +233,45 @@ withSingleTransaction prepare extract utx action =
232233 (action <<< NonEmptyArray .head)
233234
234235-- | Execute an action on an array of balanced
235- -- | transactions (`balanceTxs ` will be called). Within
236+ -- | transactions (`balanceMultipleTxs ` will be called). Within
236237-- | this function, all transaction inputs used by these
237238-- | transactions will be locked, so that they are not used
238239-- | in any other context.
239240-- | After the function completes, the locks will be removed.
240241-- | Errors will be thrown.
241242withBalancedTxs
242- :: forall (a :: Type )
243- . Array
243+ :: forall (ctx :: Type ) (a :: Type )
244+ . TxBalancer Contract Error ctx
245+ -> Array
244246 { transaction :: Transaction
245- , usedUtxos :: UtxoMap
246- , balancerConstraints :: BalancerConstraints
247+ , balancerCtx :: ctx
247248 }
248249 -> (Array Transaction -> Contract a )
249250 -> Contract a
250- withBalancedTxs = withTransactions balanceTxs identity
251+ withBalancedTxs balancer = withTransactions (balanceMultipleTxs balancer)
252+ identity
251253
252- -- | Execute an action on a balanced transaction (`balanceTx` will
254+ -- | Execute an action on a balanced transaction (the provided balancer will
253255-- | be called). Within this function, all transaction inputs
254256-- | used by this transaction will be locked, so that they are not
255257-- | used in any other context.
256258-- | After the function completes, the locks will be removed.
257259-- | Errors will be thrown.
258260withBalancedTx
259- :: forall (a :: Type )
260- . Transaction
261- -> UtxoMap
262- -> BalancerConstraints
261+ :: forall (ctx :: Type ) ( a :: Type )
262+ . TxBalancer Contract Error ctx
263+ -> Transaction
264+ -> ctx
263265 -> (Transaction -> Contract a )
264266 -> Contract a
265- withBalancedTx tx usedUtxos balancerConstraints =
266- withSingleTransaction
267- ( \transaction -> balanceAndLock
268- { transaction, usedUtxos, balancerConstraints }
269- )
270- identity
271- tx
267+ withBalancedTx balancer transaction ctx =
268+ withSingleTransaction (liftEither <=< flip balancer ctx) identity transaction
272269
273270-- | A variant of `balanceTx` that returns a balancer error value.
274271balanceTxE
275272 :: Warn
276273 ( Text
277- " Deprecated, use a standalone transaction balancer instead (see `defaultBalancer `)"
274+ " Deprecated, use a standalone transaction balancer instead (see `defaultBalancerErr `)"
278275 )
279276 => Transaction
280277 -> UtxoMap
@@ -329,16 +326,19 @@ balanceTxs unbalancedTxs =
329326balanceMultipleTxs
330327 :: forall (ctx :: Type )
331328 . TxBalancer Contract Error ctx
332- -> Array (Transaction /\ ctx )
329+ -> Array
330+ { transaction :: Transaction
331+ , balancerCtx :: ctx
332+ }
333333 -> Contract (Array Transaction )
334334balanceMultipleTxs balancer unbalancedTxs =
335- unlockAllUtxosOnError $ traverse (uncurry ( balanceAndLockUtxos balancer) )
335+ unlockAllUtxosOnError $ traverse (balanceAndLockUtxos balancer)
336336 unbalancedTxs
337337 where
338338 unlockAllUtxosOnError :: forall (a :: Type ). Contract a -> Contract a
339339 unlockAllUtxosOnError f =
340340 catchError f $ \err -> do
341- traverse_ (withUsedTxOuts <<< unlockTransactionInputs <<< fst )
341+ traverse_ (withUsedTxOuts <<< unlockTransactionInputs <<< _ . transaction )
342342 unbalancedTxs
343343 throwError err
344344
@@ -357,11 +357,12 @@ balanceAndLock { transaction, usedUtxos, balancerConstraints } = do
357357balanceAndLockUtxos
358358 :: forall (ctx :: Type )
359359 . TxBalancer Contract Error ctx
360- -> Transaction
361- -> ctx
360+ -> { transaction :: Transaction
361+ , balancerCtx :: ctx
362+ }
362363 -> Contract Transaction
363- balanceAndLockUtxos balancer transaction ctx = do
364- balancedTx <- liftEither =<< balancer transaction ctx
364+ balanceAndLockUtxos balancer { transaction, balancerCtx } = do
365+ balancedTx <- liftEither =<< balancer transaction balancerCtx
365366 void $ withUsedTxOuts $ lockTransactionInputs balancedTx
366367 pure balancedTx
367368
0 commit comments