Skip to content

Commit

Permalink
Extract out function makeChangeForNonUserSpecifiedAssets.
Browse files Browse the repository at this point in the history
This function computes the value assigned to `changeForNonUserSpecifiedAssets`,
so that the expected behaviour can be tested in isolation.
  • Loading branch information
jonathanknowles committed Jun 8, 2021
1 parent 304e653 commit 49d0c85
Showing 1 changed file with 22 additions and 7 deletions.
Expand Up @@ -1084,11 +1084,8 @@ makeChange criteria
-- Change for non-user-specified assets: assets that were not present
-- in the original set of user-specified outputs ('outputsToCover').
changeForNonUserSpecifiedAssets :: NonEmpty TokenMap
changeForNonUserSpecifiedAssets = F.foldr
(NE.zipWith (<>)
. makeChangeForNonUserSpecifiedAsset outputMaps)
(TokenMap.empty <$ outputMaps)
nonUserSpecifiedAssets
changeForNonUserSpecifiedAssets = makeChangeForNonUserSpecifiedAssets
outputMaps nonUserSpecifiedAssetQuantities

totalInputValueInsufficient = error
"makeChange: not (totalOutputValue <= totalInputValue)"
Expand Down Expand Up @@ -1137,8 +1134,8 @@ makeChange criteria
--
-- Each asset is paired with the complete list of quantities of that asset
-- present in the selected inputs.
nonUserSpecifiedAssets :: [(AssetId, NonEmpty TokenQuantity)]
nonUserSpecifiedAssets = Map.toList $
nonUserSpecifiedAssetQuantities :: Map AssetId (NonEmpty TokenQuantity)
nonUserSpecifiedAssetQuantities =
collateNonUserSpecifiedAssetQuantities
(view #tokens <$> inputBundles) userSpecifiedAssetIds

Expand Down Expand Up @@ -1337,6 +1334,24 @@ makeChangeForNonUserSpecifiedAsset
makeChangeForNonUserSpecifiedAsset n (asset, quantities) =
TokenMap.singleton asset <$> padCoalesce quantities n

-- | Constructs change outputs for all non-user-specified assets: assets that
-- were not present in the original set of outputs.
--
-- The resultant list is sorted into ascending order when maps are compared
-- with the `leq` function.
--
makeChangeForNonUserSpecifiedAssets
:: NonEmpty a
-- ^ Determines the number of change maps to create.
-> Map AssetId (NonEmpty TokenQuantity)
-- ^ A map of asset quantities to distribute.
-> NonEmpty TokenMap
makeChangeForNonUserSpecifiedAssets n nonUserSpecifiedAssetQuantities =
F.foldr
(NE.zipWith (<>) . makeChangeForNonUserSpecifiedAsset n)
(TokenMap.empty <$ n)
(Map.toList nonUserSpecifiedAssetQuantities)

-- | Constructs a list of ada change outputs based on the given distribution.
--
-- If the sum of weights in given distribution is equal to zero, this function
Expand Down

0 comments on commit 49d0c85

Please sign in to comment.