Skip to content

Commit cb3f550

Browse files
authored
Merge pull request #988 from IntersectMBO/abstract-grpc-module
Abstract out GRPC module
2 parents 602de99 + a6edcc6 commit cb3f550

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

cardano-wasm/src/Cardano/Wasm/Internal/Api/GRPC.hs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,54 @@ module Cardano.Wasm.Internal.Api.GRPC where
22

33
import Cardano.Wasm.Internal.Api.Tx qualified as Wasm
44
import Cardano.Wasm.Internal.ExceptionHandling (rightOrError, toMonadFail)
5-
import Cardano.Wasm.Internal.JavaScript.GRPCTypes
6-
( JSGRPCClient
7-
, JSUtxos
8-
)
95

106
import Data.ByteString.Base16 qualified as Base16
117
import Data.ByteString.Base64 qualified as Base64
128
import Data.ByteString.Char8 qualified as BS
139

14-
-- | Internal data for the GrpcConnection virtual object. Currently, it is just a wrapper around the JSGRPCClient,
15-
-- which is a JavaScript object that allows us to interact with the Cardano Node via gRPC-web.
16-
newtype GrpcObject
17-
= GrpcObject JSGRPCClient
10+
-- | Internal data for the GrpcConnection virtual object. Currently, it is just a wrapper around the grpcClient,
11+
-- which is some object that allows us to interact with the Cardano Node via gRPC or GRPC-web.
12+
newtype GrpcObject grpcClient
13+
= GrpcObject grpcClient
1814

1915
-- | Create a new unsigned transaction object for making a Conway era transaction.
20-
newGrpcConnectionImpl :: (String -> IO JSGRPCClient) -> String -> IO GrpcObject
21-
newGrpcConnectionImpl createClientJsFunc host = GrpcObject <$> createClientJsFunc host
16+
newGrpcConnectionImpl :: (String -> IO grpcClient) -> String -> IO (GrpcObject grpcClient)
17+
newGrpcConnectionImpl createClientFunc host = GrpcObject <$> createClientFunc host
2218

23-
-- | Get the era from the Cardano Node using GRPC-web.
24-
getEraImpl :: (JSGRPCClient -> IO Int) -> GrpcObject -> IO Int
25-
getEraImpl getEraJsFunc (GrpcObject client) = getEraJsFunc client
19+
-- | Get the era from the Cardano Node using gRPC or GRPC-web.
20+
getEraImpl :: (grpcClient -> IO Int) -> GrpcObject grpcClient -> IO Int
21+
getEraImpl getEraFunc (GrpcObject client) = getEraFunc client
2622

27-
-- | Get the protocol parameters from the Cardano Node using GRPC-web.
23+
-- | Get the protocol parameters from the Cardano Node using gRPC or GRPC-web.
2824
getProtocolParamsImpl
29-
:: (JSGRPCClient -> IO Wasm.ProtocolParamsJSON) -> GrpcObject -> IO Wasm.ProtocolParamsJSON
30-
getProtocolParamsImpl getProtocolParamsJsFunc (GrpcObject client) = getProtocolParamsJsFunc client
25+
:: (grpcClient -> IO Wasm.ProtocolParamsJSON)
26+
-> GrpcObject grpcClient
27+
-> IO Wasm.ProtocolParamsJSON
28+
getProtocolParamsImpl getProtocolParamsFunc (GrpcObject client) = getProtocolParamsFunc client
3129

32-
-- | Get all UTXOs from the node using a GRPC-web client.
30+
-- | Get all UTXOs from the node using a gRPC or GRPC-web client.
3331
getAllUtxosImpl
34-
:: (JSGRPCClient -> IO JSUtxos)
35-
-> GrpcObject
36-
-> IO JSUtxos
37-
getAllUtxosImpl getUtxosJsFunc (GrpcObject client) = getUtxosJsFunc client
32+
:: (grpcClient -> IO utxos)
33+
-> GrpcObject grpcClient
34+
-> IO utxos
35+
getAllUtxosImpl getUtxosFunc (GrpcObject client) = getUtxosFunc client
3836

39-
-- | Get UTXOs for a given address using a GRPC-web client.
37+
-- | Get UTXOs for a given address using a gRPC or GRPC-web client.
4038
getUtxosForAddressImpl
41-
:: (JSGRPCClient -> String -> IO JSUtxos)
42-
-> GrpcObject
39+
:: (grpcClient -> String -> IO utxos)
40+
-> GrpcObject grpcClient
4341
-> String
44-
-> IO JSUtxos
45-
getUtxosForAddressImpl getUtxosForAddressJsFunc (GrpcObject client) = getUtxosForAddressJsFunc client
42+
-> IO utxos
43+
getUtxosForAddressImpl getUtxosForAddressFunc (GrpcObject client) = getUtxosForAddressFunc client
4644

47-
-- | Submit a transaction to the Cardano Node using GRPC-web.
45+
-- | Submit a transaction to the Cardano Node using gRPC or GRPC-web.
4846
submitTxImpl
49-
:: (JSGRPCClient -> String -> IO (Either String String))
50-
-> GrpcObject
47+
:: (grpcClient -> String -> IO (Either String String))
48+
-> GrpcObject grpcClient
5149
-> String
5250
-> IO String
53-
submitTxImpl submitTxJsFunc (GrpcObject client) tx =
54-
toMonadFail . rightOrError . (base64ToBase16 =<<) =<< submitTxJsFunc client tx
51+
submitTxImpl submitTxFunc (GrpcObject client) tx =
52+
toMonadFail . rightOrError . (base64ToBase16 =<<) =<< submitTxFunc client tx
5553
where
5654
-- We reencode as Base16 because it is a more common format for txIds
5755
base64ToBase16 :: String -> Either String String

cardano-wasm/src/Cardano/Wasm/Internal/JavaScript/Bridge.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ instance ToJSVal String JSString where
147147
instance ToJSVal Wasm.ProtocolParamsJSON JSProtocolParams where
148148
toJSVal (Wasm.ProtocolParamsJSON json) = js_parse $ toJSString json
149149

150-
instance ToJSVal Wasm.GrpcObject JSGrpc where
151-
toJSVal :: Wasm.GrpcObject -> IO JSGrpc
150+
instance ToJSVal (Wasm.GrpcObject JSGRPCClient) JSGrpc where
151+
toJSVal :: (Wasm.GrpcObject JSGRPCClient) -> IO JSGrpc
152152
toJSVal (Wasm.GrpcObject client) = return client
153153

154154
instance ToJSVal Ledger.Coin JSCoin where
@@ -192,8 +192,8 @@ instance FromJSVal JSTxIx Api.TxIx where
192192
instance FromJSVal JSProtocolParams Wasm.ProtocolParamsJSON where
193193
fromJSVal = fmap Wasm.ProtocolParamsJSON . jsValToJSONString
194194

195-
instance FromJSVal JSGrpc Wasm.GrpcObject where
196-
fromJSVal :: JSGrpc -> IO Wasm.GrpcObject
195+
instance FromJSVal JSGrpc (Wasm.GrpcObject JSGRPCClient) where
196+
fromJSVal :: JSGrpc -> IO (Wasm.GrpcObject JSGRPCClient)
197197
fromJSVal jsVal = return $ Wasm.GrpcObject jsVal
198198

199199
-- * WalletObject

0 commit comments

Comments
 (0)