Skip to content

Commit

Permalink
allows for multiple filters when selecting matching quantities
Browse files Browse the repository at this point in the history
  This will be crucial for Hardware devices and other clients who won't support MA initially. It is a only a best effort solution as this would only prioritize ada-only UTxO but eventually reach out to multi-assets UTxO if there's not enough ada-only utxo to cover for a particular payment. It is also generally good for other purposes to only select Ada utxo if we can to avoid needlessly moving tokens around.
  • Loading branch information
KtorZ committed Jan 18, 2021
1 parent 96c077d commit 01969b4
Showing 1 changed file with 16 additions and 5 deletions.
Expand Up @@ -251,23 +251,34 @@ runSelection available minimumBalance =
assetSelectionLens (asset, minimumAssetQuantity) = SelectionLens
{ currentQuantity = assetQuantity asset . selected
, minimumQuantity = unTokenQuantity minimumAssetQuantity
, selectQuantity = selectMatchingQuantity $ WithAsset asset
, selectQuantity = selectMatchingQuantity
[ WithAsset asset
]
}

coinSelectionLens :: SelectionLens m SelectionState
coinSelectionLens = SelectionLens
{ currentQuantity = coinQuantity . selected
, minimumQuantity = fromIntegral $ unCoin minimumCoinQuantity
, selectQuantity = selectMatchingQuantity Any
, selectQuantity = selectMatchingQuantity
[ WithAdaOnly
, Any
]
}

selectMatchingQuantity
:: MonadRandom m
=> SelectionFilter
=> [SelectionFilter]
-- A list of selection filters, traversed from left to right if previous
-- filter failed. This allows for giving some filters priorities over
-- others.
-> SelectionState
-> m (Maybe SelectionState)
selectMatchingQuantity f s =
fmap updateState <$> UTxOIndex.selectRandom (leftover s) f
selectMatchingQuantity [] _ = pure Nothing
selectMatchingQuantity (h:q) s = do
UTxOIndex.selectRandom (leftover s) h >>= \case
Just s' -> pure $ Just $ updateState s'
Nothing -> selectMatchingQuantity q s
where
updateState ((i, o), remaining) = SelectionState
{ leftover = remaining
Expand Down

0 comments on commit 01969b4

Please sign in to comment.