Skip to content

Commit

Permalink
move 'calcSelectionDelta' to '.../MA/RoundRobin' and make it work for…
Browse files Browse the repository at this point in the history
… all 'SelectionResult' types

  We need this in two places, on 'SeletionResult TxOut' and 'SelectionResult TokenBundle'.
  • Loading branch information
KtorZ committed Jan 28, 2021
1 parent 29446a0 commit 5a60d54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
21 changes: 2 additions & 19 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -245,6 +245,7 @@ import Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
, SelectionResult (..)
, emptySkeleton
, performSelection
, selectionDelta
)
import Cardano.Wallet.Primitive.Model
( Wallet
Expand Down Expand Up @@ -1283,7 +1284,7 @@ selectAssets ctx wid tx outs = do
:: Functor f
=> f (SelectionResult TokenBundle)
-> f (Coin, SelectionResult TokenBundle)
withFee = fmap $ \s -> (calcSelectionDelta s, s)
withFee = fmap $ \s -> (selectionDelta TokenBundle.getCoin s, s)

-- Ensure that there's no existing pending withdrawals. Indeed, a withdrawal
-- is necessarily withdrawing rewards in their totality. So, after a first
Expand All @@ -1301,24 +1302,6 @@ selectAssets ctx wid tx outs = do
hasWithdrawal :: Tx -> Bool
hasWithdrawal = not . null . withdrawals

-- | Calculate the actual difference between the total outputs (incl. change)
-- and total inputs of a particular selection. By construction, this should be
-- greater than total fees and deposits.
calcSelectionDelta
:: SelectionResult TokenBundle
-> Coin
calcSelectionDelta sel =
let
totalOut
= sumCoins (TokenBundle.getCoin <$> changeGenerated sel)
& addCoin (sumCoins (txOutCoin <$> outputsCovered sel))

totalIn
= sumCoins (txOutCoin . snd <$> (inputsSelected sel))
& addCoin (fromMaybe (Coin 0) (extraCoinSource sel))
in
Coin.distance totalIn totalOut

-- | Produce witnesses and construct a transaction from a given
-- selection. Requires the encryption passphrase in order to decrypt
-- the root private key. Note that this doesn't broadcast the
Expand Down
Expand Up @@ -24,10 +24,11 @@ module Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
-- * Performing a selection
performSelection
, prepareOutputsWith
, emptySkeleton
, selectionDelta
, SelectionCriteria (..)
, SelectionLimit (..)
, SelectionSkeleton (..)
, emptySkeleton
, SelectionResult (..)
, SelectionError (..)
, BalanceInsufficientError (..)
Expand Down Expand Up @@ -75,21 +76,23 @@ import Algebra.PartialOrd
import Cardano.Numeric.Util
( padCoalesce, partitionNatural )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..), subtractCoin )
( Coin (..), addCoin, subtractCoin, sumCoins )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId, TokenMap )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..) )
import Cardano.Wallet.Primitive.Types.Tx
( TxIn, TxOut )
( TxIn, TxOut, txOutCoin )
import Cardano.Wallet.Primitive.Types.UTxOIndex
( SelectionFilter (..), UTxOIndex (..) )
import Control.Monad.Random.Class
( MonadRandom (..) )
import Control.Monad.Trans.State
( StateT (..) )
import Data.Function
( (&) )
import Data.Functor.Identity
( Identity (..) )
import Data.Generics.Internal.VL.Lens
Expand All @@ -115,6 +118,7 @@ import GHC.Stack
import Numeric.Natural
( Natural )

import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Primitive.Types.Tx as Tx
Expand Down Expand Up @@ -237,8 +241,24 @@ buildSelectionResult changeF s@SelectionResult{inputsSelected,extraCoinSource} =
inputsF :: NonEmpty (TxIn, TxOut) -> Builder
inputsF = blockListF' "+" tupleF

changeF :: NonEmpty TokenBundle -> Builder
changeF = blockListF . fmap TokenBundle.Flat
-- | Calculate the actual difference between the total outputs (incl. change)
-- and total inputs of a particular selection. By construction, this should be
-- greater than total fees and deposits.
selectionDelta
:: (change -> Coin)
-> SelectionResult change
-> Coin
selectionDelta getChangeCoin sel@SelectionResult{inputsSelected,extraCoinSource} =
let
totalOut
= sumCoins (getChangeCoin <$> changeGenerated sel)
& addCoin (sumCoins (txOutCoin <$> outputsCovered sel))

totalIn
= sumCoins (txOutCoin . snd <$> inputsSelected)
& addCoin (fromMaybe (Coin 0) extraCoinSource)
in
Coin.distance totalIn totalOut

-- | Represents the set of errors that may occur while performing a selection.
--
Expand Down

0 comments on commit 5a60d54

Please sign in to comment.