Skip to content

Commit

Permalink
prevent users from withdrawing from empty reward accounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jul 30, 2020
1 parent 3183a7f commit d3ebc72
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module Cardano.Wallet
, ErrValidateSelection
, ErrNotASequentialWallet (..)
, ErrUTxOTooSmall (..)
, ErrWithdrawalNotWorth (..)

-- ** Migration
, selectCoinsForMigration
Expand Down Expand Up @@ -2287,6 +2288,10 @@ data ErrReadChimericAccount
| ErrReadChimericAccountNoSuchWallet ErrNoSuchWallet
deriving (Generic, Eq, Show)

data ErrWithdrawalNotWorth
= ErrWithdrawalNotWorth
deriving (Generic, Eq, Show)

{-------------------------------------------------------------------------------
Utils
-------------------------------------------------------------------------------}
Expand Down
16 changes: 15 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import Cardano.Wallet
, ErrWalletAlreadyExists (..)
, ErrWalletNotResponding (..)
, ErrWithRootKey (..)
, ErrWithdrawalNotWorth (..)
, ErrWrongPassphrase (..)
, FeeEstimation (..)
, HasLogger
Expand Down Expand Up @@ -313,7 +314,7 @@ import Control.Concurrent.Async
import Control.Exception
( IOException, bracket, throwIO, tryJust )
import Control.Monad
( forM, forever, void, (>=>) )
( forM, forever, void, when, (>=>) )
import Control.Monad.Catch
( handle )
import Control.Monad.IO.Class
Expand Down Expand Up @@ -1241,6 +1242,9 @@ postTransaction ctx genChange (ApiT wid) body = do
(, const (xprv, mempty))
<$> liftIO (W.readNextWithdrawal @_ @s @t @k wrk wid wdrl)

when (isJust (body ^. #withdrawal) && wdrl == Quantity 0) $ do
liftHandler $ throwE ErrWithdrawalNotWorth

selection <- liftHandler $ W.selectCoinsForPayment @_ @s @t wrk wid outs wdrl
pure (selection, credentials)

Expand Down Expand Up @@ -2449,6 +2453,16 @@ instance LiftHandler ErrNotASequentialWallet where
, "Make sure to use a sequential wallet style, like Icarus."
]

instance LiftHandler ErrWithdrawalNotWorth where
handler = \case
ErrWithdrawalNotWorth ->
apiError err403 WithdrawalNotWorth $ mconcat
[ "I've noticed that you're requesting a withdrawal from an "
, "account that is either empty or doesn't have a balance big "
, "enough to deserve being withdrawn. I won't proceed with that "
, "request."
]

instance LiftHandler (Request, ServerError) where
handler (req, err@(ServerError code _ body headers))
| not (isJSON body) = case code of
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ data ApiErrorCode
| UtxoTooSmall
| MinWithdrawalWrong
| AlreadyWithdrawing
| WithdrawalNotWorth
deriving (Eq, Generic, Show)

-- | Defines a point in time that can be formatted as and parsed from an
Expand Down

0 comments on commit d3ebc72

Please sign in to comment.