From ef3e03bd0621e2700c655744adf12513ab49ab8e Mon Sep 17 00:00:00 2001 From: Amin Talebi Date: Tue, 27 Aug 2024 16:26:56 +0200 Subject: [PATCH] add default values --- ...fig.sample.yaml => config.all.sample.yaml} | 20 ++- configs/deploy/config.minimal.sample.yaml | 8 ++ internal/cmd/run/run.go | 36 ++--- internal/configs/config.go | 125 +++++++++++------- internal/configs/default.go | 64 +++++++++ 5 files changed, 177 insertions(+), 76 deletions(-) rename configs/deploy/{config.sample.yaml => config.all.sample.yaml} (70%) create mode 100644 configs/deploy/config.minimal.sample.yaml create mode 100644 internal/configs/default.go diff --git a/configs/deploy/config.sample.yaml b/configs/deploy/config.all.sample.yaml similarity index 70% rename from configs/deploy/config.sample.yaml rename to configs/deploy/config.all.sample.yaml index d85dfb3..66279df 100644 --- a/configs/deploy/config.sample.yaml +++ b/configs/deploy/config.all.sample.yaml @@ -10,7 +10,7 @@ MarketMaker: Slippage: 0.001 # 0.001 is 0.1% Chain: - Url: ${MARKET_MAKER_KEEPER_RPC} + Url: "SAMPLE_CHAIN_URL" BlockInterval: 500ms Tokens: @@ -28,28 +28,26 @@ Uniswap: Nobitex: Url: "https://api.nobitex.ir" - Key: ${NOBITEX_API_KEY} + Key: "SAMPLE_NOBITEX_KEY" MinimumOrderToman: 300_000 Timeout: 60s OrderStatusInterval: 2s RetryTimeOut: 360s RetrySleepDuration: 5s -ExecutorWallet: - PrivateKey: ${EXECUTOR_PRIVATE_KEY} Indexer: - StartBlock: ${DEX_TRADER_START_BLOCK} + StartBlock: 123 Contracts: - DexTrader: "0xb993318B8af82DbA6D30B8a459c954Cb9550714b" + DexTrader: "YOU_DEX_TRADER_CONTRACT_ADDRESS" UniswapV3Factory: "0x1F98431c8aD98523631AE4a59f267346ea31F984" UniswapV3Quoter: "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6" Postgres: - Host: ${ZARBAN_MAINNET_DB_HOST} - Port: ${ZARBAN_MAINNET_DB_PORT} - User: ${ZARBAN_MAINNET_DB_USER} - Password: ${ZARBAN_MAINNET_DB_PASS} - DB: ${DB_NAME} + Host: "localhost" + Port: 5432 + User: "postgres" + Password: "postgres" + DB: "postgres" MigrationsPath: '/migrations' diff --git a/configs/deploy/config.minimal.sample.yaml b/configs/deploy/config.minimal.sample.yaml new file mode 100644 index 0000000..f9580d2 --- /dev/null +++ b/configs/deploy/config.minimal.sample.yaml @@ -0,0 +1,8 @@ +Chain: + Url: "SAMPLE_CHAIN_URL" + +Nobitex: + Key: "SAMPLE_NOBITEX_KEY" + +Contracts: + DexTrader: "YOU_DEX_TRADER_CONTRACT_ADDRESS" diff --git a/internal/cmd/run/run.go b/internal/cmd/run/run.go index 3d928ac..a6022fc 100644 --- a/internal/cmd/run/run.go +++ b/internal/cmd/run/run.go @@ -2,6 +2,8 @@ package run import ( "context" + "encoding/json" + "fmt" "log" "os" "os/signal" @@ -31,12 +33,9 @@ import ( "github.com/zarbanio/market-maker-keeper/store" ) -const ( - EnvironmentMainnet = "mainnet" - EnvironmentTestnet = "testnet" -) - func main(cfg configs.Config) { + c, _ := json.MarshalIndent(cfg, "", " ") + fmt.Println(string(c)) postgresStore := store.NewPostgres(cfg.Postgres.Host, cfg.Postgres.Port, cfg.Postgres.User, cfg.Postgres.Password, cfg.Postgres.DB) err := postgresStore.Migrate(cfg.Postgres.MigrationsPath) if err != nil { @@ -58,7 +57,12 @@ func main(cfg configs.Config) { logger.Logger.Debug().Uint64("start block", cfg.Indexer.StartBlock).Msg("new block pointer created.") } - executorWallet, err := keystore.New(os.Getenv("PRIVATE_KEY")) + privateKey := os.Getenv("PRIVATE_KEY") + if privateKey == "" { + logger.Logger.Fatal().Msg("PRIVATE_KEY environment variable is not set") + } + + executorWallet, err := keystore.New(privateKey) if err != nil { logger.Logger.Fatal().Err(err).Msg("error while initializing new executor wallet") } @@ -93,17 +97,17 @@ func main(cfg configs.Config) { uniswapV3Factory := uniswapv3.NewFactory(eth, common.HexToAddress(cfg.Contracts.UniswapV3Factory)) - DAI, err := tokenStore.GetTokenBySymbol(symbol.DAI) + dai, err := tokenStore.GetTokenBySymbol(symbol.DAI) if err != nil { logger.Logger.Panic().Err(err).Msg("error while getting token by symbol") } - ZAR, err := tokenStore.GetTokenBySymbol(symbol.ZAR) + zar, err := tokenStore.GetTokenBySymbol(symbol.ZAR) if err != nil { logger.Logger.Panic().Err(err).Msg("error while getting token by symbol") } // crate pair in database if not exist - botPair := pair.Pair{QuoteAsset: DAI.Symbol(), BaseAsset: ZAR.Symbol()} + botPair := pair.Pair{QuoteAsset: dai.Symbol(), BaseAsset: zar.Symbol()} pairId, err := postgresStore.CreatePairIfNotExist(context.Background(), &botPair) if err != nil { logger.Logger.Panic().Err(err).Msg("error while creating pair") @@ -111,7 +115,7 @@ func main(cfg configs.Config) { botPair.Id = pairId poolFee := domain.ParseUniswapFee(cfg.Uniswap.PoolFee) - _, err = uniswapV3Factory.GetPool(context.Background(), DAI.Address(), ZAR.Address(), poolFee) + _, err = uniswapV3Factory.GetPool(context.Background(), dai.Address(), zar.Address(), poolFee) if err != nil { logger.Logger.Panic().Err(err).Msg("error while getting pool from uniswapV3") } @@ -134,7 +138,7 @@ func main(cfg configs.Config) { cfg.Nobitex.OrderStatusInterval, ) - if cfg.General.Environment == EnvironmentTestnet { + if cfg.General.Environment == configs.EnvironmentTestnet { nobitexExchange = nobitex.NewMockExchange( cfg.Nobitex.Url, cfg.Nobitex.Timeout, @@ -148,10 +152,10 @@ func main(cfg configs.Config) { } tokens := make(map[symbol.Symbol]domain.Token) - tokens[symbol.DAI] = DAI - tokens[symbol.ZAR] = ZAR + tokens[symbol.DAI] = dai + tokens[symbol.ZAR] = zar - configStrategy := strategy.Config{ + strategyConfig := strategy.Config{ StartQty: decimal.NewFromFloat(cfg.MarketMaker.StartQty), StepQty: decimal.NewFromFloat(cfg.MarketMaker.StepQty), EndQty: decimal.NewFromInt(cfg.MarketMaker.EndQty), @@ -159,8 +163,8 @@ func main(cfg configs.Config) { Slippage: decimal.NewFromFloat(cfg.MarketMaker.Slippage), } - buyDaiInUniswapSellTetherInNobitex := strategy.NewBuyDaiUniswapSellTetherNobitex(postgresStore, nobitexExchange, dexTrader, quoter, tokens, configStrategy) - buyTetherInNobitexSellDaiInUniswap := strategy.NewSellDaiUniswapBuyTetherNobitex(postgresStore, nobitexExchange, dexTrader, quoter, tokens, configStrategy) + buyDaiInUniswapSellTetherInNobitex := strategy.NewBuyDaiUniswapSellTetherNobitex(postgresStore, nobitexExchange, dexTrader, quoter, tokens, strategyConfig) + buyTetherInNobitexSellDaiInUniswap := strategy.NewSellDaiUniswapBuyTetherNobitex(postgresStore, nobitexExchange, dexTrader, quoter, tokens, strategyConfig) ctx := context.Background() diff --git a/internal/configs/config.go b/internal/configs/config.go index 4a08a26..0de6e09 100644 --- a/internal/configs/config.go +++ b/internal/configs/config.go @@ -8,60 +8,87 @@ import ( "github.com/spf13/viper" ) +const ( + EnvironmentMainnet = "mainnet" + EnvironmentTestnet = "testnet" +) + +type General struct { + Environment string `yaml:"Environment"` + LogLevel string `yaml:"LogLevel"` +} + +type MarketMaker struct { + StartQty float64 `yaml:"StartQty"` + StepQty float64 `yaml:"StepQty"` + EndQty int64 `yaml:"EndQty"` + ProfitThreshold int64 `yaml:"ProfitThreshold"` + Interval time.Duration `yaml:"Interval"` + Slippage float64 `yaml:"Slippage"` +} + +type Chain struct { + Url string `yaml:"Url"` + BlockInterval time.Duration `yaml:"BlockInterval"` +} + +type Token struct { + Address string `yaml:"Address"` + Decimals int `yaml:"Decimals"` + Symbol string `yaml:"Symbol"` +} + +type Uniswap struct { + PoolFee float64 `yaml:"PoolFee"` +} + +type Nobitex struct { + Url string `yaml:"Url"` + Key string `yaml:"Key"` + MinimumOrderToman int64 `yaml:"MinimumOrderToman"` + Timeout time.Duration `yaml:"Timeout"` + OrderStatusInterval time.Duration `yaml:"OrderStatusInterval"` + RetryTimeOut time.Duration `yaml:"RetryTimeOut"` + RetrySleepDuration time.Duration `yaml:"RetrySleepDuration"` +} + +type Contracts struct { + DexTrader string `yaml:"DexTrader"` + UniswapV3Factory string `yaml:"UniswapV3Factory"` + UniswapV3Quoter string `yaml:"UniswapV3Quoter"` +} + +type Indexer struct { + StartBlock uint64 `yaml:"StartBlock"` +} + +type Postgres struct { + Host string `yaml:"Host"` + Port int `yaml:"Port"` + User string `yaml:"User"` + Password string `yaml:"Password"` + DB string `yaml:"DB"` + MigrationsPath string `yaml:"MigrationsPath"` +} + type Config struct { - General struct { - Environment string `yaml:"Environment"` - LogLevel string `yaml:"LogLevel"` - } `yaml:"General"` - MarketMaker struct { - StartQty float64 `yaml:"StartQty"` - StepQty float64 `yaml:"StepQty"` - EndQty int64 `yaml:"EndQty"` - ProfitThreshold int64 `yaml:"ProfitThreshold"` - Interval time.Duration `yaml:"Interval"` - Slippage float64 `yaml:"Slippage"` - } `yaml:"MarketMaker"` - Chain struct { - Url string `yaml:"Url"` - BlockInterval time.Duration `yaml:"BlockInterval"` - } `yaml:"Chain"` - Tokens []struct { - Address string `yaml:"Address"` - Decimals int `yaml:"Decimals"` - Symbol string `yaml:"Symbol"` - } `yaml:"Tokens"` - Uniswap struct { - PoolFee float64 `yaml:"PoolFee"` - } - Nobitex struct { - Url string `yaml:"Url"` - Key string `yaml:"Key"` - MinimumOrderToman int64 `yaml:"MinimumOrderToman"` - Timeout time.Duration `yaml:"Timeout"` - OrderStatusInterval time.Duration `yaml:"OrderStatusInterval"` - RetryTimeOut time.Duration `yaml:"RetryTimeOut"` - RetrySleepDuration time.Duration `yml:"RetrySleepDuration"` - } `yaml:"nobitex"` - Contracts struct { - DexTrader string `yaml:"DexTrader"` - UniswapV3Factory string `yaml:"UniswapV3Factory"` - UniswapV3Quoter string `yaml:"UniswapV3Quoter"` - } `yaml:"Contracts"` - Indexer struct { - StartBlock uint64 `yaml:"StartBlock"` - } - Postgres struct { - Host string `yaml:"Host"` - Port int `yaml:"Port"` - User string `yaml:"User"` - Password string `yaml:"Password"` - DB string `yaml:"DB"` - MigrationsPath string `yaml:"MigrationsPath"` - } `yaml:"Postgres"` + General General `yaml:"General"` + MarketMaker MarketMaker `yaml:"MarketMaker"` + Chain Chain `yaml:"Chain"` + Tokens []Token `yaml:"Tokens"` + Uniswap Uniswap `yaml:"Uniswap"` + Nobitex Nobitex `yaml:"Nobitex"` + Contracts Contracts `yaml:"Contracts"` + Indexer Indexer `yaml:"Indexer"` + Postgres Postgres `yaml:"Postgres"` } func ReadConfig(configFile string) Config { + defaultConfig := DefaultConfig() + c := &Config{} + *c = defaultConfig + err := c.Unmarshal(c, configFile) if err != nil { log.Fatalf("Unmarshal: %v", err) diff --git a/internal/configs/default.go b/internal/configs/default.go new file mode 100644 index 0000000..72513a6 --- /dev/null +++ b/internal/configs/default.go @@ -0,0 +1,64 @@ +package configs + +import ( + "time" +) + +func DefaultConfig() Config { + return Config{ + General: General{ + Environment: EnvironmentMainnet, + LogLevel: "info", + }, + MarketMaker: MarketMaker{ + StartQty: 10.0, + StepQty: 20.0, + EndQty: 400, // max trade DAI in strategy0 and strategy1 + ProfitThreshold: 50000, // 50_000 TMN + Interval: time.Minute * 10, + Slippage: 0.001, + }, + Chain: Chain{ + BlockInterval: time.Millisecond * 500, + }, + Tokens: []Token{ + { + Address: "0xd946188a614a0d9d0685a60f541bba1e8cc421ae", + Decimals: 18, + Symbol: "ZAR", + }, + { + Address: "0xda10009cbd5d07dd0cecc66161fc93d7c9000da1", + Decimals: 18, + Symbol: "DAI", + }, + }, + Uniswap: Uniswap{ + PoolFee: 0.01, + }, + Nobitex: Nobitex{ + Url: "https://api.nobitex.ir", + Key: "", // Assuming no default value for Key + MinimumOrderToman: 300000, + Timeout: time.Second * 60, // 60s + OrderStatusInterval: time.Second * 2, // 2s + RetryTimeOut: time.Second * 360, // 360s + RetrySleepDuration: time.Second * 5, // 5s + }, + Contracts: Contracts{ + UniswapV3Factory: "0x1F98431c8aD98523631AE4a59f267346ea31F984", + UniswapV3Quoter: "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6", + }, + Indexer: Indexer{ + StartBlock: 247010149, + }, + Postgres: Postgres{ + Host: "localhost", + Port: 5432, + User: "postgres", + Password: "postgres", + DB: "postgres", + MigrationsPath: "/migrations", + }, + } +}