@@ -73,7 +73,7 @@ balanceTxIO ::
7373balanceTxIO pabConf ownPkh unbalancedTx =
7474 runEitherT $
7575 do
76- utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf $ Ledger. pubKeyHashAddress ( Ledger. PaymentPubKeyHash ownPkh) Nothing
76+ utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
7777 privKeys <- newEitherT $ Files. readPrivateKeys @ w pabConf
7878 let utxoIndex = fmap Tx. toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
7979 requiredSigs = map Ledger. unPaymentPubKeyHash $ Map. keys (unBalancedTxRequiredSignatories unbalancedTx)
@@ -101,17 +101,19 @@ balanceTxIO pabConf ownPkh unbalancedTx =
101101 -- If we have change but no change UTxO, we need to add an output for it
102102 -- We'll add a minimal output, run the loop again so it gets minUTxO, then update change
103103 balancedTxWithChange <-
104- if adaChange /= 0 && not (hasChangeUTxO ownPkh balancedTx)
105- then fst <$> loop utxoIndex privKeys minUtxos (addOutput ownPkh balancedTx)
104+ if adaChange /= 0 && not (hasChangeUTxO changeAddr balancedTx)
105+ then fst <$> loop utxoIndex privKeys minUtxos (addOutput changeAddr balancedTx)
106106 else pure balancedTx
107107
108108 -- Get the updated change, add it to the tx
109109 let finalAdaChange = getAdaChange utxoIndex balancedTxWithChange
110- fullyBalancedTx = addAdaChange ownPkh finalAdaChange balancedTxWithChange
110+ fullyBalancedTx = addAdaChange changeAddr finalAdaChange balancedTxWithChange
111111
112112 -- finally, we must update the signatories
113113 hoistEither $ addSignatories ownPkh privKeys requiredSigs fullyBalancedTx
114114 where
115+ changeAddr :: Address
116+ changeAddr = Ledger. pubKeyHashAddress (Ledger. PaymentPubKeyHash ownPkh) (pabConf. pcOwnStakePubKeyHash)
115117 loop ::
116118 Map TxOutRef TxOut ->
117119 Map PubKeyHash DummyPrivKey ->
@@ -130,7 +132,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
130132
131133 -- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
132134 txWithoutFees <-
133- hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
135+ hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` 0
134136
135137 exBudget <- newEitherT $ CardanoCLI. buildTx @ w pabConf privKeys txWithoutFees
136138 nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
@@ -140,7 +142,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
140142 lift $ printLog @ w Debug $ " Fees: " ++ show fees
141143
142144 -- Rebalance the initial tx with the above fees
143- balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` fees
145+ balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` fees
144146
145147 if balancedTx == tx
146148 then pure (balancedTx, minUtxos)
@@ -175,13 +177,13 @@ calculateMinUtxos pabConf datums txOuts =
175177balanceTxStep ::
176178 [(TxOut , Integer )] ->
177179 Map TxOutRef TxOut ->
178- PubKeyHash ->
180+ Address ->
179181 Tx ->
180182 Either Text Tx
181- balanceTxStep minUtxos utxos ownPkh tx =
183+ balanceTxStep minUtxos utxos changeAddr tx =
182184 Right (addLovelaces minUtxos tx)
183185 >>= balanceTxIns utxos
184- >>= handleNonAdaChange ownPkh utxos
186+ >>= handleNonAdaChange changeAddr utxos
185187
186188-- | Get change value of a transaction, taking inputs, outputs, mint and fees into account
187189getChange :: Map TxOutRef TxOut -> Tx -> Value
@@ -288,10 +290,9 @@ addTxCollaterals utxos tx = do
288290 filterAdaOnly = Map. filter (isAdaOnly . txOutValue)
289291
290292-- | Ensures all non ada change goes back to user
291- handleNonAdaChange :: PubKeyHash -> Map TxOutRef TxOut -> Tx -> Either Text Tx
292- handleNonAdaChange ownPkh utxos tx =
293- let changeAddr = Ledger. pubKeyHashAddress (Ledger. PaymentPubKeyHash ownPkh) Nothing
294- nonAdaChange = getNonAdaChange utxos tx
293+ handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
294+ handleNonAdaChange changeAddr utxos tx =
295+ let nonAdaChange = getNonAdaChange utxos tx
295296 outputs =
296297 case partition ((==) changeAddr . Tx. txOutAddress) $ txOutputs tx of
297298 ([] , txOuts) ->
@@ -307,38 +308,33 @@ handleNonAdaChange ownPkh utxos tx =
307308 then Right $ if Value. isZero nonAdaChange then tx else tx {txOutputs = outputs}
308309 else Left " Not enough inputs to balance tokens."
309310
310- hasChangeUTxO :: PubKeyHash -> Tx -> Bool
311- hasChangeUTxO ownPkh tx =
311+ hasChangeUTxO :: Address -> Tx -> Bool
312+ hasChangeUTxO changeAddr tx =
312313 any ((==) changeAddr . Tx. txOutAddress) $ txOutputs tx
313- where
314- changeAddr = Ledger. pubKeyHashAddress (Ledger. PaymentPubKeyHash ownPkh) Nothing
315314
316315-- | Adds ada change to a transaction, assuming there is already an output going to ownPkh. Otherwise, this is identity
317- addAdaChange :: PubKeyHash -> Integer -> Tx -> Tx
318- addAdaChange ownPkh change tx =
316+ addAdaChange :: Address -> Integer -> Tx -> Tx
317+ addAdaChange changeAddr change tx =
319318 tx
320319 { txOutputs =
321320 case partition ((==) changeAddr . Tx. txOutAddress) $ txOutputs tx of
322321 (txOut@ TxOut {txOutValue = v} : txOuts, txOuts') ->
323322 txOut {txOutValue = v <> Ada. lovelaceValueOf change} : (txOuts <> txOuts')
324323 _ -> txOutputs tx
325324 }
326- where
327- changeAddr = Ledger. pubKeyHashAddress (Ledger. PaymentPubKeyHash ownPkh) Nothing
328325
329326-- | Adds a 1 lovelace output to a transaction
330- addOutput :: PubKeyHash -> Tx -> Tx
331- addOutput ownPkh tx = tx {txOutputs = changeTxOut : txOutputs tx}
327+ addOutput :: Address -> Tx -> Tx
328+ addOutput changeAddr tx = tx {txOutputs = changeTxOut : txOutputs tx}
332329 where
333- changeAddr = Ledger. pubKeyHashAddress (Ledger. PaymentPubKeyHash ownPkh) Nothing
334330 changeTxOut =
335331 TxOut
336332 { txOutAddress = changeAddr
337333 , txOutValue = Ada. lovelaceValueOf 1
338334 , txOutDatumHash = Nothing
339335 }
340336
341- {- | Add the required signatorioes to the transaction. Be aware the the signature itself is invalid,
337+ {- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
342338 and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
343339-}
344340addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
0 commit comments