Skip to content

Commit 42daa3b

Browse files
Add UTxO.totalValue and UTxO.totalLovelace
1 parent e23d763 commit 42daa3b

File tree

13 files changed

+24
-19
lines changed

13 files changed

+24
-19
lines changed

hydra-cardano-api/src/Cardano/Api/UTxO.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Cardano.Api.UTxO where
1010

1111
import Cardano.Api hiding (UTxO, toLedgerUTxO)
1212
import Cardano.Api qualified
13+
import Cardano.Api.Ledger (Coin)
1314
import Cardano.Api.Shelley (ReferenceScript (..))
1415
import Cardano.Ledger.Babbage ()
1516
import Data.Bifunctor (second)
@@ -103,10 +104,15 @@ txOutputs = Map.elems . toMap
103104
null :: UTxO -> Bool
104105
null = Map.null . toMap
105106

106-
107107
size :: UTxO -> Int
108108
size = Map.size . toMap
109109

110+
totalValue :: UTxO -> Value
111+
totalValue = Cardano.Api.UTxO.foldMap (\(TxOut _ (txOutValueToValue -> v) _ _) -> v)
112+
113+
totalLovelace :: UTxO -> Coin
114+
totalLovelace = selectLovelace . totalValue
115+
110116
-- * Type Conversions
111117

112118
-- | Transforms a UTxO containing tx outs from any era into Babbage era.

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ singlePartyUsesWithdrawZeroTrick tracer workDir node hydraScriptsTxId =
743743
recomputeIntegrityHash pparams [PlutusV3] $
744744
toLedgerTx tx
745745
& bodyTxL . collateralInputsTxBodyL .~ Set.map toLedgerTxIn (UTxO.inputSet utxoToCommit)
746-
& bodyTxL . totalCollateralTxBodyL .~ SJust (UTxO.foldMap (selectLovelace . txOutValue) utxoToCommit)
746+
& bodyTxL . totalCollateralTxBodyL .~ SJust (UTxO.totalLovelace utxoToCommit)
747747
& bodyTxL . withdrawalsTxBodyL .~ Withdrawals (Map.singleton rewardAccount 0)
748748
& witsTxL . rdmrsTxWitsL .~ Redeemers (Map.singleton (ConwayRewarding $ AsIx 0) (redeemer, exUnits))
749749
& witsTxL . scriptTxWitsL .~ Map.singleton scriptHash script

hydra-cluster/src/Hydra/Generator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ generateDemoUTxODataset ::
131131
generateDemoUTxODataset network nodeSocket faucetSk nClients nTxs = do
132132
-- Query available funds
133133
faucetUTxO <- queryUTxOFor network nodeSocket QueryTip faucetVk
134-
let (Coin fundsAvailable) = UTxO.foldMap (selectLovelace . txOutValue) faucetUTxO
134+
let (Coin fundsAvailable) = UTxO.totalLovelace faucetUTxO
135135
-- Generate client datasets
136136
allPaymentKeys <- generate $ replicateM nClients genSigningKey
137137
clientFunds <- generate $ genClientFunds allPaymentKeys fundsAvailable

hydra-cluster/test/Test/Hydra/Cluster/FaucetSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Cardano.Api.UTxO qualified as UTxO
77
import CardanoClient (RunningNode (..))
88
import CardanoNode (withCardanoNodeDevnet)
99
import Control.Concurrent.Async (replicateConcurrently)
10-
import Hydra.Cardano.Api (Coin (..), selectLovelace, txOutValue)
10+
import Hydra.Cardano.Api (Coin (..), selectLovelace)
1111
import Hydra.Chain.CardanoClient (QueryPoint (..), queryUTxOFor)
1212
import Hydra.Cluster.Faucet (FaucetLog, publishHydraScriptsAs, returnFundsToFaucet, seedFromFaucet)
1313
import Hydra.Cluster.Fixture (Actor (..))
@@ -51,13 +51,13 @@ spec =
5151
returnFundsToFaucet tracer node actor
5252
remaining <- queryUTxOFor networkId nodeSocket QueryTip vk
5353
finalFaucetFunds <- queryUTxOFor networkId nodeSocket QueryTip faucetVk
54-
UTxO.foldMap txOutValue remaining `shouldBe` mempty
54+
UTxO.totalValue remaining `shouldBe` mempty
5555

5656
-- check the faucet has one utxo extra in the end
5757
UTxO.size finalFaucetFunds `shouldBe` UTxO.size initialFaucetFunds + 1
5858

59-
let initialFaucetValue = selectLovelace (UTxO.foldMap txOutValue initialFaucetFunds)
60-
let finalFaucetValue = selectLovelace (UTxO.foldMap txOutValue finalFaucetFunds)
59+
let initialFaucetValue = selectLovelace (UTxO.totalValue initialFaucetFunds)
60+
let finalFaucetValue = selectLovelace (UTxO.totalValue finalFaucetFunds)
6161
let difference = initialFaucetValue - finalFaucetValue
6262
-- difference between starting faucet amount and final one should
6363
-- just be the amount of paid fees

hydra-node/src/Hydra/Chain/Direct/State.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import Hydra.Cardano.Api (
4545
modifyTxOutValue,
4646
negateValue,
4747
selectAsset,
48-
selectLovelace,
4948
toCtxUTxOTxOut,
5049
toShelleyNetwork,
5150
txIns',
@@ -381,7 +380,7 @@ rejectMoreThanMainnetLimit network u = do
381380
Left $
382381
CommittedTooMuchADAForMainnet lovelaceAmt maxMainnetLovelace
383382
where
384-
lovelaceAmt = UTxO.foldMap (selectLovelace . txOutValue) u
383+
lovelaceAmt = UTxO.totalLovelace u
385384

386385
-- | Construct a abort transaction based on known, spendable UTxO. This function
387386
-- looks for head, initial and commit outputs to spend and it will fail if we
@@ -521,7 +520,7 @@ decrement ctx spendableUTxO headId headParameters decrementingSnapshot = do
521520
Left DecrementValueNegative
522521
Right $ decrementTx scriptRegistry ownVerificationKey headId headParameters headUTxO sn sigs
523522
where
524-
decommitValue = UTxO.foldMap txOutValue $ fromMaybe mempty $ utxoToDecommit sn
523+
decommitValue = UTxO.totalValue $ fromMaybe mempty $ utxoToDecommit sn
525524

526525
isNegative = any ((< 0) . snd) . IsList.toList
527526

hydra-node/test/Hydra/Chain/Direct/TxTraceSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ openHeadUTxO =
793793
openHeadTxOut =
794794
mkHeadOutput Fixture.testNetworkId Fixture.testPolicyId openHeadDatum
795795
& addParticipationTokens [alicePVk, bobPVk, carolPVk]
796-
& modifyTxOutValue (<> UTxO.foldMap txOutValue inHeadUTxO)
796+
& modifyTxOutValue (<> UTxO.totalValue inHeadUTxO)
797797

798798
openHeadDatum =
799799
mkTxOutDatumInline $

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ commitTx networkId scriptRegistry headId party commitBlueprintTx (initialInput,
131131
UTxO.fromList $ mapMaybe (\txin -> (txin,) <$> UTxO.resolveTxIn txin lookupUTxO) committedTxIns
132132

133133
commitValue =
134-
txOutValue out <> UTxO.foldMap txOutValue utxoToCommit
134+
txOutValue out <> UTxO.totalValue utxoToCommit
135135

136136
commitDatum =
137137
mkTxOutDatumInline $ mkCommitDatum party utxoToCommit (headIdToCurrencySymbol headId)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mkDepositOutput networkId headId depositUTxO deadline =
6161
depositDatum
6262
ReferenceScriptNone
6363
where
64-
depositValue = UTxO.foldMap txOutValue depositUTxO
64+
depositValue = UTxO.totalValue depositUTxO
6565

6666
deposits = mapMaybe Commit.serializeCommit $ UTxO.toList depositUTxO
6767

@@ -128,7 +128,7 @@ observeDepositTxOut network depositOut = do
128128
headId <- currencySymbolToHeadId headCurrencySymbol
129129
deposit <- do
130130
depositedUTxO <- UTxO.fromList <$> traverse (Commit.deserializeCommit network) onChainDeposits
131-
guard $ depositValue `containsValue` UTxO.foldMap txOutValue depositedUTxO
131+
guard $ depositValue `containsValue` UTxO.totalValue depositedUTxO
132132
pure depositedUTxO
133133
pure (headId, deposit, deadline)
134134
where

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ instance IsTx Tx where
162162
type ValueType Tx = Value
163163

164164
txId = getTxId . getTxBody
165-
balance = UTxO.foldMap txOutValue
165+
balance = UTxO.totalValue
166166

167167
-- NOTE: See note from `Util.hashTxOuts`.
168168
hashUTxO = fromBuiltin . Util.hashTxOuts . mapMaybe toPlutusTxOut . UTxO.txOutputs

hydra-tx/test/Hydra/Tx/Contract/Abort.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ generateCommitUTxOs parties = do
324324
commitValue =
325325
mconcat
326326
[ lovelaceToValue (Coin 2000000)
327-
, UTxO.foldMap txOutValue utxo
327+
, UTxO.totalValue utxo
328328
, fromList
329329
[ (AssetId testPolicyId (assetNameFromVerificationKey vk), 1)
330330
]

0 commit comments

Comments
 (0)