add Backup method to the local active order book

This commit is contained in:
c9s 2021-03-15 18:25:36 +08:00
parent 46c59f5009
commit c3996aee2b
3 changed files with 26 additions and 0 deletions

View File

@ -22,6 +22,10 @@ func NewLocalActiveOrderBook() *LocalActiveOrderBook {
}
}
func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder {
return append(b.Bids.Backup(), b.Asks.Backup()...)
}
func (b *LocalActiveOrderBook) BindStream(stream types.Stream) {
stream.OnOrderUpdate(b.orderUpdateHandler)
}

View File

@ -155,6 +155,14 @@ type Order struct {
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
}
// Backup backs up the current order quantity to a SubmitOrder object
// so that we can post the order later when we want to restore the orders.
func (o Order) Backup() SubmitOrder {
so := o.SubmitOrder
so.Quantity = o.Quantity - o.ExecutedQuantity
return so
}
func (o Order) String() string {
return fmt.Sprintf("order %s %f/%f at %f -> %s", o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status)
}

View File

@ -5,6 +5,14 @@ import "sync"
// OrderMap is used for storing orders by their order id
type OrderMap map[uint64]Order
func (m OrderMap) Backup() (orderForms []SubmitOrder) {
for _, order := range m {
orderForms = append(orderForms, order.Backup())
}
return orderForms
}
func (m OrderMap) Add(o Order) {
m[o.OrderID] = o
}
@ -70,6 +78,12 @@ func NewSyncOrderMap() *SyncOrderMap {
}
}
func (m *SyncOrderMap) Backup() []SubmitOrder {
m.Lock()
defer m.Unlock()
return m.orders.Backup()
}
func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
m.Lock()
defer m.Unlock()