Skip to content

Commit 3132deb

Browse files
committed
feat: add orders category of endpoints
Closes: #21
1 parent deeb3ea commit 3132deb

File tree

6 files changed

+311
-18
lines changed

6 files changed

+311
-18
lines changed

geniusyield-dex-api/src/GeniusYield/Api/Dex/PartialOrder.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module GeniusYield.Api.Dex.PartialOrder (
2828
-- * Queries
2929
partialOrders,
3030
getPartialOrderInfo,
31+
getPartialOrdersInfos,
3132

3233
-- * Tx constructors
3334
placePartialOrder,

geniusyield-orderbot/src/GeniusYield/OrderBot/Types.hs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ module GeniusYield.OrderBot.Types (
2525
DexPair (..),
2626
) where
2727

28-
import Data.Aeson (ToJSON, (.=))
28+
import Data.Aeson (FromJSON (..), ToJSON, (.=))
2929
import Data.Aeson qualified as Aeson
3030
import Data.Kind (Type)
3131
import Data.Ratio (denominator, numerator, (%))
3232
import Data.Word (Word64)
3333
import GeniusYield.Api.Dex.PartialOrder (PartialOrderInfo (..))
34-
import GeniusYield.Imports (Text)
3534
import GeniusYield.Types (rationalToGHC)
3635
import GeniusYield.Types.TxOutRef (GYTxOutRef)
3736
import GeniusYield.Types.Value (GYAssetClass (..))
3837
import Numeric.Natural (Natural)
38+
import RIO (Text)
39+
import RIO.Text qualified as Text
40+
import RIO.Text.Partial qualified as Text
41+
import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))
3942

4043
-------------------------------------------------------------------------------
4144
-- Information on DEX orders relevant to a matching strategy
@@ -194,11 +197,44 @@ data OrderAssetPair = OAssetPair
194197
deriving stock (Eq, Ord, Show)
195198

196199
instance ToJSON OrderAssetPair where
197-
toJSON OAssetPair {currencyAsset, commodityAsset} =
198-
Aeson.object
199-
[ "currencyAsset" .= currencyAsset,
200-
"commodityAsset" .= commodityAsset
201-
]
200+
toJSON oap =
201+
Aeson.String $ toUrlPiece oap
202+
203+
instance FromJSON OrderAssetPair where
204+
parseJSON = Aeson.withText "OrderAssetPair" $ \t
205+
case parseUrlPiece t of
206+
Left e fail $ Text.unpack e
207+
Right oap pure oap
208+
209+
-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace}
210+
-- "_"
211+
-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"}
212+
-- "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958"
213+
-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"}
214+
-- "_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958"
215+
instance ToHttpApiData OrderAssetPair where
216+
toUrlPiece OAssetPair {..} = toUrlPiece' currencyAsset <> "_" <> toUrlPiece' commodityAsset
217+
where
218+
toUrlPiece' t = case toUrlPiece t of
219+
"lovelace" ""
220+
anyOther anyOther
221+
222+
-- >>> parseUrlPiece "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958" :: Either Text OrderAssetPair
223+
-- Right (OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"})
224+
-- >>> parseUrlPiece "_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958" :: Either Text OrderAssetPair
225+
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"})
226+
-- >>> parseUrlPiece "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_" :: Either Text OrderAssetPair
227+
-- Right (OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYLovelace})
228+
-- >>> parseUrlPiece "_" :: Either Text OrderAssetPair
229+
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace})
230+
-- >>> parseUrlPiece "" :: Either Text OrderAssetPair
231+
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace})
232+
instance FromHttpApiData OrderAssetPair where
233+
parseUrlPiece t = do
234+
let (cur, com) = (\com' if Text.null com' then com' else Text.drop 1 com') <$> Text.breakOn "_" t
235+
curAsset parseUrlPiece cur
236+
comAsset parseUrlPiece com
237+
pure $ OAssetPair curAsset comAsset
202238

203239
{- | Two order asset pairs are considered "equivalent" (but not strictly equal, as in 'Eq'),
204240
if they contain the same 2 assets irrespective of order.
@@ -255,4 +291,4 @@ data DexPair = DexPair
255291
{ dpCurrencyToken !TokenDisplayDetails,
256292
dpCommodityToken !TokenDisplayDetails,
257293
dpMarketPairId !Text
258-
}
294+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ runSkeletonI
7777
-- ^ User's used addresses. Note that internally we prepend given change address to this list so that in case wallet's state isn't updated quickly to mark an earlier given change address as used, we'll be able to use UTxOs potentially present at this change address.
7878
GYAddress
7979
-- ^ User's change address.
80-
Maybe GYTxOutRefCbor
80+
Maybe GYTxOutRef
8181
-- ^ User's collateral.
8282
ReaderT DEXInfo GYTxMonadNode (GYTxSkeleton v)
8383
IO GYTxBody
@@ -91,7 +91,7 @@ runSkeletonWithStrategyI
9191
-- ^ User's used addresses. Note that internally we prepend given change address to this list so that in case wallet's state isn't updated quickly to mark an earlier given change address as used, we'll be able to use UTxOs potentially present at this change address.
9292
GYAddress
9393
-- ^ User's change address.
94-
Maybe GYTxOutRefCbor
94+
Maybe GYTxOutRef
9595
-- ^ User's collateral.
9696
ReaderT DEXInfo GYTxMonadNode (GYTxSkeleton v)
9797
IO GYTxBody
@@ -104,7 +104,7 @@ runSkeletonF
104104
-- ^ User's used addresses. Note that internally we prepend given change address to this list so that in case wallet's state isn't updated quickly to mark an earlier given change address as used, we'll be able to use UTxOs potentially present at this change address.
105105
GYAddress
106106
-- ^ User's change address.
107-
Maybe GYTxOutRefCbor
107+
Maybe GYTxOutRef
108108
-- ^ User's collateral.
109109
ReaderT DEXInfo GYTxMonadNode (t (GYTxSkeleton v))
110110
IO (t GYTxBody)
@@ -118,7 +118,7 @@ runSkeletonWithStrategyF
118118
-- ^ User's used addresses. Note that internally we prepend given change address to this list so that in case wallet's state isn't updated quickly to mark an earlier given change address as used, we'll be able to use UTxOs potentially present at this change address.
119119
GYAddress
120120
-- ^ User's change address.
121-
Maybe GYTxOutRefCbor
121+
Maybe GYTxOutRef
122122
-- ^ User's collateral.
123123
ReaderT DEXInfo GYTxMonadNode (t (GYTxSkeleton v))
124124
IO (t GYTxBody)
@@ -128,7 +128,7 @@ runSkeletonWithStrategyF cstrat ctx addrs addr mcollateral skeleton = do
128128
di = ctxDexInfo ctx
129129
mcollateral' = do
130130
collateral mcollateral
131-
pure (getTxOutRefHex collateral, True)
131+
pure (collateral, True)
132132

133133
runGYTxMonadNodeF cstrat nid providers (addr : addrs) addr mcollateral' $ runReaderT skeleton di
134134

0 commit comments

Comments
 (0)