Skip to content

Commit

Permalink
feat: add orders category of endpoints
Browse files Browse the repository at this point in the history
Closes: #21
  • Loading branch information
sourabhxyz committed Mar 5, 2024
1 parent deeb3ea commit 3132deb
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module GeniusYield.Api.Dex.PartialOrder (
-- * Queries
partialOrders,
getPartialOrderInfo,
getPartialOrdersInfos,

-- * Tx constructors
placePartialOrder,
Expand Down
52 changes: 44 additions & 8 deletions geniusyield-orderbot/src/GeniusYield/OrderBot/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ module GeniusYield.OrderBot.Types (
DexPair (..),
) where

import Data.Aeson (ToJSON, (.=))
import Data.Aeson (FromJSON (..), ToJSON, (.=))
import Data.Aeson qualified as Aeson
import Data.Kind (Type)
import Data.Ratio (denominator, numerator, (%))
import Data.Word (Word64)
import GeniusYield.Api.Dex.PartialOrder (PartialOrderInfo (..))
import GeniusYield.Imports (Text)
import GeniusYield.Types (rationalToGHC)
import GeniusYield.Types.TxOutRef (GYTxOutRef)
import GeniusYield.Types.Value (GYAssetClass (..))
import Numeric.Natural (Natural)
import RIO (Text)
import RIO.Text qualified as Text
import RIO.Text.Partial qualified as Text
import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))

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

instance ToJSON OrderAssetPair where
toJSON OAssetPair {currencyAsset, commodityAsset} =
Aeson.object
[ "currencyAsset" .= currencyAsset,
"commodityAsset" .= commodityAsset
]
toJSON oap =
Aeson.String $ toUrlPiece oap

instance FromJSON OrderAssetPair where
parseJSON = Aeson.withText "OrderAssetPair" $ \t
case parseUrlPiece t of
Left e fail $ Text.unpack e
Right oap pure oap

-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace}
-- "_"
-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"}
-- "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958"
-- >>> toUrlPiece $ OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"}
-- "_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958"
instance ToHttpApiData OrderAssetPair where
toUrlPiece OAssetPair {..} = toUrlPiece' currencyAsset <> "_" <> toUrlPiece' commodityAsset
where
toUrlPiece' t = case toUrlPiece t of
"lovelace" ""
anyOther anyOther

-- >>> parseUrlPiece "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958" :: Either Text OrderAssetPair
-- Right (OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"})
-- >>> parseUrlPiece "_f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958" :: Either Text OrderAssetPair
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX"})
-- >>> parseUrlPiece "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535.41474958_" :: Either Text OrderAssetPair
-- Right (OAssetPair {currencyAsset = GYToken "f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc535" "AGIX", commodityAsset = GYLovelace})
-- >>> parseUrlPiece "_" :: Either Text OrderAssetPair
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace})
-- >>> parseUrlPiece "" :: Either Text OrderAssetPair
-- Right (OAssetPair {currencyAsset = GYLovelace, commodityAsset = GYLovelace})
instance FromHttpApiData OrderAssetPair where
parseUrlPiece t = do
let (cur, com) = (\com' if Text.null com' then com' else Text.drop 1 com') <$> Text.breakOn "_" t
curAsset parseUrlPiece cur
comAsset parseUrlPiece com
pure $ OAssetPair curAsset comAsset

{- | Two order asset pairs are considered "equivalent" (but not strictly equal, as in 'Eq'),
if they contain the same 2 assets irrespective of order.
Expand Down Expand Up @@ -255,4 +291,4 @@ data DexPair = DexPair
{ dpCurrencyToken !TokenDisplayDetails,
dpCommodityToken !TokenDisplayDetails,
dpMarketPairId !Text
}
}
10 changes: 5 additions & 5 deletions geniusyield-server-lib/src/GeniusYield/Server/Ctx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ runSkeletonI
-- ^ 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.
GYAddress
-- ^ User's change address.
Maybe GYTxOutRefCbor
Maybe GYTxOutRef
-- ^ User's collateral.
ReaderT DEXInfo GYTxMonadNode (GYTxSkeleton v)
IO GYTxBody
Expand All @@ -91,7 +91,7 @@ runSkeletonWithStrategyI
-- ^ 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.
GYAddress
-- ^ User's change address.
Maybe GYTxOutRefCbor
Maybe GYTxOutRef
-- ^ User's collateral.
ReaderT DEXInfo GYTxMonadNode (GYTxSkeleton v)
IO GYTxBody
Expand All @@ -104,7 +104,7 @@ runSkeletonF
-- ^ 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.
GYAddress
-- ^ User's change address.
Maybe GYTxOutRefCbor
Maybe GYTxOutRef
-- ^ User's collateral.
ReaderT DEXInfo GYTxMonadNode (t (GYTxSkeleton v))
IO (t GYTxBody)
Expand All @@ -118,7 +118,7 @@ runSkeletonWithStrategyF
-- ^ 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.
GYAddress
-- ^ User's change address.
Maybe GYTxOutRefCbor
Maybe GYTxOutRef
-- ^ User's collateral.
ReaderT DEXInfo GYTxMonadNode (t (GYTxSkeleton v))
IO (t GYTxBody)
Expand All @@ -128,7 +128,7 @@ runSkeletonWithStrategyF cstrat ctx addrs addr mcollateral skeleton = do
di = ctxDexInfo ctx
mcollateral' = do
collateral mcollateral
pure (getTxOutRefHex collateral, True)
pure (collateral, True)

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

Expand Down
Loading

0 comments on commit 3132deb

Please sign in to comment.