From 0c2fb66283679e65d4ecbdf258d6a906a3f35f54 Mon Sep 17 00:00:00 2001 From: Emmanuel Denloye-Ito Date: Thu, 7 Dec 2023 15:50:53 -0500 Subject: [PATCH] Add PactPollResponses module This is a small step in reducing downstream projects' reliance on pact types. Some projects can't keep apace with both pact's (and chainweb's) need to use very recently published GHC versions. --- chainweb-api.cabal | 1 + lib/Chainweb/Api/PactPollResponses.hs | 60 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/Chainweb/Api/PactPollResponses.hs diff --git a/chainweb-api.cabal b/chainweb-api.cabal index 2035902..baadd35 100644 --- a/chainweb-api.cabal +++ b/chainweb-api.cabal @@ -64,6 +64,7 @@ library Chainweb.Api.NodeInfo Chainweb.Api.PactCommand Chainweb.Api.PactNumber + Chainweb.Api.PactPollResponses Chainweb.Api.ParsedNumbers Chainweb.Api.Payload Chainweb.Api.RespItems diff --git a/lib/Chainweb/Api/PactPollResponses.hs b/lib/Chainweb/Api/PactPollResponses.hs new file mode 100644 index 0000000..ff93beb --- /dev/null +++ b/lib/Chainweb/Api/PactPollResponses.hs @@ -0,0 +1,60 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} + +module Chainweb.Api.PactPollResponses where + +------------------------------------------------------------------------------ +import Data.Aeson +import Data.Hashable (Hashable) +import qualified Data.HashMap.Strict as HM +import Data.Int +import Data.Text (Text) +import Data.Word +------------------------------------------------------------------------------ + +newtype RequestKey = RequestKey { unRequestKey :: Text } + deriving (Eq, Ord, Show, Hashable, FromJSON, FromJSONKey, ToJSON, ToJSONKey) + +newtype TxId = TxId Word64 + deriving (Eq, Ord, Show, FromJSON, ToJSON) + +newtype PactResult = PactResult { unPactResult :: Either Value Value } + deriving (Eq, Show, FromJSON, ToJSON) + +newtype Gas = Gas Int64 + deriving (Eq, Ord, Show, FromJSON, ToJSON) + +data CommandResult l = CommandResult + { _crReqKey :: !RequestKey + , _crTxId :: !(Maybe TxId) + , _crResult :: !PactResult + , _crGas :: !Gas + , _crLogs :: !(Maybe l) + , _crContinuation :: !(Maybe Value) -- just accept the json? + , _crMetaData :: !(Maybe Value) + , _crEvents :: [Value] -- just accept the json? + } deriving (Eq, Show) + +instance FromJSON l => FromJSON (CommandResult l) where + parseJSON = withObject "CommandResult" $ \o -> CommandResult + <$> o .: "requestKey" + <*> o .:? "txId" + <*> o .: "result" + <*> o .: "gas" + <*> o .:? "logs" + <*> o .:? "continuation" + <*> o .:? "metadata" + <*> (events <$> o .: "events") + where + events Nothing = [] + events (Just es) = es + +newtype PactHash = PactHash { unHash :: Text } + deriving (Eq, Ord, Show, FromJSON, ToJSON) + +newtype PollResponses = PollResponses + { _prResults :: HM.HashMap RequestKey (CommandResult PactHash) + } deriving (Eq, Show) + +instance FromJSON PollResponses where + parseJSON v = PollResponses <$> withObject "PollResponses" (\_ -> parseJSON v) v