Skip to content

Commit

Permalink
Merge pull request #60 from geniusyield/57-taptools
Browse files Browse the repository at this point in the history
feat: add taptools provider, add nft token in place order response...
  • Loading branch information
ajuggler authored May 10, 2024
2 parents ff73c7e + 8c5442c commit 4ff9398
Show file tree
Hide file tree
Showing 21 changed files with 623 additions and 113 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ For details please see the following section:
maestroToken: YOUR_MAESTRO_TOKEN
# API key to protect server endpoints with. It's value must be provided under `api-key` header of request.
serverApiKey: YOUR_SECRET_KEY
# TapTools API key, to access historical prices using TapTools.
tapToolsApiKey: YOUR_TAP_TOOLS_KEY
# Optionally, wallet key details if one wants server to be able to sign transactions using this key.
wallet:
tag: mnemonicWallet
Expand Down
5 changes: 5 additions & 0 deletions geniusyield-dex-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for geniusyield-dex-api

## 0.2.1.0 -- 2024-05-07

* Adds `placePartialOrder''`, `placePartialOrderWithVersion''` to also return for order's NFT token.
* Exports `placePartialOrder''`, `placePartialOrderWithVersion`, `placePartialOrderWithVersion'` and `placePartialOrderWithVersion''`.

## 0.2.0.0 -- 2024-04-17

* Adds support for v1.1 family of scripts.
Expand Down
4 changes: 1 addition & 3 deletions geniusyield-dex-api/geniusyield-dex-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: geniusyield-dex-api
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.2.0.0
version: 0.2.1.0
synopsis: API code to interact with GeniusYield DEX.
description:
API code to interact with GeniusYield DEX. Learn more about GeniusYield by visiting https://www.geniusyield.co/.
Expand Down Expand Up @@ -72,10 +72,8 @@ library
build-depends:
, aeson
, base ^>=4.16.4.0
, bytestring
, containers
, data-default
, file-embed
, http-types
, lens
, mtl
Expand Down
72 changes: 65 additions & 7 deletions geniusyield-dex-api/src/GeniusYield/Api/Dex/PartialOrder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module GeniusYield.Api.Dex.PartialOrder (
-- * Tx constructors
placePartialOrder,
placePartialOrder',
placePartialOrder'',
placePartialOrderWithVersion,
placePartialOrderWithVersion',
placePartialOrderWithVersion'',
completelyFillPartialOrder,
partiallyFillPartialOrder,
fillPartialOrder,
Expand Down Expand Up @@ -603,7 +607,33 @@ placePartialOrder'
GYTxOutRef
PartialOrderConfigInfoF GYAddress
m (GYTxSkeleton 'PlutusV2)
placePartialOrder' pors = placePartialOrderWithVersion' pors defaultPOCVersion
placePartialOrder' pors addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd = snd <$> placePartialOrder'' pors addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd

placePartialOrder''
(GYDexApiMonad m a, HasCallStack)
PORefs
GYAddress
-- ^ Order owner
(Natural, GYAssetClass)
-- ^ Amount and asset to offer.
GYAssetClass
-- ^ The asset being asked for as payment.
GYRational
-- ^ The price for one unit of the offered asset.
Maybe GYTime
-- ^ The earliest time when the order can be filled (optional).
Maybe GYTime
-- ^ The latest time when the order can be filled (optional).
Natural
-- ^ Additional lovelace fee.
Natural
-- ^ Additional fee in offered tokens.
Maybe GYStakeCredential
-- ^ Stake credential of user. We do not support pointer reference.
GYTxOutRef
PartialOrderConfigInfoF GYAddress
m (GYAssetClass, GYTxSkeleton 'PlutusV2)
placePartialOrder'' pors = placePartialOrderWithVersion'' pors defaultPOCVersion

placePartialOrderWithVersion'
(GYDexApiMonad m a, HasCallStack)
Expand All @@ -630,7 +660,34 @@ placePartialOrderWithVersion'
GYTxOutRef
PartialOrderConfigInfoF GYAddress
m (GYTxSkeleton 'PlutusV2)
placePartialOrderWithVersion' pors pocVersion addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd = do
placePartialOrderWithVersion' pors pocVersion addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd = snd <$> placePartialOrderWithVersion'' pors pocVersion addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd

placePartialOrderWithVersion''
(GYDexApiMonad m a, HasCallStack)
PORefs
POCVersion
GYAddress
-- ^ Order owner
(Natural, GYAssetClass)
-- ^ Amount and asset to offer.
GYAssetClass
-- ^ The asset being asked for as payment.
GYRational
-- ^ The price for one unit of the offered asset.
Maybe GYTime
-- ^ The earliest time when the order can be filled (optional).
Maybe GYTime
-- ^ The latest time when the order can be filled (optional).
Natural
-- ^ Additional lovelace fee.
Natural
-- ^ Additional fee in offered tokens.
Maybe GYStakeCredential
-- ^ Stake credential of user. We do not support pointer reference.
GYTxOutRef
PartialOrderConfigInfoF GYAddress
m (GYAssetClass, GYTxSkeleton 'PlutusV2)
placePartialOrderWithVersion'' pors pocVersion addr (offerAmt, offerAC) priceAC price start end addLov addOff stakeCred cfgRef pocd = do
when (offerAmt == 0) $ throwAppError $ PodNonPositiveAmount $ toInteger offerAmt
when (price <= 0) $ throwAppError $ PodNonPositivePrice price
when (offerAC == priceAC) $ throwAppError $ PodNonDifferentAssets offerAC
Expand Down Expand Up @@ -696,11 +753,12 @@ placePartialOrderWithVersion' pors pocVersion addr (offerAmt, offerAC) priceAC p
o = mkGYTxOut outAddr' offerV (datumFromPlutusData od)

return $
mustHaveInput nftInput
<> mustHaveOutput o
<> mustMint (GYMintReference porMintRef $ mintingPolicyToScript policy) nftRedeemer nftName 1
<> mustHaveRefInput cfgRef
<> mustHaveTxMetadata stampPlaced
(nft,) $
mustHaveInput nftInput
<> mustHaveOutput o
<> mustMint (GYMintReference porMintRef $ mintingPolicyToScript policy) nftRedeemer nftName 1
<> mustHaveRefInput cfgRef
<> mustHaveTxMetadata stampPlaced

-- | Fills an order. If the provided amount of offered tokens to buy is equal to the offered amount, then we completely fill the order. Otherwise, it gets partially filled.
fillPartialOrder
Expand Down
8 changes: 8 additions & 0 deletions geniusyield-server-lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Revision history for geniusyield-server-lib

## 0.3.0 -- 2024-05-07

* Adds TapTools OHLCV endpoint.
* Adds NFT token in response of place order family of endpoints.
* Adds GET variant for getting details of an order from it's NFT token identifier.
* Clarifies which endpoints require `maestroToken` field to be set.
* Clarifies which endpoints require signing key to be configured in the server to derive for wallet's address, likewise it is clarified that which endpoints use fields such as `collateral`, etc. from server's configuration.

## 0.2.0 -- 2024-04-22

* Uses latest version of `geniusyield-dex-api` which adds support of v1.1 script.
Expand Down
13 changes: 11 additions & 2 deletions geniusyield-server-lib/geniusyield-server-lib.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.6
name: geniusyield-server-lib
version: 0.2.0
version: 0.3.0
synopsis: GeniusYield server library
description: Library for GeniusYield server.
license: Apache-2.0
Expand Down Expand Up @@ -58,10 +58,13 @@ library
GeniusYield.Server.Constants
GeniusYield.Server.Ctx
GeniusYield.Server.Dex.HistoricalPrices.Maestro
GeniusYield.Server.Dex.HistoricalPrices.TapTools
GeniusYield.Server.Dex.HistoricalPrices.TapTools.Client
GeniusYield.Server.Dex.Markets
GeniusYield.Server.Dex.PartialOrder
GeniusYield.Server.ErrorMiddleware
GeniusYield.Server.Options
GeniusYield.Server.Orphans
GeniusYield.Server.RequestLoggerMiddleware
GeniusYield.Server.Run
GeniusYield.Server.Tx
Expand All @@ -78,7 +81,7 @@ library
build-depends:
, aeson
, atlas-cardano
, base ^>=4.16.4.0
, base ^>=4.16.4.0
, binary
, bytestring
, cardano-api
Expand All @@ -89,13 +92,19 @@ library
, geniusyield-dex-api
, geniusyield-orderbot-lib
, githash
, http-client
, http-client-tls
, http-types
, insert-ordered-containers
, lens
, optparse-applicative
, plutus-ledger-api
, ply-core
, rio
, servant
, servant-checked-exceptions
, servant-client
, servant-client-core
, servant-foreign
, servant-server
, servant-swagger
Expand Down
17 changes: 14 additions & 3 deletions geniusyield-server-lib/src/GeniusYield/Server/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import GeniusYield.Server.Auth (APIKeyAuthProtect, V0)
import GeniusYield.Server.Constants (gitHash)
import GeniusYield.Server.Ctx
import GeniusYield.Server.Dex.HistoricalPrices.Maestro
import GeniusYield.Server.Dex.HistoricalPrices.TapTools (TapToolsPriceHistoryAPI, handleTapToolsPriceHistoryApi)
import GeniusYield.Server.Dex.Markets (MarketsAPI, handleMarketsApi)
import GeniusYield.Server.Dex.PartialOrder (OrderInfo (..), OrdersAPI, handleOrdersApi, poiToOrderInfo)
import GeniusYield.Server.Orphans ()
import GeniusYield.Server.Tx (TxAPI, handleTxApi)
import GeniusYield.Server.Utils
import GeniusYield.TxBuilder (GYTxQueryMonad (utxosAtAddress))
Expand Down Expand Up @@ -186,6 +188,10 @@ type OrderBookAPI = Summary "Order book" :> Description "Get order book for a sp

type BalancesAPI = Summary "Balances" :> Description "Get token balances of an address." :> Capture "address" GYAddressBech32 :> Get '[JSON] GYBalance

type HistoricalPricesAPI =
"maestro" :> MaestroPriceHistoryAPI
:<|> "tap-tools" :> TapToolsPriceHistoryAPI

type V0API =
"settings" :> SettingsAPI
:<|> "orders" :> OrdersAPI
Expand All @@ -194,7 +200,7 @@ type V0API =
:<|> "trading-fees" :> TradingFeesAPI
:<|> "assets" :> AssetsAPI
:<|> "order-books" :> OrderBookAPI
:<|> "historical-prices" :> "maestro" :> MaestroPriceHistoryAPI
:<|> "historical-prices" :> HistoricalPricesAPI
:<|> "balances" :> BalancesAPI

type GeniusYieldAPI = APIKeyAuthProtect :> V0 :> V0API
Expand Down Expand Up @@ -239,7 +245,7 @@ geniusYieldAPISwagger =
& applyTagsFor (subOperations (Proxy Proxy ("trading-fees" +> TradingFeesAPI)) (Proxy Proxy GeniusYieldAPI)) ["Trading Fees" & description ?~ "Endpoint to get trading fees of DEX."]
& applyTagsFor (subOperations (Proxy Proxy ("assets" +> AssetsAPI)) (Proxy Proxy GeniusYieldAPI)) ["Assets" & description ?~ "Endpoint to fetch asset details."]
& applyTagsFor (subOperations (Proxy Proxy ("order-books" +> OrderBookAPI)) (Proxy Proxy GeniusYieldAPI)) ["Order Book" & description ?~ "Endpoint to fetch order book."]
& applyTagsFor (subOperations (Proxy Proxy ("historical-prices" +> "maestro" :> MaestroPriceHistoryAPI)) (Proxy Proxy GeniusYieldAPI)) ["Historical Prices" & description ?~ "Endpoints to fetch historical prices."]
& applyTagsFor (subOperations (Proxy Proxy ("historical-prices" +> HistoricalPricesAPI)) (Proxy Proxy GeniusYieldAPI)) ["Historical Prices" & description ?~ "Endpoints to fetch historical prices."]
& applyTagsFor (subOperations (Proxy Proxy ("balances" +> BalancesAPI)) (Proxy Proxy GeniusYieldAPI)) ["Balances" & description ?~ "Endpoint to fetch token balances."]

geniusYieldServer Ctx ServerT GeniusYieldAPI IO
Expand All @@ -252,11 +258,16 @@ geniusYieldServer ctx =
:<|> handleTradingFeesApi ctx
:<|> handleAssetsApi ctx
:<|> handleOrderBookApi ctx
:<|> handleMaestroPriceHistoryApi ctx
:<|> handleHistoricalPricesApi ctx
:<|> handleBalancesApi ctx
where
ignoredAuthResult f _authResult = f

handleHistoricalPricesApi Ctx ServerT HistoricalPricesAPI IO
handleHistoricalPricesApi ctx =
handleMaestroPriceHistoryApi ctx
:<|> handleTapToolsPriceHistoryApi ctx

type MainAPI =
GeniusYieldAPI

Expand Down
3 changes: 2 additions & 1 deletion geniusyield-server-lib/src/GeniusYield/Server/Assets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ module GeniusYield.Server.Assets (
) where

import Fmt
import GHC.TypeLits (AppendSymbol)
import GeniusYield.OrderBot.Domain.Assets
import GeniusYield.Server.Ctx
import GeniusYield.Server.Utils
import GeniusYield.Types
import RIO hiding (logDebug, logInfo)
import Servant

type AssetsAPI = Summary "Get assets information" :> Description "Get information for a specific asset." :> Capture "asset" GYAssetClass :> Get '[JSON] AssetDetails
type AssetsAPI = Summary "Get assets information" :> Description ("Get information for a specific asset. " `AppendSymbol` CommonMaestroKeyRequirementText) :> Capture "asset" GYAssetClass :> Get '[JSON] AssetDetails

handleAssetsApi Ctx GYAssetClass IO AssetDetails
handleAssetsApi ctx@Ctx {..} ac = do
Expand Down
72 changes: 0 additions & 72 deletions geniusyield-server-lib/src/GeniusYield/Server/Auth.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# OPTIONS_GHC -Wno-orphans #-}

module GeniusYield.Server.Auth (
V0,
ApiKey,
Expand All @@ -10,17 +8,12 @@ module GeniusYield.Server.Auth (
APIKeyAuthProtect,
) where

import Control.Lens (at, (?~))
import Data.HashMap.Strict.InsOrd qualified as IOHM
import Data.Swagger
import GHC.TypeLits (Symbol, symbolVal)
import Network.Wai (Request (requestHeaders))
import RIO
import RIO.Text qualified as T
import Servant
import Servant.Foreign
import Servant.Server.Experimental.Auth (AuthHandler, AuthServerData, mkAuthHandler)
import Servant.Swagger

type V0 Symbol
type V0 = "v0"
Expand Down Expand Up @@ -50,68 +43,3 @@ apiKeyAuthHandler (ApiKey key) = mkAuthHandler handler
type APIKeyAuthProtect = AuthProtect ApiKeyHeader

type instance AuthServerData APIKeyAuthProtect = ()

instance HasSwagger api HasSwagger (APIKeyAuthProtect :> api) where
toSwagger _ =
toSwagger (Proxy Proxy api)
& securityDefinitions
.~ SecurityDefinitions (IOHM.fromList [(apiKeyHeaderText, apiKeySecurityScheme)])
-- & paths
-- . at signingKeyReqEndpoint
-- . _Just
-- . post
-- . _Just
-- . responses
-- %~ add500SigningKeyFailureResponse
-- & paths
-- . at signingKeyReqEndpoint
-- . _Just
-- . delete
-- . _Just
-- . responses
-- %~ add500SigningKeyFailureResponse
& allOperations
. security
.~ [SecurityRequirement (IOHM.singleton apiKeyHeaderText [])]
& allOperations
. responses
%~ addCommonResponses
where
apiKeySecurityScheme SecurityScheme
apiKeySecurityScheme =
SecurityScheme
{ _securitySchemeType = SecuritySchemeApiKey (ApiKeyParams apiKeyHeaderText ApiKeyHeader),
_securitySchemeDescription = Just "API key for accessing the server's API."
}
addCommonResponses Responses Responses
addCommonResponses resps = resps & at 401 ?~ Inline response401 & at 403 ?~ Inline response403 & at 500 ?~ Inline response500

-- add500SigningKeyFailureResponse ∷ Responses → Responses
-- add500SigningKeyFailureResponse resps = resps & at 500 ?~ Inline response500SigningKeyFailure

response401 Response
response401 = mempty & description .~ "Unauthorized access - API key missing"

response403 Response
response403 = mempty & description .~ "Forbidden - The API key does not have permission to perform the request"

response500 Response
response500 = mempty & description .~ "Internal server error"

-- response500SigningKeyFailure ∷ Response
-- response500SigningKeyFailure = mempty & description .~ "Internal server error - Corresponding signing key is not configured"

-- signingKeyReqEndpoint = "/" <> symbolVal (Proxy ∷ Proxy V0) <> "/orders"

-- `HasForeign` instance for `APIKeyAuthProtect :> api` is required to generate client code using libraries such as `servant-py`.
-- This is written with help from https://github.com/haskell-servant/servant-auth/issues/8#issue-185541839.
instance lang ftype api. (HasForeign lang ftype api, HasForeignType lang ftype Text) HasForeign lang ftype (APIKeyAuthProtect :> api) where
type Foreign ftype (APIKeyAuthProtect :> api) = Foreign ftype api
foreignFor lang Proxy Proxy subR = foreignFor lang Proxy (Proxy Proxy api) subR'
where
subR' = subR {_reqHeaders = HeaderArg arg : _reqHeaders subR}
arg =
Arg
{ _argName = "api-key",
_argType = typeFor lang (Proxy Proxy ftype) (Proxy Proxy Text)
}
1 change: 1 addition & 0 deletions geniusyield-server-lib/src/GeniusYield/Server/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ data ServerConfig = ServerConfig
scPort !Port,
scWallet !(Maybe UserWallet),
scServerApiKey !(Confidential Text),
scTapToolsApiKey !(Maybe (Confidential Text)),
scCollateral !(Maybe GYTxOutRef),
scStakeAddress !(Maybe GYStakeAddressBech32)
}
Expand Down
Loading

0 comments on commit 4ff9398

Please sign in to comment.