|
| 1 | +{-# LANGUAGE InstanceSigs #-} |
| 2 | + |
| 3 | +module Cardano.Wasm.Internal.Api.Info (apiInfo) where |
| 4 | + |
| 5 | +import Data.Aeson qualified as Aeson |
| 6 | +import Data.Text qualified as Text |
| 7 | + |
| 8 | +-- * API Information Data Types |
| 9 | + |
| 10 | +-- | Describes the return type of a method. |
| 11 | +data MethodReturnTypeInfo |
| 12 | + = -- | Returns an instance of the same object type (fluent interface). |
| 13 | + Fluent |
| 14 | + | -- | Returns a new instance of a specified virtual object type. |
| 15 | + NewObject String |
| 16 | + | -- | Returns a non-virtual-object type (e.g., JSString, number). |
| 17 | + OtherType String |
| 18 | + deriving (Show, Eq) |
| 19 | + |
| 20 | +instance Aeson.ToJSON MethodReturnTypeInfo where |
| 21 | + toJSON Fluent = Aeson.object ["type" Aeson..= Text.pack "fluent"] |
| 22 | + toJSON (NewObject objTypeName) = Aeson.object ["type" Aeson..= Text.pack "newObject", "objectType" Aeson..= objTypeName] |
| 23 | + toJSON (OtherType typeName) = Aeson.object ["type" Aeson..= Text.pack "other", "typeName" Aeson..= typeName] |
| 24 | + |
| 25 | +-- | Information about a single method of a virtual object. |
| 26 | +data MethodInfo = MethodInfo |
| 27 | + { methodName :: String |
| 28 | + , methodParams :: [String] |
| 29 | + -- ^ Names of parameters, excluding 'this'. |
| 30 | + , methodReturnType :: MethodReturnTypeInfo |
| 31 | + } |
| 32 | + deriving (Show, Eq) |
| 33 | + |
| 34 | +instance Aeson.ToJSON MethodInfo where |
| 35 | + toJSON :: MethodInfo -> Aeson.Value |
| 36 | + toJSON (MethodInfo name params retType) = |
| 37 | + Aeson.object |
| 38 | + [ "name" Aeson..= name |
| 39 | + , "params" Aeson..= params |
| 40 | + , "return" Aeson..= retType |
| 41 | + ] |
| 42 | + |
| 43 | +-- | Information about a virtual object and its methods. |
| 44 | +data VirtualObjectInfo = VirtualObjectInfo |
| 45 | + { virtualObjectName :: String |
| 46 | + , virtualObjectMethods :: [MethodInfo] |
| 47 | + } |
| 48 | + deriving (Show, Eq) |
| 49 | + |
| 50 | +instance Aeson.ToJSON VirtualObjectInfo where |
| 51 | + toJSON :: VirtualObjectInfo -> Aeson.Value |
| 52 | + toJSON (VirtualObjectInfo name methods) = |
| 53 | + Aeson.object |
| 54 | + [ "objectName" Aeson..= name |
| 55 | + , "methods" Aeson..= methods |
| 56 | + ] |
| 57 | + |
| 58 | +-- | Aggregate type for all API information. |
| 59 | +data ApiInfo = ApiInfo |
| 60 | + { staticMethods :: [MethodInfo] |
| 61 | + , virtualObjects :: [VirtualObjectInfo] |
| 62 | + } |
| 63 | + deriving (Show, Eq) |
| 64 | + |
| 65 | +instance Aeson.ToJSON ApiInfo where |
| 66 | + toJSON :: ApiInfo -> Aeson.Value |
| 67 | + toJSON (ApiInfo staticObjs virtualObjs) = |
| 68 | + Aeson.object |
| 69 | + [ "staticMethods" Aeson..= staticObjs |
| 70 | + , "virtualObjects" Aeson..= virtualObjs |
| 71 | + ] |
| 72 | + |
| 73 | +-- | Provides metadata about the "virtual objects" and their methods. |
| 74 | +-- This is intended to help generate JavaScript wrappers. |
| 75 | +apiInfo :: ApiInfo |
| 76 | +apiInfo = |
| 77 | + let unsignedTxObjectName = "UnsignedTx" |
| 78 | + signedTxObjectName = "SignedTx" |
| 79 | + |
| 80 | + staticApiMethods = |
| 81 | + [ MethodInfo |
| 82 | + { methodName = "newConwayTx" |
| 83 | + , methodParams = [] |
| 84 | + , methodReturnType = NewObject unsignedTxObjectName |
| 85 | + } |
| 86 | + ] |
| 87 | + |
| 88 | + unsignedTxObj = |
| 89 | + VirtualObjectInfo |
| 90 | + { virtualObjectName = unsignedTxObjectName |
| 91 | + , virtualObjectMethods = |
| 92 | + [ MethodInfo |
| 93 | + { methodName = "addTxInput" |
| 94 | + , methodParams = ["txId", "txIx"] |
| 95 | + , methodReturnType = Fluent |
| 96 | + } |
| 97 | + , MethodInfo |
| 98 | + { methodName = "addSimpleTxOut" |
| 99 | + , methodParams = ["destAddr", "lovelaceAmount"] |
| 100 | + , methodReturnType = Fluent |
| 101 | + } |
| 102 | + , MethodInfo |
| 103 | + { methodName = "setFee" |
| 104 | + , methodParams = ["lovelaceAmount"] |
| 105 | + , methodReturnType = Fluent |
| 106 | + } |
| 107 | + , MethodInfo |
| 108 | + { methodName = "signWithPaymentKey" |
| 109 | + , methodParams = ["signingKey"] |
| 110 | + , methodReturnType = NewObject signedTxObjectName |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | + |
| 115 | + signedTxObj = |
| 116 | + VirtualObjectInfo |
| 117 | + { virtualObjectName = signedTxObjectName |
| 118 | + , virtualObjectMethods = |
| 119 | + [ MethodInfo |
| 120 | + { methodName = "alsoSignWithPaymentKey" |
| 121 | + , methodParams = ["signingKey"] |
| 122 | + , methodReturnType = Fluent |
| 123 | + } |
| 124 | + , MethodInfo |
| 125 | + { methodName = "txToCbor" |
| 126 | + , methodParams = [] |
| 127 | + , methodReturnType = OtherType "string" |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | + in ApiInfo |
| 132 | + { staticMethods = staticApiMethods |
| 133 | + , virtualObjects = [unsignedTxObj, signedTxObj] |
| 134 | + } |
0 commit comments