Skip to content

Commit

Permalink
add default values
Browse files Browse the repository at this point in the history
  • Loading branch information
amintalebi committed Aug 27, 2024
1 parent 540295a commit ef3e03b
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
8 changes: 8 additions & 0 deletions configs/deploy/config.minimal.sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Chain:
Url: "SAMPLE_CHAIN_URL"

Nobitex:
Key: "SAMPLE_NOBITEX_KEY"

Contracts:
DexTrader: "YOU_DEX_TRADER_CONTRACT_ADDRESS"
36 changes: 20 additions & 16 deletions internal/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package run

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down Expand Up @@ -93,25 +97,25 @@ 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")
}
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")
}
Expand All @@ -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,
Expand All @@ -148,19 +152,19 @@ 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),
ProfitThreshold: decimal.NewFromInt(cfg.MarketMaker.ProfitThreshold),
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()

Expand Down
125 changes: 76 additions & 49 deletions internal/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions internal/configs/default.go
Original file line number Diff line number Diff line change
@@ -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",
},
}
}

0 comments on commit ef3e03b

Please sign in to comment.