Skip to content

Commit

Permalink
Replace sumCoins with Data.Foldable.fold.
Browse files Browse the repository at this point in the history
The `Coin` type has both `Semigroup` and `Monoid` instances.

Therefore we can just use `Data.Foldable.fold` to sum coins.
  • Loading branch information
jonathanknowles committed Nov 23, 2021
1 parent 651aac8 commit 32ad6ba
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
8 changes: 4 additions & 4 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, sumCoins )
( Coin (..), addCoin )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Redeemer
Expand Down Expand Up @@ -1588,7 +1588,7 @@ balanceTransaction
, "when balancing a transaction, but it was!"
]
)
(sumCoins wdrlMap)
(F.fold wdrlMap)
in (outs, wdrl, meta, toMint, toBurn)

-- | Wallet coin selection is unaware of many kinds of transaction content
Expand Down Expand Up @@ -2055,13 +2055,13 @@ mkTxMeta
-> IO (UTCTime, TxMeta)
mkTxMeta ti' blockHeader wState txCtx sel =
let
amtOuts = sumCoins $
amtOuts = F.fold $
(txOutCoin <$> view #change sel)
++
mapMaybe ourCoin (view #outputs sel)

amtInps
= sumCoins (txOutCoin . snd <$> view #inputs sel)
= F.fold (txOutCoin . snd <$> view #inputs sel)
-- NOTE: In case where rewards were pulled from an external
-- source, they aren't added to the calculation because the
-- money is considered to come from outside of the wallet; which
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/Primitive/Model.hs
Expand Up @@ -67,7 +67,7 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..), distance, sumCoins )
( Coin (..), distance )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
Expand Down Expand Up @@ -531,7 +531,7 @@ prefilterBlock b u0 = runState $ do

(Nothing, Outgoing) -> -- Byron
let
totalOut = sumCoins (txOutCoin <$> outputs tx)
totalOut = F.fold (txOutCoin <$> outputs tx)

totalIn = TB.getCoin spent
in
Expand Down
7 changes: 0 additions & 7 deletions lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Expand Up @@ -32,7 +32,6 @@ module Cardano.Wallet.Primitive.Types.Coin
-- * Arithmetic operations
, addCoin
, subtractCoin
, sumCoins
, difference
, distance

Expand All @@ -52,8 +51,6 @@ import Control.DeepSeq
( NFData (..) )
import Data.Bits
( Bits )
import Data.Foldable
( foldl' )
import Data.Hashable
( Hashable )
import Data.IntCast
Expand Down Expand Up @@ -245,10 +242,6 @@ subtractCoin (Coin a) (Coin b)
addCoin :: Coin -> Coin -> Coin
addCoin (Coin a) (Coin b) = Coin (a + b)

-- | Add a list of coins together.
sumCoins :: Foldable t => t Coin -> Coin
sumCoins = foldl' addCoin (Coin 0)

-- | Subtracts the second coin from the first.
--
-- Returns 'Coin 0' if the second coin is strictly greater than the first.
Expand Down
2 changes: 1 addition & 1 deletion lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -1176,7 +1176,7 @@ mkTxSkeleton witness context skeleton = TxSkeleton
--
estimateTxCost :: ProtocolParameters -> TxSkeleton -> Coin
estimateTxCost pp skeleton =
Coin.sumCoins
F.fold
[ computeFee (estimateTxSize skeleton)
, scriptExecutionCosts
]
Expand Down

0 comments on commit 32ad6ba

Please sign in to comment.