Skip to content

Commit 13fa459

Browse files
authored
Merge pull request #924 from IntersectMBO/mgalazyn/feature/add-pparams-json-query-wasm
cardano-wasm | Add getProtocolParams query to cardano-wasm
2 parents 917156e + 55abf92 commit 13fa459

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

cardano-wasm/grpc-example/example.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ async function do_async_work() {
4646
log("Era number:");
4747
log(eraNum);
4848

49+
let pparams = await grpcApi.getProtocolParams();
50+
log("Protocol Parameters:");
51+
log(pparams);
52+
console.log(typeof pparams);
53+
4954
finish_test();
5055
}
5156

cardano-wasm/lib-wrapper/cardano-api.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ declare interface GrpcConnection {
102102
* @returns A promise that resolves to the transaction ID.
103103
*/
104104
submitTx(txCbor: string): Promise<string>;
105+
106+
/**
107+
* Get the protocol parameters in the cardano-ledger format from the Cardano Node using a GRPC-web client.
108+
* @returns A promise that resolves to the current protocol parameters.
109+
*/
110+
getProtocolParams(): Promise<any>;
105111
}
106112

107113
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Cardano.Wasm.Internal.Api.GRPC where
22

3+
import Cardano.Wasm.Internal.Api.Tx qualified as Wasm
34
import Cardano.Wasm.Internal.ExceptionHandling (rightOrError, toMonadFail)
45
import Cardano.Wasm.Internal.JavaScript.GRPCTypes (JSGRPCClient)
56

@@ -20,6 +21,11 @@ newGrpcConnectionImpl createClientJsFunc host = GrpcObject <$> createClientJsFun
2021
getEraImpl :: (JSGRPCClient -> IO Int) -> GrpcObject -> IO Int
2122
getEraImpl getEraJsFunc (GrpcObject client) = getEraJsFunc client
2223

24+
-- | Get the protocol parameters from the Cardano Node using GRPC-web.
25+
getProtocolParamsImpl
26+
:: (JSGRPCClient -> IO Wasm.ProtocolParamsJSON) -> GrpcObject -> IO Wasm.ProtocolParamsJSON
27+
getProtocolParamsImpl getProtocolParamsJsFunc (GrpcObject client) = getProtocolParamsJsFunc client
28+
2329
-- | Submit a transaction to the Cardano Node using GRPC-web.
2430
submitTxImpl
2531
:: (JSGRPCClient -> String -> IO (Either String String))

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ apiInfo =
266266
, methodReturnType = OtherType "string"
267267
, methodReturnDoc = "A promise that resolves to the transaction ID."
268268
}
269+
, MethodInfo
270+
{ methodName = "getProtocolParams"
271+
, methodDoc =
272+
"Get the protocol parameters in the cardano-ledger format from the Cardano Node using a GRPC-web client."
273+
, methodParams = []
274+
, methodReturnType = OtherType "any"
275+
, methodReturnDoc = "A promise that resolves to the current protocol parameters."
276+
}
269277
]
270278
}
271279
in ApiInfo

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import Cardano.Wasm.Internal.Api.GRPC qualified as Wasm
2121
import Cardano.Wasm.Internal.Api.Info (apiInfo)
2222
import Cardano.Wasm.Internal.Api.Tx qualified as Wasm
2323
import Cardano.Wasm.Internal.ExceptionHandling (rightOrError)
24-
import Cardano.Wasm.Internal.JavaScript.GRPC (js_getEra, js_newWebGrpcClient, js_submitTx)
24+
import Cardano.Wasm.Internal.JavaScript.GRPC (js_getEra, js_newWebGrpcClient, js_submitTx, js_getProtocolParams)
2525
import Cardano.Wasm.Internal.JavaScript.GRPCTypes (JSGRPCClient)
2626

2727
import Control.Exception (evaluate)
28-
import Control.Monad (join)
28+
import Control.Monad
2929
import Data.Aeson qualified as Aeson
3030
import Data.ByteString.UTF8 (fromString, toString)
3131
import Data.Text (Text)
@@ -133,6 +133,9 @@ instance ToJSVal String JSString where
133133
toJSVal :: String -> IO JSString
134134
toJSVal = return . toJSString
135135

136+
instance ToJSVal Wasm.ProtocolParamsJSON JSProtocolParams where
137+
toJSVal (Wasm.ProtocolParamsJSON json) = js_parse $ toJSString json
138+
136139
instance ToJSVal Wasm.GrpcObject JSGrpc where
137140
toJSVal :: Wasm.GrpcObject -> IO JSGrpc
138141
toJSVal (Wasm.GrpcObject client) = return client
@@ -171,7 +174,7 @@ instance FromJSVal JSTxIx Api.TxIx where
171174
fromJSVal :: JSTxIx -> IO Api.TxIx
172175
fromJSVal = return . Api.TxIx . fromIntegral
173176

174-
instance FromJSVal JSVal Wasm.ProtocolParamsJSON where
177+
instance FromJSVal JSProtocolParams Wasm.ProtocolParamsJSON where
175178
fromJSVal = fmap Wasm.ProtocolParamsJSON . jsValToJSONString
176179

177180
instance FromJSVal JSGrpc Wasm.GrpcObject where
@@ -357,6 +360,9 @@ foreign export javascript "newGrpcConnection"
357360
foreign export javascript "getEra"
358361
getEra :: JSGrpc -> IO Int
359362

363+
foreign export javascript "getProtocolParams"
364+
getProtocolParams :: JSGrpc -> IO JSVal
365+
360366
foreign export javascript "submitTx"
361367
submitTx :: JSGrpc -> JSString -> IO JSString
362368

@@ -368,6 +374,10 @@ newGrpcConnection webGrpcUrl = toJSVal =<< join (Wasm.newGrpcConnectionImpl js_n
368374
getEra :: HasCallStack => JSGrpc -> IO Int
369375
getEra grpcObject = Wasm.getEraImpl js_getEra =<< fromJSVal grpcObject
370376

377+
-- | Get the protocol parameters from the Cardano Node using GRPC-web.
378+
getProtocolParams :: HasCallStack => JSGrpc -> IO JSProtocolParams
379+
getProtocolParams = toJSVal <=< Wasm.getProtocolParamsImpl js_getProtocolParams <=< fromJSVal
380+
371381
-- | Submit a transaction to the Cardano Node using GRPC-web.
372382
submitTx :: HasCallStack => JSGrpc -> JSString -> IO JSString
373383
submitTx grpcObject tx = toJSVal =<< join (Wasm.submitTxImpl js_submitTx <$> fromJSVal grpcObject <*> fromJSVal tx)
@@ -380,4 +390,4 @@ foreign export javascript "getApiInfo"
380390
getApiInfo :: IO JSApiInfo
381391
getApiInfo = toJSVal apiInfo
382392

383-
#endif
393+
#endif

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
module Cardano.Wasm.Internal.JavaScript.GRPC where
55
#else
66

7-
module Cardano.Wasm.Internal.JavaScript.GRPC (js_newWebGrpcClient, js_getEra, js_submitTx) where
7+
module Cardano.Wasm.Internal.JavaScript.GRPC (js_newWebGrpcClient, js_getEra, js_submitTx, js_getProtocolParams) where
88

99
import GHC.Wasm.Prim
10+
import Cardano.Wasm.Internal.Api.Tx (ProtocolParamsJSON(..))
1011

1112
-- | Create a GRPC-web client for the Cardano API.
1213
foreign import javascript safe "{ node: new cardano_node.node.NodePromiseClient($1, null, null), \
@@ -22,6 +23,13 @@ js_newWebGrpcClient = js_newWebGrpcClientImpl . toJSString
2223
foreign import javascript safe "($1).node.getEra(new proto.Empty(), {})"
2324
js_getEra :: JSVal -> IO Int
2425
26+
foreign import javascript safe "atob((await ($1).node.getProtocolParamsJson(new proto.Empty(), {})).toObject().json)"
27+
js_getProtocolParamsImpl :: JSVal -> IO JSString
28+
29+
js_getProtocolParams :: JSVal -> IO ProtocolParamsJSON
30+
js_getProtocolParams client =
31+
ProtocolParamsJSON . fromJSString <$> js_getProtocolParamsImpl client
32+
2533
-- | Submit a transaction to the Cardano API using a GRPC-web client.
2634
foreign import javascript safe "{ let tx = new cardano_node.submit.AnyChainTx(); \
2735
tx.setRaw(new Uint8Array($2.match(/[0-9a-fA-F]{2}/g).map((byte) => parseInt(byte, 16)))); \

0 commit comments

Comments
 (0)