From 3d4469cedec7e4bcb256440eac5c490f3853639e Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 27 Aug 2020 19:22:00 +0200 Subject: [PATCH] pass down withdrawal value to 'handleCannotCoverFee' to prevent underflow in fee calculation. --- lib/core/src/Cardano/Wallet.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/core/src/Cardano/Wallet.hs b/lib/core/src/Cardano/Wallet.hs index eaff67baf02..5d64ead107f 100644 --- a/lib/core/src/Cardano/Wallet.hs +++ b/lib/core/src/Cardano/Wallet.hs @@ -1459,7 +1459,7 @@ estimateFeeForPayment ctx wid recipients withdrawal = do guardCoinSelection minUtxo cs estimateFeeForCoinSelection $ (Fee . feeBalance <$> selectCoins) - `catchE` handleCannotCover utxo recipients + `catchE` handleCannotCover utxo withdrawal recipients -- | When estimating fee, it is rather cumbersome to return "cannot cover fee" -- whereas clients are just asking for an estimation. Therefore, we convert @@ -1468,13 +1468,19 @@ estimateFeeForPayment ctx wid recipients withdrawal = do handleCannotCover :: Monad m => UTxO + -> Quantity "lovelace" Word64 -> NonEmpty TxOut -> ErrSelectForPayment e -> ExceptT (ErrSelectForPayment e) m Fee -handleCannotCover utxo outs = \case +handleCannotCover utxo (Quantity withdrawal) outs = \case ErrSelectForPaymentFee (ErrCannotCoverFee missing) -> do - let available = fromIntegral (W.balance utxo) - sum (getCoin . coin <$> outs) - pure $ Fee $ available + missing + let available + = fromIntegral (W.balance utxo) + + fromIntegral withdrawal + let payment + = sum (getCoin . coin <$> outs) + pure $ Fee $ + available + missing - payment e -> throwE e