Skip to content

Commit 942b157

Browse files
committed
Change return type to RawTx from Text
Change-type: patch Signed-off-by: Giovanni Garufi <giovanni@mlabs.city>
1 parent 6dd9fde commit 942b157

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

src/BotPlutusInterface/Server.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import BotPlutusInterface.Types (
88
ContractEnvironment (..),
99
ContractState (ContractState, csActivity, csObservableState),
1010
PABConfig (..),
11+
RawTx,
1112
SomeContractState (SomeContractState),
1213
)
1314
import Control.Concurrent (ThreadId, forkIO)
@@ -17,14 +18,14 @@ import Control.Monad.Error.Class (throwError)
1718
import Control.Monad.IO.Class (liftIO)
1819
import Data.Aeson (FromJSON, ToJSON (toJSON))
1920
import Data.Aeson qualified as JSON
21+
import Data.ByteString.Lazy qualified as LBS
2022
import Data.Either.Combinators (leftToMaybe)
2123
import Data.Kind (Type)
2224
import Data.Map qualified as Map
2325
import Data.Maybe (catMaybes)
2426
import Data.Proxy (Proxy (Proxy))
2527
import Data.Row (Row)
2628
import Data.Text (Text, unpack)
27-
import Data.Text.IO qualified as Text
2829
import Data.UUID.V4 qualified as UUID
2930
import Network.WebSockets (
3031
Connection,
@@ -70,7 +71,7 @@ type API a =
7071
)
7172
:<|> ( "rawTx"
7273
:> Capture "hash" Text
73-
:> Get '[JSON] Text
74+
:> Get '[JSON] RawTx
7475
)
7576

7677
server :: HasDefinitions t => PABConfig -> AppState -> Server (API t)
@@ -219,7 +220,7 @@ handleContract pabConf state@(AppState st) contract = liftIO $ do
219220
pure contractInstanceID
220221

221222
-- | This handler will allow to retrieve raw transactions from the pcTxFileDir if pcEnableTxEndpoint is True
222-
rawTxHandler :: PABConfig -> Text -> Handler Text
223+
rawTxHandler :: PABConfig -> Text -> Handler RawTx
223224
rawTxHandler config hash = do
224225
-- Check that endpoint is enabled
225226
assert (pcEnableTxEndpoint config)
@@ -237,8 +238,10 @@ rawTxHandler config hash = do
237238
fileExists <- liftIO $ doesFileExist path
238239
assert fileExists
239240

240-
-- Read contents of path
241-
liftIO $ Text.readFile path
241+
contents <- liftIO $ LBS.readFile path
242+
case JSON.decode contents of
243+
Just rawTx -> pure rawTx
244+
Nothing -> throwError err404
242245
where
243246
assert :: Bool -> Handler ()
244247
assert True = pure ()

src/BotPlutusInterface/Types.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE RankNTypes #-}
2+
{-# LANGUAGE TemplateHaskell #-}
23

34
module BotPlutusInterface.Types (
45
PABConfig (..),
@@ -11,16 +12,19 @@ module BotPlutusInterface.Types (
1112
HasDefinitions (..),
1213
SomeBuiltin (SomeBuiltin),
1314
endpointsToSchemas,
15+
RawTx (..),
1416
) where
1517

1618
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
1719
import Cardano.Api.ProtocolParameters (ProtocolParameters)
1820
import Control.Concurrent.STM (TVar)
1921
import Data.Aeson (ToJSON)
22+
import Data.Aeson.TH (Options (..), defaultOptions, deriveJSON)
2023
import Data.Default (Default (def))
2124
import Data.Kind (Type)
2225
import Data.Map (Map)
2326
import Data.Text (Text)
27+
import GHC.Generics (Generic)
2428
import Ledger (PubKeyHash)
2529
import Network.Wai.Handler.Warp (Port)
2630
import Plutus.PAB.Core.ContractInstance.STM (Activity)
@@ -103,3 +107,12 @@ instance Default PABConfig where
103107
, pcPort = 9080
104108
, pcEnableTxEndpoint = False
105109
}
110+
111+
data RawTx = RawTx
112+
{ rawType :: Text
113+
, rawDescription :: Text
114+
, rawCborHex :: Text
115+
}
116+
deriving (Generic, Eq, Show)
117+
118+
$(deriveJSON defaultOptions {fieldLabelModifier = drop 3} ''RawTx)

test/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Main (main) where
22

33
import Spec.BotPlutusInterface.Contract qualified
44
import Spec.BotPlutusInterface.PreBalance qualified
5-
import Spec.BotPlutusInterface.UtxoParser qualified
65
import Spec.BotPlutusInterface.Server qualified
6+
import Spec.BotPlutusInterface.UtxoParser qualified
77
import Test.Tasty (TestTree, defaultMain, testGroup)
88
import Prelude
99

test/Spec/BotPlutusInterface/Server.hs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BotPlutusInterface.Server (app, initState)
44
import BotPlutusInterface.Types (
55
HasDefinitions (..),
66
PABConfig (..),
7+
RawTx (..),
78
SomeBuiltin (..),
89
)
910

@@ -20,7 +21,8 @@ import Servant.API (Capture, Get, JSON, (:>))
2021
import Servant.Client (ClientEnv, ClientError (..), client, mkClientEnv, responseStatusCode, runClientM)
2122
import Servant.Client.Core.BaseUrl (BaseUrl (..), parseBaseUrl)
2223

23-
import Data.Aeson (FromJSON, ToJSON)
24+
import Data.Aeson (FromJSON, ToJSON, encode)
25+
import Data.ByteString.Lazy qualified as LBS
2426
import Data.Default (def)
2527
import Data.Proxy (Proxy (..))
2628
import Data.Text (Text, pack, unpack)
@@ -29,13 +31,13 @@ import System.FilePath ((</>))
2931
import System.IO.Temp (withSystemTempDirectory)
3032
import Prelude
3133

32-
type RawTxEndpointResponse = Either ClientError Text
34+
type RawTxEndpointResponse = Either ClientError RawTx
3335
type RawTxTest a = (Text -> IO RawTxEndpointResponse) -> IO a
3436

3537
tests :: TestTree
3638
tests =
3739
testGroup
38-
"Server"
40+
"BotPlutusInterface.Server"
3941
[ rawTxTests
4042
]
4143

@@ -53,13 +55,13 @@ rawTxTests =
5355
fetchTx = do
5456
initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do
5557
result <- runRawTxClient txHash
56-
result @?= Right (pack txFileContents)
58+
result @?= Right rawTx
5759

5860
fetchSignedTx :: IO ()
5961
fetchSignedTx = do
6062
initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do
6163
result <- runRawTxClient $ txHash <> ".signed"
62-
result @?= Right (pack txFileContents)
64+
result @?= Right rawTx
6365

6466
fetchOutsideTxFolder :: IO ()
6567
fetchOutsideTxFolder = do
@@ -79,7 +81,7 @@ txProxy ::
7981
Proxy
8082
( "rawTx"
8183
:> Capture "hash" Text
82-
:> Get '[JSON] Text
84+
:> Get '[JSON] RawTx
8385
)
8486
txProxy = Proxy
8587

@@ -89,7 +91,7 @@ initServerAndClient config test = do
8991
let pabConfig :: PABConfig
9092
pabConfig = config {pcTxFileDir = pack path}
9193
state <- initState
92-
writeFile (path </> txFileName) txFileContents
94+
LBS.writeFile (path </> txFileName) txFileContents
9395
testWithApplication (pure $ app @EmptyContract pabConfig state) (initClientOnPort test)
9496
where
9597
initClientOnPort :: RawTxTest a -> Int -> IO a
@@ -106,13 +108,21 @@ initServerAndClient config test = do
106108
testToRun runRawTxClient
107109

108110
txHash :: Text
109-
txHash = "aaaa"
111+
txHash = "test"
110112

111113
txFileName :: FilePath
112114
txFileName = "tx-" <> unpack txHash <> ".raw"
113115

114-
txFileContents :: String
115-
txFileContents = "test"
116+
rawTx :: RawTx
117+
rawTx =
118+
RawTx
119+
{ rawType = "TxBodyAlonzo"
120+
, rawDescription = "description"
121+
, rawCborHex = "hex"
122+
}
123+
124+
txFileContents :: LBS.ByteString
125+
txFileContents = encode rawTx
116126

117127
enableTxEndpointConfig :: PABConfig
118128
enableTxEndpointConfig = def {pcEnableTxEndpoint = True}

0 commit comments

Comments
 (0)