@@ -7,53 +7,124 @@ import Cardano.Api.UTxO qualified as UTxO
7
7
import Data.Map.Strict qualified as Map
8
8
import Data.Set qualified as Set
9
9
import Hydra.Cardano.Api (AssetId (.. ), AssetName , Coin (.. ), PolicyAssets (.. ), PolicyId , Quantity (.. ), UTxO , selectLovelace , txOutValue , valueToPolicyAssets )
10
- import Hydra.Tx.Deposit (capUTxO , checkTokens )
10
+ import Hydra.Tx.Deposit (capUTxO , pickTokensToDeposit , splitTokens )
11
11
import Test.Hydra.Tx.Fixture (testPolicyId )
12
12
import Test.Hydra.Tx.Gen (genUTxOSized )
13
13
import Test.QuickCheck (Property , chooseInteger , counterexample , (===) , (==>) )
14
14
15
15
spec :: Spec
16
16
spec =
17
17
parallel $ do
18
- describe " checkTokens " $ do
18
+ describe " pickTokensToDeposit " $ do
19
19
describe " tests" $ do
20
20
it " returns empty results when no tokens are specified" $ do
21
21
let utxo = genUTxOSized 3 `generateWith` 42
22
- let (valid, invalid) = checkTokens utxo mempty
22
+ let toDeposit = pickTokensToDeposit utxo mempty
23
+ toDeposit `shouldBe` mempty
24
+ it " returns empty results when UTxO is empty" $ do
25
+ let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 10 )])]
26
+ let toDeposit = pickTokensToDeposit mempty tokens
27
+ toDeposit `shouldBe` mempty
28
+ it " returns all tokens as invalid when policy is missing from UTxO" $ do
29
+ let utxo = genUTxOSized 3 `generateWith` 42 -- UTxO with only ADA
30
+ let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 10 )])]
31
+ let toDeposit = pickTokensToDeposit utxo tokens
32
+ toDeposit `shouldBe` mempty
33
+ it " validates tokens correctly when exact quantities match" $ do
34
+ let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 100 )]
35
+ let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
36
+ let toDeposit = pickTokensToDeposit testUTxO tokens
37
+ toDeposit `shouldBe` testUTxO
38
+ it " validates tokens correctly when UTxO has more than required" $ do
39
+ let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 150 )]
40
+ let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
41
+ let toDeposit = pickTokensToDeposit testUTxO tokens
42
+ toDeposit `shouldBe` utxoWithTokens [(testPolicyId, testAssetName, Quantity 100 )]
43
+ it " returns tokens intact when UTxO has less than required" $ do
44
+ let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 50 )]
45
+ let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
46
+ let toDeposit = pickTokensToDeposit testUTxO tokens
47
+ toDeposit `shouldBe` mempty
48
+ it " handles mixed scenarios with multiple tokens" $ do
49
+ let testUTxO =
50
+ utxoWithTokens
51
+ [ (testPolicyId, testAssetName, Quantity 100 )
52
+ , (testPolicyId2, testAssetName, Quantity 50 )
53
+ ]
54
+ let tokens =
55
+ Map. fromList
56
+ [ (testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )]) -- Valid
57
+ , (testPolicyId2, PolicyAssets $ Map. fromList [(testAssetName, Quantity 75 )]) -- Invalid - insufficient
58
+ , (testPolicyId3, PolicyAssets $ Map. fromList [(testAssetName, Quantity 25 )]) -- Invalid - missing policy
59
+ ]
60
+ let toDeposit = pickTokensToDeposit testUTxO tokens
61
+
62
+ toDeposit `shouldBe` utxoWithTokens [(testPolicyId, testAssetName, Quantity 100 )]
63
+
64
+ fit " handles multiple assets within the same policy" $ do
65
+ let testUTxO =
66
+ utxoWithTokens
67
+ [ (testPolicyId, testAssetName, Quantity 100 )
68
+ , (testPolicyId, testAssetName2, Quantity 200 )
69
+ ]
70
+ let tokens =
71
+ Map. fromList
72
+ [
73
+ ( testPolicyId
74
+ , PolicyAssets $
75
+ Map. fromList
76
+ [ (testAssetName, Quantity 50 ) -- Valid
77
+ , (testAssetName2, Quantity 150 ) -- Valid
78
+ , (testAssetName3, Quantity 10 ) -- Invalid - missing asset
79
+ ]
80
+ )
81
+ ]
82
+ let toDeposit = pickTokensToDeposit testUTxO tokens
83
+ let additionalUTxO =
84
+ utxoWithTokens
85
+ [ (testPolicyId, testAssetName, 50 )
86
+ , (testPolicyId, testAssetName2, 150 )
87
+ ]
88
+ toDeposit `shouldBe` additionalUTxO
89
+ describe " splitTokens" $ do
90
+ describe " tests" $ do
91
+ it " returns empty results when no tokens are specified" $ do
92
+ let utxo = genUTxOSized 3 `generateWith` 42
93
+ let (valid, invalid) = splitTokens utxo mempty
23
94
valid `shouldBe` mempty
24
95
invalid `shouldBe` mempty
25
96
26
97
it " returns empty results when UTxO is empty" $ do
27
98
let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 10 )])]
28
- let (valid, invalid) = checkTokens mempty tokens
99
+ let (valid, invalid) = splitTokens mempty tokens
29
100
valid `shouldBe` mempty
30
101
invalid `shouldBe` tokens
31
102
32
103
it " returns all tokens as invalid when policy is missing from UTxO" $ do
33
104
let utxo = genUTxOSized 3 `generateWith` 42 -- UTxO with only ADA
34
105
let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 10 )])]
35
- let (valid, invalid) = checkTokens utxo tokens
106
+ let (valid, invalid) = splitTokens utxo tokens
36
107
valid `shouldBe` mempty
37
108
invalid `shouldBe` tokens
38
109
39
110
it " validates tokens correctly when exact quantities match" $ do
40
111
let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 100 )]
41
112
let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
42
- let (valid, invalid) = checkTokens testUTxO tokens
113
+ let (valid, invalid) = splitTokens testUTxO tokens
43
114
valid `shouldBe` tokens
44
115
invalid `shouldBe` mempty
45
116
46
117
it " validates tokens correctly when UTxO has more than required" $ do
47
118
let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 150 )]
48
119
let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
49
- let (valid, invalid) = checkTokens testUTxO tokens
120
+ let (valid, invalid) = splitTokens testUTxO tokens
50
121
valid `shouldBe` tokens
51
122
invalid `shouldBe` mempty
52
123
53
124
it " returns tokens as invalid when UTxO has less than required" $ do
54
125
let testUTxO = utxoWithTokens [(testPolicyId, testAssetName, Quantity 50 )]
55
126
let tokens = Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
56
- let (valid, invalid) = checkTokens testUTxO tokens
127
+ let (valid, invalid) = splitTokens testUTxO tokens
57
128
valid `shouldBe` mempty
58
129
invalid `shouldBe` tokens
59
130
@@ -69,7 +140,7 @@ spec =
69
140
, (testPolicyId2, PolicyAssets $ Map. fromList [(testAssetName, Quantity 75 )]) -- Invalid - insufficient
70
141
, (testPolicyId3, PolicyAssets $ Map. fromList [(testAssetName, Quantity 25 )]) -- Invalid - missing policy
71
142
]
72
- let (valid, invalid) = checkTokens testUTxO tokens
143
+ let (valid, invalid) = splitTokens testUTxO tokens
73
144
74
145
valid `shouldBe` Map. fromList [(testPolicyId, PolicyAssets $ Map. fromList [(testAssetName, Quantity 100 )])]
75
146
invalid
@@ -96,7 +167,7 @@ spec =
96
167
]
97
168
)
98
169
]
99
- let (valid, invalid) = checkTokens testUTxO tokens
170
+ let (valid, invalid) = splitTokens testUTxO tokens
100
171
valid `shouldBe` mempty -- All assets in policy must be valid for policy to be valid
101
172
invalid `shouldBe` tokens
102
173
@@ -248,7 +319,7 @@ propNoUTxOLoss utxo target =
248
319
& counterexample (" Selected set size: " <> show (Set. size selectedSet))
249
320
& counterexample (" Leftover set size: " <> show (Set. size leftoverSet))
250
321
251
- -- * Helper functions for checkTokens tests
322
+ -- * Helper functions for splitTokens tests
252
323
253
324
-- | Create additional test PolicyIds for testing (using the existing testPolicyId from fixtures)
254
325
testPolicyId2 :: PolicyId
@@ -279,12 +350,12 @@ utxoWithTokens tokens =
279
350
txOut = baseTxOut{txOutValue = valueWithAda}
280
351
in UTxO. singleton txIn txOut
281
352
282
- -- * Property tests for checkTokens
353
+ -- * Property tests for splitTokens
283
354
284
355
-- | Property: All input tokens are preserved in either valid or invalid results
285
356
propPreservesAllTokens :: UTxO -> Map PolicyId PolicyAssets -> Property
286
357
propPreservesAllTokens utxo specifiedTokens =
287
- let (valid, invalid) = checkTokens utxo specifiedTokens
358
+ let (valid, invalid) = splitTokens utxo specifiedTokens
288
359
inputPolicies = Map. keysSet specifiedTokens
289
360
validPolicies = Map. keysSet valid
290
361
invalidPolicies = Map. keysSet invalid
@@ -297,7 +368,7 @@ propPreservesAllTokens utxo specifiedTokens =
297
368
-- | Property: Valid and invalid results are disjoint sets
298
369
propValidInvalidDisjoint :: UTxO -> Map PolicyId PolicyAssets -> Property
299
370
propValidInvalidDisjoint utxo specifiedTokens =
300
- let (valid, invalid) = checkTokens utxo specifiedTokens
371
+ let (valid, invalid) = splitTokens utxo specifiedTokens
301
372
validPolicies = Map. keysSet valid
302
373
invalidPolicies = Map. keysSet invalid
303
374
intersection = Set. intersection validPolicies invalidPolicies
@@ -309,7 +380,7 @@ propValidInvalidDisjoint utxo specifiedTokens =
309
380
-- | Property: All valid tokens must exist in UTxO with sufficient quantities
310
381
propValidTokensExistInUTxO :: UTxO -> Map PolicyId PolicyAssets -> Property
311
382
propValidTokensExistInUTxO utxo specifiedTokens =
312
- let (valid, _) = checkTokens utxo specifiedTokens
383
+ let (valid, _) = splitTokens utxo specifiedTokens
313
384
utxoValue = UTxO. totalValue utxo
314
385
utxoPolicyAssets = valueToPolicyAssets utxoValue
315
386
in all (checkValidTokenInUTxO utxoPolicyAssets) (Map. toList valid)
@@ -333,8 +404,8 @@ propValidTokensExistInUTxO utxo specifiedTokens =
333
404
propMonotonicUTxOAdditions :: UTxO -> UTxO -> Map PolicyId PolicyAssets -> Property
334
405
propMonotonicUTxOAdditions utxo1 utxo2 specifiedTokens =
335
406
let combinedUTxO = utxo1 <> utxo2
336
- (valid1, _) = checkTokens utxo1 specifiedTokens
337
- (validCombined, _) = checkTokens combinedUTxO specifiedTokens
407
+ (valid1, _) = splitTokens utxo1 specifiedTokens
408
+ (validCombined, _) = splitTokens combinedUTxO specifiedTokens
338
409
valid1Policies = Map. keysSet valid1
339
410
validCombinedPolicies = Map. keysSet validCombined
340
411
in valid1Policies `Set.isSubsetOf` validCombinedPolicies
0 commit comments