mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 07:26:46 +00:00
Improvement: Subsystem separation (#664)
* Initial codes for a trade tracker * Moving everything in a broken fashion * Removes tradetracker. Removes some errors for subsystems * Cleans up some subsystems, renames stuttering types. Removes some global Bot usage * More basic subsystem renaming and file moving * Removes engine dependency from events,ntpserver,ordermanager,comms manager * Exports eventManager, fixes rpcserver. puts rpcserver back for now * Removes redundant error message, further removes engine dependencies * experimental end of day interface usage * adds ability to build the application * Withdraw and event manager handling * cleans up apiserver and communications manager * Cleans up some start/setup processes. Though should separate * More consistency with Setup Start Stop IsRunning funcs * Final consistency pass before testing phase * Fixes engine tests. Fixes stop nil issue * api server tests * Communications manager testing * Connection manager tests and nilsubsystem error * End of day currencypairsyncer tests * Adds databaseconnection/databaseconnection_test.go * Adds withdrawal manager tests * Deposit address testing. Moved orderbook sync first as its more important * Adds test for event manager * More full eventmanager testing * Adds testfile. Enables skipped test. * ntp manager tests * Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles * Adds websocket routine manager tests * Basic portfolio manager testing * Fixes issue with currency pair sync startup * Fixes issue with event manager startup * Starts the order manager before backtester starts * Fixes fee tests. Expands testing. Doesnt fix races * Fixes most test races * Resolves data races * Fixes subsystem test issues * currency pair syncer coverage tests * Refactors portfolio. Fixes tests. Withdraw validation Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager is in charge how the portfolio is handled and all portfolio functions are attached to the base instead of just exported at the package level Withdrawal validation occurred at the exchange level when it can just be run at the withdrawal manager level. All withdrawal requests go through that endpoint * lint -fix * golang lint fixes * lints and comments everything * Updates GCT logo, adds documentation for some subsystems * More documentation and more logo updates * Fixes backtesting and apiserver errors encountered * Fixes errors and typos from reviewing * More minor fixes * Changes %h verb to %w * reverbs to %s * Humbly begins reverting to more flat engine package The main reasoning for this is that the subsystem split doesn't make sense in a golang environment. The subsystems are only meant to be used with engine and so by placing them in a non-engine area, it does not work and is inconsistent with the rest of the application's package layout. This will begin salvaging the changes made by reverting to a flat engine package, but maintaining the consistent designs introduced. Further, I will look to remove any TestMains and decrease the scope of testing to be more local and decrease the issues that have been caused from our style of testing. * Manages to re-flatten things. Everything is within its own file * mini fixes * Fixes tests and data races and lints * Updates docs tool for engine to create filename readmes * os -> ioutil * remove err * Appveyor version increase test * Removes tCleanup as its unsupported on appveyor * Adds stuff that I thought was in previous merge master commit * Removes cancel from test * Fixes really fun test-exclusive data race * minor nit fixes * niterinos * docs gen * rm;rf test * Remove typoline. expands startstop helper. Splits apiserver * Removes accidental folder * Uses update instead of replace for order upsert * addresses nits. Renames files. Regenerates documentation. * lint and removal of comments * Add new test for default scenario * Fixes typo * regen docs
This commit is contained in:
509
engine/engine.go
509
engine/engine.go
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -15,39 +16,42 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency/coinmarketcap"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
||||
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
|
||||
gctlog "github.com/thrasher-corp/gocryptotrader/log"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
"github.com/thrasher-corp/gocryptotrader/utils"
|
||||
)
|
||||
|
||||
// Engine contains configuration, portfolio, exchange & ticker data and is the
|
||||
// Engine contains configuration, portfolio manager, exchange & ticker data and is the
|
||||
// overarching type across this code base.
|
||||
type Engine struct {
|
||||
Config *config.Config
|
||||
Portfolio *portfolio.Base
|
||||
ExchangeCurrencyPairManager *ExchangeCurrencyPairSyncer
|
||||
NTPManager ntpManager
|
||||
ConnectionManager connectionManager
|
||||
DatabaseManager databaseManager
|
||||
GctScriptManager *gctscript.GctScriptManager
|
||||
OrderManager orderManager
|
||||
PortfolioManager portfolioManager
|
||||
CommsManager commsManager
|
||||
exchangeManager exchangeManager
|
||||
DepositAddressManager *DepositAddressManager
|
||||
Settings Settings
|
||||
Uptime time.Time
|
||||
ServicesWG sync.WaitGroup
|
||||
Config *config.Config
|
||||
apiServer *apiServerManager
|
||||
CommunicationsManager *CommunicationManager
|
||||
connectionManager *connectionManager
|
||||
currencyPairSyncer *syncManager
|
||||
DatabaseManager *DatabaseConnectionManager
|
||||
DepositAddressManager *DepositAddressManager
|
||||
eventManager *eventManager
|
||||
ExchangeManager *ExchangeManager
|
||||
ntpManager *ntpManager
|
||||
OrderManager *OrderManager
|
||||
portfolioManager *portfolioManager
|
||||
gctScriptManager *gctscript.GctScriptManager
|
||||
websocketRoutineManager *websocketRoutineManager
|
||||
WithdrawManager *WithdrawManager
|
||||
Settings Settings
|
||||
uptime time.Time
|
||||
ServicesWG sync.WaitGroup
|
||||
}
|
||||
|
||||
// Vars for engine
|
||||
var (
|
||||
Bot *Engine
|
||||
)
|
||||
// Bot is a happy global engine to allow various areas of the application
|
||||
// to access its setup services and functions
|
||||
var Bot *Engine
|
||||
|
||||
// New starts a new engine
|
||||
func New() (*Engine, error) {
|
||||
@@ -60,10 +64,6 @@ func New() (*Engine, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load config. Err: %s", err)
|
||||
}
|
||||
b.GctScriptManager, err = gctscript.NewManager(&b.Config.GCTScript)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create script manager. Err: %s", err)
|
||||
}
|
||||
|
||||
return &b, nil
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func NewFromSettings(settings *Settings, flagSet map[string]bool) (*Engine, erro
|
||||
return nil, fmt.Errorf("unable to adjust runtime GOMAXPROCS value. Err: %s", err)
|
||||
}
|
||||
|
||||
b.GctScriptManager, err = gctscript.NewManager(&b.Config.GCTScript)
|
||||
b.gctScriptManager, err = gctscript.NewManager(&b.Config.GCTScript)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create script manager. Err: %s", err)
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func validateSettings(b *Engine, s *Settings, flagSet map[string]bool) {
|
||||
|
||||
if flagSet["maxvirtualmachines"] {
|
||||
maxMachines := uint8(s.MaxVirtualMachines)
|
||||
b.GctScriptManager.MaxVirtualMachines = &maxMachines
|
||||
b.gctScriptManager.MaxVirtualMachines = &maxMachines
|
||||
}
|
||||
|
||||
if flagSet["withdrawcachesize"] {
|
||||
@@ -344,36 +344,57 @@ func (bot *Engine) Start() error {
|
||||
if bot == nil {
|
||||
return errors.New("engine instance is nil")
|
||||
}
|
||||
|
||||
var err error
|
||||
newEngineMutex.Lock()
|
||||
defer newEngineMutex.Unlock()
|
||||
|
||||
if bot.Settings.EnableDatabaseManager {
|
||||
if err := bot.DatabaseManager.Start(bot); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Database manager unable to start: %v", err)
|
||||
bot.DatabaseManager, err = SetupDatabaseConnectionManager(&bot.Config.Database)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Database manager unable to setup: %v", err)
|
||||
} else {
|
||||
err = bot.DatabaseManager.Start(&bot.ServicesWG)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Database manager unable to start: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableDispatcher {
|
||||
if err := dispatch.Start(bot.Settings.DispatchMaxWorkerAmount, bot.Settings.DispatchJobsLimit); err != nil {
|
||||
if err = dispatch.Start(bot.Settings.DispatchMaxWorkerAmount, bot.Settings.DispatchJobsLimit); err != nil {
|
||||
gctlog.Errorf(gctlog.DispatchMgr, "Dispatcher unable to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up internet connectivity monitor
|
||||
if bot.Settings.EnableConnectivityMonitor {
|
||||
if err := bot.ConnectionManager.Start(&bot.Config.ConnectionMonitor); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Connection manager unable to start: %v", err)
|
||||
bot.connectionManager, err = setupConnectionManager(&bot.Config.ConnectionMonitor)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Connection manager unable to setup: %v", err)
|
||||
} else {
|
||||
err = bot.connectionManager.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Connection manager unable to start: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableNTPClient {
|
||||
if err := bot.NTPManager.Start(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "NTP manager unable to start: %v", err)
|
||||
if bot.Config.NTPClient.Level == 0 {
|
||||
var responseMessage string
|
||||
responseMessage, err = bot.Config.SetNTPCheck(os.Stdin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to set NTP check: %w", err)
|
||||
}
|
||||
gctlog.Info(gctlog.TimeMgr, responseMessage)
|
||||
}
|
||||
bot.ntpManager, err = setupNTPManager(&bot.Config.NTPClient, *bot.Config.Logging.Enabled)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "NTP manager unable to start: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
bot.Uptime = time.Now()
|
||||
bot.uptime = time.Now()
|
||||
gctlog.Debugf(gctlog.Global, "Bot '%s' started.\n", bot.Config.Name)
|
||||
gctlog.Debugf(gctlog.Global, "Using data dir: %s\n", bot.Settings.DataDir)
|
||||
if *bot.Config.Logging.Enabled && strings.Contains(bot.Config.Logging.Output, "file") {
|
||||
@@ -398,15 +419,22 @@ func (bot *Engine) Start() error {
|
||||
bot.Config.PurgeExchangeAPICredentials()
|
||||
}
|
||||
|
||||
bot.ExchangeManager = SetupExchangeManager()
|
||||
gctlog.Debugln(gctlog.Global, "Setting up exchanges..")
|
||||
err := bot.SetupExchanges()
|
||||
err = bot.SetupExchanges()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bot.Settings.EnableCommsRelayer {
|
||||
if err = bot.CommsManager.Start(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Communications manager unable to start: %v\n", err)
|
||||
bot.CommunicationsManager, err = SetupCommunicationManager(&bot.Config.Communications)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Communications manager unable to setup: %s", err)
|
||||
} else {
|
||||
err = bot.CommunicationsManager.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Communications manager unable to start: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if bot.Settings.EnableCoinmarketcapAnalysis ||
|
||||
@@ -433,7 +461,7 @@ func (bot *Engine) Start() error {
|
||||
},
|
||||
bot.Settings.DataDir)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "ExchangeSettings updater system failed to start %v", err)
|
||||
gctlog.Errorf(gctlog.Global, "ExchangeSettings updater system failed to start %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,34 +469,79 @@ func (bot *Engine) Start() error {
|
||||
go StartRPCServer(bot)
|
||||
}
|
||||
|
||||
if bot.Settings.EnableDeprecatedRPC {
|
||||
go StartRESTServer(bot)
|
||||
}
|
||||
|
||||
if bot.Settings.EnableWebsocketRPC {
|
||||
go StartWebsocketServer(bot)
|
||||
StartWebsocketHandler()
|
||||
}
|
||||
|
||||
if bot.Settings.EnablePortfolioManager {
|
||||
if err = bot.PortfolioManager.Start(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Fund manager unable to start: %v", err)
|
||||
if bot.portfolioManager == nil {
|
||||
bot.portfolioManager, err = setupPortfolioManager(bot.ExchangeManager, bot.Settings.PortfolioManagerDelay, &bot.Config.Portfolio)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "portfolio manager unable to setup: %s", err)
|
||||
} else {
|
||||
err = bot.portfolioManager.Start(&bot.ServicesWG)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "portfolio manager unable to start: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bot.WithdrawManager, err = SetupWithdrawManager(bot.ExchangeManager, bot.portfolioManager, bot.Settings.EnableDryRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bot.Settings.EnableDeprecatedRPC ||
|
||||
bot.Settings.EnableWebsocketRPC {
|
||||
var filePath string
|
||||
filePath, err = config.GetAndMigrateDefaultPath(bot.Settings.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bot.apiServer, err = setupAPIServerManager(&bot.Config.RemoteControl, &bot.Config.Profiler, bot.ExchangeManager, bot, bot.portfolioManager, filePath)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "API Server unable to start: %s", err)
|
||||
} else {
|
||||
if bot.Settings.EnableDeprecatedRPC {
|
||||
err = bot.apiServer.StartRESTServer()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "could not start REST API server: %s", err)
|
||||
}
|
||||
}
|
||||
if bot.Settings.EnableWebsocketRPC {
|
||||
err = bot.apiServer.StartWebsocketServer()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "could not start websocket API server: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableDepositAddressManager {
|
||||
bot.DepositAddressManager = new(DepositAddressManager)
|
||||
go bot.DepositAddressManager.Sync()
|
||||
bot.DepositAddressManager = SetupDepositAddressManager()
|
||||
go func() {
|
||||
err = bot.DepositAddressManager.Sync(bot.GetExchangeCryptocurrencyDepositAddresses())
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Deposit address manager unable to setup: %s", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if bot.Settings.EnableOrderManager {
|
||||
if err = bot.OrderManager.Start(bot); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Order manager unable to start: %v", err)
|
||||
bot.OrderManager, err = SetupOrderManager(
|
||||
bot.ExchangeManager,
|
||||
bot.CommunicationsManager,
|
||||
&bot.ServicesWG,
|
||||
bot.Settings.Verbose)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Order manager unable to setup: %s", err)
|
||||
} else {
|
||||
err = bot.OrderManager.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Order manager unable to start: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableExchangeSyncManager {
|
||||
exchangeSyncCfg := CurrencyPairSyncerConfig{
|
||||
exchangeSyncCfg := &Config{
|
||||
SyncTicker: bot.Settings.EnableTickerSyncing,
|
||||
SyncOrderbook: bot.Settings.EnableOrderbookSyncing,
|
||||
SyncTrades: bot.Settings.EnableTradeSyncing,
|
||||
@@ -479,25 +552,54 @@ func (bot *Engine) Start() error {
|
||||
SyncTimeoutWebsocket: bot.Settings.SyncTimeoutWebsocket,
|
||||
}
|
||||
|
||||
bot.ExchangeCurrencyPairManager, err = NewCurrencyPairSyncer(exchangeSyncCfg)
|
||||
bot.currencyPairSyncer, err = setupSyncManager(
|
||||
exchangeSyncCfg,
|
||||
bot.ExchangeManager,
|
||||
bot.websocketRoutineManager,
|
||||
&bot.Config.RemoteControl)
|
||||
if err != nil {
|
||||
gctlog.Warnf(gctlog.Global, "Unable to initialise exchange currency pair syncer. Err: %s", err)
|
||||
gctlog.Errorf(gctlog.Global, "Unable to initialise exchange currency pair syncer. Err: %s", err)
|
||||
} else {
|
||||
go bot.ExchangeCurrencyPairManager.Start()
|
||||
go func() {
|
||||
err = bot.currencyPairSyncer.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "failed to start exchange currency pair manager. Err: %s", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableEventManager {
|
||||
go EventManger(bot.Settings.Verbose, &bot.CommsManager)
|
||||
bot.eventManager, err = setupEventManager(bot.CommunicationsManager, bot.ExchangeManager, bot.Settings.EventManagerDelay, bot.Settings.EnableDryRun)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Unable to initialise event manager. Err: %s", err)
|
||||
} else {
|
||||
err = bot.eventManager.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "failed to start event manager. Err: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableWebsocketRoutine {
|
||||
go bot.WebsocketRoutine()
|
||||
bot.websocketRoutineManager, err = setupWebsocketRoutineManager(bot.ExchangeManager, bot.OrderManager, bot.currencyPairSyncer, &bot.Config.Currency, bot.Settings.Verbose)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Unable to initialise websocket routine manager. Err: %s", err)
|
||||
} else {
|
||||
err = bot.websocketRoutineManager.Start()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "failed to start websocket routine manager. Err: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableGCTScriptManager {
|
||||
if err := bot.GctScriptManager.Start(&bot.ServicesWG); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "GCTScript manager unable to start: %v", err)
|
||||
bot.gctScriptManager, err = gctscript.NewManager(&bot.Config.GCTScript)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "failed to create script manager. Err: %s", err)
|
||||
}
|
||||
if err := bot.gctScriptManager.Start(&bot.ServicesWG); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "GCTScript manager unable to start: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,46 +613,64 @@ func (bot *Engine) Stop() {
|
||||
|
||||
gctlog.Debugln(gctlog.Global, "Engine shutting down..")
|
||||
|
||||
if len(portfolio.Portfolio.Addresses) != 0 {
|
||||
bot.Config.Portfolio = portfolio.Portfolio
|
||||
if len(bot.portfolioManager.GetAddresses()) != 0 {
|
||||
bot.Config.Portfolio = *bot.portfolioManager.GetPortfolio()
|
||||
}
|
||||
|
||||
if bot.GctScriptManager.Started() {
|
||||
if err := bot.GctScriptManager.Stop(); err != nil {
|
||||
if bot.gctScriptManager.IsRunning() {
|
||||
if err := bot.gctScriptManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "GCTScript manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
if bot.OrderManager.Started() {
|
||||
if bot.OrderManager.IsRunning() {
|
||||
if err := bot.OrderManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Order manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.NTPManager.Started() {
|
||||
if err := bot.NTPManager.Stop(); err != nil {
|
||||
if bot.eventManager.IsRunning() {
|
||||
if err := bot.eventManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "event manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.ntpManager.IsRunning() {
|
||||
if err := bot.ntpManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "NTP manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.CommsManager.Started() {
|
||||
if err := bot.CommsManager.Stop(); err != nil {
|
||||
if bot.CommunicationsManager.IsRunning() {
|
||||
if err := bot.CommunicationsManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Communication manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.PortfolioManager.Started() {
|
||||
if err := bot.PortfolioManager.Stop(); err != nil {
|
||||
if bot.portfolioManager.IsRunning() {
|
||||
if err := bot.portfolioManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Fund manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.ConnectionManager.Started() {
|
||||
if err := bot.ConnectionManager.Stop(); err != nil {
|
||||
if bot.connectionManager.IsRunning() {
|
||||
if err := bot.connectionManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Connection manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.DatabaseManager.Started() {
|
||||
if bot.apiServer.IsRESTServerRunning() {
|
||||
if err := bot.apiServer.StopRESTServer(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "API Server unable to stop REST server. Error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.apiServer.IsWebsocketServerRunning() {
|
||||
if err := bot.apiServer.StopWebsocketServer(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "API Server unable to stop websocket server. Error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.DatabaseManager.IsRunning() {
|
||||
if err := bot.DatabaseManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "Database manager unable to stop. Error: %v", err)
|
||||
}
|
||||
@@ -561,6 +681,11 @@ func (bot *Engine) Stop() {
|
||||
gctlog.Errorf(gctlog.DispatchMgr, "Dispatch system unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
if bot.websocketRoutineManager.IsRunning() {
|
||||
if err := bot.websocketRoutineManager.Stop(); err != nil {
|
||||
gctlog.Errorf(gctlog.Global, "websocket routine manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableCoinmarketcapAnalysis ||
|
||||
bot.Settings.EnableCurrencyConverter ||
|
||||
@@ -589,3 +714,233 @@ func (bot *Engine) Stop() {
|
||||
log.Printf("Failed to close logger. Error: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetExchangeByName returns an exchange given an exchange name
|
||||
func (bot *Engine) GetExchangeByName(exchName string) exchange.IBotExchange {
|
||||
return bot.ExchangeManager.GetExchangeByName(exchName)
|
||||
}
|
||||
|
||||
// UnloadExchange unloads an exchange by name
|
||||
func (bot *Engine) UnloadExchange(exchName string) error {
|
||||
exchCfg, err := bot.Config.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = bot.ExchangeManager.RemoveExchange(exchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
exchCfg.Enabled = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetExchanges retrieves the loaded exchanges
|
||||
func (bot *Engine) GetExchanges() []exchange.IBotExchange {
|
||||
return bot.ExchangeManager.GetExchanges()
|
||||
}
|
||||
|
||||
// LoadExchange loads an exchange by name
|
||||
func (bot *Engine) LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error {
|
||||
exch, err := bot.ExchangeManager.NewExchangeByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exch.GetBase() == nil {
|
||||
return ErrExchangeFailedToLoad
|
||||
}
|
||||
|
||||
var localWG sync.WaitGroup
|
||||
localWG.Add(1)
|
||||
go func() {
|
||||
exch.SetDefaults()
|
||||
localWG.Done()
|
||||
}()
|
||||
exchCfg, err := bot.Config.GetExchangeConfig(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bot.Settings.EnableAllPairs &&
|
||||
exchCfg.CurrencyPairs != nil {
|
||||
assets := exchCfg.CurrencyPairs.GetAssetTypes()
|
||||
for x := range assets {
|
||||
var pairs currency.Pairs
|
||||
pairs, err = exchCfg.CurrencyPairs.GetPairs(assets[x], false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
exchCfg.CurrencyPairs.StorePairs(assets[x], pairs, true)
|
||||
}
|
||||
}
|
||||
|
||||
if bot.Settings.EnableExchangeVerbose {
|
||||
exchCfg.Verbose = true
|
||||
}
|
||||
if exchCfg.Features != nil {
|
||||
if bot.Settings.EnableExchangeWebsocketSupport &&
|
||||
exchCfg.Features.Supports.Websocket {
|
||||
exchCfg.Features.Enabled.Websocket = true
|
||||
}
|
||||
if bot.Settings.EnableExchangeAutoPairUpdates &&
|
||||
exchCfg.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
||||
exchCfg.Features.Enabled.AutoPairUpdates = true
|
||||
}
|
||||
if bot.Settings.DisableExchangeAutoPairUpdates {
|
||||
if exchCfg.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
||||
exchCfg.Features.Enabled.AutoPairUpdates = false
|
||||
}
|
||||
}
|
||||
}
|
||||
if bot.Settings.HTTPUserAgent != "" {
|
||||
exchCfg.HTTPUserAgent = bot.Settings.HTTPUserAgent
|
||||
}
|
||||
if bot.Settings.HTTPProxy != "" {
|
||||
exchCfg.ProxyAddress = bot.Settings.HTTPProxy
|
||||
}
|
||||
if bot.Settings.HTTPTimeout != exchange.DefaultHTTPTimeout {
|
||||
exchCfg.HTTPTimeout = bot.Settings.HTTPTimeout
|
||||
}
|
||||
if bot.Settings.EnableExchangeHTTPDebugging {
|
||||
exchCfg.HTTPDebugging = bot.Settings.EnableExchangeHTTPDebugging
|
||||
}
|
||||
|
||||
localWG.Wait()
|
||||
if !bot.Settings.EnableExchangeHTTPRateLimiter {
|
||||
gctlog.Warnf(gctlog.ExchangeSys,
|
||||
"Loaded exchange %s rate limiting has been turned off.\n",
|
||||
exch.GetName(),
|
||||
)
|
||||
err = exch.DisableRateLimiter()
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.ExchangeSys,
|
||||
"Loaded exchange %s rate limiting cannot be turned off: %s.\n",
|
||||
exch.GetName(),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
exchCfg.Enabled = true
|
||||
err = exch.Setup(exchCfg)
|
||||
if err != nil {
|
||||
exchCfg.Enabled = false
|
||||
return err
|
||||
}
|
||||
|
||||
bot.ExchangeManager.Add(exch)
|
||||
base := exch.GetBase()
|
||||
if base.API.AuthenticatedSupport ||
|
||||
base.API.AuthenticatedWebsocketSupport {
|
||||
assetTypes := base.GetAssetTypes()
|
||||
var useAsset asset.Item
|
||||
for a := range assetTypes {
|
||||
err = base.CurrencyPairs.IsAssetEnabled(assetTypes[a])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
useAsset = assetTypes[a]
|
||||
break
|
||||
}
|
||||
err = exch.ValidateCredentials(useAsset)
|
||||
if err != nil {
|
||||
gctlog.Warnf(gctlog.ExchangeSys,
|
||||
"%s: Cannot validate credentials, authenticated support has been disabled, Error: %s\n",
|
||||
base.Name,
|
||||
err)
|
||||
base.API.AuthenticatedSupport = false
|
||||
base.API.AuthenticatedWebsocketSupport = false
|
||||
exchCfg.API.AuthenticatedSupport = false
|
||||
exchCfg.API.AuthenticatedWebsocketSupport = false
|
||||
}
|
||||
}
|
||||
|
||||
if useWG {
|
||||
exch.Start(wg)
|
||||
} else {
|
||||
tempWG := sync.WaitGroup{}
|
||||
exch.Start(&tempWG)
|
||||
tempWG.Wait()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bot *Engine) dryRunParamInteraction(param string) {
|
||||
if !bot.Settings.CheckParamInteraction {
|
||||
return
|
||||
}
|
||||
|
||||
if !bot.Settings.EnableDryRun {
|
||||
gctlog.Warnf(gctlog.Global,
|
||||
"Command line argument '-%s' induces dry run mode."+
|
||||
" Set -dryrun=false if you wish to override this.",
|
||||
param)
|
||||
bot.Settings.EnableDryRun = true
|
||||
}
|
||||
}
|
||||
|
||||
// SetupExchanges sets up the exchanges used by the Bot
|
||||
func (bot *Engine) SetupExchanges() error {
|
||||
var wg sync.WaitGroup
|
||||
configs := bot.Config.GetAllExchangeConfigs()
|
||||
if bot.Settings.EnableAllPairs {
|
||||
bot.dryRunParamInteraction("enableallpairs")
|
||||
}
|
||||
if bot.Settings.EnableAllExchanges {
|
||||
bot.dryRunParamInteraction("enableallexchanges")
|
||||
}
|
||||
if bot.Settings.EnableExchangeVerbose {
|
||||
bot.dryRunParamInteraction("exchangeverbose")
|
||||
}
|
||||
if bot.Settings.EnableExchangeWebsocketSupport {
|
||||
bot.dryRunParamInteraction("exchangewebsocketsupport")
|
||||
}
|
||||
if bot.Settings.EnableExchangeAutoPairUpdates {
|
||||
bot.dryRunParamInteraction("exchangeautopairupdates")
|
||||
}
|
||||
if bot.Settings.DisableExchangeAutoPairUpdates {
|
||||
bot.dryRunParamInteraction("exchangedisableautopairupdates")
|
||||
}
|
||||
if bot.Settings.HTTPUserAgent != "" {
|
||||
bot.dryRunParamInteraction("httpuseragent")
|
||||
}
|
||||
if bot.Settings.HTTPProxy != "" {
|
||||
bot.dryRunParamInteraction("httpproxy")
|
||||
}
|
||||
if bot.Settings.HTTPTimeout != exchange.DefaultHTTPTimeout {
|
||||
bot.dryRunParamInteraction("httptimeout")
|
||||
}
|
||||
if bot.Settings.EnableExchangeHTTPDebugging {
|
||||
bot.dryRunParamInteraction("exchangehttpdebugging")
|
||||
}
|
||||
|
||||
for x := range configs {
|
||||
if !configs[x].Enabled && !bot.Settings.EnableAllExchanges {
|
||||
gctlog.Debugf(gctlog.ExchangeSys, "%s: Exchange support: Disabled\n", configs[x].Name)
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
cfg := configs[x]
|
||||
go func(currCfg config.ExchangeConfig) {
|
||||
defer wg.Done()
|
||||
err := bot.LoadExchange(currCfg.Name, true, &wg)
|
||||
if err != nil {
|
||||
gctlog.Errorf(gctlog.ExchangeSys, "LoadExchange %s failed: %s\n", currCfg.Name, err)
|
||||
return
|
||||
}
|
||||
gctlog.Debugf(gctlog.ExchangeSys,
|
||||
"%s: Exchange support: Enabled (Authenticated API support: %s - Verbose mode: %s).\n",
|
||||
currCfg.Name,
|
||||
common.IsEnabled(currCfg.API.AuthenticatedSupport),
|
||||
common.IsEnabled(currCfg.Verbose),
|
||||
)
|
||||
}(cfg)
|
||||
}
|
||||
wg.Wait()
|
||||
if len(bot.ExchangeManager.GetExchanges()) == 0 {
|
||||
return ErrNoExchangesLoaded
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user