{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Bitcoin.Core.RPC.Transactions
( getTransaction
, sendRawTransaction
, sendTransaction
, MempoolTestResult (..)
, testMempoolAccept
) where
import Data.Aeson (FromJSON (..), withObject, (.:),
(.:?))
import Data.Proxy (Proxy (..))
import qualified Data.Serialize as S
import Data.Text (Text)
import Network.Haskoin.Block (BlockHash)
import Network.Haskoin.Transaction (Tx, TxHash)
import Network.Haskoin.Util (encodeHex)
import Servant.API ((:<|>) (..))
import Servant.Bitcoind (BitcoindClient, BitcoindEndpoint,
C, DefFalse, F, I, O,
toBitcoindClient)
data MempoolTestResult = MempoolTestResult
{ testTxid :: TxHash
, txAccepted :: Bool
, rejectReason :: Maybe Text
} deriving (Eq, Show)
instance FromJSON MempoolTestResult where
parseJSON = withObject "MempoolTestResult" $ \o ->
MempoolTestResult <$> o .: "txid" <*> o .: "allowed" <*> o .:? "reject-reason"
type RawTxRpc
= BitcoindEndpoint "sendrawtransaction" (I Text -> O Double -> C TxHash)
:<|> BitcoindEndpoint "getrawtransaction" (I TxHash -> F DefFalse Bool -> O BlockHash -> C Tx)
:<|> BitcoindEndpoint "testmempoolaccept" (I [Tx] -> O Double -> C [MempoolTestResult])
sendRawTransaction :: Text -> Maybe Double -> BitcoindClient TxHash
sendTransaction :: Tx -> Maybe Double -> BitcoindClient TxHash
sendTransaction = sendRawTransaction . encodeHex . S.encode
getTransaction :: TxHash -> Maybe BlockHash -> BitcoindClient Tx
testMempoolAccept :: [Tx] -> Maybe Double -> BitcoindClient [MempoolTestResult]
sendRawTransaction :<|> getTransaction :<|> testMempoolAccept = toBitcoindClient $ Proxy @RawTxRpc