Skip to content

Commit

Permalink
WIP start work on OpenAPI schema
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed Jan 2, 2025
1 parent d7aa324 commit 4d154fc
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/exe/write-openapi-schema/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main where

import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString.Lazy qualified as BSL
import Data.Proxy (Proxy (..))
import Servant.OpenApi (toOpenApi)
import System.Environment qualified
import Wst.Server.Types (APIInEra)

main :: IO ()
main = do
fp:_ <- System.Environment.getArgs
BSL.writeFile fp $ encodePretty $ toOpenApi $ Proxy @APIInEra
27 changes: 26 additions & 1 deletion src/lib/SmartTokens/Types/ProtocolParams.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-deferred-type-errors #-}
{-# LANGUAGE InstanceSigs #-}

module SmartTokens.Types.ProtocolParams (
ProgrammableLogicGlobalParams (..),
PProgrammableLogicGlobalParams (..),
) where

import Cardano.Api.Shelley qualified as C
import Control.Lens ((&), (.~), (?~))
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject, (.:), (.=))
import Data.Aeson qualified as Aeson
import Data.Bifunctor (Bifunctor (..))
import Data.OpenApi.Internal (OpenApiType (OpenApiArray, OpenApiObject, OpenApiString),
Referenced (Inline))
import Data.OpenApi.Lens qualified as L
import Data.OpenApi.ParamSchema (ToParamSchema (..))
import Generics.SOP qualified as SOP
import Plutarch.Core.PlutusDataList (DerivePConstantViaDataList (..),
PlutusTypeDataList, ProductIsData (..))
Expand Down Expand Up @@ -80,3 +85,23 @@ instance FromJSON ProgrammableLogicGlobalParams where
ProgrammableLogicGlobalParams
<$> (obj .: "directory_node_currency_symbol" >>= either fail pure . plutusDataFromJSON)
<*> (obj .: "programmable_logic_credential" >>= either fail pure . plutusDataFromJSON)

instance ToParamSchema ProgrammableLogicGlobalParams where
toParamSchema _proxy =
mempty
& L.type_ ?~ OpenApiObject
& L.description ?~ "Global parameters of the programmable token directory"
& L.properties .~
[ ( "directory_node_currency_symbol"
, Inline $ mempty
& L.type_ ?~ OpenApiString
& L.description ?~ "base16-encoded script payment credential of the programmable logic script"
& L.example ?~ "0xc0000000000000000000000000000000000000000000000000000000"
)
, ( "programmable_logic_credential"
, Inline $ mempty
& L.type_ ?~ OpenApiArray
& L.description ?~ "plutus-data-encoded payment credential of the programmable logic"
& L.example ?~ toJSON @[Aeson.Value] [toJSON @Int 0, toJSON @[String] ["0x0a0eb28fbaec9e61d20e9fe4c6ac5e5ee4520bb274b1e3292721d26f"]]
)
]
4 changes: 4 additions & 0 deletions src/lib/Wst/Offchain/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ data UTxODat era a =
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)

-- TODO:
-- better json deriving for utxo dat
-- auto derive param

{-| Find all UTxOs that make up the registry
-}
registryNodes :: forall era env m. (MonadReader env m, HasDirectoryEnv env, MonadUtxoQuery m, C.IsBabbageBasedEra era) => m [UTxODat era DirectorySetNode]
Expand Down
11 changes: 11 additions & 0 deletions src/lib/Wst/Server/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ module Wst.Server.Types (

import Cardano.Api (AssetName, Quantity)
import Cardano.Api qualified as C
import Control.Lens ((&), (?~))
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.OpenApi.Internal (OpenApiType (OpenApiString))
import Data.OpenApi.Lens qualified as L
import Data.OpenApi.ParamSchema (ToParamSchema (..))
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic)
import Servant (FromHttpApiData (..), ToHttpApiData (toUrlPiece))
Expand All @@ -47,6 +51,13 @@ instance C.HasTextEnvelope a => FromJSON (TextEnvelopeJSON a) where

newtype SerialiseAddress a = SerialiseAddress{unSerialiseAddress :: a }

instance ToParamSchema (SerialiseAddress a) where
toParamSchema _proxy =
mempty
& L.type_ ?~ OpenApiString
& L.description ?~ "bech32-serialised cardano address"
& L.example ?~ "addr1q9d42egme33z960rr8vlnt69lpmythdpm7ydk2e6k5nj5ghay9rg60vw49kejfah76sqeh4yshlsntgg007y0wgjlfwju6eksr"

instance C.SerialiseAddress a => FromHttpApiData (SerialiseAddress a) where
parseUrlPiece =
maybe (Left "Failed to deserialise address") (Right . SerialiseAddress) . C.deserialiseAddress (C.proxyToAsType Proxy)
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/Wst/Test/UnitTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,3 @@ nodeParamsFor = \case
mockchainSucceedsWithTarget :: ScriptTarget -> ReaderT ScriptTarget (MockchainT C.ConwayEra IO) a -> Assertion
mockchainSucceedsWithTarget target =
mockchainSucceedsWith (nodeParamsFor target) . flip runReaderT target

13 changes: 13 additions & 0 deletions src/wst-poc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ library
, generics-sop
, lens
, mtl
, openapi3
, optparse-applicative
, plutarch
, plutarch-ledger-api
Expand All @@ -114,6 +115,7 @@ library
, servant
, servant-client
, servant-client-core
, servant-openapi3
, servant-server
, text
, warp
Expand Down Expand Up @@ -196,3 +198,14 @@ executable wst-poc-mock-server
build-depends:
, base
, wst-poc:test-lib

executable write-openapi-schema
import: lang
main-is: Main.hs
hs-source-dirs: exe/write-openapi-schema
build-depends:
, aeson-pretty
, base
, bytestring
, servant-openapi3
, wst-poc

0 comments on commit 4d154fc

Please sign in to comment.