extract client order id generation

This commit is contained in:
c9s 2021-04-28 19:20:55 +08:00
parent 36beabaa0b
commit 5d2296eddd
2 changed files with 44 additions and 5 deletions

View File

@ -580,17 +580,39 @@ func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrde
return createdOrder, err
}
// BBGO is a broker on Binance
const spotBrokerID = "NSUYEBKM"
func newSpotClientOrderID(originalID string) (clientOrderID string) {
prefix := "x-" + spotBrokerID
prefixLen := len(prefix)
if originalID != "" {
// try to keep the whole original client order ID if user specifies it.
if prefixLen + len(originalID) > 32 {
return originalID
}
clientOrderID = prefix + originalID
return clientOrderID
}
clientOrderID = uuid.New().String()
clientOrderID = prefix + clientOrderID
if len(clientOrderID) > 32 {
return clientOrderID[0:32]
}
return clientOrderID
}
func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
orderType, err := toLocalOrderType(order.Type)
if err != nil {
return nil, err
}
clientOrderID := uuid.New().String()
if len(order.ClientOrderID) > 0 {
clientOrderID = order.ClientOrderID
}
clientOrderID := newSpotClientOrderID(order.ClientOrderID)
req := e.Client.NewCreateOrderService().
Symbol(order.Symbol).
Side(binance.SideType(order.Side)).

View File

@ -0,0 +1,17 @@
package binance
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_newClientOrderID(t *testing.T) {
cID := newSpotClientOrderID("")
assert.Len(t, cID, 32)
strings.HasPrefix(cID, "x-" + spotBrokerID)
cID = newSpotClientOrderID("myid1")
assert.Equal(t, cID, "x-" + spotBrokerID + "myid1")
}