From 2e96484a3494016d280c0adefe4d2c71a56826fb Mon Sep 17 00:00:00 2001 From: Johannes Lund Date: Mon, 13 Sep 2021 19:04:12 +0200 Subject: [PATCH] Expose unsafeNaturalToCoin with guards This should be safer than using the `Coin` constructor. --- lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs index 3801544c385..bf24f050ef4 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs @@ -16,6 +16,7 @@ module Cardano.Wallet.Primitive.Types.Coin , coinQuantity , coinToInteger , coinToNatural + , unsafeNaturalToCoin -- * Checks , isValidCoin @@ -125,7 +126,10 @@ coinToNatural :: Coin -> Natural coinToNatural = fromIntegral . unCoin unsafeNaturalToCoin :: Natural -> Coin -unsafeNaturalToCoin = Coin . fromIntegral +unsafeNaturalToCoin x | x <= maxBoundNatural = Coin $ fromIntegral x + | otherwise = error "unsafeNaturalToCoin: overflow" + where + maxBoundNatural = fromIntegral . unCoin $ maxBound @Coin {------------------------------------------------------------------------------- Checks