module Network.Bitcoin.BlockChain ( Auth(..)
, TransactionID
, BTC
, ScriptSig(..)
, getBlockCount
, getDifficulty
, setTransactionFee
, getRawMemoryPool
, BlockHash
, getBlockHash
, Block(..)
, getBlock
, OutputSetInfo(..)
, getOutputSetInfo
, OutputInfo(..)
, getOutputInfo
) where
import Control.Applicative
import Control.Monad
import Data.Aeson
import Network.Bitcoin.Internal
getBlockCount :: Auth -> IO Integer
getBlockCount auth = callApi auth "getblockcount" []
getDifficulty :: Auth -> IO Integer
getDifficulty auth = callApi auth "getdifficulty" []
setTransactionFee :: Auth -> BTC -> IO ()
setTransactionFee auth fee =
stupidAPI <$> callApi auth "settxfee" [ tj fee ]
where stupidAPI :: Bool -> ()
stupidAPI = const ()
getRawMemoryPool :: Auth -> IO (Vector TransactionID)
getRawMemoryPool auth = callApi auth "getrawmempool" []
type BlockHash = HexString
getBlockHash :: Auth
-> Integer
-> IO BlockHash
getBlockHash auth idx = callApi auth "getblockhash" [ tj idx ]
data Block = Block { blockHash :: BlockHash
, blkConfirmations :: Integer
, blkSize :: Integer
, blkHeight :: Integer
, blkVersion :: Integer
, merkleRoot :: BlockHash
, subTransactions :: Vector TransactionID
, blkTime :: Integer
, blkNonce :: Integer
, blkBits :: HexString
, blkDifficulty :: Integer
, nextBlock :: Maybe BlockHash
, prevBlock :: Maybe BlockHash
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON Block where
parseJSON (Object o) = Block <$> o .: "hash"
<*> o .: "confirmations"
<*> o .: "size"
<*> o .: "height"
<*> o .: "version"
<*> o .: "merkleroot"
<*> o .: "tx"
<*> o .: "time"
<*> o .: "nonce"
<*> o .: "bits"
<*> o .: "difficulty"
<*> o .:? "nextblockhash"
<*> o .:? "previousblockhash"
parseJSON _ = mzero
getBlock :: Auth -> BlockHash -> IO Block
getBlock auth bh = callApi auth "getblock" [ tj bh ]
data OutputSetInfo =
OutputSetInfo { osiBestBlock :: BlockHash
, numTransactions :: Integer
, transactionOutputs :: Integer
, serializedSize :: Integer
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON OutputSetInfo where
parseJSON (Object o) = OutputSetInfo <$> o .: "bestblock"
<*> o .: "transactions"
<*> o .: "txouts"
<*> o .: "bytes_serialized"
parseJSON _ = mzero
getOutputSetInfo :: Auth -> IO OutputSetInfo
getOutputSetInfo auth = callApi auth "gettxoutsetinfo" []
data OutputInfo =
OutputInfo { oiBestBlock :: BlockHash
, oiConfirmations :: Integer
, oiAmount :: BTC
, oiScriptPubKey :: ScriptSig
, oiVersion :: Integer
, oiCoinBase :: Bool
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON OutputInfo where
parseJSON (Object o) = OutputInfo <$> o .: "bestblock"
<*> o .: "confirmations"
<*> o .: "amount"
<*> o .: "scriptPubKey"
<*> o .: "version"
<*> o .: "coinbase"
parseJSON _ = mzero
getOutputInfo :: Auth
-> TransactionID
-> Integer
-> IO OutputInfo
getOutputInfo auth txid n = callApi auth "gettxout" [ tj txid, tj n ]