Skip to content

Commit

Permalink
fixes estimateSmartFee
Browse files Browse the repository at this point in the history
  • Loading branch information
GambolingPangolin committed Mar 7, 2019
1 parent d14e796 commit 9959246
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/Network/Bitcoin/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ module Network.Bitcoin.Wallet ( Client
, isAddressValid
) where

import Control.Exception (throw)
import Control.Monad
import Data.Aeson as A
import Data.Aeson.Types (parseEither)
import Data.Bifunctor (first)
import Data.Bool (bool)
import qualified Data.ByteString.Lazy.Char8 as BSL8
import qualified Data.HashMap.Lazy as HM
import qualified Data.List as List
import Data.Maybe
import Data.Text
import Data.Time.Clock.POSIX
Expand Down Expand Up @@ -769,13 +768,9 @@ instance ToJSON EstimationMode where


-- | Estimate the fee per kb to send a transaction
estimateSmartFee :: Client -> Word32 -> Maybe EstimationMode -> IO (Either [String] Double)
estimateSmartFee :: Client -> Word32 -> Maybe EstimationMode -> IO Double
estimateSmartFee client target mode =
parse <$> callApi client "estimatesmartfee" (catMaybes [ Just $ tj target, tj <$> mode ])
where
parse = join . first pure . parseEither parseResp
parseResp = withObject "estimatesmartfee response" $ \obj -> do
merrs <- obj .:? "errors"
flip (maybe (parseVals obj)) merrs $ \errs ->
bool (pure $ Left errs) (parseVals obj) . List.null $ errs
parseVals = fmap Right . (.: "feerate")
parse = either (throw . BitcoinResultTypeError . BSL8.pack) id . parseEither parseResp
parseResp = withObject "estimatesmartfee response" (.: "feerate")

0 comments on commit 9959246

Please sign in to comment.