From a04dd649ee17c7ab872a20cd5e414ad8c4aab332 Mon Sep 17 00:00:00 2001 From: sourabhxyz Date: Fri, 18 Oct 2024 16:56:00 +0530 Subject: [PATCH] feat(#114): handle price configuration --- cabal.project | 5 +- .../geniusyield-orderbot-framework.cabal | 3 +- .../src/GeniusYield/OrderBot.hs | 89 ++++++++++++++++--- .../GeniusYield/OrderBot/OrderBotConfig.hs | 2 +- geniusyield-orderbot.cabal | 2 +- 5 files changed, 84 insertions(+), 17 deletions(-) diff --git a/cabal.project b/cabal.project index d83186b..56a125e 100644 --- a/cabal.project +++ b/cabal.project @@ -38,10 +38,11 @@ source-repository-package source-repository-package type: git location: https://github.com/geniusyield/dex-contracts-api - tag: v0.11.0 - --sha256: sha256-fV6jQVxoPfv1DdssmuHDmyvKcFpFCReiSeZ3n76zC9M= + tag: 90cd415a0120d15164a802df74c30c4b8db6b643 subdir: geniusyield-dex-api + geniusyield-server-lib + geniusyield-orderbot-lib geniusyield-onchain/geniusyield-common -------- Begin contents from @atlas@'s @cabal.project@ file. -------- diff --git a/geniusyield-orderbot-framework/geniusyield-orderbot-framework.cabal b/geniusyield-orderbot-framework/geniusyield-orderbot-framework.cabal index f621613..b9c3c79 100644 --- a/geniusyield-orderbot-framework/geniusyield-orderbot-framework.cabal +++ b/geniusyield-orderbot-framework/geniusyield-orderbot-framework.cabal @@ -1,4 +1,4 @@ -cabal-version: 3.4 +cabal-version: 3.12 name: geniusyield-orderbot-framework synopsis: Smart Order Router framework version: 0.5.0 @@ -185,6 +185,7 @@ library geniusyield-orderbot-framework:datasource, geniusyield-orderbot-framework:orderbook, geniusyield-orderbot-framework:strategies, + geniusyield-server-lib, maestro-sdk, vector, diff --git a/geniusyield-orderbot-framework/src/GeniusYield/OrderBot.hs b/geniusyield-orderbot-framework/src/GeniusYield/OrderBot.hs index f25aaf9..96646da 100644 --- a/geniusyield-orderbot-framework/src/GeniusYield/OrderBot.hs +++ b/geniusyield-orderbot-framework/src/GeniusYield/OrderBot.hs @@ -6,7 +6,7 @@ Maintainer : support@geniusyield.co Stability : develop -} module GeniusYield.OrderBot ( - PriceProvider (..), + PriceProviderConfig (..), OrderBot (..), ExecutionStrategy (..), runOrderBot, @@ -25,6 +25,7 @@ import Control.Monad ( filterM, forever, unless, + when, ) import Control.Monad.Reader (runReaderT) import Data.Aeson (encode) @@ -45,6 +46,7 @@ import GeniusYield.GYConfig ( GYCoreConfig (cfgNetworkId), withCfgProviders, ) +import GeniusYield.Imports (coerce) import GeniusYield.OrderBot.DataSource (closeDB, connectDB) import GeniusYield.OrderBot.MatchingStrategy ( IndependentStrategy, @@ -67,6 +69,9 @@ import GeniusYield.OrderBot.Types ( assetInfo, ) import GeniusYield.Providers.Common (SubmitTxException) +import GeniusYield.Providers.Maestro (networkIdToMaestroEnv) +import GeniusYield.Server.Ctx (TapToolsEnv (..)) +import GeniusYield.Server.Dex.HistoricalPrices.TapTools.Client import GeniusYield.Transaction (GYCoinSelectionStrategy (GYLegacy)) import GeniusYield.TxBuilder ( GYTxBuildResult (..), @@ -75,10 +80,12 @@ import GeniusYield.TxBuilder ( buildTxBodyParallelWithStrategy, runGYTxBuilderMonadIO, runGYTxQueryMonadIO, + utxosAtAddresses, utxosAtTxOutRefs, ) import GeniusYield.TxBuilder.Errors (GYTxMonadException) import GeniusYield.Types +import qualified Maestro.Client.V1 as Maestro import qualified Maestro.Types.V1 as Maestro import System.Exit (exitSuccess) @@ -93,7 +100,7 @@ data MaestroConfig = MaestroConfig data TapToolsConfig = TapToolsConfig { ttcApiKey :: !(Confidential Text) - , ttcPairOverride :: !(Maybe TaptoolsPairOverride) + , ttcPairOverride :: !(Maybe TapToolsPairOverride) } deriving stock (Show, Generic) deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "ttc", Maestro.LowerFirst]] TapToolsConfig @@ -105,19 +112,60 @@ data MaestroPairOverride = MaestroPairOverride deriving stock (Show, Generic) deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "mpo", Maestro.LowerFirst]] MaestroPairOverride -data TaptoolsPairOverride = TaptoolsPairOverride +data TapToolsPairOverride = TapToolsPairOverride { ttpoAsset :: !GYAssetClass , ttpoPrecision :: !Natural } deriving stock (Show, Generic) - deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "ttpo", Maestro.LowerFirst]] TaptoolsPairOverride + deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "ttpo", Maestro.LowerFirst]] TapToolsPairOverride -- | Price provider to get ADA price of a token. -data PriceProvider - = TapToolsPriceProvider !TapToolsConfig - | MaestroPriceProvider !MaestroConfig +data PriceProviderConfig + = TapToolsPriceProviderConfig !TapToolsConfig + | MaestroPriceProviderConfig !MaestroConfig deriving stock (Show, Generic) - deriving (FromJSON, ToJSON) via CustomJSON '[ConstructorTagModifier '[Maestro.LowerFirst]] PriceProvider + deriving (FromJSON, ToJSON) via CustomJSON '[ConstructorTagModifier '[Rename "TapToolsPriceProviderConfig" "tapTools", Rename "MaestroPriceProviderConfig" "maestro"]] PriceProviderConfig + +data MaestroPP = MaestroPP + { mppEnv :: !(Maestro.MaestroEnv 'Maestro.V1) + , mppResolution :: !Maestro.Resolution + , mppDex :: !Maestro.Dex + , mppPairOverride :: !(Maybe MaestroPairOverride) + } + +data TapToolsPP = TapToolsPP + { ttppEnv :: !TapToolsEnv + , ttppPairOverride :: !(Maybe TapToolsPairOverride) + } + +data PriceProvider + = TapToolsPriceProvider !TapToolsPP + | MaestroPriceProvider !MaestroPP + +buildTapToolsPP :: TapToolsConfig -> IO TapToolsPP +buildTapToolsPP TapToolsConfig {..} = do + tcenv <- tapToolsClientEnv + let tenv = TapToolsEnv tcenv (coerce ttcApiKey) + pure + TapToolsPP + { ttppEnv = tenv + , ttppPairOverride = ttcPairOverride + } + +buildMaestroPP :: MaestroConfig -> IO MaestroPP +buildMaestroPP MaestroConfig {..} = do + env <- networkIdToMaestroEnv (coerce mcApiKey) GYMainnet + pure + MaestroPP + { mppEnv = env + , mppResolution = mcResolution + , mppDex = mcDex + , mppPairOverride = mcPairOverride + } + +buildPP :: PriceProviderConfig -> IO PriceProvider +buildPP (TapToolsPriceProviderConfig ttpc) = TapToolsPriceProvider <$> buildTapToolsPP ttpc +buildPP (MaestroPriceProviderConfig mpc) = MaestroPriceProvider <$> buildMaestroPP mpc -- | The order bot is product type between bot info and "execution strategies". data OrderBot = OrderBot @@ -145,7 +193,7 @@ data OrderBot = OrderBot -- submit every iteration. , botLovelaceWarningThreshold :: Maybe Natural -- ^ See 'botCLovelaceWarningThreshold'. - , botPriceProvider :: Maybe PriceProvider + , botPriceProvider :: Maybe PriceProviderConfig -- ^ The price provider for the bot, used in case arbitrage is in non-ada token & we need to decide if the arbitraged tokens compensate the ada lost due to transaction fees. } @@ -178,8 +226,10 @@ runOrderBot , botPriceProvider } = do withCfgProviders cfg "" $ \providers -> do - let logInfo = gyLogInfo providers "SOR" - logDebug = gyLogDebug providers "SOR" + let logInfo = gyLogInfo providers sorNS + logDebug = gyLogDebug providers sorNS + logWarn = gyLogWarning providers sorNS + sorNS = "SOR" netId = cfgNetworkId cfg botPkh = paymentKeyHash $ paymentVerificationKey botSkey @@ -204,7 +254,22 @@ runOrderBot bracket (connectDB netId providers) closeDB $ \conn -> forever $ handle (handleAnyException providers) $ do logInfo "Rescanning for orders..." - + botUtxos <- runGYTxQueryMonadIO netId providers $ utxosAtAddresses botAddrs + let botBalance = foldMapUTxOs utxoValue botUtxos + botLovelaceBalance = valueAssetClass botBalance GYLovelace + logInfo $ + unwords + [ "Bot balance:" + , show botBalance + ] + when (botLovelaceBalance < maybe 0 fromIntegral botLovelaceWarningThreshold) $ + logWarn $ + unwords + [ "Bot lovelace balance is below the warning threshold. Threshold:" + , show botLovelaceWarningThreshold + , ", bot lovelace balance:" + , show botLovelaceBalance + ] -- First we populate the multi asset orderbook, using the provided -- @populateOrderBook@. book <- populateOrderBook conn di botAssetPairFilter diff --git a/geniusyield-orderbot-framework/src/GeniusYield/OrderBot/OrderBotConfig.hs b/geniusyield-orderbot-framework/src/GeniusYield/OrderBot/OrderBotConfig.hs index b9f4d1e..31aaace 100644 --- a/geniusyield-orderbot-framework/src/GeniusYield/OrderBot/OrderBotConfig.hs +++ b/geniusyield-orderbot-framework/src/GeniusYield/OrderBot/OrderBotConfig.hs @@ -82,7 +82,7 @@ data OrderBotConfig -- random (to decrease collisions), or not (to maximize profit) , botCLovelaceWarningThreshold :: Maybe Natural -- ^ If bot's lovelace balance falls below this value, bot would log warning logs. - , botCPriceProvider :: Maybe PriceProvider + , botCPriceProvider :: Maybe PriceProviderConfig } deriving stock (Show, Generic) diff --git a/geniusyield-orderbot.cabal b/geniusyield-orderbot.cabal index 790c38f..bbfef07 100644 --- a/geniusyield-orderbot.cabal +++ b/geniusyield-orderbot.cabal @@ -1,4 +1,4 @@ -cabal-version: 3.4 +cabal-version: 3.12 name: geniusyield-orderbot version: 0.2.0 synopsis: Smart Order Router