pull out db parameter from the constructor

This commit is contained in:
c9s 2020-10-26 15:06:39 +08:00
parent 3aa40f3aab
commit cd666fdf9e
4 changed files with 8 additions and 12 deletions

View File

@ -89,8 +89,8 @@ type Environment struct {
} }
// NewDefaultEnvironment prepares the exchange sessions from the viper settings. // NewDefaultEnvironment prepares the exchange sessions from the viper settings.
func NewDefaultEnvironment(db *sqlx.DB) *Environment { func NewDefaultEnvironment() *Environment {
environment := NewEnvironment(db) environment := NewEnvironment()
for _, n := range SupportedExchanges { for _, n := range SupportedExchanges {
if viper.IsSet(string(n) + "-api-key") { if viper.IsSet(string(n) + "-api-key") {
@ -106,14 +106,8 @@ func NewDefaultEnvironment(db *sqlx.DB) *Environment {
return environment return environment
} }
func NewEnvironment(db *sqlx.DB) *Environment { func NewEnvironment() *Environment {
tradeService := &service.TradeService{DB: db}
return &Environment{ return &Environment{
TradeService: tradeService,
TradeSync: &service.TradeSync{
Service: tradeService,
},
// default trade scan time // default trade scan time
tradeScanTime: time.Now().AddDate(0, 0, -7), // sync from 7 days ago tradeScanTime: time.Now().AddDate(0, 0, -7), // sync from 7 days ago
sessions: make(map[string]*ExchangeSession), sessions: make(map[string]*ExchangeSession),

View File

@ -56,7 +56,7 @@ func TestEnvironment_Connect(t *testing.T) {
xdb, err := sqlx.Connect("mysql", mysqlURL) xdb, err := sqlx.Connect("mysql", mysqlURL)
assert.NoError(t, err) assert.NoError(t, err)
environment := NewEnvironment(xdb) environment := NewEnvironment()
environment.AddExchange("binance", exchange). environment.AddExchange("binance", exchange).
Subscribe(types.KLineChannel,"BTCUSDT", types.SubscribeOptions{}) Subscribe(types.KLineChannel,"BTCUSDT", types.SubscribeOptions{})

View File

@ -86,7 +86,8 @@ func runConfig(ctx context.Context, config *config.Config) error {
return err return err
} }
environ := bbgo.NewDefaultEnvironment(db) environ := bbgo.NewDefaultEnvironment()
environ.SyncTrades(db)
environ.ReportTrade(notifierSet) environ.ReportTrade(notifierSet)
trader := bbgo.NewTrader(environ) trader := bbgo.NewTrader(environ)

View File

@ -61,7 +61,8 @@ var Cmd = &cobra.Command{
return err return err
} }
environ := bbgo.NewDefaultEnvironment(db) environ := bbgo.NewDefaultEnvironment()
environ.SyncTrades(db)
trader := bbgo.NewTrader(environ) trader := bbgo.NewTrader(environ)
trader.AttachStrategyOn(string(exchangeName), New(symbol, interval, baseQuantity)) trader.AttachStrategyOn(string(exchangeName), New(symbol, interval, baseQuantity))