Skip to content

Commit

Permalink
Report output index in handler for SelectionOutputCoinInsufficient.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Oct 27, 2022
1 parent 04465ac commit 92ef253
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
41 changes: 21 additions & 20 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import Cardano.Wallet.CoinSelection
( SelectionBalanceError (..)
, SelectionCollateralError
, SelectionError (..)
, SelectionOutputCoinInsufficientError
, SelectionOutputError (..)
, SelectionOutputErrorInfo (..)
, SelectionOutputSizeExceedsLimitError
Expand All @@ -110,6 +109,8 @@ import Control.Monad.Trans.Except
( throwE )
import Data.Generics.Internal.VL
( view, (^.) )
import Data.IntCast
( intCastMaybe )
import Data.List
( isInfixOf, isPrefixOf, isSubsequenceOf )
import Data.Maybe
Expand All @@ -118,6 +119,8 @@ import Data.Text
( Text )
import Data.Text.Class
( ToText (..) )
import Data.Word
( Word32 )
import Fmt
( blockListF', build, fmt, listF, pretty )
import Network.HTTP.Media
Expand All @@ -126,6 +129,8 @@ import Network.HTTP.Types
( hContentType )
import Network.Wai
( Request (pathInfo) )
import Safe
( fromJustNote )
import Servant
( Accept (contentType), JSON, Proxy (Proxy) )
import Servant.Server
Expand Down Expand Up @@ -770,37 +775,33 @@ instance IsServerError (ErrInvalidDerivationIndex 'Soft level) where
]

instance IsServerError (SelectionOutputError WalletSelectionContext) where
toServerError (SelectionOutputError _index info) = case info of
-- TODO: ADP-2299: report the index
toServerError (SelectionOutputError index info) = case info of
SelectionOutputCoinInsufficient e ->
toServerError e
flip (apiError err403) selectionOutputCoinInsufficientMessage $
UtxoTooSmall ApiErrorTxOutputLovelaceInsufficient
{ txOutputIndex =
flip fromJustNote (intCastMaybe @Int @Word32 index) $
unwords
[ "SelectionOutputError: index out of bounds:"
, show index
]
, txOutputLovelaceSpecified =
Coin.toQuantity $ TokenBundle.getCoin $ snd $ view #output e
, txOutputLovelaceRequiredMinimum =
Coin.toQuantity $ view #minimumExpectedCoin e
}
SelectionOutputSizeExceedsLimit e ->
toServerError e
SelectionOutputTokenQuantityExceedsLimit e ->
toServerError e

instance IsServerError
(SelectionOutputCoinInsufficientError WalletSelectionContext)
where
toServerError e =
apiError err403 (UtxoTooSmall details) message
where
message = T.unwords
selectionOutputCoinInsufficientMessage = T.unwords
[ "One of the outputs you've specified has an ada quantity that is"
, "below the minimum required. Either increase the ada quantity to"
, "at least the minimum, or specify an ada quantity of zero, in"
, "which case the wallet will automatically assign the correct"
, "minimum ada quantity to the output."
]
details = ApiErrorTxOutputLovelaceInsufficient
{ txOutputIndex =
-- TODO: ADP-2299
0
, txOutputLovelaceSpecified =
Coin.toQuantity $ TokenBundle.getCoin $ snd $ view #output e
, txOutputLovelaceRequiredMinimum =
Coin.toQuantity $ view #minimumExpectedCoin e
}

instance IsServerError
(SelectionOutputSizeExceedsLimitError WalletSelectionContext)
Expand Down
1 change: 1 addition & 0 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ library cardano-wallet-api-http
, quiet
, random
, retry
, safe
, servant
, servant-client
, servant-client-core
Expand Down

0 comments on commit 92ef253

Please sign in to comment.