Skip to content

Commit 20e8219

Browse files
committed
minor fixs
1 parent 941d22b commit 20e8219

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import Data.List qualified as List
3737
import Data.Map (Map)
3838
import Data.Map qualified as Map
3939
import Data.Maybe (fromMaybe, mapMaybe)
40-
import Data.Proxy (Proxy (Proxy))
4140
import Data.Set (Set)
4241
import Data.Set qualified as Set
4342
import Data.Text (Text)
@@ -79,11 +78,12 @@ import Prettyprinter (pretty, viaShow, (<+>))
7978
import Prelude
8079

8180
data TxWithScript
81+
8282
data 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

8989
instance 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+
95102
class KnownBalanceConstraint (a :: Type) where
96-
knownConstraint :: proxy a -> BalanceTxConstraint a
103+
knownConstraint :: BalanceTxConstraint a
97104

98105
instance KnownBalanceConstraint TxWithScript where
99-
knownConstraint _ = TxWithScript
106+
knownConstraint = TxWithScript
100107

101108
instance 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
-}
109116
balanceTxIO ::
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
-}
121129
balanceTxIO' ::
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 =
228235
utxosAndCollateralAtAddress ::
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
422429
addAdaChange _ _ 0 tx = tx
423430
addAdaChange balanceTxconstraints changeAddr change tx
424431
| TxWithSeparateChange `elem` balanceTxconstraints =

src/BotPlutusInterface/Contract.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,14 @@ balanceTx contractEnv unbalancedTx = do
297297
eitherPreBalancedTx <-
298298
if PreBalance.txUsesScripts (unBalancedTxTx unbalancedTx)
299299
then
300-
PreBalance.balanceTxIO' @w
300+
PreBalance.balanceTxIO @w
301301
[PreBalance.TxWithScript]
302302
pabConf
303303
pabConf.pcOwnPubKeyHash
304304
unbalancedTx
305305
else
306306
PreBalance.balanceTxIO @w
307+
[PreBalance.TxWithoutScript]
307308
pabConf
308309
pabConf.pcOwnPubKeyHash
309310
unbalancedTx
@@ -507,7 +508,7 @@ makeCollateral cEnv = runEitherT $ do
507508

508509
balancedTx <-
509510
newEitherT $
510-
PreBalance.balanceTxIO' @w
511+
PreBalance.balanceTxIO @w
511512
[PreBalance.TxWithoutScript, PreBalance.TxWithSeparateChange]
512513
pabConf
513514
pabConf.pcOwnPubKeyHash unbalancedTx

0 commit comments

Comments
 (0)