|
| 1 | +module GeniusYield.Server.Tx ( |
| 2 | + TxAPI, |
| 3 | + handleTxApi, |
| 4 | +) where |
| 5 | + |
| 6 | +import Control.Lens ((?~)) |
| 7 | +import Data.Maybe (fromJust) |
| 8 | +import Data.Ratio ((%)) |
| 9 | +import Data.Swagger qualified as Swagger |
| 10 | +import Data.Swagger.Internal.Schema qualified as Swagger |
| 11 | +import Data.Text qualified as T |
| 12 | +import Deriving.Aeson |
| 13 | +import GHC.TypeLits (Symbol) |
| 14 | +import GeniusYield.Api.Dex.PartialOrder (PORefs (..), PartialOrderInfo (..), cancelMultiplePartialOrders, getPartialOrdersInfos, partialOrders, placePartialOrder') |
| 15 | +import GeniusYield.Api.Dex.PartialOrderConfig (fetchPartialOrderConfig) |
| 16 | +import GeniusYield.HTTP.Errors ( |
| 17 | + GYApiError (..), |
| 18 | + IsGYApiError (..), |
| 19 | + ) |
| 20 | +import GeniusYield.Imports |
| 21 | +import GeniusYield.OrderBot.Types (OrderAssetPair (..), mkEquivalentAssetPair, mkOrderAssetPair) |
| 22 | +import GeniusYield.Scripts.Dex.PartialOrderConfig (PartialOrderConfigInfoF (..)) |
| 23 | +import GeniusYield.Server.Ctx |
| 24 | +import GeniusYield.Server.Utils (addSwaggerDescription, dropAndCamelToSnake, logInfo, unsignedTxHex) |
| 25 | +import GeniusYield.TxBuilder.Class |
| 26 | +import GeniusYield.Types |
| 27 | +import Network.HTTP.Types (status400) |
| 28 | +import PlutusLedgerApi.V1.Address (pubKeyHashAddress) |
| 29 | +import RIO.Map qualified as Map |
| 30 | +import Servant |
| 31 | + |
| 32 | +type TransactionSignPrefix ∷ Symbol |
| 33 | +type TransactionSignPrefix = "tf" |
| 34 | + |
| 35 | +-- TODO: JSON & Swagger instances. |
| 36 | +data TransactionSign = TransactionSign |
| 37 | + { tsTransaction ∷ !GYTx |
| 38 | + } |
| 39 | + deriving stock (Generic) |
| 40 | + deriving |
| 41 | + (FromJSON, ToJSON) |
| 42 | + via CustomJSON '[FieldLabelModifier '[StripPrefix TransactionSignPrefix, CamelToSnake]] TransactionSign |
| 43 | + |
| 44 | +instance Swagger.ToSchema TransactionSign where |
| 45 | + declareNamedSchema = |
| 46 | + Swagger.genericDeclareNamedSchema Swagger.defaultSchemaOptions {Swagger.fieldLabelModifier = dropAndCamelToSnake @TransactionSignPrefix} |
| 47 | + |
| 48 | +type TxAPI = |
| 49 | + "tx" |
| 50 | + :> "sign" |
| 51 | + :> ReqBody '[JSON] GYTx |
| 52 | + :> Post '[JSON] GYTx |
| 53 | + :<|> "tx" |
| 54 | + :> "sign_and_submit" |
| 55 | + :> ReqBody '[JSON] GYTx |
| 56 | + :> Post '[JSON] GYTxId |
| 57 | + :<|> "tx" |
| 58 | + :> "submit" |
| 59 | + :> ReqBody '[JSON] GYTx |
| 60 | + :> Post '[JSON] GYTxId |
| 61 | + |
| 62 | +handleTxApi ∷ Ctx → ServerT TxAPI IO |
| 63 | +handleTxApi ctx = |
| 64 | + handleTxSign ctx |
| 65 | + :<|> handleTxSignAndSubmit ctx |
| 66 | + :<|> handleTxSubmit ctx |
| 67 | + |
| 68 | +handleTxSign ∷ Ctx → GYTx → IO GYTx |
| 69 | +handleTxSign ctx@Ctx {..} tx = do |
| 70 | + -- TODO: Add log. |
| 71 | + -- TODO: add signature. |
| 72 | + pure tx |
| 73 | + |
| 74 | +handleTxSignAndSubmit ∷ Ctx → GYTx → IO GYTxId |
| 75 | +handleTxSignAndSubmit ctx@Ctx {..} tx = do |
| 76 | + -- TODO: Add log. |
| 77 | + -- TODO: add logic. |
| 78 | + pure $ "6c751d3e198c5608dfafdfdffe16aeac8a28f88f3a769cf22dd45e8bc84f47e8" |
| 79 | + |
| 80 | +handleTxSubmit ∷ Ctx → GYTx → IO GYTxId |
| 81 | +handleTxSubmit ctx@Ctx {..} tx = do |
| 82 | + -- TODO: Add log. |
| 83 | + -- TODO: add logic. |
| 84 | + pure $ "6c751d3e198c5608dfafdfdffe16aeac8a28f88f3a769cf22dd45e8bc84f47e8" |
0 commit comments