Skip to content

Commit

Permalink
move 'addCoin' and 'subtractCoin' to the Primitive/Coin module
Browse files Browse the repository at this point in the history
  They better fit there than in the RoundRobin module.
  • Loading branch information
KtorZ committed Jan 20, 2021
1 parent 0dde4a5 commit c1d62da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ module Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
-- * Utility functions
, distance
, mapMaybe
, subtractCoin
, addCoin
) where

import Prelude
Expand All @@ -71,7 +69,7 @@ import Algebra.PartialOrd
import Cardano.Numeric.Util
( padCoalesce, partitionNatural )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
( Coin (..), subtractCoin )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.TokenMap
Expand Down Expand Up @@ -839,11 +837,3 @@ mapMaybe predicate (x :| xs) = go (x:xs)
case predicate a of
Just b -> b : go as
Nothing -> go as

subtractCoin :: Coin -> Coin -> Maybe Coin
subtractCoin (Coin a) (Coin b)
| a >= b = Just $ Coin (a - b)
| otherwise = Nothing

addCoin :: Coin -> Coin -> Coin
addCoin (Coin a) (Coin b) = Coin (a + b)
28 changes: 27 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ module Cardano.Wallet.Primitive.Types.Coin
-- * Type
Coin (..)

-- * Operations
, addCoin
, subtractCoin

-- * Checks
, isValidCoin

) where

import Prelude
Expand Down Expand Up @@ -72,5 +75,28 @@ instance Bounded Coin where
instance Buildable Coin where
build = build . unCoin

--------------------------------------------------------------------------------
-- Operations
--------------------------------------------------------------------------------

-- | Subtract a coin quantity to another, returning nothing if the second
-- argument is strictly bigger than the first.
subtractCoin :: Coin -> Coin -> Maybe Coin
subtractCoin (Coin a) (Coin b)
| a >= b = Just $ Coin (a - b)
| otherwise = Nothing

-- NOTE: It is generally safe to add coins and stay in the same domain because
-- the max supply is known (45B), which largely fits within a `Word64`. So in
-- the vaste majority of usages of this function within cardano-wallet, it is a
-- safe operation.
--
addCoin :: Coin -> Coin -> Coin
addCoin (Coin a) (Coin b) = Coin (a + b)

--------------------------------------------------------------------------------
-- Checks
--------------------------------------------------------------------------------

isValidCoin :: Coin -> Bool
isValidCoin c = c >= minBound && c <= maxBound
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
, SelectionResult (..)
, SelectionSkeleton (..)
, SelectionState (..)
, addCoin
, fullBalance
, groupByKey
, makeChange
Expand All @@ -44,7 +43,7 @@ import Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
, ungroupByKey
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
( Coin (..), addCoin )
import Cardano.Wallet.Primitive.Types.Coin.Gen
( genCoinSmall, genCoinSmallPositive, shrinkCoinSmallPositive )
import Cardano.Wallet.Primitive.Types.Hash
Expand Down

0 comments on commit c1d62da

Please sign in to comment.