Skip to content

Commit

Permalink
Replace function coinToInteger with safer equivalent.
Browse files Browse the repository at this point in the history
We use `intCast` instead of `fromIntegral`, which statically checks
that the conversion is safe.

We also use the same naming convention as the existing safe conversion
functions.
  • Loading branch information
jonathanknowles committed Nov 23, 2021
1 parent 5d57215 commit 1add44c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -373,7 +373,7 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Address
( Address (..), AddressState (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..), addCoin, coinToInteger, sumCoins )
( Coin (..), addCoin, sumCoins )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Redeemer
Expand Down Expand Up @@ -1160,7 +1160,7 @@ readNextWithdrawal ctx (Coin withdrawal) = do
calcMinimumCost tl pp (mkTxCtx $ Coin 0) emptySkeleton

let costOfWithdrawal =
coinToInteger costWith - coinToInteger costWithout
Coin.toInteger costWith - Coin.toInteger costWithout

if toInteger withdrawal < 2 * costOfWithdrawal
then pure (Coin 0)
Expand Down
12 changes: 7 additions & 5 deletions lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Expand Up @@ -18,6 +18,7 @@ module Cardano.Wallet.Primitive.Types.Coin
-- * Conversions (Safe)
, fromIntegral
, fromWord64
, toInteger
, toQuantity
, toWord64

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

-- * Compatibility
, coinToInteger
, coinToNatural
, unsafeNaturalToCoin

Expand All @@ -49,7 +49,7 @@ module Cardano.Wallet.Primitive.Types.Coin
) where

import Prelude hiding
( fromIntegral )
( fromIntegral, toInteger )

import Cardano.Numeric.Util
( equipartitionNatural, partitionNatural )
Expand Down Expand Up @@ -144,6 +144,11 @@ fromIntegral i = Coin <$> intCastMaybe i
fromWord64 :: Word64 -> Coin
fromWord64 = Coin . intCast

-- | Converts a 'Coin' to an 'Integer' value.
--
toInteger :: Coin -> Integer
toInteger = intCast . unCoin

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

coinToInteger :: Coin -> Integer
coinToInteger = Prelude.fromIntegral . unCoin

coinToNatural :: Coin -> Natural
coinToNatural = unCoin

Expand Down
Expand Up @@ -84,7 +84,7 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..), coinToInteger )
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Coin.Gen
( genCoinPositive, shrinkCoinPositive )
import Cardano.Wallet.Primitive.Types.Hash
Expand Down Expand Up @@ -661,11 +661,11 @@ feeCalculationSpec = describe "fee calculations" $ do
fp = LinearFee (Quantity 100_000) (Quantity 100)

minFee :: TransactionCtx -> Integer
minFee ctx = coinToInteger $ calcMinimumCost testTxLayer pp ctx sel
minFee ctx = Coin.toInteger $ calcMinimumCost testTxLayer pp ctx sel
where sel = emptySkeleton

minFeeSkeleton :: TxSkeleton -> Integer
minFeeSkeleton = coinToInteger . estimateTxCost pp
minFeeSkeleton = Coin.toInteger . estimateTxCost pp

estimateTxSize' :: TxSkeleton -> Integer
estimateTxSize' = fromIntegral . unTxSize . estimateTxSize
Expand Down

0 comments on commit 1add44c

Please sign in to comment.