Skip to content

Commit

Permalink
Merge pull request #53 from kadena-io/emmanuel/poll-responses-from-pact
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueldenloye authored Dec 11, 2023
2 parents 8d66b53 + 9f5b5e5 commit f9c8e66
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions chainweb-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions lib/Chainweb/Api/PactPollResponses.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Chainweb.Api.PactPollResponses where

------------------------------------------------------------------------------
import Control.Applicative ((<|>))
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)

newtype TxId = TxId Word64
deriving (Eq, Ord, Show, FromJSON)

newtype PactResult = PactResult { unPactResult :: Either Value Value }
deriving (Eq, Show)


instance FromJSON PactResult where
parseJSON (Object o) = PactResult <$> (Right <$> o .: "data" <|> Left <$> o .: "error")
parseJSON p = fail $ "Invalid PactResult: " ++ show p

newtype Gas = Gas Int64
deriving (Eq, Ord, Show, FromJSON)

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 .: "reqKey"
<*> 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)

newtype PollResponses = PollResponses
{ _prResults :: HM.HashMap RequestKey (CommandResult PactHash)
} deriving (Eq, Show)

instance FromJSON PollResponses where
parseJSON v = PollResponses <$> withObject "PollResponses" (\_ -> parseJSON v) v

0 comments on commit f9c8e66

Please sign in to comment.