Skip to content

Commit aa20a2b

Browse files
committed
Make withBalancedTx and withBalancedTxs balancer-agnostic
1 parent a37b8ba commit aa20a2b

File tree

7 files changed

+103
-79
lines changed

7 files changed

+103
-79
lines changed

examples/AdditionalUtxos.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import Contract.Transaction
4545
, buildTx
4646
, createAdditionalUtxos
4747
, defaultBalancer
48+
, emptyBalancerCtx
4849
, signTransaction
4950
, submit
5051
, submitTxFromBlueprint
@@ -54,7 +55,7 @@ import Contract.Utxos (UtxoMap)
5455
import Contract.Value (Value)
5556
import Contract.Value (lovelaceValueOf) as Value
5657
import Ctl.Examples.PlutusV2.Scripts.AlwaysSucceeds (alwaysSucceedsScriptV2)
57-
import Data.Map (difference, empty, filter) as Map
58+
import Data.Map (difference, filter) as Map
5859
import JS.BigInt (fromInt) as BigInt
5960
import Test.QuickCheck (arbitrary)
6061
import Test.QuickCheck.Gen (randomSampleOne)
@@ -75,7 +76,7 @@ contract testAdditionalUtxoOverlap = withoutSync do
7576
validator <- alwaysSucceedsScriptV2
7677
let vhash = PlutusScript.hash validator
7778
{ unbalancedTx, datum } <- payToValidator vhash
78-
withBalancedTx unbalancedTx Map.empty mempty \balancedTx -> do
79+
withBalancedTx defaultBalancer unbalancedTx emptyBalancerCtx \balancedTx -> do
7980
balancedSignedTx <- signTransaction balancedTx
8081
txHash <- submit balancedSignedTx
8182
when testAdditionalUtxoOverlap $ awaitTxConfirmed txHash

examples/KeyWallet/SignMultiple.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Contract.ScriptLookups as Lookups
99
import Contract.Transaction
1010
( TransactionHash
1111
, awaitTxConfirmed
12+
, defaultBalancer
1213
, signTransaction
1314
, submit
1415
, withBalancedTxs
@@ -46,14 +47,18 @@ main = runKeyWalletContract_ \pkh lovelace unlock -> do
4647
unbalancedTx1 /\ usedUtxos1 <- mkUnbalancedTx lookups constraints
4748

4849
txIds <-
49-
withBalancedTxs
50+
withBalancedTxs defaultBalancer
5051
[ { transaction: unbalancedTx0
51-
, usedUtxos: usedUtxos0
52-
, balancerConstraints: mempty
52+
, balancerCtx:
53+
{ balancerConstraints: mempty
54+
, extraUtxos: usedUtxos0
55+
}
5356
}
5457
, { transaction: unbalancedTx1
55-
, usedUtxos: usedUtxos1
56-
, balancerConstraints: mempty
58+
, balancerCtx:
59+
{ balancerConstraints: mempty
60+
, extraUtxos: usedUtxos1
61+
}
5762
}
5863
] $ \balancedTxs -> do
5964
locked <- getLockedInputs

examples/SignMultiple.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import Contract.Transaction
3939
, awaitTxConfirmed
4040
, awaitTxConfirmedWithTimeout
4141
, buildTx
42+
, defaultBalancer
43+
, emptyBalancerCtx
4244
, signTransaction
4345
, submit
4446
, submitTxFromBuildPlan
@@ -98,14 +100,12 @@ contract = do
98100
unbalancedTx1 <- buildTx plan
99101

100102
txIds <-
101-
withBalancedTxs
103+
withBalancedTxs defaultBalancer
102104
[ { transaction: unbalancedTx0
103-
, usedUtxos: Map.empty
104-
, balancerConstraints: mempty
105+
, balancerCtx: emptyBalancerCtx
105106
}
106107
, { transaction: unbalancedTx1
107-
, usedUtxos: Map.empty
108-
, balancerConstraints: mempty
108+
, balancerCtx: emptyBalancerCtx
109109
}
110110
] $ \balancedTxs -> do
111111
locked <- getLockedInputs

examples/TxChaining.purs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import Contract.Log (logInfo')
3636
import Contract.Monad (Contract, launchAff_, liftedM, runContract)
3737
import Contract.Transaction
3838
( awaitTxConfirmed
39-
, balanceTx
4039
, buildTx
4140
, createAdditionalUtxos
41+
, defaultBalancer
42+
, emptyBalancerCtx
4243
, signTransaction
4344
, submit
4445
, withBalancedTx
@@ -76,24 +77,27 @@ contract = do
7677

7778
unbalancedTx0 <- buildTx plan
7879

79-
withBalancedTx unbalancedTx0 Map.empty mempty \balancedTx0 -> do
80-
logInfo' $ "balanced"
81-
balancedSignedTx0 <- signTransaction balancedTx0
80+
withBalancedTx defaultBalancer unbalancedTx0 emptyBalancerCtx \balancedTx0 ->
81+
do
82+
logInfo' $ "balanced"
83+
balancedSignedTx0 <- signTransaction balancedTx0
8284

83-
additionalUtxos <- createAdditionalUtxos balancedSignedTx0
84-
logInfo' $ "Additional utxos: " <> show additionalUtxos
85-
when (Map.isEmpty additionalUtxos) do
86-
liftEffect $ throw "empty utxos"
87-
let
88-
balanceTxConstraints :: BalancerConstraints
89-
balanceTxConstraints =
90-
mustUseAdditionalUtxos additionalUtxos
91-
unbalancedTx1 <- buildTx plan
92-
balancedTx1 <- balanceTx unbalancedTx1 additionalUtxos balanceTxConstraints
93-
balancedSignedTx1 <- signTransaction balancedTx1
85+
additionalUtxos <- createAdditionalUtxos balancedSignedTx0
86+
logInfo' $ "Additional utxos: " <> show additionalUtxos
87+
when (Map.isEmpty additionalUtxos) do
88+
liftEffect $ throw "empty utxos"
89+
let
90+
balancerConstraints :: BalancerConstraints
91+
balancerConstraints = mustUseAdditionalUtxos additionalUtxos
92+
unbalancedTx1 <- buildTx plan
93+
balancedTx1 <- liftEither =<< defaultBalancer unbalancedTx1
94+
{ balancerConstraints
95+
, extraUtxos: additionalUtxos
96+
}
97+
balancedSignedTx1 <- signTransaction balancedTx1
9498

95-
txId0 <- submit balancedSignedTx0
96-
txId1 <- submit balancedSignedTx1
99+
txId0 <- submit balancedSignedTx0
100+
txId1 <- submit balancedSignedTx1
97101

98-
awaitTxConfirmed txId0
99-
awaitTxConfirmed txId1
102+
awaitTxConfirmed txId0
103+
awaitTxConfirmed txId1

src/Contract/Transaction.purs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
-- | A module that defines the different transaction data types, balancing
22
-- | functionality, transaction fees, signing and submission.
33
module 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

2727
import Prelude
@@ -106,6 +106,7 @@ import Ctl.Internal.BalanceTx
106106
, CtlBalancerContext
107107
, defaultBalancer
108108
, defaultBalancerErr
109+
, emptyBalancerCtx
109110
) as X
110111
import Ctl.Internal.BalanceTx (defaultBalancerErr)
111112
import Ctl.Internal.Contract.AwaitTxConfirmed
@@ -137,8 +138,8 @@ import Data.Maybe (Maybe(Nothing))
137138
import Data.Newtype (unwrap)
138139
import Data.String.Utils (startsWith)
139140
import 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 ((/\))
142143
import Data.UInt (UInt)
143144
import Effect.Aff (bracket, error)
144145
import 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.
241242
withBalancedTxs
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.
258260
withBalancedTx
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.
274271
balanceTxE
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 =
329326
balanceMultipleTxs
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)
334334
balanceMultipleTxs 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
357357
balanceAndLockUtxos
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

src/Internal/BalanceTx/BalanceTx.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Ctl.Internal.BalanceTx
33
, CtlBalancerContext
44
, defaultBalancer
55
, defaultBalancerErr
6+
, emptyBalancerCtx
67
) where
78

89
import Prelude
@@ -33,6 +34,7 @@ import Ctl.Internal.Contract.Wallet
3334
) as Wallet
3435
import Ctl.Internal.Types.TxBalancer (TxBalancer)
3536
import Data.Bifunctor (lmap)
37+
import Data.Map (empty) as Map
3638
import Data.Maybe (isNothing)
3739
import Data.Newtype (unwrap)
3840
import Effect.Aff.Class (liftAff)
@@ -43,6 +45,12 @@ type CtlBalancerContext =
4345
, extraUtxos :: UtxoMap
4446
}
4547

48+
emptyBalancerCtx :: CtlBalancerContext
49+
emptyBalancerCtx =
50+
{ balancerConstraints: mempty
51+
, extraUtxos: Map.empty
52+
}
53+
4654
type CtlBalancer (err :: Type) = TxBalancer Contract err CtlBalancerContext
4755

4856
defaultBalancer :: CtlBalancer Error

test/Testnet/Contract.purs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ import Contract.BalanceTxConstraints
5050
, mustUseCollateralUtxos
5151
)
5252
import Contract.Chain (currentTime, waitUntilSlot)
53-
import Contract.Config
54-
( KnownWallet(Eternl, Gero, Lode, NuFi)
55-
, walletName
56-
)
53+
import Contract.Config (KnownWallet(Eternl, Gero, Lode, NuFi), walletName)
5754
import Contract.Hashing (datumHash, nativeScriptHash)
5855
import Contract.Keys (privateKeyFromBytes)
5956
import Contract.Log (logInfo')
@@ -104,6 +101,8 @@ import Contract.Transaction
104101
, balanceTxE
105102
, buildTx
106103
, createAdditionalUtxos
104+
, defaultBalancer
105+
, emptyBalancerCtx
107106
, getTxAuxiliaryData
108107
, lookupTxHash
109108
, signTransaction
@@ -1672,7 +1671,8 @@ suite = do
16721671

16731672
unbalancedTx0 /\ usedUtxos0 <- mkUnbalancedTx lookups0 constraints0
16741673

1675-
withBalancedTx unbalancedTx0 usedUtxos0 mempty \balancedTx0 -> do
1674+
let ctx = emptyBalancerCtx { extraUtxos = usedUtxos0 }
1675+
withBalancedTx defaultBalancer unbalancedTx0 ctx \balancedTx0 -> do
16761676
balancedSignedTx0 <- signTransaction balancedTx0
16771677

16781678
additionalUtxos <- createAdditionalUtxos balancedSignedTx0
@@ -1787,7 +1787,8 @@ suite = do
17871787

17881788
unbalancedTx0 /\ usedUtxos <- mkUnbalancedTx lookups0 constraints0
17891789

1790-
withBalancedTx unbalancedTx0 usedUtxos mempty \balancedTx0 -> do
1790+
let ctx = emptyBalancerCtx { extraUtxos = usedUtxos }
1791+
withBalancedTx defaultBalancer unbalancedTx0 ctx \balancedTx0 -> do
17911792
balancedSignedTx0 <- signTransaction balancedTx0
17921793

17931794
additionalUtxos <- createAdditionalUtxos balancedSignedTx0
@@ -2131,14 +2132,18 @@ signMultipleContract = do
21312132
ubTx1 /\ usedUtxos1 <- mkUnbalancedTx lookups constraints
21322133
ubTx2 /\ usedUtxos2 <- mkUnbalancedTx lookups constraints
21332134

2134-
withBalancedTxs
2135+
withBalancedTxs defaultBalancer
21352136
[ { transaction: ubTx1
2136-
, usedUtxos: usedUtxos1
2137-
, balancerConstraints: mempty
2137+
, balancerCtx:
2138+
{ balancerConstraints: mempty
2139+
, extraUtxos: usedUtxos1
2140+
}
21382141
}
21392142
, { transaction: ubTx2
2140-
, usedUtxos: usedUtxos2
2141-
, balancerConstraints: mempty
2143+
, balancerCtx:
2144+
{ balancerConstraints: mempty
2145+
, extraUtxos: usedUtxos2
2146+
}
21422147
}
21432148
] $ \txs -> do
21442149
locked <- getLockedInputs

0 commit comments

Comments
 (0)