Skip to content

Commit

Permalink
Use truncated subtraction in assignCoinsToChangeMaps. (#4123)
Browse files Browse the repository at this point in the history
## Issues

- Follow-on from #4110
- ADP-1449

## Description

This PR replaces the use of `Coin.distance` with
[`Monus.<\>`](https://hackage.haskell.org/package/monoid-subclasses-1.2.3/docs/Data-Monoid-Monus.html#v:-60--92--62-),
which (in the case of `Coin` arguments) performs truncated subtraction
of the second argument from the first.
  • Loading branch information
jonathanknowles authored Sep 7, 2023
2 parents f3694cc + c7b8303 commit e873d7a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/coin-selection/lib/Cardano/CoinSelection/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ assignCoinsToChangeMaps adaAvailable minCoinFor pairsAtStart
-- Calculate the amount of ada that remains after assigning the
-- minimum amount to each map. This should be safe, as we have
-- already determined that we have enough ada available:
adaRemaining = adaAvailable `Coin.distance` adaRequired
adaRemaining = adaAvailable <\> adaRequired
-- Partition any remaining ada according to the weighted
-- distribution of output coins that remain in our list:
outputCoinsRemaining = snd <$> (pair :| pairs)
Expand All @@ -1590,7 +1590,7 @@ assignCoinsToChangeMaps adaAvailable minCoinFor pairsAtStart
-- required amount of every asset map, but we do have an empty
-- asset map that is safe to drop. This will reduce the amount of
-- ada required by a small amount:
let adaRequired' = adaRequired `Coin.distance` minCoinFor m in
let adaRequired' = adaRequired <\> minCoinFor m in
loop adaRequired' (p :| ps)

(m, _) :| [] | TokenMap.isEmpty m && adaAvailable < adaRequired ->
Expand Down

0 comments on commit e873d7a

Please sign in to comment.