Skip to content

Commit f33f64f

Browse files
committed
Resolve two todo's in commit/deposit
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent c283fe8 commit f33f64f

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

hydra-tx/src/Hydra/Tx/Commit.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ commitTx networkId scriptRegistry headId party commitBlueprintTx (initialInput,
6767
& spendFromInitial
6868
& bodyTxL . outputsTxBodyL
6969
.~ ( StrictSeq.singleton (toLedgerTxOut commitOutput)
70-
<> StrictSeq.singleton (toLedgerTxOut leftoverOutput)
70+
<> leftoverOutput
7171
)
7272
& bodyTxL . mintTxBodyL .~ mempty
7373
& addMetadata (mkHydraHeadV1TxName "CommitTx") blueprintTx
@@ -149,11 +149,15 @@ commitTx networkId scriptRegistry headId party commitBlueprintTx (initialInput,
149149

150150
commitOutput =
151151
TxOut commitAddress commitValue commitDatum ReferenceScriptNone
152-
-- TODO: don't use head here
153-
leftoverAddress = List.head $ txOutAddress <$> UTxO.txOutputs leftoverUTxO
154152

155153
leftoverOutput =
156-
TxOut leftoverAddress (UTxO.totalValue leftoverUTxO) TxOutDatumNone ReferenceScriptNone
154+
if UTxO.null leftoverUTxO
155+
then StrictSeq.empty
156+
else
157+
let leftoverAddress = List.head $ txOutAddress <$> UTxO.txOutputs leftoverUTxO
158+
in StrictSeq.singleton $
159+
toLedgerTxOut $
160+
TxOut leftoverAddress (UTxO.totalValue leftoverUTxO) TxOutDatumNone ReferenceScriptNone
157161

158162
commitAddress =
159163
mkScriptAddress networkId commitValidatorScript
@@ -178,10 +182,6 @@ updateTxOutValue :: TxOut ctx -> Coin -> TxOut ctx
178182
updateTxOutValue (TxOut addr _ datum refScript) newValue =
179183
TxOut addr (fromLedgerValue $ mkAdaValue ShelleyBasedEraConway newValue) datum refScript
180184

181-
-- | Generates a new TxIn by appending an index to avoid collisions
182-
newTxIn :: TxIn -> Word -> TxIn
183-
newTxIn (TxIn txId (TxIx txIx)) suffix = TxIn txId (TxIx $ txIx + suffix)
184-
185185
-- | Caps a UTxO set to a specified target amount of Lovelace, splitting outputs if necessary.
186186
--
187187
-- Given a 'UTxO' set and a target 'Coin' value (in Lovelace), this function selects unspent transaction outputs

hydra-tx/src/Hydra/Tx/Deposit.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ depositTx networkId headId commitBlueprintTx upperSlot deadline amount =
4040
& addDepositInputs
4141
& bodyTxL . outputsTxBodyL
4242
.~ ( StrictSeq.singleton (toLedgerTxOut $ mkDepositOutput networkId headId utxoToDeposit deadline)
43-
<> if txOutValue leftoverOutput == mempty
44-
then mempty
45-
else StrictSeq.singleton (toLedgerTxOut leftoverOutput)
43+
<> leftoverOutput
4644
)
4745
& bodyTxL . vldtTxBodyL .~ ValidityInterval{invalidBefore = SNothing, invalidHereafter = SJust upperSlot}
4846
& addMetadata (mkHydraHeadV1TxName "DepositTx") blueprintTx
@@ -55,11 +53,14 @@ depositTx networkId headId commitBlueprintTx upperSlot deadline amount =
5553

5654
(utxoToDeposit, leftoverUTxO) = maybe (depositUTxO, mempty) (capUTxO depositUTxO) amount
5755

58-
-- TODO: don't use head here
59-
leftoverAddress = List.head $ txOutAddress <$> UTxO.txOutputs leftoverUTxO
60-
6156
leftoverOutput =
62-
TxOut leftoverAddress (UTxO.totalValue leftoverUTxO) TxOutDatumNone ReferenceScriptNone
57+
if UTxO.null leftoverUTxO
58+
then StrictSeq.empty
59+
else
60+
let leftoverAddress = List.head $ txOutAddress <$> UTxO.txOutputs leftoverUTxO
61+
in StrictSeq.singleton $
62+
toLedgerTxOut $
63+
TxOut leftoverAddress (UTxO.totalValue leftoverUTxO) TxOutDatumNone ReferenceScriptNone
6364

6465
depositInputsList = toList (UTxO.inputSet utxoToDeposit)
6566

0 commit comments

Comments
 (0)