Skip to content

Commit

Permalink
Use runSelectionNonEmpty in performSelection.
Browse files Browse the repository at this point in the history
This usage replaces `runSelection`, which may return an empty selection.
  • Loading branch information
jonathanknowles committed Sep 12, 2021
1 parent 2fcfb4a commit 53a8221
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection/Balance.hs
Expand Up @@ -513,20 +513,21 @@ performSelection minCoinFor costFor bundleSizeAssessor criteria
NE.fromList insufficientMinCoinValues

| otherwise = do
state <- runSelection RunSelectionParams
maybeSelection <- runSelectionNonEmpty RunSelectionParams
{ selectionLimit
, extraCoinSource
, utxoAvailable
, minimumBalance = balanceRequired
}
let balanceSelected = fullBalance (selected state) extraCoinSource
if balanceRequired `leq` balanceSelected then
makeChangeRepeatedly state
else
pure $ Left $ SelectionInsufficient $ SelectionInsufficientError
{ inputsSelected = UTxOIndex.toList (selected state)
, balanceRequired
}
case maybeSelection of
Nothing ->
selectionInsufficientError []
Just selection -> do
let utxoSelected = selected selection
let balanceSelected = fullBalance utxoSelected extraCoinSource
if balanceRequired `leq` balanceSelected
then makeChangeRepeatedly selection
else selectionInsufficientError $ UTxOIndex.toList utxoSelected
where
SelectionCriteria
{ outputsToCover
Expand All @@ -537,6 +538,13 @@ performSelection minCoinFor costFor bundleSizeAssessor criteria
, assetsToBurn
} = criteria

selectionInsufficientError :: [(TxIn, TxOut)] -> m (Either SelectionError a)
selectionInsufficientError inputsSelected =
pure $ Left $ SelectionInsufficient $ SelectionInsufficientError
{ inputsSelected
, balanceRequired
}

requestedOutputs = F.foldMap (view #tokens) outputsToCover
requestedOutputAssets = view #tokens requestedOutputs

Expand Down

0 comments on commit 53a8221

Please sign in to comment.