Skip to content

Commit 891e808

Browse files
authored
Merge pull request #946 from IntersectMBO/sum-type-for-js-types
Use sum type to represent TypeScript types
2 parents d105f17 + 3f2e7ef commit 891e808

2 files changed

Lines changed: 59 additions & 33 deletions

File tree

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

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Cardano.Wasm.Internal.Api.Info
77
, MethodInfo (..)
88
, ParamInfo (..)
99
, MethodReturnTypeInfo (..)
10+
, tsTypeAsString
1011
)
1112
where
1213

@@ -15,14 +16,38 @@ import Data.Text qualified as Text
1516

1617
-- * API Information Data Types
1718

19+
-- | TypeScript types that are not defined by this code.
20+
data TSType
21+
= TSString
22+
| TSNumber
23+
| TSBigInt
24+
| TSAny
25+
| TSUtxoList
26+
| TSUtxosForAddressList
27+
deriving (Show, Eq)
28+
29+
tsTypeAsString :: TSType -> String
30+
tsTypeAsString TSString = "string"
31+
tsTypeAsString TSNumber = "number"
32+
tsTypeAsString TSBigInt = "bigint"
33+
tsTypeAsString TSAny = "any"
34+
tsTypeAsString TSUtxoList =
35+
"{ address: string, txId: string, txIndex: number, lovelace: bigint, assets: any[], datum?: any, script?: any }[]"
36+
tsTypeAsString TSUtxosForAddressList =
37+
"{ txId: string, txIndex: number, lovelace: bigint, assets: any[], datum?: any, script?: any }[]"
38+
39+
instance Aeson.ToJSON TSType where
40+
toJSON :: TSType -> Aeson.Value
41+
toJSON = Aeson.String . Text.pack . tsTypeAsString
42+
1843
-- | Describes the return type of a method.
1944
data MethodReturnTypeInfo
2045
= -- | Returns an instance of the same object type (fluent interface).
2146
Fluent
2247
| -- | Returns a new instance of a specified virtual object type.
2348
NewObject String
2449
| -- | Returns a non-virtual-object type (e.g., JSString, number).
25-
OtherType String
50+
OtherType TSType
2651
deriving (Show, Eq)
2752

2853
instance Aeson.ToJSON MethodReturnTypeInfo where
@@ -35,7 +60,7 @@ instance Aeson.ToJSON MethodReturnTypeInfo where
3560
data ParamInfo = ParamInfo
3661
{ paramName :: String
3762
-- ^ Name of the parameter.
38-
, paramType :: String
63+
, paramType :: TSType
3964
-- ^ Type of the parameter (as a TypeScript type).
4065
, paramDoc :: String
4166
-- ^ Documentation for the parameter.
@@ -138,30 +163,30 @@ apiInfo =
138163
{ methodName = "getAddressBech32"
139164
, methodDoc = "Get the Bech32 representation of the address. (Can be shared for receiving funds.)"
140165
, methodParams = []
141-
, methodReturnType = OtherType "string"
166+
, methodReturnType = OtherType TSString
142167
, methodReturnDoc = "The Bech32 representation of the address."
143168
}
144169
, MethodInfo
145170
{ methodName = "getBech32ForVerificationKey"
146171
, methodDoc =
147172
"Get the Bech32 representation of the verification key of the wallet. (Can be shared for verification.)"
148173
, methodParams = []
149-
, methodReturnType = OtherType "string"
174+
, methodReturnType = OtherType TSString
150175
, methodReturnDoc = "The Bech32 representation of the verification key."
151176
}
152177
, MethodInfo
153178
{ methodName = "getBech32ForSigningKey"
154179
, methodDoc =
155180
"Get the Bech32 representation of the signing key of the wallet. (Must be kept secret.)"
156181
, methodParams = []
157-
, methodReturnType = OtherType "string"
182+
, methodReturnType = OtherType TSString
158183
, methodReturnDoc = "The Bech32 representation of the signing key."
159184
}
160185
, MethodInfo
161186
{ methodName = "getBase16ForVerificationKeyHash"
162187
, methodDoc = "Get the base16 representation of the hash of the verification key of the wallet."
163188
, methodParams = []
164-
, methodReturnType = OtherType "string"
189+
, methodReturnType = OtherType TSString
165190
, methodReturnDoc = "The base16 representation of the verification key hash."
166191
}
167192
]
@@ -176,8 +201,8 @@ apiInfo =
176201
{ methodName = "addTxInput"
177202
, methodDoc = "Adds a simple transaction input to the transaction."
178203
, methodParams =
179-
[ ParamInfo "txId" "string" "The transaction ID of the input UTxO."
180-
, ParamInfo "txIx" "number" "The index of the input within the UTxO."
204+
[ ParamInfo "txId" TSString "The transaction ID of the input UTxO."
205+
, ParamInfo "txIx" TSNumber "The index of the input within the UTxO."
181206
]
182207
, methodReturnType = Fluent
183208
, methodReturnDoc = "The `UnsignedTx` object with the added input."
@@ -186,38 +211,38 @@ apiInfo =
186211
{ methodName = "addSimpleTxOut"
187212
, methodDoc = "Adds a simple transaction output to the transaction."
188213
, methodParams =
189-
[ ParamInfo "destAddr" "string" "The destination address."
190-
, ParamInfo "lovelaceAmount" "bigint" "The amount in lovelaces to output."
214+
[ ParamInfo "destAddr" TSString "The destination address."
215+
, ParamInfo "lovelaceAmount" TSBigInt "The amount in lovelaces to output."
191216
]
192217
, methodReturnType = Fluent
193218
, methodReturnDoc = "The `UnsignedTx` object with the added output."
194219
}
195220
, MethodInfo
196221
{ methodName = "setFee"
197222
, methodDoc = "Sets the fee for the transaction."
198-
, methodParams = [ParamInfo "lovelaceAmount" "bigint" "The fee amount in lovelaces."]
223+
, methodParams = [ParamInfo "lovelaceAmount" TSBigInt "The fee amount in lovelaces."]
199224
, methodReturnType = Fluent
200225
, methodReturnDoc = "The `UnsignedTx` object with the set fee."
201226
}
202227
, MethodInfo
203228
{ methodName = "estimateMinFee"
204229
, methodDoc = "Estimates the minimum fee for the transaction."
205230
, methodParams =
206-
[ ParamInfo "protocolParams" "any" "The protocol parameters."
231+
[ ParamInfo "protocolParams" TSAny "The protocol parameters."
207232
, ParamInfo
208233
"numKeyWitnesses"
209-
"number"
234+
TSNumber
210235
"The number of key witnesses."
211-
, ParamInfo "numByronKeyWitnesses" "number" "The number of Byron key witnesses."
212-
, ParamInfo "totalRefScriptSize" "number" "The total size of reference scripts in bytes."
236+
, ParamInfo "numByronKeyWitnesses" TSNumber "The number of Byron key witnesses."
237+
, ParamInfo "totalRefScriptSize" TSNumber "The total size of reference scripts in bytes."
213238
]
214-
, methodReturnType = OtherType "bigint"
239+
, methodReturnType = OtherType TSBigInt
215240
, methodReturnDoc = "A promise that resolves to the estimated minimum fee in lovelaces."
216241
}
217242
, MethodInfo
218243
{ methodName = "signWithPaymentKey"
219244
, methodDoc = "Signs the transaction with a payment key."
220-
, methodParams = [ParamInfo "signingKey" "string" "The signing key to witness the transaction."]
245+
, methodParams = [ParamInfo "signingKey" TSString "The signing key to witness the transaction."]
221246
, methodReturnType = NewObject signedTxObjectName
222247
, methodReturnDoc = "A promise that resolves to a `SignedTx` object."
223248
}
@@ -232,15 +257,15 @@ apiInfo =
232257
[ MethodInfo
233258
{ methodName = "alsoSignWithPaymentKey"
234259
, methodDoc = "Adds an extra signature to the transaction with a payment key."
235-
, methodParams = [ParamInfo "signingKey" "string" "The signing key to witness the transaction."]
260+
, methodParams = [ParamInfo "signingKey" TSString "The signing key to witness the transaction."]
236261
, methodReturnType = Fluent
237262
, methodReturnDoc = "The `SignedTx` object with the additional signature."
238263
}
239264
, MethodInfo
240265
{ methodName = "txToCbor"
241266
, methodDoc = "Converts the signed transaction to its CBOR representation."
242267
, methodParams = []
243-
, methodReturnType = OtherType "string"
268+
, methodReturnType = OtherType TSString
244269
, methodReturnDoc =
245270
"A promise that resolves to the CBOR representation of the transaction as a hex string."
246271
}
@@ -256,22 +281,22 @@ apiInfo =
256281
{ methodName = "getEra"
257282
, methodDoc = "Get the era from the Cardano Node using a GRPC-web client."
258283
, methodParams = []
259-
, methodReturnType = OtherType "number"
284+
, methodReturnType = OtherType TSNumber
260285
, methodReturnDoc = "A promise that resolves to the current era number."
261286
}
262287
, MethodInfo
263288
{ methodName = "submitTx"
264289
, methodDoc = "Submit a signed and CBOR-encoded transaction to the Cardano node."
265-
, methodParams = [ParamInfo "txCbor" "string" "The CBOR-encoded transaction as a hex string."]
266-
, methodReturnType = OtherType "string"
290+
, methodParams = [ParamInfo "txCbor" TSString "The CBOR-encoded transaction as a hex string."]
291+
, methodReturnType = OtherType TSString
267292
, methodReturnDoc = "A promise that resolves to the transaction ID."
268293
}
269294
, MethodInfo
270295
{ methodName = "getProtocolParams"
271296
, methodDoc =
272297
"Get the protocol parameters in the cardano-ledger format from the Cardano Node using a GRPC-web client."
273298
, methodParams = []
274-
, methodReturnType = OtherType "any"
299+
, methodReturnType = OtherType TSAny
275300
, methodReturnDoc = "A promise that resolves to the current protocol parameters."
276301
}
277302
, MethodInfo
@@ -281,16 +306,16 @@ apiInfo =
281306
, methodParams = []
282307
, methodReturnType =
283308
OtherType
284-
"{ address: string, txId: string, txIndex: number, lovelace: bigint, assets: any[], datum?: any, script?: any }[]"
309+
TSUtxoList
285310
, methodReturnDoc = "A promise that resolves to the current UTXO set."
286311
}
287312
, MethodInfo
288313
{ methodName = "getUtxosForAddress"
289314
, methodDoc = "Get UTXOs for a given address using a GRPC-web client."
290-
, methodParams = [ParamInfo "address" "string" "The address to get UTXOs for."]
315+
, methodParams = [ParamInfo "address" TSString "The address to get UTXOs for."]
291316
, methodReturnType =
292317
OtherType
293-
"{ txId: string, txIndex: number, lovelace: bigint, assets: any[], datum?: any, script?: any }[]"
318+
TSUtxosForAddressList
294319
, methodReturnDoc = "A promise that resolves to the UTXOs for the given address."
295320
}
296321
]
@@ -311,7 +336,7 @@ apiInfo =
311336
, MethodInfo
312337
{ methodName = "newGrpcConnection"
313338
, methodDoc = "Create a new client connection for communicating with a Cardano node through gRPC-web."
314-
, methodParams = [ParamInfo "webGrpcUrl" "string" "The URL of the gRPC-web server."]
339+
, methodParams = [ParamInfo "webGrpcUrl" TSString "The URL of the gRPC-web server."]
315340
, methodReturnType = NewObject grpcConnectionName
316341
, methodReturnDoc = "A promise that resolves to a new `GrpcConnection`."
317342
}
@@ -325,23 +350,23 @@ apiInfo =
325350
, MethodInfo
326351
{ methodName = "restorePaymentWalletFromSigningKeyBech32"
327352
, methodDoc = "Restore a mainnet payment wallet from a Bech32 encoded signing key."
328-
, methodParams = [ParamInfo "signingKeyBech32" "string" "The Bech32 encoded signing key."]
353+
, methodParams = [ParamInfo "signingKeyBech32" TSString "The Bech32 encoded signing key."]
329354
, methodReturnType = NewObject walletObjectName
330355
, methodReturnDoc = "A promise that resolves to a new `Wallet` object."
331356
}
332357
, MethodInfo
333358
{ methodName = "generateTestnetPaymentWallet"
334359
, methodDoc = "Generate a simple payment wallet for testnet, given the testnet's network magic."
335-
, methodParams = [ParamInfo "networkMagic" "number" "The network magic for the testnet."]
360+
, methodParams = [ParamInfo "networkMagic" TSNumber "The network magic for the testnet."]
336361
, methodReturnType = NewObject walletObjectName
337362
, methodReturnDoc = "A promise that resolves to a new `Wallet` object."
338363
}
339364
, MethodInfo
340365
{ methodName = "restoreTestnetPaymentWalletFromSigningKeyBech32"
341366
, methodDoc = "Restore a testnet payment wallet from a Bech32 encoded signing key."
342367
, methodParams =
343-
[ ParamInfo "networkMagic" "number" "The network magic for the testnet."
344-
, ParamInfo "signingKeyBech32" "string" "The Bech32 encoded signing key."
368+
[ ParamInfo "networkMagic" TSNumber "The network magic for the testnet."
369+
, ParamInfo "signingKeyBech32" TSString "The Bech32 encoded signing key."
345370
]
346371
, methodReturnType = NewObject walletObjectName
347372
, methodReturnDoc = "A promise that resolves to a new `Wallet` object."

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

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

3+
import Cardano.Wasm.Internal.Api.Info (tsTypeAsString)
34
import Cardano.Wasm.Internal.Api.Info qualified as Info
45
import Cardano.Wasm.Internal.Api.TypeScriptDefs qualified as TypeScript
56

@@ -63,10 +64,10 @@ paramInfoToFunctionParam :: Info.ParamInfo -> TypeScript.FunctionParam
6364
paramInfoToFunctionParam p =
6465
TypeScript.FunctionParam
6566
{ TypeScript.paramName = Info.paramName p
66-
, TypeScript.paramType = Info.paramType p
67+
, TypeScript.paramType = tsTypeAsString $ Info.paramType p
6768
}
6869

6970
methodReturnTypeToString :: String -> Info.MethodReturnTypeInfo -> String
7071
methodReturnTypeToString selfTypeName Info.Fluent = selfTypeName
7172
methodReturnTypeToString _ (Info.NewObject objTypeName) = "Promise<" <> objTypeName <> ">"
72-
methodReturnTypeToString _ (Info.OtherType typeName) = "Promise<" <> typeName <> ">"
73+
methodReturnTypeToString _ (Info.OtherType typeName) = "Promise<" <> tsTypeAsString typeName <> ">"

0 commit comments

Comments
 (0)