Skip to content

Commit

Permalink
Merge pull request #77 from geniusyield/v1.1-support
Browse files Browse the repository at this point in the history
feat: add support of v1.1 scripts
  • Loading branch information
brunjlar authored May 2, 2024
2 parents 1b8f494 + 3c14bc5 commit 1c71fab
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 150 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The configuration values used for these environment variables in the example abo
configuration values. A MAINNET Maestro API key is needed, a payment signing key must be generated and a collateral UTxO must be provided after
sending funds to the address given by the payment signing key and the (optional) stake address.

In order to determine this address, you could use `cardano-cli address build`, but you can also just run the market maker - the address will be printed to the console in the first line of output:
In order to determine this address, you could use `cardano-cli address build`, but you can also just run the market maker - the address will be printed as the first log of "Info" severity:

```
Genius Yield Market Maker: <MARKET MAKER ADDRESS>
Expand Down Expand Up @@ -133,7 +133,6 @@ https://github.com/geniusyield/market-maker/blob/eeb410f3936e6610797e6402c4dd2fd
> Sample mnemonic provided above is a valid one and can be used to toy around with configuration to understand implications better.
* `ur_coll` (optional) is the UTxO to be reserved as collateral. Though specifying `ur_coll` is optional but it is advised to set it as then this UTxO would be reserved (i.e., would not be spent) and thus be always available to serve as collateral. It is preferred for `ur_coll` to be pure 5 ADA only UTxO (i.e., no other tokens besides ADA).
* Fields `mbc_fp_nft_policy`, `mbc_fp_order_validator`, `mbc_po_config_addr` and `mbc_po_refs` relate to DEX smart contracts and can be left as it is. See sample files corresponding to the network to know for these values.
* `mbc_delay` - Bot in single iteration tries to determine which orders need to be placed and which are needed to be cancelled. Once determined, it tries building the transactions and proceeds with submitting them, completing this single iteration. `mbc_delay` determines time in microseconds that bot must wait before proceeding with next iteration.
* `mbc_price_config` gives the configuration on how to get market price using https://docs.gomaestro.org/DefiMarketAPI/mkt-dex-ohlc Maestro endpoint, for a token.
* `pc_api_key` is the Maestro API key.
Expand Down
15 changes: 9 additions & 6 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ package strict-containers
source-repository-package
type: git
location: https://github.com/geniusyield/dex-contracts-api
tag: 8087ed4da70512cf2327f353d5a3a590511be0b6
--sha256: sha256-Fw9OdzGrvteW1asVctmqkZa8k4G6osgvFDleNozYjW4=
tag: d1212bba3112e3057543a159303f0bdec294feca
--sha256: sha256-fgFz50aaEr1uZQTBO1C1Y+G08SQmoZjs0ajqh5pWFzg=
subdir:
geniusyield-dex-api
geniusyield-onchain/geniusyield-common

source-repository-package
type: git
location: https://github.com/maestro-org/haskell-sdk
tag: 392af75cfe55b1847c0a7b8c848a18e039476124
--sha256: sha256-9ee8hCEIxbRJaXL3bMYkYlQ2gQOPJGRnw4aXFns3dEw=
tag: 609515b3f8861efaa17adf4e599aa1b845775a6d
--sha256: sha256-8se+xlIB1BDEuKGwjaldmW5G4LpCujD1ABgBaY0cY6Y=

-- Unfortunately, cardano-node 8.1.2 is constrained with plutus-ledger-api 1.5.0.0 and we would like at least 1.6.0.0.
-- This is done in accordance with changes in https://github.com/input-output-hk/cardano-ledger/pull/3430/files.
Expand Down Expand Up @@ -71,8 +74,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/geniusyield/atlas
tag: 1d495bdd45be6afcc41b0e49c8ace99c0eff2efd
--sha256: sha256-C9Pe+u+qVyVMYezMnfyGP0vUe96D0yQLEU4JG5fOTwY=
tag: 105ff78b32935ae5c18c53ccf4fb8462b8c1ca03
--sha256: sha256-Ee/UIoJ8OYBxgdnSv61hN8DH7CrIOPn4DjqOBxFL3Js=

source-repository-package
type: git
Expand Down
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ services:
"ur_s_key_path": "/root/payment-signing-key.skey",
"ur_coll": "${COLLATERAL_UTXO}"
},
"mbc_fp_nft_policy": "compiled-scripts/minting-policy",
"mbc_fp_order_validator": "compiled-scripts/partial-order",
"mbc_po_config_addr": "addr1w9zr09hgj7z6vz3d7wnxw0u4x30arsp5k8avlcm84utptls8uqd0z",
"mbc_po_refs": {
"por_ref_nft": "fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.4aff78908ef2dce98bfe435fb3fd2529747b1c4564dff5adebedf4e46d0fc63d",
"por_val_ref": "062f97b0e64130bc18b4a227299a62d6d59a4ea852a4c90db3de2204a2cd19ea#2",
"por_mint_ref": "062f97b0e64130bc18b4a227299a62d6d59a4ea852a4c90db3de2204a2cd19ea#1"
},
"mbc_delay": 120000000,
"mbc_price_config": {
"pc_api_key": "${MAESTRO_API_KEY}",
Expand Down
5 changes: 5 additions & 0 deletions geniusyield-market-maker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for geniusyield-market-maker

## 0.4.0 -- 2024-05-02

* Supports latest version of [`dex-contracts-api`](https://github.com/geniusyield/dex-contracts-api) which adds support of v1.1 family of scripts.
* Updates to latest version of Maestro's Haskell SDK.

## 0.3.0 -- 2024-04-11

* Adds ability to specify different spread for each (sell & buy) side. The change is backwards compatible with respect to configuration.
Expand Down
21 changes: 15 additions & 6 deletions geniusyield-market-maker/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module Main (main) where

import Control.Exception (throwIO)
import GeniusYield.Api.Dex.Constants (dexInfoDefaultMainnet,
dexInfoDefaultPreprod)
import GeniusYield.GYConfig
import GeniusYield.MarketMaker.Constants (logNS)
import GeniusYield.MarketMaker.MakerBot
import GeniusYield.MarketMaker.MakerBotConfig
import GeniusYield.MarketMaker.Prices
import GeniusYield.MarketMaker.Strategies
import GeniusYield.MarketMaker.Utils (addrUser)
import GeniusYield.OrderBot.DataSource.Providers (connectDB)
import GeniusYield.Types (addressToText)
import GeniusYield.Types (GYNetworkId (..),
addressToText)
import GeniusYield.Types.Providers (gyLogInfo)
import System.Environment

-----------------------------------------------------------------------
Expand Down Expand Up @@ -37,13 +42,17 @@ main = do
coreCfg <- coreConfigIO frameworkCfgPath
mbc <- readMBotConfig mBotConfigFile
mb <- buildMakerBot mbc
di <- getDexInfo mbc

putStrLn $ "Genius Yield Market Maker: "
++ show (addressToText $ addrUser (cfgNetworkId coreCfg) $ mbUser mb)
di <-
case cfgNetworkId coreCfg of
GYTestnetPreprod -> pure dexInfoDefaultPreprod
GYMainnet -> pure dexInfoDefaultMainnet
_ -> throwIO $ userError "Only Preprod and Mainnet are supported."

let netId = cfgNetworkId coreCfg
withCfgProviders coreCfg "" $ \providers ->
withCfgProviders coreCfg "" $ \providers -> do
gyLogInfo providers logNS $
"Genius Yield Market Maker: "
++ show (addressToText $ addrUser (cfgNetworkId coreCfg) $ mbUser mb)
case action of
"Run" -> do
c <- connectDB netId providers
Expand Down
3 changes: 1 addition & 2 deletions geniusyield-market-maker/geniusyield-market-maker.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.4
name: geniusyield-market-maker
version: 0.3.0
version: 0.4.0
build-type: Simple
extra-doc-files: CHANGELOG.md
synopsis:
Expand Down Expand Up @@ -126,7 +126,6 @@ executable geniusyield-market-maker-exe
GeniusYield.MarketMaker.Equity
GeniusYield.MarketMaker.MakerBot
GeniusYield.MarketMaker.MakerBotConfig
GeniusYield.MarketMaker.Orphans
GeniusYield.MarketMaker.Prices
GeniusYield.MarketMaker.Spread
GeniusYield.MarketMaker.Strategies
Expand Down
10 changes: 5 additions & 5 deletions geniusyield-market-maker/src/GeniusYield/MarketMaker/MakerBot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import Control.Monad (forM_, forever, when)
import Control.Monad.Reader (runReaderT)
import Data.List.Split (chunksOf)
import qualified Data.Map.Strict as M
import GeniusYield.Api.Dex.Constants (DEXInfo (..))
import GeniusYield.Api.Dex.PartialOrder (PartialOrderInfo (poiOwnerKey),
cancelMultiplePartialOrders,
cancelMultiplePartialOrders',
partialOrders,
placePartialOrder)
import GeniusYield.Imports (printf, (&))
import GeniusYield.MarketMaker.Constants (awaitTxParams, logNS)
import GeniusYield.MarketMaker.Prices
import GeniusYield.MarketMaker.Strategies
import GeniusYield.MarketMaker.User
import GeniusYield.MarketMaker.Utils (DEXInfo (dexPORefs),
addrUser, pkhUser)
import GeniusYield.MarketMaker.Utils (addrUser, pkhUser)
import GeniusYield.Providers.Common (SubmitTxException)
import GeniusYield.Transaction (BuildTxException)
import GeniusYield.TxBuilder
Expand Down Expand Up @@ -49,7 +49,7 @@ cancelAllOrders MakerBot {mbUser} netId providers di = do
userAddr = addrUser netId mbUser
txBody
runGYTxMonadNode netId providers [userAddr] userAddr (uColl mbUser)
$ runReaderT (cancelMultiplePartialOrders (dexPORefs di) batch) di
$ runReaderT (cancelMultiplePartialOrders' (dexPORefs di) batch) di
let signedTx =
signGYTxBody' txBody [uSKeyToSomeSigningKey mbUser]
tid gySubmitTx providers signedTx
Expand All @@ -70,7 +70,7 @@ buildAndSubmitActions user@User {uColl, uStakeCred} providers netId ua di = flip

forM_ (chunksOf 6 cancelActions) $ \cancelChunk -> do
logInfo $ "Building for cancel action(s): " <> show cancelChunk
txBody runGYTxMonadNode netId providers [userAddr] userAddr uColl $ flip runReaderT di $ cancelMultiplePartialOrders (dexPORefs di) (map coaPoi cancelChunk)
txBody runGYTxMonadNode netId providers [userAddr] userAddr uColl $ flip runReaderT di $ cancelMultiplePartialOrders' (dexPORefs di) (map coaPoi cancelChunk)
buildCommon txBody

forM_ placeActions $ \pa@PlaceOrderAction {..} -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,20 @@ import Data.Bifunctor (first)
import Data.String (IsString (..))
import qualified Data.Yaml as Yaml
import Deriving.Aeson
import GeniusYield.Api.Dex.PartialOrder (PORefs (..))
import GeniusYield.MarketMaker.MakerBot (MakerBot (..))
import GeniusYield.MarketMaker.Orphans ()
import GeniusYield.MarketMaker.Prices
import GeniusYield.MarketMaker.Strategies
import GeniusYield.MarketMaker.User
import GeniusYield.MarketMaker.Utils
import GeniusYield.Types
import PlutusLedgerApi.V1 (Address)
import PlutusLedgerApi.V1.Scripts (ScriptHash)
import PlutusLedgerApi.V1.Value (AssetClass)
import Ply (ScriptRole (..),
TypedScript,
readTypedScript)
import System.Envy (FromEnv (fromEnv),
decodeEnv, env)
import System.FilePath (takeExtension)

data MakerBotConfig = MakerBotConfig
{ mbcUser !UserRaw,
mbcFPNftPolicy !FilePath,
mbcFPOrderValidator !FilePath,
mbcPOConfigAddr !GYAddressBech32,
mbcPORefs !PORefs,
mbcDelay !Int,
mbcToken !MMToken,
mbcStrategyConfig !StrategyConfig,
mbcPriceConfig !PriceConfig
{ mbcUser !UserRaw,
mbcDelay !Int,
mbcToken !MMToken,
mbcStrategyConfig !StrategyConfig,
mbcPriceConfig !PriceConfig
}
deriving stock (Show, Generic)
deriving (FromJSON) via CustomJSON '[FieldLabelModifier '[CamelToSnake]] MakerBotConfig
Expand All @@ -43,10 +29,6 @@ instance FromEnv MakerBotConfig where
fromEnv _ =
MakerBotConfig
<$> (forceFromJson <$> env "MBC_USER")
<*> env "MBC_FP_NFT_POLICY"
<*> env "MBC_FP_ORDER_VALIDATOR"
<*> (forceFromJson <$> env "MBC_PO_CONFIG_ADDR")
<*> (forceFromJson <$> env "MBC_PO_REFS")
<*> env "MBC_DELAY"
<*> (forceFromJson <$> env "MBC_TOKEN")
<*> (forceFromJson <$> env "MBC_STRATEGY_CONFIG")
Expand Down Expand Up @@ -81,37 +63,3 @@ buildMakerBot
mbDelay = mbcDelay,
mbToken = mbcToken
}

-- | Read the compiled scripts from the paths.
getDexInfo MakerBotConfig IO DEXInfo
getDexInfo
MakerBotConfig
{ mbcFPNftPolicy,
mbcFPOrderValidator,
mbcPOConfigAddr,
mbcPORefs
} = do
dexMintRaw readNftPolicy
dexValRaw readOrderValidator

let porefs =
PORefs
{ porRefNft = porRefNft mbcPORefs,
porMintRef = porMintRef mbcPORefs,
porValRef = porValRef mbcPORefs
}
return
DEXInfo
{ dexPartialOrderValidator = dexValRaw,
dexNftPolicy = dexMintRaw,
dexPartialOrderConfigAddr = addressFromBech32 mbcPOConfigAddr,
dexPORefs = porefs
}
where
readNftPolicy
IO (TypedScript 'MintingPolicyRole '[ScriptHash, Address, AssetClass])
readNftPolicy = readTypedScript mbcFPNftPolicy

readOrderValidator
IO (TypedScript 'ValidatorRole '[Address, AssetClass])
readOrderValidator = readTypedScript mbcFPOrderValidator
12 changes: 0 additions & 12 deletions geniusyield-market-maker/src/GeniusYield/MarketMaker/Orphans.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import qualified Data.Map.Strict as M
import Data.Maybe (fromJust)
import Data.Text (Text, pack)
import Deriving.Aeson
import GeniusYield.Api.Dex.Constants (DEXInfo (..))
import GeniusYield.GYConfig
import GeniusYield.Imports (Proxy)
import GeniusYield.MarketMaker.Orphans ()
import GeniusYield.MarketMaker.Spread (Spread (..))
import GeniusYield.MarketMaker.Utils
import GeniusYield.OrderBot.DataSource.Providers (Connection)
Expand Down Expand Up @@ -250,7 +250,7 @@ getMaestroPrice PP {maestroPP = MaestroPP {..}} mmtp = do

let pair = TaggedText pairName

ohlInfo handleMaestroError (functionLocationIdent <> " - fetching price from pair") <=< try $ pricesFromDex mppEnv mppDex pair (Just mppResolution) (Just Descending)
ohlInfo handleMaestroError (functionLocationIdent <> " - fetching price from pair") <=< try $ pricesFromDex mppEnv mppDex pair (Just mppResolution) Nothing Nothing Nothing (Just Descending)

let info = head ohlInfo
curPrecision :: Int = fromIntegral $ mmtPrecision $ mmtpCurrency mmtp
Expand Down
42 changes: 8 additions & 34 deletions geniusyield-market-maker/src/GeniusYield/MarketMaker/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
module GeniusYield.MarketMaker.Utils where

import qualified Cardano.Api as Api
import Data.Aeson (camelTo2)
import qualified Data.Text as Text
import GeniusYield.Api.Dex.PartialOrder (PORefs)
import GeniusYield.Imports (coerce, first, (&))
import GeniusYield.MarketMaker.User (Secret (getSecret),
User (..))
import GeniusYield.Providers.Common (SomeDeserializeError (DeserializeErrorAssetClass))
import GeniusYield.Scripts (HasPartialOrderConfigAddr (..),
HasPartialOrderNftScript (..),
HasPartialOrderScript (..))
import qualified Cardano.Api as Api
import Data.Aeson (camelTo2)
import qualified Data.Text as Text
import GeniusYield.Imports (coerce, first, (&))
import GeniusYield.MarketMaker.User (Secret (getSecret), User (..))
import GeniusYield.Providers.Common (SomeDeserializeError (DeserializeErrorAssetClass))
import GeniusYield.Types
import qualified Maestro.Types.V1 as Maestro
import PlutusLedgerApi.V1.Scripts (ScriptHash)
import PlutusLedgerApi.V1.Value (AssetClass)
import PlutusLedgerApi.V2 (Address)
import Ply (ScriptRole (..), TypedScript)
import Unsafe.Coerce (unsafeCoerce)
import qualified Maestro.Types.V1 as Maestro
import Unsafe.Coerce (unsafeCoerce)

camelToSnake :: String -> String
camelToSnake = camelTo2 '_'
Expand All @@ -36,20 +27,3 @@ addrUser netId user = addressFromCredential netId
assetClassFromMaestro :: (Maestro.TokenName, Maestro.PolicyId) Either SomeDeserializeError GYAssetClass
assetClassFromMaestro ("", "") = pure GYLovelace
assetClassFromMaestro (tokenName, policyId) = first (DeserializeErrorAssetClass . Text.pack) $ parseAssetClassWithSep '#' (coerce policyId <> "#" <> coerce tokenName)

-- | Type that encapsulates the scripts needed for the dex api.
data DEXInfo = DEXInfo
{ dexPartialOrderValidator :: !(TypedScript 'ValidatorRole '[Address, AssetClass]),
dexNftPolicy :: !(TypedScript 'MintingPolicyRole '[ScriptHash, Address, AssetClass]),
dexPartialOrderConfigAddr :: !GYAddress,
dexPORefs :: !PORefs
}

instance HasPartialOrderScript DEXInfo where
getPartialOrderValidator = dexPartialOrderValidator

instance HasPartialOrderNftScript DEXInfo where
getPartialOrderNftPolicy = dexNftPolicy

instance HasPartialOrderConfigAddr DEXInfo where
getPartialOrderConfigAddr = dexPartialOrderConfigAddr
8 changes: 0 additions & 8 deletions sample-mainnet-maker-bot-config-gens.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,3 @@ mbc_token:
ac: dda5fdb1002f7389b33e036b6afee82a8189becb6cba852e8b79b4fb.0014df1047454e53
# Registered decimal places for the token. As an examples, for `ADA` it is 6 and for `SNEK` it is 0.
precision: 6
# Boiler plate for Mainnet.
mbc_fp_nft_policy: compiled-scripts/minting-policy
mbc_fp_order_validator: compiled-scripts/partial-order
mbc_po_config_addr: addr1w9zr09hgj7z6vz3d7wnxw0u4x30arsp5k8avlcm84utptls8uqd0z
mbc_po_refs:
por_ref_nft: fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.4aff78908ef2dce98bfe435fb3fd2529747b1c4564dff5adebedf4e46d0fc63d
por_val_ref: 062f97b0e64130bc18b4a227299a62d6d59a4ea852a4c90db3de2204a2cd19ea#2
por_mint_ref: 062f97b0e64130bc18b4a227299a62d6d59a4ea852a4c90db3de2204a2cd19ea#1
8 changes: 0 additions & 8 deletions sample-preprod-maker-bot-config-gens.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,3 @@ mbc_token:
ac: c6e65ba7878b2f8ea0ad39287d3e2fd256dc5c4160fc19bdf4c4d87e.7447454e53
# Registered decimal places for the token. As an examples, for `ADA` it is 6 and for `SNEK` it is 0.
precision: 6
# Boiler plate for Pre-production testnet.
mbc_fp_nft_policy: compiled-scripts/minting-policy
mbc_fp_order_validator: compiled-scripts/partial-order
mbc_po_config_addr: addr_test1wrgvy8fermjrruaf7fnndtmpuw4xx4cnvfqjp5zqu8kscfcvh32qk
mbc_po_refs:
por_ref_nft: fae686ea8f21d567841d703dea4d4221c2af071a6f2b433ff07c0af2.8309f9861928a55d37e84f6594b878941edce5e351f7904c2c63b559bde45c5c
por_val_ref: be6f8dc16d4e8d5aad566ff6b5ffefdda574817a60d503e2a0ea95f773175050#2
por_mint_ref: be6f8dc16d4e8d5aad566ff6b5ffefdda574817a60d503e2a0ea95f773175050#1

0 comments on commit 1c71fab

Please sign in to comment.