-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Micrograx/43-support-market-endpoints
Feat #43: Adds support for ohcl and pair endpoints
- Loading branch information
Showing
7 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |