Skip to content

Commit

Permalink
Feat 30: V1 Endpoints support - Singleton type for api versions for a…
Browse files Browse the repository at this point in the history
…dded type safety
  • Loading branch information
sourabhxyz committed Jul 6, 2023
1 parent 8a668d6 commit c598820
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 103 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
```haskell
import Maestro.Client.Env

myEnvPreprod <- mkMaestroEnv "Your-API-Key" Preprod
myEnvMainnet <- mkMaestroEnv "Your-API-Key" Mainnet
myEnvPreprod <- mkMaestroEnv @'V0 "Your-API-Key" Preprod
myEnvMainnet <- mkMaestroEnv @'V0 "Your-API-Key" Mainnet
```
4. Example: chain tip
```haskell
Expand Down
8 changes: 8 additions & 0 deletions maestro-exe/Maestro/Run/Address.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Maestro.Run.Address where

import Maestro.Client.Env
import qualified Maestro.Client.V0 as V0
import qualified Maestro.Client.V1 as V1

runAddressAPI :: MaestroEnv 'V0 -> IO ()
runAddressAPI mEnv = undefined
9 changes: 0 additions & 9 deletions maestro-exe/Maestro/Run/AddressV1.hs

This file was deleted.

2 changes: 1 addition & 1 deletion maestro-exe/Maestro/Run/Datum.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Maestro.Run.Datum where
import Maestro.Client.V0
import Text.Printf (printf)

runDatumAPI :: MaestroEnv -> IO ()
runDatumAPI :: MaestroEnv 'V0 -> IO ()
runDatumAPI mEnv = do
let datumHash = "938dc15a5faa3da8e7f1e3ed8ca50b49248f8fffdfc04ff3cf7dffa0d06343eb" -- Quiet an involved datum.
printf "Fetching datum from hash %s...\n" datumHash
Expand Down
2 changes: 1 addition & 1 deletion maestro-exe/Maestro/Run/Epochs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Maestro.Run.Epochs where

import Maestro.Client.V0

runEpochsAPI :: MaestroEnv -> IO ()
runEpochsAPI :: MaestroEnv 'V0 -> IO ()
runEpochsAPI mEnv = do
putStrLn "Fetching Current Epoch's Info ..."
currentEpochInfo <- getCurrentEpoch mEnv
Expand Down
2 changes: 1 addition & 1 deletion maestro-exe/Maestro/Run/General.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Maestro.Run.General where
import Maestro.Client.V0
import Text.Printf (printf)

runGeneralAPI :: MaestroEnv -> IO ()
runGeneralAPI :: MaestroEnv 'V0 -> IO ()
runGeneralAPI mEnv = do
chainTip <- getChainTip mEnv
printf "Querying chain-tip, received: ⮯\n%s\n" (show chainTip)
18 changes: 9 additions & 9 deletions maestro-exe/Maestro/Run/Pools.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Maestro.Types.V0
poolId :: Bech32StringOf PoolId
poolId = "pool1rkfs9glmfva3jd0q9vnlqvuhnrflpzj4l07u6sayfx5k7d788us"

runPoolsAPI :: MaestroEnv -> IO ()
runPoolsAPI :: MaestroEnv 'V0 -> IO ()
runPoolsAPI mEnv = do
putStrLn "Fetching List Pools ..."
lstPools <- runListPools mEnv
Expand Down Expand Up @@ -40,26 +40,26 @@ runPoolsAPI mEnv = do
updates <- runPoolInfo mEnv
putStrLn $ "fetched pool Updates: \n " ++ show updates

runPoolUpdates :: MaestroEnv -> IO [PoolUpdate]
runPoolUpdates :: MaestroEnv 'V0 -> IO [PoolUpdate]
runPoolUpdates mEnv = poolUpdates mEnv poolId

runListPools :: MaestroEnv -> IO [PoolListInfo]
runListPools :: MaestroEnv 'V0 -> IO [PoolListInfo]
runListPools mEnv = listPools mEnv (Page 1 1)

runPoolBlocks :: MaestroEnv -> IO [PoolBlock]
runPoolBlocks :: MaestroEnv 'V0 -> IO [PoolBlock]
runPoolBlocks mEnv = poolBlocks mEnv poolId (Page 1 1) Nothing (Just Ascending)

runPoolDelegators :: MaestroEnv -> IO [DelegatorInfo]
runPoolDelegators :: MaestroEnv 'V0 -> IO [DelegatorInfo]
runPoolDelegators mEnv = poolDelegators mEnv poolId (Page 1 1)

runPoolHistory :: MaestroEnv -> IO [PoolHistory]
runPoolHistory :: MaestroEnv 'V0 -> IO [PoolHistory]
runPoolHistory mEnv = poolHistory mEnv poolId (Page 1 1) Nothing (Just Ascending)

runPoolInfo :: MaestroEnv -> IO PoolInfo
runPoolInfo :: MaestroEnv 'V0 -> IO PoolInfo
runPoolInfo mEnv = poolInfo mEnv poolId

runPoolMetadata :: MaestroEnv -> IO PoolMetadata
runPoolMetadata :: MaestroEnv 'V0 -> IO PoolMetadata
runPoolMetadata mEnv = poolMetadata mEnv poolId

runPoolRelay :: MaestroEnv -> IO [PoolRelay]
runPoolRelay :: MaestroEnv 'V0 -> IO [PoolRelay]
runPoolRelay mEnv = poolRelays mEnv poolId
2 changes: 1 addition & 1 deletion maestro-exe/Maestro/Run/Scripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Maestro.Run.Scripts where
import Maestro.Client.V0
import Text.Printf (printf)

runScriptsAPI :: MaestroEnv -> IO ()
runScriptsAPI :: MaestroEnv 'V0 -> IO ()
runScriptsAPI mEnv = do
let scriptHash = "3a888d65f16790950a72daee1f63aa05add6d268434107cfa5b67712"
printf "Fetching script from hash %s...\n" scriptHash
Expand Down
8 changes: 4 additions & 4 deletions maestro-exe/Maestro/Run/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Maestro.Types.V0
txHash :: HashStringOf Tx
txHash = "7fdf7a20ba50d841344ab0cb368da6a047ce1e2a29b707586f61f0b8fea6bcf2"

runTxApi :: MaestroEnv -> IO ()
runTxApi :: MaestroEnv 'V0 -> IO ()
runTxApi mEnv = do
putStrLn "Fetching Tx Address ..."
txAddr <- runTxAddress mEnv
Expand All @@ -20,11 +20,11 @@ runTxApi mEnv = do
utxo <- runTxUtxo mEnv
putStrLn $ "fetched Tx Utxos: \n " ++ show utxo

runTxAddress :: MaestroEnv -> IO UtxoAddress
runTxAddress :: MaestroEnv 'V0 -> IO UtxoAddress
runTxAddress mEnv = txAddress mEnv txHash $ TxIndex 0

runTxCbor :: MaestroEnv -> IO TxCbor
runTxCbor :: MaestroEnv 'V0 -> IO TxCbor
runTxCbor mEnv = txCbor mEnv txHash

runTxUtxo :: MaestroEnv -> IO Utxo
runTxUtxo :: MaestroEnv 'V0 -> IO Utxo
runTxUtxo mEnv = txUtxo mEnv txHash (TxIndex 0) (Just True) (Just True)
9 changes: 5 additions & 4 deletions maestro-exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Main (main) where

import qualified Data.Text as T
import Maestro.Client.Env
import Maestro.Run.Address
import Maestro.Run.Datum
import Maestro.Run.Epochs
import Maestro.Run.General
import Maestro.Run.Pools
import Maestro.Run.Scripts
import Maestro.Run.Tx
import Maestro.Run.AddressV1
import System.Environment (getEnv)


Expand All @@ -17,15 +17,16 @@ main :: IO ()
main = do
apiKey <- maestroKey
apiKeyMain <- maestroMainKey
env <- mkMaestroEnv (T.pack apiKey) Preprod V0
env <- mkMaestroEnv @'V0 (T.pack apiKey) Preprod
runPoolsAPI env
runTxApi env
runEpochsAPI env
runDatumAPI env
runScriptsAPI env
runGeneralAPI env
env' <- mkMaestroEnv (T.pack apiKeyMain) Mainnet V1
runV1AddressAPI env'
-- env' <- mkMaestroEnv @'V1 (T.pack apiKeyMain) Mainnet
env' <- mkMaestroEnv @'V0 (T.pack apiKeyMain) Mainnet
runAddressAPI env'

where
maestroKey = getEnv "MAESTRO_API_KEY"
Expand Down
3 changes: 2 additions & 1 deletion maestro-sdk.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ source-repository head
common common
ghc-options: -Wall
default-extensions:
GADTs
DataKinds
DeriveGeneric
DerivingStrategies
Expand Down Expand Up @@ -154,7 +155,7 @@ executable maestro-exe
Maestro.Run.Scripts
Maestro.Run.Tx
Maestro.Run.Epochs
Maestro.Run.AddressV1
Maestro.Run.Address
-- other-extensions:
hs-source-dirs: maestro-exe
main-is: Main.hs
Expand Down
39 changes: 27 additions & 12 deletions src/Maestro/Client/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,40 @@ import qualified Servant.Client as Servant

type MaestroToken = Text

data MaestroEnv = MaestroEnv
data MaestroApiVersion = V0 | V1

instance Show MaestroApiVersion where
show V0 = "v0"
show V1 = "v1"

data SingMaestroApiVersion (v :: MaestroApiVersion) where
SingV0 :: SingMaestroApiVersion 'V0
SingV1 :: SingMaestroApiVersion 'V1

fromSingMaestroApiVersion :: SingMaestroApiVersion v -> MaestroApiVersion
fromSingMaestroApiVersion SingV0 = V0
fromSingMaestroApiVersion SingV1 = V1

class SingMaestroApiVersionI (v :: MaestroApiVersion)
where singMaestroApiVersion :: SingMaestroApiVersion v

instance SingMaestroApiVersionI 'V0 where singMaestroApiVersion = SingV0
instance SingMaestroApiVersionI 'V1 where singMaestroApiVersion = SingV1

data MaestroEnv (v :: MaestroApiVersion) = MaestroEnv
{ _maeClientEnv :: !Servant.ClientEnv
, _maeToken :: !MaestroToken
}

data MaestroNetwork = Mainnet | Preprod

data MaestroApiVersion = V0 | V1

-- TODO : Move version check inside.
maestroBaseUrl :: MaestroNetwork -> MaestroApiVersion -> String
maestroBaseUrl Preprod V0 = "https://preprod.gomaestro-api.org/v0"
maestroBaseUrl Preprod V1 = "https://preprod.gomaestro-api.org/v1"
maestroBaseUrl Mainnet V0 = "https://mainnet.gomaestro-api.org/v0"
maestroBaseUrl Mainnet V1 = "https://mainnet.gomaestro-api.org/v1"

mkMaestroEnv :: MaestroToken -> MaestroNetwork -> MaestroApiVersion -> IO MaestroEnv
mkMaestroEnv token nid apiVersion = do
clientEnv <- servantClientEnv $ maestroBaseUrl nid apiVersion
maestroBaseUrl Preprod v = "https://preprod.gomaestro-api.org/" <> show v
maestroBaseUrl Mainnet v = "https://mainnet.gomaestro-api.org/" <> show v

mkMaestroEnv :: forall (apiVersion :: MaestroApiVersion). SingMaestroApiVersionI apiVersion => MaestroToken -> MaestroNetwork -> IO (MaestroEnv apiVersion)
mkMaestroEnv token nid = do
clientEnv <- servantClientEnv $ maestroBaseUrl nid (fromSingMaestroApiVersion $ singMaestroApiVersion @apiVersion)
pure $ MaestroEnv { _maeClientEnv = clientEnv, _maeToken = token }

servantClientEnv :: String -> IO Servant.ClientEnv
Expand Down
14 changes: 7 additions & 7 deletions src/Maestro/Client/V0/Accounts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import Maestro.Types.V0
import Servant.API.Generic
import Servant.Client

accountsClient :: MaestroEnv -> AccountsAPI (AsClientT IO)
accountsClient :: MaestroEnv 'V0 -> AccountsAPI (AsClientT IO)
accountsClient = fromServant . _accounts . apiV0Client

getAccount :: MaestroEnv -> Text -> IO AccountInfo
getAccount :: MaestroEnv 'V0 -> Text -> IO AccountInfo
getAccount = _account . accountsClient

listAccountAddresses :: MaestroEnv -> Text -> Page -> IO [Text]
listAccountAddresses :: MaestroEnv 'V0 -> Text -> Page -> IO [Text]
listAccountAddresses = _accountAddresses . accountsClient

listAccountAssets :: MaestroEnv -> Text -> Page -> IO [Asset]
listAccountAssets :: MaestroEnv 'V0 -> Text -> Page -> IO [Asset]
listAccountAssets = _accountAssets . accountsClient

listAccountHistory :: MaestroEnv -> Text -> Maybe EpochNo -> Page -> IO [AccountHistory]
listAccountHistory :: MaestroEnv 'V0 -> Text -> Maybe EpochNo -> Page -> IO [AccountHistory]
listAccountHistory = _accountsHistory . accountsClient

listAccountRewards :: MaestroEnv -> Text -> Page -> IO [AccountReward]
listAccountRewards :: MaestroEnv 'V0 -> Text -> Page -> IO [AccountReward]
listAccountRewards = _accountsReward . accountsClient

listAccountUpdates :: MaestroEnv -> Text -> Page -> IO [AccountUpdate]
listAccountUpdates :: MaestroEnv 'V0 -> Text -> Page -> IO [AccountUpdate]
listAccountUpdates = _accountsUpdates . accountsClient
10 changes: 5 additions & 5 deletions src/Maestro/Client/V0/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import Maestro.Types.V0
import Servant.API.Generic
import Servant.Client

addressClient :: MaestroEnv -> AddressAPI (AsClientT IO)
addressClient :: MaestroEnv 'V0 -> AddressAPI (AsClientT IO)
addressClient = fromServant . _address . apiV0Client

-- |
-- Returns list of utxos for multiple addresses
utxosAtMultiAddresses ::
-- | The Maestro Environment
MaestroEnv ->
MaestroEnv 'V0 ->
-- | Query param to include the corresponding datums for datum hashes
Maybe Bool ->
-- | Query Param to include the CBOR encodings of the transaction outputs in the response
Expand All @@ -31,7 +31,7 @@ utxosAtMultiAddresses = _addressesUtxos . addressClient
-- |
-- Returns list of utxo for specific address
utxosAtAddress ::
MaestroEnv ->
MaestroEnv 'V0 ->
-- | The Address in bech32 format
Text ->
-- | Query param to include the corresponding datums for datum hashes
Expand All @@ -46,7 +46,7 @@ utxosAtAddress = _addressUtxo . addressClient
-- |
-- Returns list of utxo ref for address
getRefsAtAddress ::
MaestroEnv ->
MaestroEnv 'V0 ->
-- | The Address in bech32 format
Text ->
-- | The pagination attributes
Expand All @@ -57,7 +57,7 @@ getRefsAtAddress = _addressUtxoRefs . addressClient
-- |
-- Get the transaction count for an address
getTxCountForAddress ::
MaestroEnv ->
MaestroEnv 'V0 ->
-- | The Address in bech32 format
Text ->
IO [AddressTxCount]
Expand Down
Loading

0 comments on commit c598820

Please sign in to comment.