@@ -37,7 +37,6 @@ import Data.List qualified as List
3737import Data.Map (Map )
3838import Data.Map qualified as Map
3939import Data.Maybe (fromMaybe , mapMaybe )
40- import Data.Proxy (Proxy (Proxy ))
4140import Data.Set (Set )
4241import Data.Set qualified as Set
4342import Data.Text (Text )
@@ -79,11 +78,12 @@ import Prettyprinter (pretty, viaShow, (<+>))
7978import Prelude
8079
8180data TxWithScript
81+
8282data TxWithoutScript
8383
84- data BalanceTxConstraint a where
85- TxWithScript :: BalanceTxConstraint TxWithScript
84+ data BalanceTxConstraint (a :: Type ) where
8685 TxWithoutScript :: BalanceTxConstraint TxWithoutScript
86+ TxWithScript :: BalanceTxConstraint TxWithScript
8787 TxWithSeparateChange :: BalanceTxConstraint a
8888
8989instance Eq (BalanceTxConstraint a ) where
@@ -92,36 +92,44 @@ instance Eq (BalanceTxConstraint a) where
9292 TxWithSeparateChange == TxWithSeparateChange = True
9393 _ == _ = False
9494
95+ instance Ord (BalanceTxConstraint a ) where
96+ _ <= TxWithSeparateChange = True
97+ TxWithScript <= TxWithScript = True
98+ TxWithSeparateChange <= TxWithScript = False
99+ TxWithoutScript <= TxWithoutScript = True
100+ _ <= TxWithoutScript = False
101+
95102class KnownBalanceConstraint (a :: Type ) where
96- knownConstraint :: proxy a -> BalanceTxConstraint a
103+ knownConstraint :: BalanceTxConstraint a
97104
98105instance KnownBalanceConstraint TxWithScript where
99- knownConstraint _ = TxWithScript
106+ knownConstraint = TxWithScript
100107
101108instance KnownBalanceConstraint TxWithoutScript where
102- knownConstraint _ = TxWithoutScript
109+ knownConstraint = TxWithoutScript
103110
104111{- | Collect necessary tx inputs and collaterals, add minimum lovelace values and balance non ada
105112 assets. `balanceTxIO` assumes that the `Tx` we are balancing is of type `TxWithoutScript`.
106113 If you want to add custom constraints while balancing the `Tx` like, saying that `Tx` contains
107114 scripts refer to `balanceTxIO'`.
108115-}
109116balanceTxIO ::
110- forall (w :: Type ) (effs :: [Type -> Type ]).
111- (Member (PABEffect w ) effs ) =>
117+ forall (w :: Type ) (effs :: [Type -> Type ]) (a :: Type ).
118+ (Member (PABEffect w ) effs , KnownBalanceConstraint a ) =>
119+ [BalanceTxConstraint a ] ->
112120 PABConfig ->
113121 PubKeyHash ->
114122 UnbalancedTx ->
115123 Eff effs (Either Text Tx )
116- balanceTxIO = balanceTxIO' @ w [ TxWithoutScript ]
124+ balanceTxIO = balanceTxIO' @ w . Set. fromList
117125
118126{- | This is just a more flexible version of `balanceTxIO` which let us specify the `BalanceTxConstraint`(s)
119127 - for the `Tx` that we are balancing.
120128-}
121129balanceTxIO' ::
122130 forall (w :: Type ) (effs :: [Type -> Type ]) (a :: Type ).
123131 (Member (PABEffect w ) effs , KnownBalanceConstraint a ) =>
124- [ BalanceTxConstraint a ] ->
132+ Set ( BalanceTxConstraint a ) ->
125133 PABConfig ->
126134 PubKeyHash ->
127135 UnbalancedTx ->
@@ -139,7 +147,7 @@ balanceTxIO' balanceTxconstraints pabConf ownPkh unbalancedTx =
139147 requiredSigs = map Ledger. unPaymentPubKeyHash $ Map. keys (unBalancedTxRequiredSignatories unbalancedTx)
140148
141149 txType :: BalanceTxConstraint a
142- txType = knownConstraint @ a Proxy
150+ txType = knownConstraint @ a
143151
144152 lift $ printBpiLog @ w Debug $ viaShow utxoIndex
145153
@@ -173,8 +181,7 @@ balanceTxIO' balanceTxconstraints pabConf ownPkh unbalancedTx =
173181 case adaChange /= 0 of
174182 True | TxWithSeparateChange `elem` balanceTxconstraints -> bTx
175183 True | not (hasChangeUTxO changeAddr balancedTx) -> bTx
176- True -> pure balancedTx
177- False -> pure balancedTx
184+ _ -> pure balancedTx
178185
179186 -- Get the updated change, add it to the tx
180187 let finalAdaChange = getAdaChange utxoIndex balancedTxWithChange
@@ -228,7 +235,7 @@ balanceTxIO' balanceTxconstraints pabConf ownPkh unbalancedTx =
228235utxosAndCollateralAtAddress ::
229236 forall (w :: Type ) (effs :: [Type -> Type ]) (a :: Type ).
230237 (Member (PABEffect w ) effs , KnownBalanceConstraint a ) =>
231- [ BalanceTxConstraint a ] ->
238+ Set ( BalanceTxConstraint a ) ->
232239 PABConfig ->
233240 Address ->
234241 Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
@@ -237,7 +244,7 @@ utxosAndCollateralAtAddress _ pabConf changeAddr =
237244 utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
238245 inMemCollateral <- lift $ getInMemCollateral @ w
239246
240- case knownConstraint @ a Proxy of
247+ case knownConstraint @ a of
241248 TxWithScript ->
242249 maybe
243250 ( throwE $
@@ -418,7 +425,7 @@ handleNonAdaChange changeAddr utxos tx =
418425 constraints, if it is then we add the ada change to seperate `TxOut`, else we add it to
419426 any `TxOut` present at changeAddr.
420427-}
421- addAdaChange :: forall (a :: Type ). [ BalanceTxConstraint a ] -> Address -> Integer -> Tx -> Tx
428+ addAdaChange :: forall (a :: Type ). Set ( BalanceTxConstraint a ) -> Address -> Integer -> Tx -> Tx
422429addAdaChange _ _ 0 tx = tx
423430addAdaChange balanceTxconstraints changeAddr change tx
424431 | TxWithSeparateChange `elem` balanceTxconstraints =
0 commit comments