Skip to content

Commit

Permalink
Replace compatibility function coinToNatural.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Nov 23, 2021
1 parent 5a8cd40 commit 43aa71e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 9 additions & 7 deletions lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Expand Up @@ -19,6 +19,7 @@ module Cardano.Wallet.Primitive.Types.Coin
, fromIntegral
, fromWord64
, toInteger
, toNatural
, toQuantity
, toWord64

Expand All @@ -28,7 +29,6 @@ module Cardano.Wallet.Primitive.Types.Coin
, unsafeToWord64

-- * Compatibility
, coinToNatural
, unsafeNaturalToCoin

-- * Checks
Expand Down Expand Up @@ -149,6 +149,11 @@ fromWord64 = Coin . intCast
toInteger :: Coin -> Integer
toInteger = intCast . unCoin

-- | Converts a 'Coin' to a 'Natural' value.
--
toNatural :: Coin -> Natural
toNatural = unCoin

-- | Converts a 'Coin' to a 'Quantity'.
--
-- Returns 'Nothing' if the given value does not fit within the bounds of
Expand Down Expand Up @@ -229,9 +234,6 @@ unsafeToWord64 c = fromMaybe onError (toWord64 c)
Compatibility
-------------------------------------------------------------------------------}

coinToNatural :: Coin -> Natural
coinToNatural = unCoin

unsafeNaturalToCoin :: Natural -> Coin
unsafeNaturalToCoin = Coin

Expand Down Expand Up @@ -302,7 +304,7 @@ equipartition
equipartition c =
-- Note: the natural-to-coin conversion is safe, as partitioning guarantees
-- to produce values that are less than or equal to the original value.
fmap unsafeNaturalToCoin . equipartitionNatural (coinToNatural c)
fmap unsafeNaturalToCoin . equipartitionNatural (toNatural c)

-- | Partitions a coin into a number of parts, where the size of each part is
-- proportional to the size of its corresponding element in the given list
Expand All @@ -321,8 +323,8 @@ partition c
-- Note: the natural-to-coin conversion is safe, as partitioning guarantees
-- to produce values that are less than or equal to the original value.
= fmap (fmap unsafeNaturalToCoin)
. partitionNatural (coinToNatural c)
. fmap coinToNatural
. partitionNatural (toNatural c)
. fmap toNatural

-- | Partitions a coin into a number of parts, where the size of each part is
-- proportional to the size of its corresponding element in the given list
Expand Down
12 changes: 6 additions & 6 deletions lib/core/test/unit/Cardano/Wallet/Primitive/Types/CoinSpec.hs
Expand Up @@ -78,8 +78,8 @@ prop_add_toNatural :: Coin -> Coin -> Property
prop_add_toNatural a b =
checkCoverageCoin a b $
(===)
(Coin.coinToNatural (a `Coin.addCoin` b))
(Coin.coinToNatural a + Coin.coinToNatural b)
(Coin.toNatural (a `Coin.addCoin` b))
(Coin.toNatural a + Coin.toNatural b)

prop_difference_distance :: Coin -> Coin -> Property
prop_difference_distance a b =
Expand All @@ -105,13 +105,13 @@ prop_subtract_toNatural a b =
checkCoverageCoin a b $
if (a >= b)
then
(Coin.coinToNatural <$> (a `Coin.subtractCoin` b))
(Coin.toNatural <$> (a `Coin.subtractCoin` b))
===
(Just (Coin.coinToNatural a - Coin.coinToNatural b))
(Just (Coin.toNatural a - Coin.toNatural b))
else
(Coin.coinToNatural <$> (b `Coin.subtractCoin` a))
(Coin.toNatural <$> (b `Coin.subtractCoin` a))
===
(Just (Coin.coinToNatural b - Coin.coinToNatural a))
(Just (Coin.toNatural b - Coin.toNatural a))

--------------------------------------------------------------------------------
-- Coins that can be zero
Expand Down

0 comments on commit 43aa71e

Please sign in to comment.