Skip to content

Commit

Permalink
Merge pull request #44 from Micrograx/43-support-market-endpoints
Browse files Browse the repository at this point in the history
Feat #43: Adds support for ohcl and pair endpoints
  • Loading branch information
Vardominator authored Dec 12, 2023
2 parents 68a59cd + 35dac53 commit 7f4faa2
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
3 changes: 3 additions & 0 deletions maestro-sdk.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ library
Maestro.API.V1.Addresses
Maestro.API.V1.Blocks
Maestro.API.V1.Datum
Maestro.API.V1.DefiMarkets
Maestro.API.V1.General
Maestro.API.V1.Pools
Maestro.API.V1.Transactions
Expand All @@ -64,6 +65,7 @@ library
Maestro.Client.V1.Addresses
Maestro.Client.V1.Blocks
Maestro.Client.V1.Datum
Maestro.Client.V1.DefiMarkets
Maestro.Client.V1.General
Maestro.Client.V1.Pools
Maestro.Client.V1.Transactions
Expand All @@ -76,6 +78,7 @@ library
Maestro.Types.V1.Addresses
Maestro.Types.V1.Blocks
Maestro.Types.V1.Datum
Maestro.Types.V1.DefiMarkets
Maestro.Types.V1.Common
Maestro.Types.V1.Common.Pagination
Maestro.Types.V1.Common.Timestamped
Expand Down
2 changes: 2 additions & 0 deletions src/Maestro/API/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Maestro.API.V1.Accounts
import Maestro.API.V1.Addresses
import Maestro.API.V1.Blocks
import Maestro.API.V1.Datum
import Maestro.API.V1.DefiMarkets
import Maestro.API.V1.General
import Maestro.API.V1.Pools
import Maestro.API.V1.Transactions
Expand All @@ -18,6 +19,7 @@ data MaestroApiV1 route = MaestroApiV1
, addresses :: route :- "addresses" :> ToServantApi AddressesAPI
, blocks :: route :- "blocks" :> ToServantApi BlocksAPI
, datums :: route :- "datums" :> ToServantApi DatumAPI
, defiMarkets :: route :- "markets" :> "dexs" :> ToServantApi DefiMarketsAPI
, pools :: route :- "pools" :> ToServantApi PoolsAPI
, txManager :: route :- "txmanager" :> ToServantApi TxManagerAPI
, transactions :: route :- "transactions" :> ToServantApi TransactionsAPI
Expand Down
23 changes: 23 additions & 0 deletions src/Maestro/API/V1/DefiMarkets.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Maestro.API.V1.DefiMarkets where

import Maestro.Types.V1
import Servant.API
import Servant.API.Generic

data DefiMarketsAPI route = DefiMarketsAPI
{
dexOHLC
:: route
:- "ohlc"
:> Capture "dex" Dex
:> Capture "pair" (TaggedText PairOfDexTokens)
:> QueryParam "resolution" Resolution
:> QueryParam "sort" Order
:> Get '[JSON] [OHLCCandleInfo]

, dexPairs
:: route
:- Capture "dex" Dex
:> Get '[JSON] DexPairResponse

} deriving (Generic)
2 changes: 2 additions & 0 deletions src/Maestro/Client/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Maestro.Client.V1
, module Maestro.Client.V1.Addresses
, module Maestro.Client.V1.Blocks
, module Maestro.Client.V1.Datum
, module Maestro.Client.V1.DefiMarkets
, module Maestro.Client.V1.General
, module Maestro.Client.V1.Pools
, module Maestro.Client.V1.Transactions
Expand All @@ -18,6 +19,7 @@ import Maestro.Client.V1.Addresses
import Maestro.Client.V1.Blocks
import Maestro.Client.V1.Core
import Maestro.Client.V1.Datum
import Maestro.Client.V1.DefiMarkets
import Maestro.Client.V1.General
import Maestro.Client.V1.Pools
import Maestro.Client.V1.Transactions
Expand Down
35 changes: 35 additions & 0 deletions src/Maestro/Client/V1/DefiMarkets.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- | Module to query for /"DeFi Markets"/ category of endpoints defined at [docs.gomaestro.org](https://docs.gomaestro.org/category/defi-market-api).

module Maestro.Client.V1.DefiMarkets (
pricesFromDex,
pairsFromDex
) where

import Maestro.API.V1
import Maestro.API.V1.DefiMarkets
import Maestro.Client.Env
import Maestro.Client.V1.Core
import Maestro.Types.Common (Order)
import Maestro.Types.V1 (Resolution, Dex, TaggedText, PairOfDexTokens,DexPairResponse, OHLCCandleInfo)
import Servant.API.Generic
import Servant.Client

defiMarketsClient :: MaestroEnv 'V1 -> DefiMarketsAPI (AsClientT IO)
defiMarketsClient = fromServant . defiMarkets . apiV1Client

-- | Returns a list of OHLC formatted candles from the desired dex
pricesFromDex ::
MaestroEnv 'V1 ->
Dex ->
TaggedText PairOfDexTokens ->
Maybe Resolution ->
Maybe Order ->
IO [OHLCCandleInfo]
pricesFromDex = dexOHLC . defiMarketsClient

-- | Returns a the list of pairs supported by the dex
pairsFromDex ::
MaestroEnv 'V1 ->
Dex ->
IO DexPairResponse
pairsFromDex = dexPairs . defiMarketsClient
2 changes: 2 additions & 0 deletions src/Maestro/Types/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Maestro.Types.V1
, module Maestro.Types.V1.Blocks
, module Maestro.Types.V1.Common
, module Maestro.Types.V1.Datum
, module Maestro.Types.V1.DefiMarkets
, module Maestro.Types.V1.General
, module Maestro.Types.V1.Pools
, module Maestro.Types.V1.Transactions
Expand All @@ -16,6 +17,7 @@ import Maestro.Types.V1.Addresses
import Maestro.Types.V1.Blocks
import Maestro.Types.V1.Common
import Maestro.Types.V1.Datum
import Maestro.Types.V1.DefiMarkets
import Maestro.Types.V1.General
import Maestro.Types.V1.Pools
import Maestro.Types.V1.Transactions
90 changes: 90 additions & 0 deletions src/Maestro/Types/V1/DefiMarkets.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- | Module to define types for /"DeFi Markets"/ category of endpoints defined at [docs.gomaestro.org](https://docs.gomaestro.org/category/defi-market-api).

module Maestro.Types.V1.DefiMarkets (
Dex(..),
PairOfDexTokens,
Resolution (..),
DexPairResponse(..),
DexPairInfo (..),
OHLCCandleInfo (..)
) where

import qualified Data.Text as T
import Deriving.Aeson
import Maestro.Types.V1.Common
import qualified Data.Aeson as Aeson
import Servant.API

-- | Denotes which dex to use
data Dex = Minswap
deriving stock (Eq, Ord, Generic)

instance Show Dex where
show Minswap = "minswap"

instance ToHttpApiData Dex where
toQueryParam = T.pack . show

-- | Because there is only one dex at the moment, the derivation returns "[]". This can be removed once support for a new dex is added.
instance FromJSON Dex where
parseJSON (Aeson.String "minswap") = return Minswap
parseJSON _ = fail "Expecting oneof [minswap]"

-- | Token Pair that is queried
type PairOfDexTokens = "Token pair to look for. Format: XXX-YYY"

-- | Time resolution for OHLC Candles
data Resolution = Res1m | Res5m | Res15m | Res30m | Res1h | Res4h | Res1d | Res1w | Res1mo
deriving stock (Eq, Ord, Generic)
deriving (FromJSON, ToJSON) via CustomJSON '[ConstructorTagModifier '[StripPrefix "Res"]] Resolution

instance Show Resolution where
show Res1m = "1m"
show Res5m = "5m"
show Res15m = "15m"
show Res30m = "30m"
show Res1h = "1h"
show Res4h = "4h"
show Res1d = "1d"
show Res1w = "1w"
show Res1mo = "1mo"

instance ToHttpApiData Resolution where
toQueryParam = T.pack . show

data DexPairInfo = DexPairInfo
{ dexPairInfoCoinAAssetName :: TokenName
, dexPairInfoCoinAPolicy :: PolicyId
, dexPairInfoCoinBAssetName :: TokenName
, dexPairInfoCoinBPolicy :: PolicyId
, dexPairInfoPair :: String
}
deriving stock (Show, Eq, Ord, Generic)
deriving (FromJSON)
via CustomJSON '[FieldLabelModifier '[StripPrefix "dexPairInfo", CamelToSnake]] DexPairInfo

data DexPairResponse = DexPairResponse
{ dexPairResponseDex :: Dex
, dexPairResponsePairs :: [DexPairInfo]
}
deriving stock (Show, Eq, Ord, Generic)
deriving (FromJSON)
via CustomJSON '[FieldLabelModifier '[StripPrefix "dexPairResponse", LowerFirst]] DexPairResponse

-- | Candle data according to the [OHLC format](https://en.wikipedia.org/wiki/Open-high-low-close_chart)
data OHLCCandleInfo = OHLCCandleInfo
{ ohlcCandleInfoCoinAClose :: Double
, ohlcCandleInfoCoinAHigh :: Double
, ohlcCandleInfoCoinALow :: Double
, ohlcCandleInfoCoinAOpen :: Double
, ohlcCandleInfoCoinAVolume :: Double
, ohlcCandleInfoCoinBClose :: Double
, ohlcCandleInfoCoinBHigh :: Double
, ohlcCandleInfoCoinBLow :: Double
, ohlcCandleInfoCoinBOpen :: Double
, ohlcCandleInfoCoinBVolume :: Double
, ohlcCandleInfoCount :: Integer
}
deriving stock (Show, Eq, Ord, Generic)
deriving (FromJSON)
via CustomJSON '[FieldLabelModifier '[StripPrefix "ohlcCandleInfo", CamelToSnake]] OHLCCandleInfo

0 comments on commit 7f4faa2

Please sign in to comment.