Skip to content

Commit 9c9e07b

Browse files
Add UTxO.totalValue and UTxO.totalLovelace
1 parent 91cde7f commit 9c9e07b

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
@@ -744,7 +744,7 @@ singlePartyUsesWithdrawZeroTrick tracer workDir backend hydraScriptsTxId =
744744
recomputeIntegrityHash pparams [PlutusV3] $
745745
toLedgerTx tx
746746
& bodyTxL . collateralInputsTxBodyL .~ Set.map toLedgerTxIn (UTxO.inputSet utxoToCommit)
747-
& bodyTxL . totalCollateralTxBodyL .~ SJust (UTxO.foldMap (selectLovelace . txOutValue) utxoToCommit)
747+
& bodyTxL . totalCollateralTxBodyL .~ SJust (UTxO.totalLovelace utxoToCommit)
748748
& bodyTxL . withdrawalsTxBodyL .~ Withdrawals (Map.singleton rewardAccount 0)
749749
& witsTxL . rdmrsTxWitsL .~ Redeemers (Map.singleton (ConwayRewarding $ AsIx 0) (redeemer, exUnits))
750750
& 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
@@ -134,7 +134,7 @@ generateDemoUTxODataset ::
134134
generateDemoUTxODataset network nodeSocket faucetSk nClients nTxs = do
135135
-- Query available funds
136136
faucetUTxO <- queryUTxOFor network nodeSocket QueryTip faucetVk
137-
let (Coin fundsAvailable) = UTxO.foldMap (selectLovelace . txOutValue) faucetUTxO
137+
let (Coin fundsAvailable) = UTxO.totalLovelace faucetUTxO
138138
-- Generate client datasets
139139
allPaymentKeys <- generate $ replicateM nClients genSigningKey
140140
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
@@ -8,7 +8,7 @@ import Test.Hydra.Prelude
88
import Cardano.Api.UTxO qualified as UTxO
99
import CardanoNode (withCardanoNodeDevnet)
1010
import Control.Concurrent.Async (replicateConcurrently)
11-
import Hydra.Cardano.Api (Coin (..), selectLovelace, txOutValue)
11+
import Hydra.Cardano.Api (Coin (..), selectLovelace)
1212
import Hydra.Chain.Backend qualified as Backend
1313
import Hydra.Chain.CardanoClient (QueryPoint (..))
1414
import Hydra.Chain.Direct (DirectBackend (..))
@@ -54,13 +54,13 @@ spec =
5454
returnFundsToFaucet tracer backend actor
5555
remaining <- Backend.queryUTxOFor backend QueryTip vk
5656
finalFaucetFunds <- Backend.queryUTxOFor backend QueryTip faucetVk
57-
UTxO.foldMap txOutValue remaining `shouldBe` mempty
57+
UTxO.totalValue remaining `shouldBe` mempty
5858

5959
-- check the faucet has one utxo extra in the end
6060
UTxO.size finalFaucetFunds `shouldBe` UTxO.size initialFaucetFunds + 1
6161

62-
let initialFaucetValue = selectLovelace (UTxO.foldMap txOutValue initialFaucetFunds)
63-
let finalFaucetValue = selectLovelace (UTxO.foldMap txOutValue finalFaucetFunds)
62+
let initialFaucetValue = selectLovelace (UTxO.totalValue initialFaucetFunds)
63+
let finalFaucetValue = selectLovelace (UTxO.totalValue finalFaucetFunds)
6464
let difference = initialFaucetValue - finalFaucetValue
6565
-- difference between starting faucet amount and final one should
6666
-- 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)