Skip to content

Commit

Permalink
engine/order manager: Initial REST managed order updating (resolves t…
Browse files Browse the repository at this point in the history
…hrasher-corp#772) (thrasher-corp#775)

* Initial REST managed order updating

* Apply gloriousCode's changes.go patch

* Update internal order ID handling

* Check error

* Replace string with string pointer

* Avoid nil pointers in upsert

* Update test for UpdateOrderFromDetail()

* Add tests for orders.go

* Remove unnecessary newline

* Address comments

* Add missing nil check

* Add tests for new functions in order_manager.go

* Remove empty line

* Change log level for updates from Info to Debug (keep added orders at Info)

* Initialize orders before running the timer

* [TEMP] Add verbosity for debugging

* Nil checking on exchangeManager in GetExchanges()

- exchangeManager.GetExchanges() and iExchangeManager.GetExchanges() return an error on nil
- bot.GetExchanges() wraps exchangeManager.GetExchanges() and returns an empty slice

* Revert thrasher-corp@b5afe1a

* Do not start the order manager runner thread

Instead, mark the order manager as running

* Remove redundant error.Is() and remove print wrapper on msg

* Add atomic blocker and waitgroup on processOrders()

* Disable unnecessary orderManager runner thread for rpcserver_test

* Remove redundant err from orderStore.getActiveOrders()

* [FIX] Populate requiresProcessing using UpsertResponse data instead of REST return data

.. because the data returned by the REST calls do not include the internal user ID's

* [TEST] Verify that processOrders() actually processes queried order data

* Remove leftover warning and add nil check on wg.Done()

* Apply suggestions from code review

Log category changes - as suggested

Co-authored-by: Adrian Gallagher <[email protected]>

* Return when no exchanges available

Co-authored-by: Adrian Gallagher <[email protected]>
  • Loading branch information
TaltaM and thrasher- authored Sep 14, 2021
1 parent 068b375 commit fd60097
Show file tree
Hide file tree
Showing 17 changed files with 833 additions and 79 deletions.
15 changes: 12 additions & 3 deletions engine/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ func (m *apiServerManager) getIndex(w http.ResponseWriter, _ *http.Request) {
// getAllActiveOrderbooks returns all enabled exchanges orderbooks
func getAllActiveOrderbooks(m iExchangeManager) []EnabledExchangeOrderbooks {
var orderbookData []EnabledExchangeOrderbooks
exchanges := m.GetExchanges()
exchanges, err := m.GetExchanges()
if err != nil {
log.Errorf(log.APIServerMgr, "Cannot get exchanges: %v", err)
}
for x := range exchanges {
assets := exchanges[x].GetAssetTypes(true)
exchName := exchanges[x].GetName()
Expand Down Expand Up @@ -340,7 +343,10 @@ func getAllActiveOrderbooks(m iExchangeManager) []EnabledExchangeOrderbooks {
// getAllActiveTickers returns all enabled exchanges tickers
func getAllActiveTickers(m iExchangeManager) []EnabledExchangeCurrencies {
var tickers []EnabledExchangeCurrencies
exchanges := m.GetExchanges()
exchanges, err := m.GetExchanges()
if err != nil {
log.Errorf(log.APIServerMgr, "Cannot get exchanges: %v", err)
}
for x := range exchanges {
assets := exchanges[x].GetAssetTypes(true)
exchName := exchanges[x].GetName()
Expand Down Expand Up @@ -377,7 +383,10 @@ func getAllActiveTickers(m iExchangeManager) []EnabledExchangeCurrencies {
// getAllActiveAccounts returns all enabled exchanges accounts
func getAllActiveAccounts(m iExchangeManager) []AllEnabledExchangeAccounts {
var accounts []AllEnabledExchangeAccounts
exchanges := m.GetExchanges()
exchanges, err := m.GetExchanges()
if err != nil {
log.Errorf(log.APIServerMgr, "Cannot get exchanges: %v", err)
}
for x := range exchanges {
assets := exchanges[x].GetAssetTypes(true)
exchName := exchanges[x].GetName()
Expand Down
9 changes: 7 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,12 @@ func (bot *Engine) UnloadExchange(exchName string) error {

// GetExchanges retrieves the loaded exchanges
func (bot *Engine) GetExchanges() []exchange.IBotExchange {
return bot.ExchangeManager.GetExchanges()
exch, err := bot.ExchangeManager.GetExchanges()
if err != nil {
gctlog.Warnf(gctlog.ExchangeSys, "Cannot get exchanges: %v", err)
return []exchange.IBotExchange{}
}
return exch
}

// LoadExchange loads an exchange by name. Optional wait group can be added for
Expand Down Expand Up @@ -917,7 +922,7 @@ func (bot *Engine) SetupExchanges() error {
}(configs[x])
}
wg.Wait()
if len(bot.ExchangeManager.GetExchanges()) == 0 {
if len(bot.GetExchanges()) == 0 {
return ErrNoExchangesLoaded
}
return nil
Expand Down
7 changes: 5 additions & 2 deletions engine/exchange_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ func (m *ExchangeManager) Add(exch exchange.IBotExchange) {
}

// GetExchanges returns all stored exchanges
func (m *ExchangeManager) GetExchanges() []exchange.IBotExchange {
func (m *ExchangeManager) GetExchanges() ([]exchange.IBotExchange, error) {
if m == nil {
return nil, fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
}
m.m.Lock()
defer m.m.Unlock()
var exchs []exchange.IBotExchange
for _, x := range m.exchanges {
exchs = append(exchs, x)
}
return exchs
return exchs, nil
}

// RemoveExchange removes an exchange from the manager
Expand Down
18 changes: 15 additions & 3 deletions engine/exchange_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,33 @@ func TestExchangeManagerAdd(t *testing.T) {
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
if exch := m.GetExchanges(); exch[0].GetName() != "Bitfinex" {
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}

func TestExchangeManagerGetExchanges(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
if exchanges := m.GetExchanges(); exchanges != nil {
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges != nil {
t.Error("unexpected value")
}
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
if exch := m.GetExchanges(); exch[0].GetName() != "Bitfinex" {
exchanges, err = m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
Expand Down
2 changes: 1 addition & 1 deletion engine/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (bot *Engine) GetExchangeCryptocurrencyDepositAddresses() map[string]map[st

// GetExchangeNames returns a list of enabled or disabled exchanges
func (bot *Engine) GetExchangeNames(enabledOnly bool) []string {
exchanges := bot.ExchangeManager.GetExchanges()
exchanges := bot.GetExchanges()
var response []string
for i := range exchanges {
if !enabledOnly || (enabledOnly && exchanges[i].IsEnabled()) {
Expand Down
Loading

0 comments on commit fd60097

Please sign in to comment.