Skip to content

Commit 267f9af

Browse files
Slight refactor
1 parent 34017c2 commit 267f9af

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,13 @@ balanceTxIO pabConf ownPkh unbalancedTx =
8989
-- Balance the tx
9090
(balancedTx, minUtxos) <- loop utxoIndex privKeys [] preBalancedTx
9191

92-
-- Check if we have Ada change
92+
-- Get current Ada change
9393
let adaChange = getAdaChange utxoIndex balancedTx
94-
-- If we have no change UTxO, but we do have change, we need to add an output for it
94+
-- If we have change but no change UTxO, we need to add an output for it
9595
-- We'll add a minimal output, run the loop again so it gets minUTxO, then update change
9696
balancedTxWithChange <-
9797
if adaChange /= 0 && not (hasChangeUTxO ownPkh balancedTx)
98-
then
99-
let changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
100-
changeTxOut =
101-
TxOut
102-
{ txOutAddress = changeAddr
103-
, txOutValue = Ada.lovelaceValueOf 1
104-
, txOutDatumHash = Nothing
105-
}
106-
preBalancedTxWithChange = balancedTx {txOutputs = changeTxOut : txOutputs balancedTx}
107-
in fst <$> loop utxoIndex privKeys minUtxos preBalancedTxWithChange
98+
then fst <$> loop utxoIndex privKeys minUtxos (addOutput ownPkh balancedTx)
10899
else pure balancedTx
109100

110101
-- Get the updated change, add it to the tx
@@ -314,6 +305,18 @@ addAdaChange ownPkh change tx =
314305
where
315306
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
316307

308+
-- | Adds a 1 lovelace output to a transaction
309+
addOutput :: PubKeyHash -> Tx -> Tx
310+
addOutput ownPkh tx = tx {txOutputs = changeTxOut : txOutputs tx}
311+
where
312+
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
313+
changeTxOut =
314+
TxOut
315+
{ txOutAddress = changeAddr
316+
, txOutValue = Ada.lovelaceValueOf 1
317+
, txOutDatumHash = Nothing
318+
}
319+
317320
{- | Add the required signatorioes to the transaction. Be aware the the signature itself is invalid,
318321
and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
319322
-}

0 commit comments

Comments
 (0)