gctrpc/ordermanager/binance: Add new getManagedOrders command and various improvements (#712)

* first draft of getmanaged orders RPC call

* - ClientIDs for binance, especially spot asset
- applied old ClientOrderId for cancelled orders
- added clientOrderId to GCTRPC

* added tests for Matchfilter and GetManagedOrders

* smaller fixes

* comment fix
added getFilteredOrders to store
changed store mutex to RWMutex
smaller fixes

* fixed bug in Detail Copy and added test

* fixes for Scotts review

* processSubmittedOrder was missing clientOrderId

* changed: TestGetOrdersFiltered expanded
fixed: warning, where variable name collided with package name
fixed: used req.AssetType in binance_wrapper.go

Co-authored-by: Mark Dzulko <81071907+Mark-numus@users.noreply.github.com>
This commit is contained in:
Mark Dzulko
2021-07-20 02:27:16 +02:00
committed by GitHub
parent 6182dd6fbc
commit e1eceeafe8
19 changed files with 1061 additions and 313 deletions

View File

@@ -379,15 +379,28 @@ func (m *OrderManager) GetOrdersSnapshot(s order.Status) ([]order.Detail, time.T
if v[i].LastUpdated.After(latestUpdate) {
latestUpdate = v[i].LastUpdated
}
cpy := *v[i]
os = append(os, cpy)
os = append(os, *v[i])
}
}
return os, latestUpdate
}
// GetOrdersFiltered returns a snapshot of all orders in the order store.
// Filtering is applied based on the order.Filter unless entries are empty
func (m *OrderManager) GetOrdersFiltered(f *order.Filter) ([]order.Detail, error) {
if m == nil {
return nil, fmt.Errorf("order manager %w", ErrNilSubsystem)
}
if f == nil {
return nil, fmt.Errorf("order manager, GetOrdersFiltered: Filter is nil")
}
if atomic.LoadInt32(&m.started) == 0 {
return nil, fmt.Errorf("order manager %w", ErrSubSystemNotStarted)
}
return m.orderStore.getFilteredOrders(f)
}
// processSubmittedOrder adds a new order to the manager
func (m *OrderManager) processSubmittedOrder(newOrder *order.Submit, result order.SubmitResponse) (*OrderSubmitResponse, error) {
if !result.IsOrderPlaced {
@@ -439,6 +452,7 @@ func (m *OrderManager) processSubmittedOrder(newOrder *order.Submit, result orde
ID: result.OrderID,
AccountID: newOrder.AccountID,
ClientID: newOrder.ClientID,
ClientOrderID: newOrder.ClientOrderID,
WalletAddress: newOrder.WalletAddress,
Type: newOrder.Type,
Side: newOrder.Side,
@@ -661,8 +675,8 @@ func (s *store) upsert(od *order.Detail) error {
// getByExchange returns orders by exchange
func (s *store) getByExchange(exchange string) ([]*order.Detail, error) {
s.m.Lock()
defer s.m.Unlock()
s.m.RLock()
defer s.m.RUnlock()
r, ok := s.Orders[strings.ToLower(exchange)]
if !ok {
return nil, ErrExchangeNotFound
@@ -673,8 +687,8 @@ func (s *store) getByExchange(exchange string) ([]*order.Detail, error) {
// getByInternalOrderID will search all orders for our internal orderID
// and return the order
func (s *store) getByInternalOrderID(internalOrderID string) (*order.Detail, error) {
s.m.Lock()
defer s.m.Unlock()
s.m.RLock()
defer s.m.RUnlock()
for _, v := range s.Orders {
for x := range v {
if v[x].InternalOrderID == internalOrderID {
@@ -690,8 +704,8 @@ func (s *store) exists(det *order.Detail) bool {
if det == nil {
return false
}
s.m.Lock()
defer s.m.Unlock()
s.m.RLock()
defer s.m.RUnlock()
r, ok := s.Orders[strings.ToLower(det.Exchange)]
if !ok {
return false
@@ -736,3 +750,35 @@ func (s *store) add(det *order.Detail) error {
return nil
}
// getFilteredOrders returns a filtered copy of the orders
func (s *store) getFilteredOrders(f *order.Filter) ([]order.Detail, error) {
if f == nil {
return nil, errors.New("filter is nil")
}
s.m.RLock()
defer s.m.RUnlock()
var os []order.Detail
// optimization if Exchange is filtered
if f.Exchange != "" {
if e, ok := s.Orders[strings.ToLower(f.Exchange)]; ok {
for i := range e {
if !e[i].MatchFilter(f) {
continue
}
os = append(os, e[i].Copy())
}
}
} else {
for _, e := range s.Orders {
for i := range e {
if !e[i].MatchFilter(f) {
continue
}
os = append(os, e[i].Copy())
}
}
}
return os, nil
}