Skip to content

Commit

Permalink
Use SelectionContext parameter consistently in SelectionOutputError.
Browse files Browse the repository at this point in the history
This refactoring adjusts type `SelectionOutputCoinInsufficientError` so
that it takes a `ctx` parameter, rather than an `Address ctx` parameter.

As a result, all `SelectionOutputError` constructors now use the same
style of parameterisation:

```
data SelectionOutputError ctx
    = SelectionOutputCoinInsufficient
        (SelectionOutputCoinInsufficientError ctx)
    | SelectionOutputSizeExceedsLimit
        (SelectionOutputSizeExceedsLimitError ctx)
    | SelectionOutputTokenQuantityExceedsLimit
        (SelectionOutputTokenQuantityExceedsLimitError ctx)
```
  • Loading branch information
jonathanknowles committed Aug 2, 2022
1 parent fe2841c commit 3bfe1bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5090,7 +5090,7 @@ instance IsServerError (SelectionOutputError WalletSelectionContext) where
toServerError e

instance IsServerError
(SelectionOutputCoinInsufficientError Address)
(SelectionOutputCoinInsufficientError WalletSelectionContext)
where
toServerError e =
apiError err403 UtxoTooSmall $ T.unlines [preamble, details]
Expand Down
28 changes: 16 additions & 12 deletions lib/core/src/Cardano/Wallet/CoinSelection/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -799,29 +799,34 @@ verifySelectionInputCountWithinLimit cs _ps selection =
-- Selection verification: minimum ada quantities
--------------------------------------------------------------------------------

newtype FailureToVerifySelectionOutputCoinsSufficient address =
newtype FailureToVerifySelectionOutputCoinsSufficient ctx =
FailureToVerifySelectionOutputCoinsSufficient
(NonEmpty (SelectionOutputCoinInsufficientError address))
(NonEmpty (SelectionOutputCoinInsufficientError ctx))
deriving (Eq, Show)

data SelectionOutputCoinInsufficientError address =
data SelectionOutputCoinInsufficientError ctx =
SelectionOutputCoinInsufficientError
{ minimumExpectedCoin :: Coin
, output :: (address, TokenBundle)
, output :: (Address ctx, TokenBundle)
}
deriving (Eq, Generic, Show)
deriving Generic

deriving instance SelectionContext ctx =>
Eq (SelectionOutputCoinInsufficientError ctx)
deriving instance SelectionContext ctx =>
Show (SelectionOutputCoinInsufficientError ctx)

verifySelectionOutputCoinsSufficient
:: forall ctx. SelectionContext ctx => VerifySelection ctx
verifySelectionOutputCoinsSufficient cs _ps selection =
verifyEmpty errors FailureToVerifySelectionOutputCoinsSufficient
where
errors :: [SelectionOutputCoinInsufficientError (Address ctx)]
errors :: [SelectionOutputCoinInsufficientError ctx]
errors = mapMaybe maybeError (selectionAllOutputs cs selection)

maybeError
:: (Address ctx, TokenBundle)
-> Maybe (SelectionOutputCoinInsufficientError (Address ctx))
-> Maybe (SelectionOutputCoinInsufficientError ctx)
maybeError output
| snd output ^. #coin < minimumExpectedCoin =
Just SelectionOutputCoinInsufficientError
Expand Down Expand Up @@ -963,8 +968,7 @@ data FailureToVerifySelectionOutputCoinInsufficientError address =

verifySelectionOutputCoinInsufficientError
:: SelectionContext ctx
=> VerifySelectionError
(SelectionOutputCoinInsufficientError (Address ctx)) ctx
=> VerifySelectionError (SelectionOutputCoinInsufficientError ctx) ctx
verifySelectionOutputCoinInsufficientError cs _ps e =
verifyAll
[ reportedMinCoinValue == verifiedMinCoinValue
Expand Down Expand Up @@ -1434,7 +1438,7 @@ prepareOutputsInternal constraints outputsUnprepared
-- The complete list of outputs whose ada quantities are below the minimum
-- required:
insufficientCoins
:: [SelectionOutputCoinInsufficientError (Address ctx)]
:: [SelectionOutputCoinInsufficientError ctx]
insufficientCoins =
mapMaybe (verifyOutputCoinSufficient constraints) outputsToCover

Expand Down Expand Up @@ -1472,7 +1476,7 @@ prepareOutputsWith minCoinValueFor =
--
data SelectionOutputError ctx
= SelectionOutputCoinInsufficient
(SelectionOutputCoinInsufficientError (Address ctx))
(SelectionOutputCoinInsufficientError ctx)
| SelectionOutputSizeExceedsLimit
(SelectionOutputSizeExceedsLimitError ctx)
| SelectionOutputTokenQuantityExceedsLimit
Expand Down Expand Up @@ -1559,7 +1563,7 @@ verifyOutputTokenQuantities out =
verifyOutputCoinSufficient
:: SelectionConstraints ctx
-> (Address ctx, TokenBundle)
-> Maybe (SelectionOutputCoinInsufficientError (Address ctx))
-> Maybe (SelectionOutputCoinInsufficientError ctx)
verifyOutputCoinSufficient constraints output
| actualCoin >= minimumExpectedCoin =
Nothing
Expand Down

0 comments on commit 3bfe1bb

Please sign in to comment.