Skip to content

Commit

Permalink
Expose unsafeNaturalToCoin with guards
Browse files Browse the repository at this point in the history
This should be safer than using the `Coin` constructor.
  • Loading branch information
Anviking committed Sep 13, 2021
1 parent 0a36e51 commit 2e96484
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Expand Up @@ -16,6 +16,7 @@ module Cardano.Wallet.Primitive.Types.Coin
, coinQuantity
, coinToInteger
, coinToNatural
, unsafeNaturalToCoin

-- * Checks
, isValidCoin
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e96484

Please sign in to comment.