Skip to content

Commit f879637

Browse files
authored
Merge pull request #64 from geniusyield/62-order-details-get-patch
fix: remove unnecessary nesting of get order details response
2 parents d7ccfc7 + ce8ef79 commit f879637

File tree

6 files changed

+121
-39
lines changed

6 files changed

+121
-39
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ using the [Trading Strategy Executor Framework](https://github.com/geniusyield/s
7575
Thanks to the programming language agnostic RESTful API, any modern programming
7676
language could be used to implement trading strategies and/or SOR, MMBs.
7777

78-
Intergration with the Genius Yield DEX has never been easier.
78+
Integration with the Genius Yield DEX has never been easier.
79+
80+
> [!TIP]
81+
> Have a look at sample configuration in [Building locally from source using the Haskell Toolchain](#building-locally-from-source-using-the-haskell-toolchain) section for thorough explanation of options made available to configure the server.
7982
8083
### Building locally from source using Docker
8184

@@ -106,20 +109,16 @@ For details please see the following section:
106109

107110
```yaml
108111
# Blockchain provider used by Atlas, our off-chain transaction building tool.
109-
# Supported values of `coreProvider` represented as JSON for brevity:
110-
# Local node in combination of Kupo, `{ socketPath: string, kupoUrl: string }`
111-
# Maestro, `{ maestroToken: string, turboSubmit: boolean }`
112-
# Blockfrost, `{ blockfrostKey: string }`
113-
# Note that Blockfrost is not recommended as some of the operations performed aren't optimal with it.
112+
# Head over to https://atlas-app.io/getting-started/endpoints#providing-data-provider section to know how to configure `coreProvider` and what all options are available for it.
114113
coreProvider:
115114
maestroToken: YOUR_MAESTRO_TOKEN
116115
turboSubmit: false
117116
# Network id, only `mainnet` and `preprod` are supported for at the moment.
118117
networkId: mainnet
119118
# Logging configuration. It's an array to cater for potentially multiple scribes.
119+
# See it's description mentioned at https://atlas-app.io/getting-started/endpoints#providing-data-provider for more information.
120120
logging:
121121
- type:
122-
# TODO: Possible values of `tag` are to be documented.
123122
tag: stderr
124123
# Possible values of `severity` are `Debug`, `Info`, `Warning` and `Error`.
125124
severity: Debug

geniusyield-server-lib/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Revision history for geniusyield-server-lib
22

3+
## 0.4.0 -- 2024-05-20
4+
5+
* Fix response of GET `/v0/orders/details/{nft-token}` endpoint to not return response under a `data` field nesting.
6+
* Added `/v0/orders/fill` endpoint.
7+
38
## 0.3.0 -- 2024-05-07
49

510
* Adds TapTools OHLCV endpoint.

geniusyield-server-lib/geniusyield-server-lib.cabal

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.6
22
name: geniusyield-server-lib
3-
version: 0.3.0
3+
version: 0.4.0
44
synopsis: GeniusYield server library
55
description: Library for GeniusYield server.
66
license: Apache-2.0
@@ -98,11 +98,8 @@ library
9898
, insert-ordered-containers
9999
, lens
100100
, optparse-applicative
101-
, plutus-ledger-api
102-
, ply-core
103101
, rio
104102
, servant
105-
, servant-checked-exceptions
106103
, servant-client
107104
, servant-client-core
108105
, servant-foreign

geniusyield-server-lib/src/GeniusYield/Server/Dex/PartialOrder.hs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module GeniusYield.Server.Dex.PartialOrder (
55
poiToOrderInfo,
66
PodServerException (..),
77
PodOrderNotFound (..),
8-
ErrDescription (..),
98
) where
109

1110
import Data.Aeson (ToJSON (..))
@@ -33,7 +32,6 @@ import RIO.Map qualified as Map
3332
import RIO.NonEmpty qualified as NonEmpty
3433
import RIO.Text qualified as T
3534
import Servant
36-
import Servant.Checked.Exceptions
3735

3836
-- | Number of orders that we at most allow to be filled in a single transaction.
3937
maxFillOrders GYNatural
@@ -52,8 +50,10 @@ data PodOrderNotFound = PodOrderNotFound
5250
deriving (Eq, Show, Generic)
5351
deriving anyclass (Exception, ToJSON)
5452

55-
instance ErrStatus PodOrderNotFound where
56-
toErrStatus _ = status404
53+
instance Swagger.ToSchema PodOrderNotFound where
54+
declareNamedSchema =
55+
Swagger.genericDeclareNamedSchema Swagger.defaultSchemaOptions
56+
& addSwaggerDescription (toErrDescription PodOrderNotFound)
5757

5858
class ErrDescription e where
5959
toErrDescription e Text
@@ -320,6 +320,23 @@ instance Swagger.ToSchema FillOrderParameters where
320320
& addSwaggerDescription "Fill order(s) request parameters."
321321
& addSwaggerExample (toJSON $ FillOrderParameters {fopAddresses = pure "addr_test1qrsuhwqdhz0zjgnf46unas27h93amfghddnff8lpc2n28rgmjv8f77ka0zshfgssqr5cnl64zdnde5f8q2xt923e7ctqu49mg5", fopChangeAddress = Just (ChangeAddress "addr_test1qrsuhwqdhz0zjgnf46unas27h93amfghddnff8lpc2n28rgmjv8f77ka0zshfgssqr5cnl64zdnde5f8q2xt923e7ctqu49mg5"), fopCollateral = Just "4293386fef391299c9886dc0ef3e8676cbdbc2c9f2773507f1f838e00043a189#1", fopOrderReferencesWithAmount = ("0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747#0", 100) :| [("0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e144444#0", 100)]})
322322

323+
newtype BotFillOrderParameters = BotFillOrderParameters
324+
{ bfopOrderReferencesWithAmount NonEmpty (GYTxOutRef, GYNatural)
325+
}
326+
deriving stock (Show, Generic)
327+
deriving
328+
(FromJSON, ToJSON)
329+
via CustomJSON '[FieldLabelModifier '[StripPrefix BotFillOrderReqPrefix, CamelToSnake]] BotFillOrderParameters
330+
331+
type BotFillOrderReqPrefix Symbol
332+
type BotFillOrderReqPrefix = "bfop"
333+
334+
instance Swagger.ToSchema BotFillOrderParameters where
335+
declareNamedSchema =
336+
Swagger.genericDeclareNamedSchema Swagger.defaultSchemaOptions {Swagger.fieldLabelModifier = dropSymbolAndCamelToSnake @BotFillOrderReqPrefix}
337+
& addSwaggerDescription "Fill order(s) request parameters specialized towards configured bot."
338+
& addSwaggerExample (toJSON $ BotFillOrderParameters {bfopOrderReferencesWithAmount = ("0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747#0", 100) :| [("0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e144444#0", 100)]})
339+
323340
type FillOrderResPrefix Symbol
324341
type FillOrderResPrefix = "fotd"
325342

@@ -344,7 +361,7 @@ type CommonCollateralText ∷ Symbol
344361
type CommonCollateralText = "Note that if \"collateral\" field is not provided, then framework would try to pick collateral UTxO on it's own and in that case would also be free to spend it (i.e., would be made available to coin balancer)."
345362

346363
type CommonSignText Symbol
347-
type CommonSignText = "It uses the signing key from configuration to compute for wallet address. If collateral is specified in the configuration, then it would be used for."
364+
type CommonSignText = "This endpoint would also sign & submit the built transaction. It uses the signing key from configuration to compute for wallet address. If collateral is specified in the configuration, then it would be used for."
348365

349366
type OrdersAPI =
350367
Summary "Build transaction to create order"
@@ -354,7 +371,7 @@ type OrdersAPI =
354371
:> ReqBody '[JSON] PlaceOrderParameters
355372
:> Post '[JSON] PlaceOrderTransactionDetails
356373
:<|> Summary "Create an order"
357-
:> Description ("Create an order. This endpoint would also sign & submit the built transaction. " `AppendSymbol` CommonSignText `AppendSymbol` " \"stakeAddress\" field from configuration, if provided, is used to place order at a mangled address.")
374+
:> Description ("Create an order. " `AppendSymbol` CommonSignText `AppendSymbol` " \"stakeAddress\" field from configuration, if provided, is used to place order at a mangled address.")
358375
:> ReqBody '[JSON] BotPlaceOrderParameters
359376
:> Post '[JSON] PlaceOrderTransactionDetails
360377
:<|> Summary "Build transaction to cancel order(s)"
@@ -364,7 +381,7 @@ type OrdersAPI =
364381
:> ReqBody '[JSON] CancelOrderParameters
365382
:> Post '[JSON] CancelOrderTransactionDetails
366383
:<|> Summary "Cancel order(s)"
367-
:> Description ("Cancel order(s). This endpoint would also sign & submit the built transaction. " `AppendSymbol` CommonSignText)
384+
:> Description ("Cancel order(s). " `AppendSymbol` CommonSignText)
368385
:> ReqBody '[JSON] BotCancelOrderParameters
369386
:> Delete '[JSON] CancelOrderTransactionDetails
370387
:<|> Summary "Get order(s) details"
@@ -376,14 +393,18 @@ type OrdersAPI =
376393
:> Description "Get details of an order using it's unique NFT token. Note that each order is identified uniquely by an associated NFT token which can then later be used to retrieve it's details across partial fills."
377394
:> "details"
378395
:> Capture "nft-token" GYAssetClass
379-
:> Throws PodOrderNotFound
380-
:> Get '[JSON] OrderInfoDetailed
396+
:> UVerb 'GET '[JSON] '[WithStatus 200 OrderInfoDetailed, WithStatus 404 PodOrderNotFound]
381397
:<|> Summary "Build transaction to fill order(s)"
382398
:> Description ("Build a transaction to fill order(s). " `AppendSymbol` CommonCollateralText)
383399
:> "tx"
384400
:> "build-fill"
385401
:> ReqBody '[JSON] FillOrderParameters
386402
:> Post '[JSON] FillOrderTransactionDetails
403+
:<|> Summary "Build transaction to fill order(s)"
404+
:> Description ("Build a transaction to fill order(s). " `AppendSymbol` CommonSignText)
405+
:> "fill"
406+
:> ReqBody '[JSON] BotFillOrderParameters
407+
:> Post '[JSON] FillOrderTransactionDetails
387408

388409
handleOrdersApi Ctx ServerT OrdersAPI IO
389410
handleOrdersApi ctx =
@@ -394,6 +415,7 @@ handleOrdersApi ctx =
394415
:<|> handleOrdersDetails ctx
395416
:<|> handleOrderDetails ctx
396417
:<|> handleFillOrders ctx
418+
:<|> handleFillOrdersAndSignSubmit ctx
397419

398420
handlePlaceOrder Ctx PlaceOrderParameters IO PlaceOrderTransactionDetails
399421
handlePlaceOrder ctx@Ctx {..} pops@PlaceOrderParameters {..} = do
@@ -477,14 +499,14 @@ handleCancelOrdersAndSignSubmit ctx BotCancelOrderParameters {..} = do
477499
-- Though transaction id would be same, but we are returning it again, just in case...
478500
pure $ details {cotdTransactionId = txId, cotdTransaction = signedTx}
479501

480-
handleOrderDetails Ctx GYAssetClass IO (Envelope '[PodOrderNotFound] OrderInfoDetailed)
502+
handleOrderDetails Ctx GYAssetClass IO (Union '[WithStatus 200 OrderInfoDetailed, WithStatus 404 PodOrderNotFound])
481503
handleOrderDetails ctx@Ctx {..} ac = do
482504
logInfo ctx $ "Getting order details for NFT token: " +|| ac ||+ ""
483505
let porefs = dexPORefs ctxDexInfo
484506
os runQuery ctx $ fmap poiToOrderInfoDetailed <$> orderByNft porefs ac
485507
case os of
486-
Nothing throwIO PodOrderNotFound
487-
Just o pureSuccEnvelope o
508+
Nothing throwIO PodOrderNotFound -- We could use `respond` here as well but then as it would not have @application/json@ header, it would not be caught by our @errorJsonWrapMiddleware@.
509+
Just o respond (WithStatus @200 o)
488510

489511
handleOrdersDetails Ctx [GYAssetClass] IO [OrderInfoDetailed]
490512
handleOrdersDetails ctx@Ctx {..} acs = do
@@ -539,3 +561,13 @@ handleFillOrders ctx@Ctx {..} fops@FillOrderParameters {..} = do
539561
takerFee =
540562
Map.foldlWithKey' (\acc ac amt acc <> valueSingleton ac (roundFunctionForPOCVersion overallPocVersion $ toRational amt * rationalToGHC takerFeeRatio)) mempty takerACWithAmt
541563
in takerFee
564+
565+
handleFillOrdersAndSignSubmit Ctx BotFillOrderParameters IO FillOrderTransactionDetails
566+
handleFillOrdersAndSignSubmit ctx BotFillOrderParameters {..} = do
567+
logInfo ctx "Filling order(s) and signing & submitting the transaction."
568+
ctxAddr addressToBech32 <$> resolveCtxAddr ctx
569+
details handleFillOrders ctx $ FillOrderParameters {fopAddresses = pure ctxAddr, fopChangeAddress = Just (ChangeAddress ctxAddr), fopCollateral = ctxCollateral ctx, fopOrderReferencesWithAmount = bfopOrderReferencesWithAmount}
570+
signedTx handleTxSign ctx $ fotdTransaction details
571+
txId handleTxSubmit ctx signedTx
572+
-- Though transaction id would be same, but we are returning it again, just in case...
573+
pure $ details {fotdTransactionId = txId, fotdTransaction = signedTx}

geniusyield-server-lib/src/GeniusYield/Server/Orphans.hs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,11 @@ import Control.Lens (at, (?~))
66
import Data.HashMap.Strict.InsOrd qualified as IOHM
77
import Data.Swagger
88
import GeniusYield.Server.Auth (APIKeyAuthProtect, apiKeyHeaderText)
9-
import GeniusYield.Server.Dex.PartialOrder (ErrDescription (..))
109
import RIO
1110
import Servant
12-
import Servant.Checked.Exceptions
1311
import Servant.Foreign
1412
import Servant.Swagger
1513

16-
type IsErr err = (ErrDescription err, ErrStatus err)
17-
18-
instance (IsErr err, HasSwagger sub) HasSwagger (Throws err :> sub) where
19-
toSwagger _ =
20-
toSwagger (Proxy Proxy sub)
21-
& setResponseWith
22-
(\old _ addDescription old)
23-
(fromEnum $ toErrStatus (undefined err))
24-
(return $ mempty & description .~ errDescription)
25-
where
26-
addDescription = description %~ ((errDescription <> " OR ") <>)
27-
errDescription = toErrDescription (undefined err)
28-
2914
instance HasSwagger api HasSwagger (APIKeyAuthProtect :> api) where
3015
toSwagger _ =
3116
toSwagger (Proxy Proxy api)

web/swagger/api.yaml

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ definitions:
3333
required:
3434
- order_references
3535
type: object
36+
BotFillOrderParameters:
37+
description: Fill order(s) request parameters specialized towards configured bot.
38+
example:
39+
order_references_with_amount:
40+
- - 0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747#0
41+
- '100'
42+
- - 0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e144444#0
43+
- '100'
44+
properties:
45+
order_references_with_amount:
46+
items:
47+
items:
48+
- $ref: '#/definitions/GYTxOutRef'
49+
- $ref: '#/definitions/GYNatural'
50+
maxItems: 2
51+
minItems: 2
52+
type: array
53+
minItems: 1
54+
type: array
55+
required:
56+
- order_references_with_amount
57+
type: object
3658
BotPlaceOrderParameters:
3759
description: Place order request parameters specialized towards configured bot.
3860
properties:
@@ -478,6 +500,11 @@ definitions:
478500
- order_ref
479501
- nft_token
480502
type: object
503+
PodOrderNotFound:
504+
description: Order not found
505+
enum:
506+
- PodOrderNotFound
507+
type: string
481508
Settings:
482509
description: Genius Yield Server settings.
483510
properties:
@@ -957,14 +984,51 @@ paths:
957984
description: Forbidden - The API key does not have permission to perform
958985
the request
959986
'404':
960-
description: Order not found
987+
description: ''
988+
schema:
989+
$ref: '#/definitions/PodOrderNotFound'
961990
'500':
962991
description: Internal server error
963992
security:
964993
- api-key: []
965994
summary: Get order details
966995
tags:
967996
- Orders
997+
/v0/orders/fill:
998+
post:
999+
consumes:
1000+
- application/json;charset=utf-8
1001+
description: Build a transaction to fill order(s). This endpoint would also
1002+
sign & submit the built transaction. It uses the signing key from configuration
1003+
to compute for wallet address. If collateral is specified in the configuration,
1004+
then it would be used for.
1005+
parameters:
1006+
- in: body
1007+
name: body
1008+
required: true
1009+
schema:
1010+
$ref: '#/definitions/BotFillOrderParameters'
1011+
produces:
1012+
- application/json;charset=utf-8
1013+
responses:
1014+
'200':
1015+
description: ''
1016+
schema:
1017+
$ref: '#/definitions/FillOrderTransactionDetails'
1018+
'400':
1019+
description: Invalid `body`
1020+
'401':
1021+
description: Unauthorized access - API key missing
1022+
'403':
1023+
description: Forbidden - The API key does not have permission to perform
1024+
the request
1025+
'500':
1026+
description: Internal server error
1027+
security:
1028+
- api-key: []
1029+
summary: Build transaction to fill order(s)
1030+
tags:
1031+
- Orders
9681032
/v0/orders/tx/build-cancel:
9691033
post:
9701034
consumes:

0 commit comments

Comments
 (0)