@@ -82,18 +82,27 @@ filterExistingAssets utxoToFilter utxoToLookup =
82
82
let assets = valueToPolicyAssets val
83
83
originalLovelace = selectLovelace val
84
84
filteredAssets =
85
- foldMap (uncurry policyAssetsToValue) $
86
- Map. assocs $
87
- Map. mapWithKey
88
- ( \ pid (PolicyAssets x) ->
89
- let samePid = List. filter (\ (pid', _) -> pid' == pid) forLookup
90
- in case List. lookup pid samePid of
91
- Nothing -> PolicyAssets x
92
- Just (PolicyAssets foundAssets) -> PolicyAssets $ x `Map.difference` foundAssets
93
- )
94
- assets
85
+ foldMap (uncurry policyAssetsToValue) $
86
+ filterAssets assets forLookup
95
87
in (i, TxOut a (lovelaceToValue originalLovelace <> filteredAssets) d r)
96
- forLookup = concatMap (Map. toList . valueToPolicyAssets . txOutValue . snd ) $ UTxO. toList utxoToLookup
88
+ forLookup =
89
+ -- NOTE: Uses a list to store all policies, preserving multiple entries
90
+ -- with the same policyId but different assets. A Map would silently
91
+ -- overwrite duplicates.
92
+ concatMap (Map. toList . valueToPolicyAssets . txOutValue . snd ) $ UTxO. toList utxoToLookup
93
+
94
+ -- | Filter the first argument map of assets in case any asset exists in the second argument.
95
+ filterAssets :: Map PolicyId PolicyAssets -> [(PolicyId , PolicyAssets )] -> [(PolicyId , PolicyAssets )]
96
+ filterAssets assets forLookup =
97
+ Map. assocs $
98
+ Map. mapWithKey
99
+ ( \ pid (PolicyAssets x) ->
100
+ let samePid = List. filter (\ (pid', _) -> pid' == pid) forLookup
101
+ in case List. lookup pid samePid of
102
+ Nothing -> PolicyAssets x
103
+ Just (PolicyAssets foundAssets) -> PolicyAssets $ x `Map.difference` foundAssets
104
+ )
105
+ assets
97
106
98
107
-- | Merges the two 'UTxO' favoring data coming from the first argument 'UTxO'.
99
108
-- In case the same 'TxIn' was found in the first 'UTxO' - second 'UTxO' value
0 commit comments