From 43aa71eeb592b51fcbb0e17f1a3e8af9089c799d Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Tue, 23 Nov 2021 05:56:12 +0000 Subject: [PATCH] Replace compatibility function `coinToNatural`. --- .../src/Cardano/Wallet/Primitive/Types/Coin.hs | 16 +++++++++------- .../Cardano/Wallet/Primitive/Types/CoinSpec.hs | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs index 545607af8aa..67598577011 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs @@ -19,6 +19,7 @@ module Cardano.Wallet.Primitive.Types.Coin , fromIntegral , fromWord64 , toInteger + , toNatural , toQuantity , toWord64 @@ -28,7 +29,6 @@ module Cardano.Wallet.Primitive.Types.Coin , unsafeToWord64 -- * Compatibility - , coinToNatural , unsafeNaturalToCoin -- * Checks @@ -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 @@ -229,9 +234,6 @@ unsafeToWord64 c = fromMaybe onError (toWord64 c) Compatibility -------------------------------------------------------------------------------} -coinToNatural :: Coin -> Natural -coinToNatural = unCoin - unsafeNaturalToCoin :: Natural -> Coin unsafeNaturalToCoin = Coin @@ -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 @@ -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 diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/CoinSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/CoinSpec.hs index b294af859f9..a7cb8541f5b 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/CoinSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/CoinSpec.hs @@ -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 = @@ -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