{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wall #-}
module Network.Bitcoin.Net ( Client
, getClient
, getConnectionCount
, PeerInfo(..)
, getPeerInfo
, AddNodeCommand(..)
, addNode
, disconnectNode
) where
import Control.Monad
import Data.Aeson
import Network.Bitcoin.Internal
getConnectionCount :: Client -> IO Integer
getConnectionCount client = callApi client "getconnectioncount" []
data PeerInfo =
PeerInfo {
addressName :: Text
, services :: Text
, lastSend :: Integer
, lastRecv :: Integer
, bytesSent :: Integer
, bytesRecv :: Integer
, connectionTime :: Integer
, peerVersion :: Integer
, peerSubversion :: Text
, inbound :: Bool
, startingHeight :: Integer
, banScore :: Integer
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON PeerInfo where
parseJSON (Object o) = PeerInfo <$> o .: "addr"
<*> o .: "services"
<*> o .: "lastsend"
<*> o .: "lastrecv"
<*> o .: "bytessent"
<*> o .: "bytesrecv"
<*> o .: "conntime"
<*> o .: "version"
<*> o .: "subver"
<*> o .: "inbound"
<*> o .: "startingheight"
<*> o .: "banscore"
parseJSON _ = mzero
getPeerInfo :: Client -> IO [PeerInfo]
getPeerInfo client = callApi client "getpeerinfo" []
data AddNodeCommand = Add | Remove | OneTry
deriving (Show, Read, Eq)
instance ToJSON AddNodeCommand where
toJSON Add = String "add"
toJSON Remove = String "remove"
toJSON OneTry = String "onetry"
addNode :: Client -> Text -> AddNodeCommand -> IO ()
addNode client url com = unNil <$> callApi client "addnode" [tj url, tj com]
disconnectNode :: Client -> Maybe Text -> Maybe Int -> IO ()
disconnectNode client url nodeId = unNil <$> callApi client "disconnectnode" [tj url, tj nodeId]